Search in sources :

Example 91 with Session

use of org.ow2.proactive_grid_cloud_portal.common.Session 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 92 with Session

use of org.ow2.proactive_grid_cloud_portal.common.Session 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)

Example 93 with Session

use of org.ow2.proactive_grid_cloud_portal.common.Session in project scheduling by ow2-proactive.

the class SchedulerRestClient method delete.

public boolean delete(String sessionId, String dataspacePath, String path, List<String> includes, List<String> excludes) 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).delete();
        if (response.getStatus() != HttpURLConnection.HTTP_NO_CONTENT) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedRestException("User not authenticated or session timeout.");
            } else {
                throwException(String.format("Cannot delete file(s). Status code: %s", response.getStatus()), response);
            }
        }
        return true;
    } 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 94 with Session

use of org.ow2.proactive_grid_cloud_portal.common.Session in project scheduling by ow2-proactive.

the class SchedulerRestClient method upload.

public boolean upload(String sessionId, File file, List<String> includes, List<String> excludes, String dataspacePath, final String path) throws Exception {
    StringBuffer uriTmpl = (new StringBuffer()).append(restEndpointURL).append(addSlashIfMissing(restEndpointURL)).append("data/").append(dataspacePath).append('/').append(escapeUrlPathSegment(path));
    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).put(Entity.entity(new CompressedStreamingOutput(file, includes, excludes), new Variant(MediaType.APPLICATION_OCTET_STREAM_TYPE, (Locale) null, encoding(file))));
        if (response.getStatus() != HttpURLConnection.HTTP_CREATED) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedRestException("User not authenticated or session timeout.");
            } else {
                throwException(String.format("File upload failed. Status code: %d", response.getStatus()), response);
            }
        }
        return true;
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) Variant(javax.ws.rs.core.Variant) Locale(java.util.Locale) 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 95 with Session

use of org.ow2.proactive_grid_cloud_portal.common.Session in project scheduling by ow2-proactive.

the class SchedulerFrontendState method checkLinkResourceManager.

synchronized void checkLinkResourceManager() throws NotConnectedException, PermissionException {
    UniqueID id = checkAccess();
    UserIdentificationImpl ident = identifications.get(id).getUser();
    // renew session for this user
    renewUserSession(id, ident);
    try {
        ident.checkPermission(new ConnectToResourceManagerPermission(), ident.getUsername() + " does not have permissions to change RM in the scheduler");
    } catch (PermissionException ex) {
        logger.info(ex.getMessage());
        throw ex;
    }
}
Also used : PermissionException(org.ow2.proactive.scheduler.common.exception.PermissionException) UniqueID(org.objectweb.proactive.core.UniqueID) ConnectToResourceManagerPermission(org.ow2.proactive.scheduler.permissions.ConnectToResourceManagerPermission) UserIdentificationImpl(org.ow2.proactive.scheduler.job.UserIdentificationImpl)

Aggregations

Path (javax.ws.rs.Path)49 Produces (javax.ws.rs.Produces)47 NotConnectedRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)39 NotConnectedException (org.ow2.proactive.scheduler.common.exception.NotConnectedException)37 Scheduler (org.ow2.proactive.scheduler.common.Scheduler)36 PermissionException (org.ow2.proactive.scheduler.common.exception.PermissionException)34 GET (javax.ws.rs.GET)32 PermissionRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.PermissionRestException)29 UnknownJobException (org.ow2.proactive.scheduler.common.exception.UnknownJobException)25 UnknownJobRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.UnknownJobRestException)24 GZIP (org.jboss.resteasy.annotations.GZIP)23 Session (org.ow2.proactive_grid_cloud_portal.common.Session)18 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)17 ArrayList (java.util.ArrayList)16 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)15 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)15 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)15 JobState (org.ow2.proactive.scheduler.common.job.JobState)12 IOException (java.io.IOException)11 POST (javax.ws.rs.POST)11