use of com.sun.jersey.api.client.WebResource.Builder in project ORCID-Source by ORCID.
the class Orcid20APIClient method fetchPublicProfile.
/**
* Fetches the profile from the ORCID public API v1.2
*
* @param orcid
* @return
*/
public Record fetchPublicProfile(String orcid) throws LockedRecordException, DeprecatedRecordException {
WebResource webResource = jerseyClient.resource(baseUri).path(orcid + "/record");
webResource.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
Builder builder = webResource.accept(MediaType.APPLICATION_XML).header("Authorization", "Bearer " + accessToken);
ClientResponse response = builder.get(ClientResponse.class);
if (response.getStatus() != 200) {
OrcidError orcidError = null;
switch(response.getStatus()) {
case 301:
orcidError = response.getEntity(OrcidError.class);
throw new DeprecatedRecordException(orcidError);
case 409:
orcidError = response.getEntity(OrcidError.class);
throw new LockedRecordException(orcidError);
default:
LOG.error("Unable to fetch public record " + orcid + " on API 2.0 HTTP error code: " + response.getStatus());
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
}
return response.getEntity(Record.class);
}
use of com.sun.jersey.api.client.WebResource.Builder in project pentaho-platform by pentaho.
the class CommandLineProcessor method performREST.
/**
* call FileResource REST service example: {path+}/children example: {path+}/parameterizable example:
* {path+}/properties example: /delete?params={fileid1, fileid2}
*
* @throws ParseException
*/
private void performREST() throws ParseException, InitializationException {
String contextURL = getOptionValue(INFO_OPTION_URL_NAME, true, false);
String path = getOptionValue(INFO_OPTION_PATH_NAME, true, false);
String logFile = getOptionValue(INFO_OPTION_LOGFILE_NAME, false, true);
String exportURL = contextURL + "/api/repo/files/";
if (path != null) {
String effPath = RepositoryPathEncoder.encodeRepositoryPath(path);
exportURL += effPath;
}
String service = getOptionValue(INFO_OPTION_SERVICE_NAME, true, false);
String params = getOptionValue(INFO_OPTION_PARAMS_NAME, false, true);
exportURL += "/" + service;
if (params != null) {
exportURL += "?params=" + params;
}
initRestService();
WebResource resource = client.resource(exportURL);
// Response response
Builder builder = resource.type(MediaType.APPLICATION_JSON).type(MediaType.TEXT_XML_TYPE);
ClientResponse response = builder.put(ClientResponse.class);
if (response != null && response.getStatus() == 200) {
String message = Messages.getInstance().getString("CommandLineProcessor.INFO_REST_COMPLETED").concat("\n");
message += Messages.getInstance().getString("CommandLineProcessor.INFO_REST_RESPONSE_STATUS", response.getStatus());
message += "\n";
if (logFile != null && !"".equals(logFile)) {
message += Messages.getInstance().getString("CommandLineProcessor.INFO_REST_FILE_WRITTEN", logFile);
System.out.println(message);
writeFile(message, logFile);
}
} else {
System.out.println(Messages.getInstance().getErrorString("CommandLineProcessor.ERROR_0002_INVALID_RESPONSE"));
}
}
use of com.sun.jersey.api.client.WebResource.Builder in project pentaho-platform by pentaho.
the class CommandLineProcessor method performBackup.
/**
* REST Service Backup
*
* @throws ParseException
* --backup --url=http://localhost:8080/pentaho --username=admin --password=password
* --logfile=c:/temp/steel-wheels.log --file-path=c:/temp/backup.zip
* @throws java.io.IOException
*/
private void performBackup() throws ParseException, InitializationException, IOException {
String contextURL = getOptionValue(INFO_OPTION_URL_NAME, true, false);
String logFile = getOptionValue(INFO_OPTION_LOGFILE_NAME, false, true);
String backupURL = contextURL + "/api/repo/files/backup";
initRestService();
// check if the user has permissions to upload/download data
WebResource authResource = client.resource(contextURL + "/api/authorization/action/isauthorized?authAction=" + AdministerSecurityAction.NAME);
String authResponse = authResource.get(String.class);
if (!authResponse.equals("true")) {
System.err.println(Messages.getInstance().getString("CommandLineProcessor.ERROR_0006_NON_ADMIN_CREDENTIALS"));
}
WebResource resource = client.resource(backupURL);
// Response response
Builder builder = resource.type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_HTML_TYPE);
ClientResponse response = builder.get(ClientResponse.class);
if (response != null && response.getStatus() == 200) {
String filename = getOptionValue(INFO_OPTION_FILEPATH_NAME, true, false);
final InputStream input = response.getEntityInputStream();
createZipFile(filename, input);
input.close();
String message = Messages.getInstance().getString("CommandLineProcessor.INFO_EXPORT_COMPLETED").concat("\n");
message += Messages.getInstance().getString("CommandLineProcessor.INFO_RESPONSE_STATUS", response.getStatus());
message += "\n";
message += Messages.getInstance().getString("CommandLineProcessor.INFO_EXPORT_WRITTEN_TO", filename);
if (logFile != null && !"".equals(logFile)) {
System.out.println(message);
writeFile(message, logFile);
}
} else {
System.out.println(Messages.getInstance().getErrorString("CommandLineProcessor.ERROR_0002_INVALID_RESPONSE"));
}
}
use of com.sun.jersey.api.client.WebResource.Builder in project ORCID-Source by ORCID.
the class Orcid20APIClient method fetchPublicRecord.
/**
* Fetches the public record from the ORCID API v2.0
*
* Caches based on message.
*
* @param orcid
* @return Record
*/
public Record fetchPublicRecord(BaseMessage message) throws LockedRecordException, DeprecatedRecordException {
Record cached = v2PerMessageQueue.getIfPresent(message);
if (cached != null) {
return cached;
}
WebResource webResource = jerseyClient.resource(baseUri).path(message.getOrcid() + "/record");
webResource.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
Builder builder = webResource.accept(MediaType.APPLICATION_XML).header("Authorization", "Bearer " + accessToken);
ClientResponse response = builder.get(ClientResponse.class);
if (response.getStatus() != 200) {
OrcidError orcidError = null;
switch(response.getStatus()) {
case 301:
orcidError = response.getEntity(OrcidError.class);
throw new DeprecatedRecordException(orcidError);
case 409:
orcidError = response.getEntity(OrcidError.class);
throw new LockedRecordException(orcidError);
default:
LOG.error("Unable to fetch public record " + message.getOrcid() + " on API 2.0 HTTP error code: " + response.getStatus());
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
}
cached = response.getEntity(Record.class);
v2PerMessageQueue.put(message, cached);
return cached;
}
use of com.sun.jersey.api.client.WebResource.Builder in project eureka by Netflix.
the class AbstractJerseyEurekaHttpClient method sendHeartBeat.
@Override
public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id, InstanceInfo info, InstanceStatus overriddenStatus) {
String urlPath = "apps/" + appName + '/' + id;
ClientResponse response = null;
try {
WebResource webResource = jerseyClient.resource(serviceUrl).path(urlPath).queryParam("status", info.getStatus().toString()).queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString());
if (overriddenStatus != null) {
webResource = webResource.queryParam("overriddenstatus", overriddenStatus.name());
}
Builder requestBuilder = webResource.getRequestBuilder();
addExtraHeaders(requestBuilder);
response = requestBuilder.put(ClientResponse.class);
EurekaHttpResponseBuilder<InstanceInfo> eurekaResponseBuilder = anEurekaHttpResponse(response.getStatus(), InstanceInfo.class).headers(headersOf(response));
if (response.hasEntity() && !HTML.equals(response.getType().getSubtype())) {
// don't try and deserialize random html errors from the server
eurekaResponseBuilder.entity(response.getEntity(InstanceInfo.class));
}
return eurekaResponseBuilder.build();
} finally {
if (logger.isDebugEnabled()) {
logger.debug("Jersey HTTP PUT {}{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
}
if (response != null) {
response.close();
}
}
}
Aggregations