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:org.apache.gobblin.HttpTestUtils.java

public static void assertEqual(RequestBuilder actual, RequestBuilder expect) throws IOException {
    // Check entity
    HttpEntity actualEntity = actual.getEntity();
    HttpEntity expectedEntity = expect.getEntity();
    if (actualEntity == null) {
        Assert.assertTrue(expectedEntity == null);
    } else {//from   w w w  .  java 2 s  .  c o m
        Assert.assertEquals(actualEntity.getContentLength(), expectedEntity.getContentLength());
        String actualContent = IOUtils.toString(actualEntity.getContent(), StandardCharsets.UTF_8);
        String expectedContent = IOUtils.toString(expectedEntity.getContent(), StandardCharsets.UTF_8);
        Assert.assertEquals(actualContent, expectedContent);
    }

    // Check request
    HttpUriRequest actualRequest = actual.build();
    HttpUriRequest expectedRequest = expect.build();
    Assert.assertEquals(actualRequest.getMethod(), expectedRequest.getMethod());
    Assert.assertEquals(actualRequest.getURI().toString(), expectedRequest.getURI().toString());

    Header[] actualHeaders = actualRequest.getAllHeaders();
    Header[] expectedHeaders = expectedRequest.getAllHeaders();
    Assert.assertEquals(actualHeaders.length, expectedHeaders.length);
    for (int i = 0; i < actualHeaders.length; i++) {
        Assert.assertEquals(actualHeaders[i].toString(), expectedHeaders[i].toString());
    }
}

From source file:spark.protocol.SparqlCall.java

/**
 * Add headers to a request.//  w  w w  .java2 s .c  o  m
 * @param req The request to set the headers on.
 */
static void addHeaders(HttpUriRequest req, String mimeType) {
    if (POST.equalsIgnoreCase(req.getMethod())) {
        req.addHeader(CONTENT_TYPE, FORM_ENCODED);
    }
    if (mimeType != null)
        req.setHeader(ACCEPT, mimeType);
}

From source file:org.elasticsearch.client.RequestLogger.java

/**
 * Creates curl output for given request
 *///w ww .  j a  v a2s. c  o  m
static String buildTraceRequest(HttpUriRequest request, HttpHost host) throws IOException {
    String requestLine = "curl -iX " + request.getMethod() + " '" + host + getUri(request.getRequestLine())
            + "'";
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
        if (enclosingRequest.getEntity() != null) {
            requestLine += " -d '";
            HttpEntity entity = enclosingRequest.getEntity();
            if (entity.isRepeatable() == false) {
                entity = new BufferedHttpEntity(enclosingRequest.getEntity());
                enclosingRequest.setEntity(entity);
            }
            requestLine += EntityUtils.toString(entity, StandardCharsets.UTF_8) + "'";
        }
    }
    return requestLine;
}

From source file:com.helger.httpclient.HttpDebugger.java

/**
 * Call before an invocation//from   w  ww.j a v  a  2  s . c o m
 *
 * @param aRequest
 *        The request to be executed. May not be <code>null</code>.
 * @param aHttpContext
 *        The special HTTP content for this call. May be <code>null</code>.
 */
public static void beforeRequest(@Nonnull final HttpUriRequest aRequest,
        @Nullable final HttpContext aHttpContext) {
    if (isEnabled())
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Before HTTP call: " + aRequest.getMethod() + " " + aRequest.getURI()
                    + (aHttpContext != null ? " (with special HTTP context)" : ""));
}

From source file:org.elasticsearch.client.RequestLogger.java

static String buildWarningMessage(HttpUriRequest request, HttpHost host, Header[] warnings) {
    StringBuilder message = new StringBuilder("request [").append(request.getMethod()).append(" ").append(host)
            .append(getUri(request.getRequestLine())).append("] returned ").append(warnings.length)
            .append(" warnings: ");
    for (int i = 0; i < warnings.length; i++) {
        if (i > 0) {
            message.append(",");
        }//  www .jav  a 2s. c om
        message.append("[").append(warnings[i].getValue()).append("]");
    }
    return message.toString();
}

From source file:org.elasticsearch.client.RequestLogger.java

/**
 * Logs a request that failed/*from   w  ww .  j  a v a  2 s . c o m*/
 */
static void logFailedRequest(Log logger, HttpUriRequest request, HttpHost host, Exception e) {
    if (logger.isDebugEnabled()) {
        logger.debug(
                "request [" + request.getMethod() + " " + host + getUri(request.getRequestLine()) + "] failed",
                e);
    }
    if (tracer.isTraceEnabled()) {
        String traceRequest;
        try {
            traceRequest = buildTraceRequest(request, host);
        } catch (IOException e1) {
            tracer.trace("error while reading request for trace purposes", e);
            traceRequest = "";
        }
        tracer.trace(traceRequest);
    }
}

From source file:org.elasticsearch.client.RequestLogger.java

/**
 * Logs a request that yielded a response
 *//* www. j  a  v a  2  s .c om*/
static void logResponse(Log logger, HttpUriRequest request, HttpHost host, HttpResponse httpResponse) {
    if (logger.isDebugEnabled()) {
        logger.debug("request [" + request.getMethod() + " " + host + getUri(request.getRequestLine())
                + "] returned [" + httpResponse.getStatusLine() + "]");
    }
    if (logger.isWarnEnabled()) {
        Header[] warnings = httpResponse.getHeaders("Warning");
        if (warnings != null && warnings.length > 0) {
            logger.warn(buildWarningMessage(request, host, warnings));
        }
    }
    if (tracer.isTraceEnabled()) {
        String requestLine;
        try {
            requestLine = buildTraceRequest(request, host);
        } catch (IOException e) {
            requestLine = "";
            tracer.trace("error while reading request for trace purposes", e);
        }
        String responseLine;
        try {
            responseLine = buildTraceResponse(httpResponse);
        } catch (IOException e) {
            responseLine = "";
            tracer.trace("error while reading response for trace purposes", e);
        }
        tracer.trace(requestLine + '\n' + responseLine);
    }
}

From source file:org.dasein.cloud.azurepack.tests.HttpMethodAsserts.java

private static void assertHttpMethod(HttpUriRequest actualHttpRequest, String expectedHttpMethod,
        String expectedUrl) {/*from  w  w  w.j  a va 2  s  . com*/
    assertEquals(String.format("Method does not performs a HTTP %s", expectedHttpMethod), expectedHttpMethod,
            actualHttpRequest.getMethod());
    assertEquals(String.format("Method does not performs a HTTP %s at the correct URL", expectedHttpMethod),
            expectedUrl, actualHttpRequest.getURI().toString());
}

From source file:org.soyatec.windowsazure.internal.MessageCanonicalizer.java

/**
 * Create a canonicalized string with the httpRequest and resourceUriComponents. 
 * /*from w  ww. ja v  a  2  s. com*/
 * @param request
 *          The HttpWebRequest object.
 * @param uriComponents
 *          Components of the Uri extracted out of the request.
 * @return A canonicalized string of the HTTP request.
 */
public static String canonicalizeHttpRequest(HttpRequest request, ResourceUriComponents uriComponents) {
    if (!(request instanceof HttpUriRequest)) {
        throw new IllegalArgumentException("Request should be a URI http request");
    }
    HttpUriRequest rq = (HttpUriRequest) request;
    return canonicalizeHttpRequest(rq.getURI(), uriComponents, rq.getMethod(),
            HttpUtilities.parseRequestContentType(rq), Utilities.emptyString(),
            HttpUtilities.parseHttpHeaders(rq));
}

From source file:org.sonatype.nexus.apachehttpclient.page.Page.java

/**
 * Returns a page for given HTTP request.
 *///w  w  w .  j av a2s.  com
public static Page buildPageFor(final PageContext context, final HttpUriRequest httpUriRequest)
        throws IOException {
    checkNotNull(context);
    checkNotNull(httpUriRequest);
    // TODO: detect redirects
    LOG.debug("Executing HTTP {} request against {}", httpUriRequest.getMethod(), httpUriRequest.getURI());
    final HttpResponse response = context.executeHttpRequest(httpUriRequest);
    try {
        if (context.isExpectedResponse(response)) {
            if (response.getEntity() != null) {
                return new Page(httpUriRequest, response, Jsoup.parse(response.getEntity().getContent(), null,
                        httpUriRequest.getURI().toString()));
            } else {
                // no body
                return new Page(httpUriRequest, response, null);
            }
        } else {
            throw new UnexpectedPageResponse(httpUriRequest.getURI().toString(), response.getStatusLine());
        }
    } finally {
        EntityUtils.consumeQuietly(response.getEntity());
    }
}