Search in sources :

Example 6 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project dropwizard by dropwizard.

the class DropwizardApacheConnector method buildApacheRequest.

/**
     * Build a new Apache's {@link org.apache.http.client.methods.HttpUriRequest}
     * from Jersey's {@link org.glassfish.jersey.client.ClientRequest}
     * <p>
     * Convert a method, URI, body, headers and override a user-agent if necessary
     * </p>
     *
     * @param jerseyRequest representation of an HTTP request in Jersey
     * @return a new {@link org.apache.http.client.methods.HttpUriRequest}
     */
private HttpUriRequest buildApacheRequest(ClientRequest jerseyRequest) {
    final RequestBuilder builder = RequestBuilder.create(jerseyRequest.getMethod()).setUri(jerseyRequest.getUri()).setEntity(getHttpEntity(jerseyRequest));
    for (String headerName : jerseyRequest.getHeaders().keySet()) {
        builder.addHeader(headerName, jerseyRequest.getHeaderString(headerName));
    }
    final Optional<RequestConfig> requestConfig = addJerseyRequestConfig(jerseyRequest);
    requestConfig.ifPresent(builder::setConfig);
    return builder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) RequestBuilder(org.apache.http.client.methods.RequestBuilder)

Example 7 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project feign by OpenFeign.

the class ApacheHttpClient method toHttpUriRequest.

HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
    RequestBuilder requestBuilder = RequestBuilder.create(request.method());
    //per request timeouts
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(options.connectTimeoutMillis()).setSocketTimeout(options.readTimeoutMillis()).build();
    requestBuilder.setConfig(requestConfig);
    URI uri = new URIBuilder(request.url()).build();
    requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
    //request query params
    List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name());
    for (NameValuePair queryParam : queryParams) {
        requestBuilder.addParameter(queryParam);
    }
    //request headers
    boolean hasAcceptHeader = false;
    for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
        String headerName = headerEntry.getKey();
        if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
            hasAcceptHeader = true;
        }
        if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
            // doesn't like us to set it as well.
            continue;
        }
        for (String headerValue : headerEntry.getValue()) {
            requestBuilder.addHeader(headerName, headerValue);
        }
    }
    //some servers choke on the default accept string, so we'll set it to anything
    if (!hasAcceptHeader) {
        requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
    }
    //request body
    if (request.body() != null) {
        HttpEntity entity = null;
        if (request.charset() != null) {
            ContentType contentType = getContentType(request);
            String content = new String(request.body(), request.charset());
            entity = new StringEntity(content, contentType);
        } else {
            entity = new ByteArrayEntity(request.body());
        }
        requestBuilder.setEntity(entity);
    }
    return requestBuilder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NameValuePair(org.apache.http.NameValuePair) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project cloudstack by apache.

the class HttpUriRequestBuilder method build.

public HttpUriRequest build() {
    validate();
    final RequestBuilder builder = RequestBuilder.create(method.toString()).setUri(buildUri());
    if (!methodParameters.isEmpty()) {
        for (final Entry<String, String> entry : methodParameters.entrySet()) {
            builder.addParameter(entry.getKey(), entry.getValue());
        }
    }
    if (jsonPayload.isPresent()) {
        builder.addHeader(new BasicHeader(CONTENT_TYPE, JSON_CONTENT_TYPE)).setEntity(new StringEntity(jsonPayload.get(), ContentType.create(JSON_CONTENT_TYPE, Consts.UTF_8)));
    }
    return builder.build();
}
Also used : StringEntity(org.apache.http.entity.StringEntity) RequestBuilder(org.apache.http.client.methods.RequestBuilder) BasicHeader(org.apache.http.message.BasicHeader)

Example 9 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project asterixdb by apache.

the class TestExecutor method constructPostMethod.

private HttpUriRequest constructPostMethod(URI uri, List<CompilationUnit.Parameter> params) {
    RequestBuilder builder = RequestBuilder.post(uri);
    for (CompilationUnit.Parameter param : params) {
        builder.addParameter(param.getName(), param.getValue());
    }
    builder.setCharset(StandardCharsets.UTF_8);
    return builder.build();
}
Also used : CompilationUnit(org.apache.asterix.testframework.xml.TestCase.CompilationUnit) RequestBuilder(org.apache.http.client.methods.RequestBuilder)

Example 10 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project asterixdb by apache.

the class TestExecutor method constructPostMethodJson.

protected HttpUriRequest constructPostMethodJson(String statement, URI uri, String stmtParam, List<CompilationUnit.Parameter> otherParams) {
    if (stmtParam == null) {
        throw new NullPointerException("Statement parameter required.");
    }
    RequestBuilder builder = RequestBuilder.post(uri);
    ObjectMapper om = new ObjectMapper();
    ObjectNode content = om.createObjectNode();
    for (CompilationUnit.Parameter param : upsertParam(otherParams, stmtParam, statement)) {
        content.put(param.getName(), param.getValue());
    }
    try {
        builder.setEntity(new StringEntity(om.writeValueAsString(content), ContentType.APPLICATION_JSON));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    builder.setCharset(StandardCharsets.UTF_8);
    return builder.build();
}
Also used : CompilationUnit(org.apache.asterix.testframework.xml.TestCase.CompilationUnit) StringEntity(org.apache.http.entity.StringEntity) RequestBuilder(org.apache.http.client.methods.RequestBuilder) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

RequestBuilder (org.apache.http.client.methods.RequestBuilder)11 CompilationUnit (org.apache.asterix.testframework.xml.TestCase.CompilationUnit)4 StringEntity (org.apache.http.entity.StringEntity)4 RequestConfig (org.apache.http.client.config.RequestConfig)3 URI (java.net.URI)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 HttpServer (com.github.dreamhead.moco.HttpServer)1 Runnable (com.github.dreamhead.moco.Runnable)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 ContentType (org.apache.http.entity.ContentType)1