Search in sources :

Example 31 with NotConnectedRestException

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

the class SchedulerRestClient method pullFile.

public void pullFile(String sessionId, String space, String path, String outputPath) throws Exception {
    String uriTmpl = (new StringBuilder(restEndpointURL)).append(addSlashIfMissing(restEndpointURL)).append("scheduler/dataspace/").append(space).append(URLEncoder.encode(path, "UTF-8")).toString();
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(providerFactory).build();
    ResteasyWebTarget target = client.target(uriTmpl);
    Response 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 retrieve the file. Status code: %s", response.getStatus()), response);
        }
    }
    try {
        File file = new File(outputPath);
        if (response.hasEntity()) {
            copyInputStreamToFile(response.readEntity(InputStream.class), file);
        } else {
            // creates an empty file
            file.createNewFile();
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (response != null) {
            response.close();
        }
        if (!client.isClosed()) {
            client.close();
        }
    }
}
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) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)

Example 32 with NotConnectedRestException

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

the class SchedulerRestClient method pushFile.

public boolean pushFile(String sessionId, String space, String path, String fileName, InputStream fileContent) throws Exception {
    String uriTmpl = (new StringBuilder(restEndpointURL)).append(addSlashIfMissing(restEndpointURL)).append("scheduler/dataspace/").append(space).append(URLEncoder.encode(path, "UTF-8")).toString();
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(providerFactory).build();
    ResteasyWebTarget target = client.target(uriTmpl);
    MultipartFormDataOutput formData = new MultipartFormDataOutput();
    formData.addFormData("fileName", fileName, MediaType.TEXT_PLAIN_TYPE);
    formData.addFormData("fileContent", fileContent, MediaType.APPLICATION_OCTET_STREAM_TYPE);
    GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(formData) {
    };
    Response response = target.request().header("sessionid", sessionId).post(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
    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("File upload failed. Status code: %d", response.getStatus()), response);
        }
    }
    return response.readEntity(Boolean.class);
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) GenericEntity(javax.ws.rs.core.GenericEntity) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException) MultipartFormDataOutput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput)

Example 33 with NotConnectedRestException

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

the class SchedulerRestClient method upload.

public boolean upload(String sessionId, StreamingOutput output, String encoding, String dataspace, String path) throws Exception {
    StringBuffer uriTmpl = (new StringBuffer()).append(restEndpointURL).append(addSlashIfMissing(restEndpointURL)).append("data/").append(dataspace);
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(providerFactory).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(path);
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).put(Entity.entity(output, new Variant(MediaType.APPLICATION_OCTET_STREAM_TYPE, (Locale) null, encoding)));
        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 34 with NotConnectedRestException

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

the class SchedulerRestClient method submit.

private JobIdData submit(String sessionId, InputStream job, MediaType mediaType, Map<String, String> variables) throws Exception {
    String uriTmpl = restEndpointURL + addSlashIfMissing(restEndpointURL) + "scheduler/submit";
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(providerFactory).build();
    ResteasyWebTarget target = client.target(uriTmpl);
    if (variables != null) {
        for (String key : variables.keySet()) {
            target = target.matrixParam(key, variables.get(key));
        }
    }
    MultipartFormDataOutput formData = new MultipartFormDataOutput();
    formData.addFormData("file", job, mediaType);
    GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(formData) {
    };
    Response response = target.request().header("sessionid", sessionId).post(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
    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("Job submission failed status code: %d", response.getStatus()), response);
        }
    }
    return response.readEntity(JobIdData.class);
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) GenericEntity(javax.ws.rs.core.GenericEntity) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException) MultipartFormDataOutput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput)

Example 35 with NotConnectedRestException

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

the class SchedulerStateRest method taskResultByTag.

/**
 * Returns the task results of the set of task filtered by a given tag and
 * owned by the job <code>jobId</code>
 *
 * @param sessionId
 *            a valid session id
 * @param jobId
 *            the id of the job
 * @param taskTag
 *            the tag used to filter the tasks.
 * @return the task results of the set of tasks filtered by the given tag.
 */
@Override
@GET
@GZIP
@Path("jobs/{jobid}/tasks/tag/{tasktag}/result")
@Produces("application/json")
public List<TaskResultData> taskResultByTag(@HeaderParam("sessionid") String sessionId, @PathParam("jobid") String jobId, @PathParam("tasktag") String taskTag) throws NotConnectedRestException, UnknownJobRestException, PermissionRestException {
    try {
        Scheduler s = checkAccess(sessionId, "jobs/" + jobId + "/tasks/" + taskTag + "/result");
        List<TaskResult> taskResults = s.getTaskResultsByTag(jobId, taskTag);
        ArrayList<TaskResultData> results = new ArrayList<TaskResultData>(taskResults.size());
        for (TaskResult current : taskResults) {
            TaskResultData r = buildTaskResultData(PAFuture.getFutureValue(current));
            results.add(r);
        }
        return results;
    } catch (PermissionException e) {
        throw new PermissionRestException(e);
    } catch (UnknownJobException e) {
        throw new UnknownJobRestException(e);
    } catch (NotConnectedException e) {
        throw new NotConnectedRestException(e);
    }
}
Also used : PermissionException(org.ow2.proactive.scheduler.common.exception.PermissionException) UnknownJobRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.UnknownJobRestException) NotConnectedException(org.ow2.proactive.scheduler.common.exception.NotConnectedException) PermissionRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.PermissionRestException) UnknownJobException(org.ow2.proactive.scheduler.common.exception.UnknownJobException) Scheduler(org.ow2.proactive.scheduler.common.Scheduler) TaskResultData(org.ow2.proactive_grid_cloud_portal.scheduler.dto.TaskResultData) ArrayList(java.util.ArrayList) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

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