Search in sources :

Example 6 with ResteasyWebTarget

use of org.jboss.resteasy.client.jaxrs.ResteasyWebTarget in project scheduling by ow2-proactive.

the class DataSpaceClient method upload.

@Override
public boolean upload(final ILocalSource source, final IRemoteDestination destination) throws NotConnectedException, PermissionException {
    if (log.isDebugEnabled()) {
        log.debug("Uploading from " + source + " to " + destination);
    }
    StringBuffer uriTmpl = (new StringBuffer()).append(restDataspaceUrl).append(destination.getDataspace().value());
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(destination.getPath());
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).put(Entity.entity(new StreamingOutput() {

            @Override
            public void write(OutputStream outputStream) throws IOException, WebApplicationException {
                source.writeTo(outputStream);
            }
        }, new Variant(MediaType.APPLICATION_OCTET_STREAM_TYPE, (Locale) null, source.getEncoding())));
        if (response.getStatus() != HttpURLConnection.HTTP_CREATED) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedException("User not authenticated or session timeout.");
            } else {
                throw new RuntimeException("File upload failed. Status code:" + response.getStatus());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Upload from " + source + " to " + destination + " performed with success");
        }
        return true;
    } catch (IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : Locale(java.util.Locale) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) WebApplicationException(javax.ws.rs.WebApplicationException) NotConnectedException(org.ow2.proactive.scheduler.common.exception.NotConnectedException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 7 with ResteasyWebTarget

use of org.jboss.resteasy.client.jaxrs.ResteasyWebTarget in project scheduling by ow2-proactive.

the class HttpResourceDownloader method getResource.

public <T> T getResource(String sessionId, String url, Class<T> clazz) throws IOException {
    Response response = null;
    try {
        ResteasyWebTarget target = client.target(url);
        response = target.request().header("sessionid", sessionId).get();
        if (responseIsNotHttpOk(response)) {
            throw new IOException(String.format("Cannot access resource %s: code %d", url, response.getStatus()));
        }
        return (T) response.readEntity(clazz);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) IOException(java.io.IOException)

Example 8 with ResteasyWebTarget

use of org.jboss.resteasy.client.jaxrs.ResteasyWebTarget 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 9 with ResteasyWebTarget

use of org.jboss.resteasy.client.jaxrs.ResteasyWebTarget 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 10 with ResteasyWebTarget

use of org.jboss.resteasy.client.jaxrs.ResteasyWebTarget 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)

Aggregations

ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)23 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)19 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)19 Response (javax.ws.rs.core.Response)14 NotConnectedRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)9 IOException (java.io.IOException)7 NotConnectedException (org.ow2.proactive.scheduler.common.exception.NotConnectedException)4 HashMap (java.util.HashMap)3 Locale (java.util.Locale)3 Invocation (javax.ws.rs.client.Invocation)3 WebTarget (javax.ws.rs.client.WebTarget)3 InputStream (java.io.InputStream)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Future (java.util.concurrent.Future)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 Consumer (java.util.function.Consumer)2 Level (java.util.logging.Level)2