Search in sources :

Example 21 with RequestBuilder

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

the class RESTWorkItemHandler method configureRequest.

protected Object configureRequest(String method, String urlStr, Map<String, Object> params, String acceptHeaderValue) {
    if (HTTP_CLIENT_API_43) {
        RequestBuilder builder = null;
        if ("GET".equals(method)) {
            builder = RequestBuilder.get().setUri(urlStr);
            addAcceptHeader(builder, acceptHeaderValue);
        } else if ("POST".equals(method)) {
            builder = RequestBuilder.post().setUri(urlStr);
            setBody(builder, params);
            addAcceptHeader(builder, acceptHeaderValue);
        } else if ("PUT".equals(method)) {
            builder = RequestBuilder.put().setUri(urlStr);
            setBody(builder, params);
            addAcceptHeader(builder, acceptHeaderValue);
        } else if ("DELETE".equals(method)) {
            builder = RequestBuilder.delete().setUri(urlStr);
        } else {
            throw new IllegalArgumentException("Method " + method + " is not supported");
        }
        return builder;
    } else {
        HttpRequestBase theMethod = null;
        if ("GET".equals(method)) {
            theMethod = new HttpGet(urlStr);
            addAcceptHeader(theMethod, acceptHeaderValue);
        } else if ("POST".equals(method)) {
            theMethod = new HttpPost(urlStr);
            setBody(theMethod, params);
            addAcceptHeader(theMethod, acceptHeaderValue);
        } else if ("PUT".equals(method)) {
            theMethod = new HttpPut(urlStr);
            setBody(theMethod, params);
            addAcceptHeader(theMethod, acceptHeaderValue);
        } else if ("DELETE".equals(method)) {
            theMethod = new HttpDelete(urlStr);
        } else {
            throw new IllegalArgumentException("Method " + method + " is not supported");
        }
        return theMethod;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpPut(org.apache.http.client.methods.HttpPut)

Example 22 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 URISyntaxException {
    RequestBuilder requestBuilder = RequestBuilder.create(request.httpMethod().name());
    // per request timeouts
    RequestConfig requestConfig = (client instanceof Configurable ? RequestConfig.copy(((Configurable) client).getConfig()) : 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());
    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);
    } else {
        requestBuilder.setEntity(new ByteArrayEntity(new byte[0]));
    }
    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) Configurable(org.apache.http.client.methods.Configurable) 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 23 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 24 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 25 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project incubator-gobblin by apache.

the class ApacheHttpRequestBuilder method buildWriteRequest.

/**
 * Build a write request from a single record
 */
private ApacheHttpRequest<GenericRecord> buildWriteRequest(BufferedRecord<GenericRecord> record) {
    if (record == null) {
        return null;
    }
    ApacheHttpRequest<GenericRecord> request = new ApacheHttpRequest<>();
    HttpOperation httpOperation = HttpUtils.toHttpOperation(record.getRecord());
    // Set uri
    URI uri = HttpUtils.buildURI(urlTemplate, httpOperation.getKeys(), httpOperation.getQueryParams());
    if (uri == null) {
        return null;
    }
    RequestBuilder builder = RequestBuilder.create(verb.toUpperCase());
    builder.setUri(uri);
    // Set headers
    Map<String, String> headers = httpOperation.getHeaders();
    if (headers != null && headers.size() != 0) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
            builder.setHeader(header.getKey(), header.getValue());
        }
    }
    // Add payload
    int bytesWritten = addPayload(builder, httpOperation.getBody());
    if (bytesWritten == -1) {
        throw new RuntimeException("Fail to write payload into request");
    }
    request.setRawRequest(build(builder));
    request.markRecord(record, bytesWritten);
    return request;
}
Also used : AsyncRequestBuilder(org.apache.gobblin.async.AsyncRequestBuilder) RequestBuilder(org.apache.http.client.methods.RequestBuilder) GenericRecord(org.apache.avro.generic.GenericRecord) URI(java.net.URI) Map(java.util.Map)

Aggregations

RequestBuilder (org.apache.http.client.methods.RequestBuilder)25 StringEntity (org.apache.http.entity.StringEntity)9 Map (java.util.Map)7 URI (java.net.URI)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 CompilationUnit (org.apache.asterix.testframework.xml.TestCase.CompilationUnit)4 HttpEntity (org.apache.http.HttpEntity)4 NameValuePair (org.apache.http.NameValuePair)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 HttpHost (org.apache.http.HttpHost)2 HttpResponse (org.apache.http.HttpResponse)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 Configurable (org.apache.http.client.methods.Configurable)2 URIBuilder (org.apache.http.client.utils.URIBuilder)2 ContentType (org.apache.http.entity.ContentType)2