Search in sources :

Example 36 with Session

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

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

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

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

the class EntryPoint method run.

protected int run(String... args) {
    CommandFactory commandFactory = null;
    CommandLine cli = null;
    AbstractDevice console;
    ApplicationContext currentContext = new ApplicationContextImpl().currentContext();
    // Cannot rely on AbstractCommand#isDebugModeEnabled
    // because at this step SetDebugModeCommand#execute has not yet been executed
    // Consequently, SetDebugModeCommand.PROP_DEBUG_MODE is not set even if debug mode is enabled.
    boolean isDebugModeEnabled = isDebugModeEnabled(args);
    try {
        commandFactory = getCommandFactory(currentContext);
        Options options = commandFactory.supportedOptions();
        cli = parseArgs(options, args);
    } catch (IOException ioe) {
        System.err.println("An error occurred.");
        ioe.printStackTrace(System.err);
        return 1;
    } catch (ParseException pe) {
        writeError(currentContext, pe.getMessage(), pe, isDebugModeEnabled);
        // print usage
        Command help = commandFactory.commandForOption(new Option("h", null));
        if (help != null) {
            help.execute(currentContext);
        }
        return 1;
    }
    currentContext.setObjectMapper(new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false));
    currentContext.setRestServerUrl(DFLT_REST_SCHEDULER_URL);
    currentContext.setResourceType(resourceType());
    // retrieve the (ordered) command list corresponding to command-line
    // arguments
    List<Command> commands;
    try {
        commands = commandFactory.getCommandList(cli, currentContext);
    } catch (CLIException e) {
        writeError(currentContext, "An error occurred.", e, isDebugModeEnabled);
        return 1;
    }
    boolean retryLogin = false;
    try {
        executeCommandList(commands, currentContext);
    } catch (CLIException error) {
        if (REASON_UNAUTHORIZED_ACCESS == error.reason() && hasLoginCommand(commands)) {
            retryLogin = true;
        } else {
            writeError(currentContext, "An error occurred.", error, isDebugModeEnabled);
            return 1;
        }
    } catch (Throwable e) {
        writeError(currentContext, "An error occurred.", e, isDebugModeEnabled);
        return 1;
    }
    /*
         * in case of an existing session-id, the REST CLI reuses it without
         * obtaining a new session-id even if a login with credentials
         * specified. However if the REST server responds with an authorization
         * error (e.g. due to session timeout), it re-executes the commands list
         * with AbstractLoginCommand.PROP_RENEW_SESSION property set to 'true'.
         * This will effectively re-execute the user command with a new
         * session-id from server.
         */
    if (retryLogin && currentContext.getProperty(PROP_PERSISTED_SESSION, Boolean.TYPE, false)) {
        try {
            currentContext.setProperty(PROP_RENEW_SESSION, true);
            executeCommandList(commands, currentContext);
        } catch (Throwable error) {
            writeError(currentContext, "An error occurred while execution.", error, isDebugModeEnabled);
            return 1;
        }
    }
    return 0;
}
Also used : Options(org.apache.commons.cli.Options) IOException(java.io.IOException) CommandLine(org.apache.commons.cli.CommandLine) Command(org.ow2.proactive_grid_cloud_portal.cli.cmd.Command) AbstractLoginCommand(org.ow2.proactive_grid_cloud_portal.cli.cmd.AbstractLoginCommand) AbstractDevice(org.ow2.proactive_grid_cloud_portal.cli.console.AbstractDevice) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

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