Example usage for org.apache.http.client.methods HttpUriRequest getMethod

List of usage examples for org.apache.http.client.methods HttpUriRequest getMethod

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest getMethod.

Prototype

String getMethod();

Source Link

Document

Returns the HTTP method this request uses, such as <code>GET</code>, <code>PUT</code>, <code>POST</code>, or other.

Usage

From source file:com.cloud.utils.rest.HttpUriRequestBuilderTest.java

@Test
public void testBuildRequestWithParameters() throws Exception {
    final HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("key1", "value1");
    final HttpUriRequest request = HttpUriRequestBuilder.create().method(HttpMethods.GET).path("/path")
            .parameters(parameters).build();

    assertThat(request, notNullValue());
    assertThat(request.getURI().getPath(), equalTo("/path"));
    assertThat(request.getURI().getQuery(), equalTo("key1=value1"));
    assertThat(request.getURI().getScheme(), nullValue());
    assertThat(request.getURI().getHost(), nullValue());
    assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
}

From source file:com.cloud.network.nicira.NiciraRestClient.java

private CloseableHttpResponse execute(final HttpUriRequest request, final int previousStatusCode)
        throws CloudstackRESTException {
    if (counter.hasReachedExecutionLimit()) {
        throw new CloudstackRESTException("Reached max executions limit of " + executionLimit);
    }//ww w  .j  a va 2s .  co m
    counter.incrementExecutionCounter();
    s_logger.debug(
            "Executing " + request.getMethod() + " request [execution count = " + counter.getValue() + "]");
    final CloseableHttpResponse response = super.execute(request);

    final StatusLine statusLine = response.getStatusLine();
    final int statusCode = statusLine.getStatusCode();
    s_logger.debug("Status of last request: " + statusLine.toString());
    if (HttpStatusCodeHelper.isUnauthorized(statusCode)) {
        return handleUnauthorizedResponse(request, previousStatusCode, response, statusCode);
    } else if (HttpStatusCodeHelper.isSuccess(statusCode)) {
        return handleSuccessResponse(request, response);
    } else if (HttpStatusCodeHelper.isConflict(statusCode)) {
        throw new CloudstackRESTException("Conflict: " + statusLine.getReasonPhrase(), statusCode);
    } else {
        throw new CloudstackRESTException("Unexpected status code: " + statusCode, statusCode);
    }
}

From source file:com.cloud.utils.rest.HttpUriRequestBuilderTest.java

@Test
public void testBuildRequestWithJsonPayload() throws Exception {
    final HttpUriRequest request = HttpUriRequestBuilder.create().method(HttpMethods.GET).path("/path")
            .jsonPayload(Optional.of("{'key1':'value1'}")).build();

    assertThat(request, notNullValue());
    assertThat(request.getURI().getPath(), equalTo("/path"));
    assertThat(request.getURI().getScheme(), nullValue());
    assertThat(request.getURI().getQuery(), nullValue());
    assertThat(request.getURI().getHost(), nullValue());
    assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
    assertThat(request.containsHeader(HttpConstants.CONTENT_TYPE), equalTo(true));
    assertThat(request.getFirstHeader(HttpConstants.CONTENT_TYPE).getValue(),
            equalTo(HttpConstants.JSON_CONTENT_TYPE));
    assertThat(request, HttpUriRequestPayloadMatcher.hasPayload("{'key1':'value1'}"));
}

From source file:com.fujitsu.dc.test.jersey.DcRestAdapter.java

/**
 * ?.//from   w w w  .  j a  v a 2s.  c o m
 * @param req ??Request
 * @param body ??
 */
private void debugHttpRequest(final HttpUriRequest req, final String body) {
    log.debug(req.getURI());
    if (log.isDebugEnabled()) {
        log.debug("?Request " + req.getMethod() + "  " + req.getURI());
        Header[] headers = req.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            log.debug("RequestHeader[" + headers[i].getName() + "] : " + headers[i].getValue());
        }
        log.debug("RequestBody:  " + body);
    }
}

From source file:org.eclipse.aether.transport.http.HttpTransporter.java

private void prepare(HttpUriRequest request, SharingHttpContext context) {
    boolean put = HttpPut.METHOD_NAME.equalsIgnoreCase(request.getMethod());
    if (state.getWebDav() == null && (put || isPayloadPresent(request))) {
        try {/* ww w. ja v  a 2s .c o  m*/
            HttpOptions req = commonHeaders(new HttpOptions(request.getURI()));
            HttpResponse response = client.execute(server, req, context);
            state.setWebDav(isWebDav(response));
            EntityUtils.consumeQuietly(response.getEntity());
        } catch (IOException e) {
            LOGGER.debug("Failed to prepare HTTP context", e);
        }
    }
    if (put && Boolean.TRUE.equals(state.getWebDav())) {
        mkdirs(request.getURI(), context);
    }
}

From source file:com.okta.sdk.framework.ApiClient.java

/**
 * Logs the request method and URI to the console.
 * @param  httpUriRequest {@link HttpUriRequest} Current HTTP URI request object.
 *//*from  ww  w.j a  v  a2s.  c o m*/
protected void logRequest(HttpUriRequest httpUriRequest) {
    LOGGER.info(String.format("%s %s", httpUriRequest.getMethod(), httpUriRequest.getURI()));
}

From source file:jetbrains.buildServer.commitPublisher.github.api.impl.GitHubApiImpl.java

private void logRequest(@NotNull HttpUriRequest request, @Nullable String requestEntity) throws IOException {
    if (!LOG.isDebugEnabled())
        return;/*from  w  ww  . j  av  a 2 s .c  o  m*/

    if (requestEntity == null) {
        requestEntity = "<none>";
    }

    LOG.debug("Calling GitHub with:\n" + "  requestURL: " + request.getURI().toString() + "\n"
            + "  requestMethod: " + request.getMethod() + "\n" + "  requestEntity: " + requestEntity);
}

From source file:org.obiba.opal.rest.client.magma.OpalJavaClient.java

private HttpResponse execute(HttpUriRequest msg) throws IOException {
    msg.addHeader("Accept", "application/x-protobuf, text/html");
    authenticate(msg);//from w  w  w . j a  v  a2  s  .c  o  m
    log.debug("{} {}", msg.getMethod(), msg.getURI());
    if (log.isTraceEnabled()) {
        for (Header allHeader : msg.getAllHeaders()) {
            log.trace("  {} {}", allHeader.getName(), allHeader.getValue());
        }
    }
    try {
        return getClient().execute(msg, ctx);
    } finally {
        cleanupCache();
    }
}

From source file:org.graphity.core.util.jena.HttpOp.java

private static void exec(String url, HttpUriRequest request, String acceptHeader, HttpResponseHandler handler,
        HttpClient httpClient, HttpContext httpContext, HttpAuthenticator authenticator) {
    try {/* w ww.  j  a va  2s .  com*/
        if (handler == null)
            // This cleans up. 
            handler = nullHandler;

        long id = counter.incrementAndGet();
        String requestURI = determineRequestURI(url);
        String baseURI = determineBaseIRI(url);
        if (log.isDebugEnabled())
            log.debug(format("[%d] %s %s", id, request.getMethod(), request.getURI().toString()));
        // Accept
        if (acceptHeader != null)
            request.addHeader(HttpNames.hAccept, acceptHeader);

        // Prepare and execute
        httpClient = ensureClient(httpClient, authenticator);
        httpContext = ensureContext(httpContext);
        applyAuthentication(asAbstractClient(httpClient), url, httpContext, authenticator);
        HttpResponse response = httpClient.execute(request, httpContext);

        // Response
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (HttpSC.isClientError(statusCode) || HttpSC.isServerError(statusCode)) {
            log.debug(format("[%d] %s %s", id, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
            // Error responses can have bodies so it is important to clear up. 
            EntityUtils.consume(response.getEntity());
            throw new HttpException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }
        // Redirects are followed by HttpClient.
        if (handler != null)
            handler.handle(baseURI, response);
    } catch (IOException ex) {
        throw new HttpException(ex);
    }
}