Search in sources :

Example 1 with Response

use of org.apache.hbase.thirdparty.javax.ws.rs.core.Response in project hbase by apache.

the class RESTApiClusterManager method doRoleCommand.

/**
 * Issues a command (e.g. starting or stopping a role).
 * @return the commandId of a successfully submitted asynchronous command.
 */
private long doRoleCommand(String serviceName, String roleName, RoleCommand roleCommand) {
    URI uri = UriBuilder.fromUri(serverHostname).path("api").path(API_VERSION).path("clusters").path(clusterName).path("services").path(serviceName).path("roleCommands").path(roleCommand.toString()).build();
    String body = "{ \"items\": [ \"" + roleName + "\" ] }";
    LOG.trace("Executing POST against {} with body {} ...", uri, body);
    WebTarget webTarget = client.target(uri);
    Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
    Response response = invocationBuilder.post(Entity.json(body));
    final int statusCode = response.getStatus();
    final String responseBody = response.readEntity(String.class);
    if (statusCode != Response.Status.OK.getStatusCode()) {
        LOG.warn("RoleCommand failed with status code {} and response body {}", statusCode, responseBody);
        throw new HTTPException(statusCode);
    }
    LOG.trace("POST against {} completed with status code {} and response body {}", uri, statusCode, responseBody);
    return parser.parse(responseBody).getAsJsonObject().get("items").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsLong();
}
Also used : Response(org.apache.hbase.thirdparty.javax.ws.rs.core.Response) HTTPException(javax.xml.ws.http.HTTPException) Invocation(org.apache.hbase.thirdparty.javax.ws.rs.client.Invocation) WebTarget(org.apache.hbase.thirdparty.javax.ws.rs.client.WebTarget) URI(java.net.URI)

Example 2 with Response

use of org.apache.hbase.thirdparty.javax.ws.rs.core.Response in project hbase by apache.

the class RESTApiClusterManager method getFromURIGet.

private String getFromURIGet(URI uri) {
    LOG.trace("Executing GET against {} ...", uri);
    final Response response = client.target(uri).request(MediaType.APPLICATION_JSON_TYPE).get();
    int statusCode = response.getStatus();
    final String responseBody = response.readEntity(String.class);
    if (statusCode != Response.Status.OK.getStatusCode()) {
        LOG.warn("request failed with status code {} and response body {}", statusCode, responseBody);
        throw new HTTPException(statusCode);
    }
    // This API folds information as the value to an "items" attribute.
    LOG.trace("GET against {} completed with status code {} and response body {}", uri, statusCode, responseBody);
    return responseBody;
}
Also used : Response(org.apache.hbase.thirdparty.javax.ws.rs.core.Response) HTTPException(javax.xml.ws.http.HTTPException)

Aggregations

HTTPException (javax.xml.ws.http.HTTPException)2 Response (org.apache.hbase.thirdparty.javax.ws.rs.core.Response)2 URI (java.net.URI)1 Invocation (org.apache.hbase.thirdparty.javax.ws.rs.client.Invocation)1 WebTarget (org.apache.hbase.thirdparty.javax.ws.rs.client.WebTarget)1