use of com.sun.jersey.api.client.Client in project gfm_viewer by satyagraha.
the class WebServiceClientDefault method transform.
@Override
public String transform(String mdText) {
if (StringUtils.isBlank(config.getApiUrl())) {
String responseText = String.format("<pre>\n%s\n</pre>", StringEscapeUtils.escapeHtml4(mdText));
return responseText;
}
Client client = getClient(config.getApiUrl());
LOGGER.fine("client: " + client);
String username = config.getUsername();
if (username != null && username.length() > 0) {
String password = config.getPassword();
client.removeFilter(null);
client.addFilter(new HTTPBasicAuthFilter(username, password));
}
client.addFilter(new LoggingFilter(LOGGER));
WebResource webResource = client.resource(config.getApiUrl());
Markdown markdown = new Markdown(mdText);
ClientResponse response = webResource.path("markdown").type(MediaType.APPLICATION_JSON).entity(markdown).accept(MediaType.TEXT_HTML).post(ClientResponse.class);
String responseText = response.getEntity(String.class);
return responseText;
}
use of com.sun.jersey.api.client.Client in project neo4j by neo4j.
the class RESTRequestGenerator method retrieveResponse.
/**
* Send the request and create the documentation.
*/
private ResponseEntity retrieveResponse(final String uri, final int responseCode, final MediaType type, final List<Pair<String, Predicate<String>>> headerFields, final ClientRequest request) {
RequestData data = new RequestData();
getRequestHeaders(data, request.getHeaders());
if (request.getEntity() != null) {
data.setPayload(String.valueOf(request.getEntity()));
}
Client client = new Client();
ClientResponse response = client.handle(request);
if (response.hasEntity() && response.getStatus() != 204) {
data.setEntity(response.getEntity(String.class));
}
if (response.getType() != null) {
assertTrue("wrong response type: " + data.entity, response.getType().isCompatible(type));
}
for (Pair<String, Predicate<String>> headerField : headerFields) {
assertTrue("wrong headers: " + response.getHeaders(), headerField.other().test(response.getHeaders().getFirst(headerField.first())));
}
data.setMethod(request.getMethod());
data.setUri(uri);
data.setStatus(responseCode);
assertEquals("Wrong response status. response: " + data.entity, responseCode, response.getStatus());
getResponseHeaders(data, response.getHeaders(), headerNames(headerFields));
return new ResponseEntity(response, data.entity);
}
use of com.sun.jersey.api.client.Client in project ORCID-Source by ORCID.
the class LoadFundRefData method fetchJsonFromGeoNames.
/**
* Queries GeoNames API for a given geonameId and return the JSON string
* */
private String fetchJsonFromGeoNames(String geoNameId) {
String result = null;
if (cache.containsKey("geoname_json_" + geoNameId)) {
return cache.get("geoname_json_" + geoNameId);
} else {
Client c = Client.create();
WebResource r = c.resource(geonamesApiUrl);
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("geonameId", geoNameId);
params.add("username", apiUser);
result = r.queryParams(params).get(String.class);
cache.put("geoname_json_" + geoNameId, result);
}
return result;
}
use of com.sun.jersey.api.client.Client in project hadoop by apache.
the class LogsCLI method getAMContainerInfoForAHSWebService.
private List<JSONObject> getAMContainerInfoForAHSWebService(Configuration conf, String appId) throws ClientHandlerException, UniformInterfaceException, JSONException {
Client webServiceClient = Client.create();
String webAppAddress = WebAppUtils.getHttpSchemePrefix(conf) + WebAppUtils.getAHSWebAppURLWithoutScheme(conf);
WebResource webResource = webServiceClient.resource(webAppAddress);
ClientResponse response = webResource.path("ws").path("v1").path("applicationhistory").path("apps").path(appId).path("appattempts").accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
JSONObject json = response.getEntity(JSONObject.class);
JSONArray requests = json.getJSONArray("appAttempt");
List<JSONObject> amContainersList = new ArrayList<JSONObject>();
for (int i = 0; i < requests.length(); i++) {
amContainersList.add(requests.getJSONObject(i));
}
Collections.reverse(amContainersList);
return amContainersList;
}
use of com.sun.jersey.api.client.Client in project hadoop by apache.
the class LogsCLI method printContainerLogsFromRunningApplication.
@Private
@VisibleForTesting
public int printContainerLogsFromRunningApplication(Configuration conf, ContainerLogsRequest request, LogCLIHelpers logCliHelper, boolean useRegex) throws IOException {
String containerIdStr = request.getContainerId().toString();
String localDir = request.getOutputLocalDir();
String nodeHttpAddress = request.getNodeHttpAddress();
if (nodeHttpAddress == null || nodeHttpAddress.isEmpty()) {
System.err.println("Can not get the logs for the container: " + containerIdStr);
System.err.println("The node http address is required to get container " + "logs for the Running application.");
return -1;
}
String nodeId = request.getNodeId();
PrintStream out = logCliHelper.createPrintStream(localDir, nodeId, containerIdStr);
try {
Set<String> matchedFiles = getMatchedContainerLogFiles(request, useRegex);
if (matchedFiles.isEmpty()) {
System.err.println("Can not find any log file matching the pattern: " + request.getLogTypes() + " for the container: " + containerIdStr + " within the application: " + request.getAppId());
return -1;
}
ContainerLogsRequest newOptions = new ContainerLogsRequest(request);
newOptions.setLogTypes(matchedFiles);
Client webServiceClient = Client.create();
boolean foundAnyLogs = false;
byte[] buffer = new byte[65536];
for (String logFile : newOptions.getLogTypes()) {
InputStream is = null;
try {
ClientResponse response = getResponeFromNMWebService(conf, webServiceClient, request, logFile);
if (response != null && response.getStatusInfo().getStatusCode() == ClientResponse.Status.OK.getStatusCode()) {
is = response.getEntityInputStream();
int len = 0;
while ((len = is.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.println();
} else {
out.println("Can not get any logs for the log file: " + logFile);
String msg = "Response from the NodeManager:" + nodeId + " WebService is " + ((response == null) ? "null" : "not successful," + " HTTP error code: " + response.getStatus() + ", Server response:\n" + response.getEntity(String.class));
out.println(msg);
}
out.flush();
foundAnyLogs = true;
} catch (ClientHandlerException | UniformInterfaceException ex) {
System.err.println("Can not find the log file:" + logFile + " for the container:" + containerIdStr + " in NodeManager:" + nodeId);
} finally {
IOUtils.closeQuietly(is);
}
}
if (foundAnyLogs) {
return 0;
} else {
return -1;
}
} finally {
logCliHelper.closePrintStream(out);
}
}
Aggregations