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

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

Introduction

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

Prototype

public abstract String getMethod();

Source Link

Usage

From source file:com.gsma.mobileconnect.impl.RequestTokenTest.java

@Test
public void requestToken_withAllOptionsSet_shouldCreateExpectedRequest()
        throws OIDCException, DiscoveryResponseExpiredException, IOException, RestException {
    // GIVEN/*from  w  w w  .  j  a v  a 2s.com*/
    RestClient mockedRestClient = mock(RestClient.class);
    IOIDC ioidc = Factory.getOIDC(mockedRestClient);
    CaptureRequestTokenResponse captureRequestTokenResponse = new CaptureRequestTokenResponse();
    DiscoveryResponse discoveryResponse = new DiscoveryResponse(true, new Date(Long.MAX_VALUE), 0, null,
            parseJson(OPERATOR_JSON_STRING));

    TokenOptions options = new TokenOptions();
    options.setTimeout(333);

    final CaptureHttpRequestBase captureHttpRequestBase = new CaptureHttpRequestBase();
    final RestResponse restResponse = new RestResponse(null, 0, null, "{}");

    doAnswer(new Answer() {
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            HttpRequestBase requestBase = (HttpRequestBase) args[0];
            captureHttpRequestBase.setValue(requestBase);
            return restResponse;
        }
    }).when(mockedRestClient).callRestEndPoint(any(HttpRequestBase.class), any(HttpClientContext.class),
            eq(options.getTimeout()), Matchers.<List<KeyValuePair>>any());

    String expectedRedirectURI = "expected-redirect-uri";
    String expectedCode = "expected-code";

    // WHEN
    ioidc.requestToken(discoveryResponse, expectedRedirectURI, expectedCode, options,
            captureRequestTokenResponse);

    // THEN
    HttpRequestBase request = captureHttpRequestBase.getValue();

    assertEquals(TOKEN_HREF, request.getURI().toString());
    assertEquals("POST", request.getMethod());
    assertEquals("application/x-www-form-urlencoded", request.getFirstHeader("Content-Type").getValue());
    assertEquals("application/json", request.getFirstHeader("accept").getValue());

    assertTrue(request instanceof HttpPost);

    HttpPost postRequest = (HttpPost) request;
    List<NameValuePair> contents = URLEncodedUtils.parse(postRequest.getEntity());

    assertEquals(expectedCode, findValueOfName(contents, "code"));
    assertEquals("authorization_code", findValueOfName(contents, "grant_type"));
    assertEquals(expectedRedirectURI, findValueOfName(contents, "redirect_uri"));
}

From source file:com.mondora.chargify.controller.ChargifyAdapter.java

protected HttpResponse executeHttpMethod(HttpRequestBase method) throws IOException {
    if (logger.isDebugEnabled())
        logger.debug(method.getMethod() + " " + method.getURI().toString());
    return getClient().execute(host, method);
}

From source file:anhttpclient.impl.HttpWebResponse.java

/**
 * Constructor. response code and original apache {@link HttpResponse} and {@link HttpRequestBase}
 * should be specified for wrapping http response
 *
 * @param httpReponse  http response to get request status code and request body
 * @param httpRequestBase original apache {@link HttpRequestBase} to get request URL
 * @param responseBodyCharset excpected charset of the response body
 *//*from w  ww.ja v  a 2 s.  c om*/
public HttpWebResponse(HttpResponse httpReponse, HttpRequestBase httpRequestBase, String responseBodyCharset) {
    this.responseBodyCharset = responseBodyCharset;
    this.responseCode = httpReponse.getStatusLine().getStatusCode();
    setResponseHeaders(httpReponse);
    try {
        url = new URL(httpRequestBase.getURI().toString());

        //HTTP HEAD request should returns only headers without body
        if (!RequestMethod.HEAD.toString().equals(httpRequestBase.getMethod())) {
            setResponseBody(httpReponse);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.dataconnector.client.fetchrequest.HttpFetchStrategy.java

/**
 * Executes an HTTP GET/POST and return the response.
 * @param request The request.//from  w w w  . java 2s .  c o  m
 * @return The HTTP response.
 * @throws StrategyException
 */
HttpResponse getHttpResponse(FetchRequest request) throws StrategyException {
    HttpRequestBase httpMethod = null;
    try {
        httpMethod = getMethod(request);
        if (httpMethod != null) {
            copyHeaders(request, httpMethod);
            return httpClient.execute(httpMethod);
        } else {
            throw new StrategyException(request.getId() + ": Unknown method.");
        }
    } catch (IOException e) {
        String method = (httpMethod != null) ? httpMethod.getMethod() : "Unknown";
        throw new StrategyException(request.getId() + ": while executing HTTP " + method + ": ", e);
    }
}

From source file:net.fizzl.redditengine.impl.SimpleHttpClient.java

/**
 * Add modhash to outgoing headers if it is available
 * @param http   HTTP request//from www. j av  a2 s.com
 */
void addModhash(HttpRequestBase http) {
    if (lastModhash != null && !lastModhash.isEmpty()) {
        http.addHeader(new BasicHeader(UrlUtils.X_MODHASH, lastModhash));
        Log.d(getClass().getName(), "adding modhash " + lastModhash + " to outgoing headers");
    }
    Log.d(http.getMethod(), http.getURI().toString());
}

From source file:com.mondora.chargify.controller.ChargifyAdapter.java

protected String executeHttp(HttpRequestBase method) throws ChargifyException, IOException {
    if (logger.isDebugEnabled())
        logger.debug(method.getMethod() + " " + method.getURI().toString());
    HttpResponse response = executeHttpMethod(method);
    return handleResponseCode(response, method);
}

From source file:com.mondora.chargify.controller.ChargifyAdapter.java

protected String handleResponseCode(HttpResponse response, HttpRequestBase method) throws ChargifyException {
    StatusLine line = response.getStatusLine();
    String errorMsg = method.getMethod() + " " + String.valueOf(method.getURI()) + " Error "
            + String.valueOf(line.getStatusCode()) + " " + line.getReasonPhrase();
    int code = line.getStatusCode();
    if (isError(code)) {
        try {//from  ww w .  j a v  a 2 s  . c om
            Errors error = (Errors) parse(Errors.class, response, method);
            if (error != null) {
                errorMsg = String.valueOf(error) + ". Method " + errorMsg;
            }
        } catch (Exception ex) {
            errorMsg += " " + ex.getMessage() + " " + ex.getStackTrace()[0];
        }
    }
    return handleResponse(code, errorMsg);
}

From source file:org.apache.jmeter.protocol.http.control.CacheManager.java

/**
 * Check the cache, and if there is a match, set the headers:
 * <ul>/*w w w .  ja  v a2s .c  o m*/
 * <li>If-Modified-Since</li>
 * <li>If-None-Match</li>
 * </ul>
 * Apache HttpClient version.
 * @param url {@link URL} to look up in cache
 * @param request where to set the headers
 */
public void setHeaders(URL url, HttpRequestBase request) {
    CacheEntry entry = getCache().get(url.toString());
    if (log.isDebugEnabled()) {
        log.debug(request.getMethod() + "(OAH) " + url.toString() + " " + entry);
    }
    if (entry != null) {
        final String lastModified = entry.getLastModified();
        if (lastModified != null) {
            request.setHeader(HTTPConstants.IF_MODIFIED_SINCE, lastModified);
        }
        final String etag = entry.getEtag();
        if (etag != null) {
            request.setHeader(HTTPConstants.IF_NONE_MATCH, etag);
        }
    }
}

From source file:net.fizzl.redditengine.impl.SimpleHttpClient.java

/**
 * Common functionality for all HTTP requests
 *//*  w  ww .  j a va2  s  .  c  o m*/
private InputStream execute(HttpRequestBase request)
        throws ClientProtocolException, IOException, UnexpectedHttpResponseException {
    addModhash(request);
    HttpResponse response = mClient.execute(request, mHttpContext);

    Header contentType = response.getFirstHeader("Content-Type");
    Log.d("Received(" + request.getMethod() + ") ", contentType.getName() + ": " + contentType.getValue());

    checkHeaders(response);
    checkStatusline(response.getStatusLine());
    InputStream is = checkContent(response);

    return is;
}