use of org.jboss.resteasy.client.jaxrs.ResteasyClient 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 = buildResteasyClient(providerFactory);
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();
}
}
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient in project scheduling by ow2-proactive.
the class SchedulerRestClient method reSubmit.
public JobIdData reSubmit(String sessionId, String jobId, Map<String, String> variables, Map<String, String> genericInfos) throws NotConnectedRestException {
String uriTmpl = restEndpointURL + addSlashIfMissing(restEndpointURL) + "scheduler/jobs/" + jobId + "/resubmit";
ResteasyClient client = buildResteasyClient(providerFactory);
ResteasyWebTarget target = client.target(uriTmpl);
// Variables
if (variables != null) {
for (String key : variables.keySet()) {
target = target.matrixParam(key, variables.get(key));
}
}
// Generic infos
if (genericInfos != null) {
for (String key : genericInfos.keySet()) {
target = target.queryParam(key, genericInfos.get(key));
}
}
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("Job submission failed status code: %d", response.getStatus()), response);
}
}
return response.readEntity(JobIdData.class);
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient in project scheduling by ow2-proactive.
the class DataSpaceClient method list.
@Override
public ListFile list(IRemoteSource source) throws NotConnectedException, PermissionException {
StringBuffer uriTmpl = (new StringBuffer()).append(restDataspaceUrl).append(source.getDataspace().value());
ResteasyClient client = new ResteasyClientBuilder().providerFactory(providerFactory).httpEngine(httpEngine).build();
ResteasyWebTarget target = client.target(uriTmpl.toString()).path(source.getPath()).queryParam("comp", "list");
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).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 list the specified location: %s", source.getPath()));
}
}
return response.readEntity(ListFile.class);
} finally {
if (response != null) {
response.close();
}
}
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient 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().providerFactory(providerFactory).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();
}
}
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient in project scheduling by ow2-proactive.
the class SchedulerStateRest method submitPlannings.
@Override
public String submitPlannings(String sessionId, PathSegment pathSegment, Map<String, String> jobContentXmlString) throws JobCreationRestException, NotConnectedRestException, IOException {
checkAccess(sessionId, "plannings");
Map<String, String> jobVariables = workflowVariablesTransformer.getWorkflowVariablesFromPathSegment(pathSegment);
if (jobContentXmlString == null || jobContentXmlString.size() != 1) {
throw new JobCreationRestException("Cannot find job body: code " + HttpURLConnection.HTTP_BAD_REQUEST);
}
Map<String, Object> requestBody = new HashMap<>(2);
requestBody.put(VARIABLES_KEY, jobVariables);
requestBody.put("xmlContentString", jobContentXmlString.entrySet().iterator().next().getValue());
Response response = null;
try {
ResteasyProviderFactory providerFactory = ResteasyProviderFactory.getInstance();
SchedulerRestClient.registerGzipEncoding(providerFactory);
ResteasyClient client = new ResteasyClientBuilder().providerFactory(providerFactory).build();
ResteasyWebTarget target = client.target(PortalConfiguration.JOBPLANNER_URL.getValueAsString());
response = target.request().header("sessionid", sessionId).post(Entity.entity(requestBody, "application/json"));
if (HttpURLConnection.HTTP_OK != response.getStatus()) {
throw new IOException(String.format("Cannot access resource %s: code %d", PortalConfiguration.JOBPLANNER_URL.getValueAsString(), response.getStatus()));
}
return response.readEntity(String.class);
} finally {
if (response != null) {
response.close();
}
}
}
Aggregations