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

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

Introduction

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

Prototype

public String getMethod() 

Source Link

Usage

From source file:com.wudaosoft.net.httpclient.ParameterRequestBuilder.java

public static HttpUriRequest build(RequestBuilder builder) {

    final HttpRequestBase result;
    URI uriNotNull = builder.getUri() != null ? builder.getUri() : URI.create("/");
    Charset charset = builder.getCharset();
    charset = charset != null ? charset : HTTP.DEF_CONTENT_CHARSET;
    String method = builder.getMethod();
    List<NameValuePair> parameters = builder.getParameters();
    HttpEntity entityCopy = builder.getEntity();

    if (parameters != null && !parameters.isEmpty()) {
        if (entityCopy == null && (HttpPost.METHOD_NAME.equalsIgnoreCase(method)
                || HttpPut.METHOD_NAME.equalsIgnoreCase(method)
                || HttpPatch.METHOD_NAME.equalsIgnoreCase(method))) {
            entityCopy = new UrlEncodedFormEntity(parameters, charset);
        } else {/*  w  w w.j a  v a 2  s.  com*/
            try {
                uriNotNull = new URIBuilder(uriNotNull).setCharset(charset).addParameters(parameters).build();
            } catch (final URISyntaxException ex) {
                // should never happen
            }
        }
    }

    if (entityCopy == null) {
        result = new InternalRequest(method);
    } else {
        final InternalEntityEclosingRequest request = new InternalEntityEclosingRequest(method);
        request.setEntity(entityCopy);
        result = request;
    }
    result.setProtocolVersion(builder.getVersion());
    result.setURI(uriNotNull);
    // if (builder.headergroup != null) {
    // result.setHeaders(builder.headergroup.getAllHeaders());
    // }
    result.setConfig(builder.getConfig());
    return result;
}

From source file:com.nexmo.client.voice.SendDtmfMethodTest.java

@Test
public void makeRequest() throws Exception {
    HttpWrapper httpWrapper = new HttpWrapper();
    SendDtmfMethod methodUnderTest = new SendDtmfMethod(httpWrapper);

    RequestBuilder request = methodUnderTest.makeRequest(new DtmfRequest("abc-123", "867"));

    assertEquals("PUT", request.getMethod());
    assertEquals("application/json", request.getFirstHeader("Content-Type").getValue());

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode node = objectMapper.readValue(request.getEntity().getContent(), JsonNode.class);
    LOG.info(request.getEntity().getContent());
    assertEquals("867", node.get("digits").asText());
}

From source file:com.nexmo.client.voice.endpoints.ModifyCallMethodTest.java

@Test
public void makeRequest() throws Exception {
    HttpWrapper httpWrapper = new HttpWrapper();
    ModifyCallMethod methodUnderTest = new ModifyCallMethod(httpWrapper);

    RequestBuilder request = methodUnderTest.makeRequest(new CallModifier("abc-123", "hangup"));

    assertEquals("PUT", request.getMethod());
    assertEquals("application/json", request.getFirstHeader("Content-Type").getValue());

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode node = objectMapper.readValue(request.getEntity().getContent(), JsonNode.class);
    LOG.info(request.getEntity().getContent());
    assertEquals("hangup", node.get("action").asText());
}

From source file:com.nexmo.client.voice.endpoints.ListCallsMethodTest.java

@Test
public void makeRequestWithNoFilter() throws Exception {
    RequestBuilder request = method.makeRequest(null);
    assertEquals("GET", request.getMethod());
    assertEquals("https://api.nexmo.com/v1/calls", request.getUri().toString());
}

From source file:com.nexmo.client.voice.endpoints.ListCallsMethodTest.java

@Test
public void makeRequestWithFilter() throws Exception {
    CallsFilter callsFilter = new CallsFilter();
    callsFilter.setPageSize(3);//ww w .ja  v a 2 s . c om
    RequestBuilder request = method.makeRequest(callsFilter);
    assertEquals("GET", request.getMethod());
    assertEquals("https://api.nexmo.com/v1/calls?page_size=3", request.getUri().toString());
}

From source file:com.nexmo.client.voice.endpoints.CreateCallMethodTest.java

@Test
public void testMakeRequest() throws Exception {
    CreateCallMethod methodUnderTest = new CreateCallMethod(null);

    // Execute test call:
    RequestBuilder request = methodUnderTest
            .makeRequest(new Call("447700900903", "447700900904", "https://example.com/answer"));

    assertEquals("POST", request.getMethod());
    assertEquals("application/json", request.getFirstHeader("Content-Type").getValue());

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode node = objectMapper.readValue(request.getEntity().getContent(), JsonNode.class);
    LOG.info(request.getEntity().getContent());
    assertEquals("447700900903", node.get("to").get(0).get("number").asText());
    assertEquals("447700900904", node.get("from").get("number").asText());
    assertEquals("https://example.com/answer", node.get("answer_url").get(0).asText());
}

From source file:org.bonitasoft.connectors.rest.RESTConnector.java

/**
 * Execute a given request//from   w ww .  java  2s.  co  m
 * 
 * @param request The request to execute
 * @return The response of the executed request
 * @throws Exception any exception that might occur
 */
public void execute(final Request request) throws Exception {
    CloseableHttpClient httpClient = null;

    try {
        final URL url = request.getUrl();
        final String urlHost = url.getHost();

        final Builder requestConfigurationBuilder = RequestConfig.custom();
        requestConfigurationBuilder.setConnectionRequestTimeout(CONNECTION_TIMEOUT);
        requestConfigurationBuilder.setRedirectsEnabled(request.isRedirect());

        final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
        setSSL(request.getSsl(), httpClientBuilder);
        setProxy(request.getProxy(), httpClientBuilder, requestConfigurationBuilder);
        setCookies(requestConfigurationBuilder, httpClientBuilder, request.getCookies(), urlHost);

        final RequestBuilder requestBuilder = getRequestBuilderFromMethod(request.getRestMethod());
        requestBuilder.setVersion(
                new ProtocolVersion(HTTP_PROTOCOL, HTTP_PROTOCOL_VERSION_MAJOR, HTTP_PROTOCOL_VERSION_MINOR));
        int urlPort = url.getPort();
        if (url.getPort() == -1) {
            urlPort = url.getDefaultPort();
        }
        final String urlProtocol = url.getProtocol();
        final String urlStr = url.toString();
        requestBuilder.setUri(urlStr);
        setHeaders(requestBuilder, request.getHeaders());
        if (!HTTPMethod.GET.equals(HTTPMethod.valueOf(requestBuilder.getMethod()))) {
            final String body = request.getBody();
            if (body != null) {
                requestBuilder.setEntity(new StringEntity(request.getBody(), request.getContentType()));
            }
        }

        final HttpContext httpContext = setAuthorizations(requestConfigurationBuilder,
                request.getAuthorization(), request.getProxy(), urlHost, urlPort, urlProtocol,
                httpClientBuilder);

        requestBuilder.setConfig(requestConfigurationBuilder.build());
        httpClientBuilder.setDefaultRequestConfig(requestConfigurationBuilder.build());

        final HttpUriRequest httpRequest = requestBuilder.build();
        httpClient = httpClientBuilder.build();
        final CloseableHttpResponse httpResponse = httpClient.execute(httpRequest, httpContext);
        LOGGER.fine("Request sent.");
        setOutputs(httpResponse, request);
    } finally {
        try {
            if (httpClient != null) {
                httpClient.close();
            }
        } catch (final IOException ex) {
            logException(ex);
        }
    }
}