Search in sources :

Example 61 with NotConnectedRestException

use of org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException in project scheduling by ow2-proactive.

the class RestDataspaceImpl method store.

/**
 * Upload a file to the specified location in the <i>dataspace</i>. The
 * format of the PUT URI is:
 * <p>
 * {@code http://<rest-server-path>/data/<dataspace>/<path-name>}
 * <p>
 * Example:
 * <p>
 * {@code http://localhost:8080/rest/rest/data/user/my-files/my-text-file.txt}
 * <ul>
 * <li><b>dataspace:</b> Can have two possible values, 'user' or 'global',
 * depending on target <i>DATASPACE</i>.</li>
 * <li><b>path-name:</b> Location in which the file is stored.</li>
 * </ul>
 * <b>Notes:</b>
 * <ul>
 * <li>If 'gzip' or 'zip' is specified in the 'Content-Encoding' header, the
 * contents of the request body will be decoded before being stored.</li>
 * <li>Any file that already exists in the specified location, it will be
 * replaced.</li>
 * </ul>
 */
@PUT
@Path("/{dataspace}/{path-name:.*}")
public Response store(@HeaderParam("sessionid") String sessionId, @HeaderParam("Content-Encoding") String encoding, @PathParam("dataspace") String dataspace, @PathParam("path-name") String pathname, InputStream is) throws NotConnectedRestException, PermissionRestException {
    Session session = checkSessionValidity(sessionId);
    try {
        checkPathParams(dataspace, pathname);
        logger.debug(String.format("Storing %s in %s", pathname, dataspace));
        writeFile(is, resolveFile(session, dataspace, pathname), encoding);
    } catch (Throwable error) {
        logger.error(String.format("Cannot save the requested file to %s in %s.", pathname, dataspace), error);
        rethrow(error);
    }
    return Response.status(Response.Status.CREATED).build();
}
Also used : Session(org.ow2.proactive_grid_cloud_portal.common.Session)

Example 62 with NotConnectedRestException

use of org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException in project scheduling by ow2-proactive.

the class RestDataspaceImpl method metadata.

/**
 * Retrieve metadata of file in the location specified in <i>dataspace</i>.
 * The format of the HEAD URI is:
 * <p>
 * {@code http://<rest-server-path>/data/<dataspace>/<path-name>}
 * <p>
 * Example:
 * {@code http://localhost:8080/rest/rest/data/user/my-files/my-text-file.txt}
 */
@HEAD
@Path("/{dataspace}/{path-name:.*}")
public Response metadata(@HeaderParam("sessionid") String sessionId, @PathParam("dataspace") String dataspacePath, @PathParam("path-name") String pathname) throws NotConnectedRestException, PermissionRestException {
    Session session = checkSessionValidity(sessionId);
    try {
        checkPathParams(dataspacePath, pathname);
        FileObject fo = resolveFile(session, dataspacePath, pathname);
        if (!fo.exists()) {
            return notFoundRes();
        }
        logger.debug(String.format("Retrieving metadata for %s in %s", pathname, dataspacePath));
        MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(FileSystem.metadata(fo));
        return Response.ok().replaceAll(headers).build();
    } catch (Throwable error) {
        logger.error(String.format("Cannot retrieve metadata for %s in %s.", pathname, dataspacePath), error);
        throw rethrow(error);
    }
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) FileObject(org.apache.commons.vfs2.FileObject) FileObject(org.apache.commons.vfs2.FileObject) Session(org.ow2.proactive_grid_cloud_portal.common.Session)

Example 63 with NotConnectedRestException

use of org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException in project scheduling by ow2-proactive.

the class SchedulerRestClient method list.

public ListFile list(String sessionId, String dataspacePath, String pathname) throws Exception {
    StringBuffer uriTmpl = (new StringBuffer()).append(restEndpointURL).append(addSlashIfMissing(restEndpointURL)).append("data/").append(dataspacePath).append('/');
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(providerFactory).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(pathname).queryParam("comp", "list");
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).get();
        if (response.getStatus() != HttpURLConnection.HTTP_OK) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedRestException("User not authenticated or session timeout.");
            } else {
                throwException(String.format("Cannot list the specified location: %s", pathname), response);
            }
        }
        return response.readEntity(ListFile.class);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)

Example 64 with NotConnectedRestException

use of org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException in project scheduling by ow2-proactive.

the class SchedulerRestClient method download.

public boolean download(String sessionId, String dataspacePath, String path, List<String> includes, List<String> excludes, File outputFile) throws Exception {
    StringBuffer uriTmpl = (new StringBuffer()).append(restEndpointURL).append(addSlashIfMissing(restEndpointURL)).append("data/").append(dataspacePath).append('/');
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(providerFactory).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(path);
    if (includes != null && !includes.isEmpty()) {
        target = target.queryParam("includes", includes.toArray(new Object[includes.size()]));
    }
    if (excludes != null && !excludes.isEmpty()) {
        target = target.queryParam("excludes", excludes.toArray(new Object[excludes.size()]));
    }
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).acceptEncoding("*", "gzip", "zip").get();
        if (response.getStatus() != HttpURLConnection.HTTP_OK) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedRestException("User not authenticated or session timeout.");
            } else {
                throwException(String.format("Cannot retrieve the file. Status code: %d", response.getStatus()), response);
            }
        }
        if (response.hasEntity()) {
            InputStream is = response.readEntity(InputStream.class);
            if (isGZipEncoded(response)) {
                if (outputFile.exists() && outputFile.isDirectory()) {
                    outputFile = new File(outputFile, response.getHeaderString("x-pds-pathname"));
                }
                Zipper.GZIP.unzip(is, outputFile);
            } else if (isZipEncoded(response)) {
                Zipper.ZIP.unzip(is, outputFile);
            } else {
                File container = outputFile.getParentFile();
                if (!container.exists()) {
                    container.mkdirs();
                }
                Files.asByteSink(outputFile).writeFrom(is);
            }
        } else {
            outputFile.createNewFile();
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
    return true;
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) InputStream(java.io.InputStream) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException) FileUtils.copyInputStreamToFile(org.apache.commons.io.FileUtils.copyInputStreamToFile) ListFile(org.ow2.proactive_grid_cloud_portal.dataspace.dto.ListFile) File(java.io.File)

Example 65 with NotConnectedRestException

use of org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException in project scheduling by ow2-proactive.

the class SchedulerRestClient method metadata.

public Map<String, Object> metadata(String sessionId, String dataspacePath, String pathname) throws Exception {
    StringBuffer uriTmpl = (new StringBuffer()).append(restEndpointURL).append(addSlashIfMissing(restEndpointURL)).append("data/").append(dataspacePath).append(escapeUrlPathSegment(pathname));
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(providerFactory).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString());
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).head();
        if (response.getStatus() != HttpURLConnection.HTTP_OK) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedRestException("User not authenticated or session timeout.");
            } else {
                throwException(String.format("Cannot get metadata from %s in %s.", pathname, dataspacePath), response);
            }
        }
        MultivaluedMap<String, Object> headers = response.getHeaders();
        Map<String, Object> metaMap = Maps.newHashMap();
        if (headers.containsKey(HttpHeaders.LAST_MODIFIED)) {
            metaMap.put(HttpHeaders.LAST_MODIFIED, headers.getFirst(HttpHeaders.LAST_MODIFIED));
        }
        return metaMap;
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)

Aggregations

NotConnectedRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)53 NotConnectedException (org.ow2.proactive.scheduler.common.exception.NotConnectedException)42 PermissionException (org.ow2.proactive.scheduler.common.exception.PermissionException)40 PermissionRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.PermissionRestException)40 Scheduler (org.ow2.proactive.scheduler.common.Scheduler)38 Path (javax.ws.rs.Path)37 Produces (javax.ws.rs.Produces)35 GET (javax.ws.rs.GET)28 UnknownJobException (org.ow2.proactive.scheduler.common.exception.UnknownJobException)28 UnknownJobRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.UnknownJobRestException)28 GZIP (org.jboss.resteasy.annotations.GZIP)17 ArrayList (java.util.ArrayList)13 JobState (org.ow2.proactive.scheduler.common.job.JobState)13 Session (org.ow2.proactive_grid_cloud_portal.common.Session)12 RestPage (org.ow2.proactive_grid_cloud_portal.scheduler.dto.RestPage)12 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)11 Response (javax.ws.rs.core.Response)10 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)10 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)10 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)10