Search in sources :

Example 76 with Client

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;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) LoggingFilter(com.sun.jersey.api.client.filter.LoggingFilter) WebResource(com.sun.jersey.api.client.WebResource) Client(com.sun.jersey.api.client.Client) WebServiceClient(code.satyagraha.gfm.support.api.WebServiceClient) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 77 with Client

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);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Client(com.sun.jersey.api.client.Client) Predicate(java.util.function.Predicate)

Example 78 with Client

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;
}
Also used : WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Client(com.sun.jersey.api.client.Client)

Example 79 with Client

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;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) YarnClient(org.apache.hadoop.yarn.client.api.YarnClient) Client(com.sun.jersey.api.client.Client)

Example 80 with Client

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);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) PrintStream(java.io.PrintStream) InputStream(java.io.InputStream) ContainerLogsRequest(org.apache.hadoop.yarn.logaggregation.ContainerLogsRequest) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) YarnClient(org.apache.hadoop.yarn.client.api.YarnClient) Client(com.sun.jersey.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Private(org.apache.hadoop.classification.InterfaceAudience.Private)

Aggregations

Client (com.sun.jersey.api.client.Client)87 ClientResponse (com.sun.jersey.api.client.ClientResponse)61 Test (org.junit.Test)59 URI (java.net.URI)51 TimelineEntity (org.apache.hadoop.yarn.api.records.timelineservice.TimelineEntity)36 Set (java.util.Set)30 WebResource (com.sun.jersey.api.client.WebResource)19 HashSet (java.util.HashSet)19 GenericType (com.sun.jersey.api.client.GenericType)17 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)9 TimelineMetric (org.apache.hadoop.yarn.api.records.timelineservice.TimelineMetric)9 JSONObject (org.codehaus.jettison.json.JSONObject)7 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)6 URLConnectionClientHandler (com.sun.jersey.client.urlconnection.URLConnectionClientHandler)4 ArrayList (java.util.ArrayList)4 FlowRunEntity (org.apache.hadoop.yarn.api.records.timelineservice.FlowRunEntity)4 YarnClient (org.apache.hadoop.yarn.client.api.YarnClient)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)3 JSONException (org.codehaus.jettison.json.JSONException)3