Search in sources :

Example 16 with ResteasyWebTarget

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

the class DataSpaceClient method delete.

@Override
public boolean delete(IRemoteSource source) throws NotConnectedException, PermissionException {
    if (log.isDebugEnabled()) {
        log.debug("Trying to delete " + source);
    }
    StringBuffer uriTmpl = (new StringBuffer()).append(restDataspaceUrl).append(source.getDataspace().value());
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(source.getPath());
    List<String> includes = source.getIncludes();
    if (includes != null && !includes.isEmpty()) {
        target = target.queryParam("includes", includes.toArray(new Object[includes.size()]));
    }
    List<String> excludes = source.getExcludes();
    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();
        boolean noContent = false;
        if (response.getStatus() != HttpURLConnection.HTTP_NO_CONTENT) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedException("User not authenticated or session timeout.");
            } else {
                throw new RuntimeException("Cannot delete file(s). Status :" + response.getStatusInfo() + " Entity : " + response.getEntity());
            }
        } else {
            noContent = true;
            log.debug("No action performed for deletion since source " + source + " was not found remotely");
        }
        if (!noContent && log.isDebugEnabled()) {
            log.debug("Removal of " + source + " performed with success");
        }
        return true;
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) NotConnectedException(org.ow2.proactive.scheduler.common.exception.NotConnectedException) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 17 with ResteasyWebTarget

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

the class DataSpaceClient method download.

@Override
public boolean download(IRemoteSource source, ILocalDestination destination) throws NotConnectedException, PermissionException {
    if (log.isDebugEnabled()) {
        log.debug("Downloading from " + source + " to " + destination);
    }
    StringBuffer uriTmpl = (new StringBuffer()).append(restDataspaceUrl).append(source.getDataspace().value());
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(source.getPath());
    List<String> includes = source.getIncludes();
    if (includes != null && !includes.isEmpty()) {
        target = target.queryParam("includes", includes.toArray(new Object[includes.size()]));
    }
    List<String> excludes = source.getExcludes();
    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 NotConnectedException("User not authenticated or session timeout.");
            } else {
                throw new RuntimeException(String.format("Cannot retrieve the file. Status code: %s", response.getStatus()));
            }
        }
        if (response.hasEntity()) {
            InputStream is = response.readEntity(InputStream.class);
            destination.readFrom(is, response.getHeaderString(HttpHeaders.CONTENT_ENCODING));
        } else {
            throw new RuntimeException(String.format("%s in %s is empty.", source.getDataspace(), source.getPath()));
        }
        if (log.isDebugEnabled()) {
            log.debug("Download from " + source + " to " + destination + " performed with success");
        }
        return true;
    } catch (IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) NotConnectedException(org.ow2.proactive.scheduler.common.exception.NotConnectedException) InputStream(java.io.InputStream) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) IOException(java.io.IOException)

Example 18 with ResteasyWebTarget

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

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

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

the class SchedulerRestClient method createRestProxy.

private static SchedulerRestInterface createRestProxy(ResteasyProviderFactory provider, String restEndpointURL, ClientHttpEngine httpEngine) {
    ResteasyClient client = new ResteasyClientBuilder().providerFactory(provider).httpEngine(httpEngine).build();
    ResteasyWebTarget target = client.target(restEndpointURL);
    SchedulerRestInterface schedulerRestClient = target.proxy(SchedulerRestInterface.class);
    return createExceptionProxy(schedulerRestClient);
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) SchedulerRestInterface(org.ow2.proactive_grid_cloud_portal.common.SchedulerRestInterface)

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