Search in sources :

Example 16 with RequestBuilder

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

the class RDF4JProtocolSession method getUpdateMethod.

@Override
protected HttpUriRequest getUpdateMethod(QueryLanguage ql, String update, String baseURI, Dataset dataset, boolean includeInferred, int maxExecutionTime, Binding... bindings) {
    RequestBuilder builder = null;
    String transactionURL = getTransactionURL();
    if (transactionURL != null) {
        builder = RequestBuilder.put(transactionURL);
        builder.addHeader("Content-Type", Protocol.SPARQL_UPDATE_MIME_TYPE + "; charset=utf-8");
        builder.addParameter(Protocol.ACTION_PARAM_NAME, Action.UPDATE.toString());
        for (NameValuePair nvp : getUpdateMethodParameters(ql, null, baseURI, dataset, includeInferred, maxExecutionTime, bindings)) {
            builder.addParameter(nvp);
        }
        // in a PUT request, we carry the only actual update string as the
        // request body - the rest is sent as request parameters
        builder.setEntity(new StringEntity(update, UTF8));
        pingTransaction();
    } else {
        builder = RequestBuilder.post(getUpdateURL());
        builder.addHeader("Content-Type", Protocol.FORM_MIME_TYPE + "; charset=utf-8");
        builder.setEntity(new UrlEncodedFormEntity(getUpdateMethodParameters(ql, update, baseURI, dataset, includeInferred, maxExecutionTime, bindings), UTF8));
    }
    // applications
    for (Map.Entry<String, String> additionalHeader : getAdditionalHttpHeaders().entrySet()) {
        builder.addHeader(additionalHeader.getKey(), additionalHeader.getValue());
    }
    return builder.build();
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) StringEntity(org.apache.http.entity.StringEntity) RequestBuilder(org.apache.http.client.methods.RequestBuilder) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Map(java.util.Map)

Example 17 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project spring-cloud-netflix by spring-cloud.

the class RibbonApacheHttpRequestTests method testEntity.

void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws IOException {
    String lengthString = String.valueOf(entityValue.length());
    Long length = null;
    URI uri = URI.create("http://example.com");
    LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    if (addContentLengthHeader) {
        headers.add("Content-Length", lengthString);
        length = (long) entityValue.length();
    }
    RibbonRequestCustomizer requestCustomizer = new RibbonRequestCustomizer<RequestBuilder>() {

        @Override
        public boolean accepts(Class builderClass) {
            return builderClass == RequestBuilder.class;
        }

        @Override
        public void customize(RequestBuilder builder) {
            builder.addHeader("from-customizer", "foo");
        }
    };
    RibbonCommandContext context = new RibbonCommandContext("example", method, uri.toString(), false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
    context.setContentLength(length);
    RibbonApacheHttpRequest httpRequest = new RibbonApacheHttpRequest(context);
    HttpUriRequest request = httpRequest.toRequest(RequestConfig.custom().build());
    assertThat("request is wrong type", request, is(instanceOf(HttpEntityEnclosingRequest.class)));
    assertThat("uri is wrong", request.getURI().toString(), startsWith(uri.toString()));
    if (addContentLengthHeader) {
        assertThat("Content-Length is missing", request.getFirstHeader("Content-Length"), is(notNullValue()));
        assertThat("Content-Length is wrong", request.getFirstHeader("Content-Length").getValue(), is(equalTo(lengthString)));
    }
    assertThat("from-customizer is missing", request.getFirstHeader("from-customizer"), is(notNullValue()));
    assertThat("from-customizer is wrong", request.getFirstHeader("from-customizer").getValue(), is(equalTo("foo")));
    HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
    assertThat("entity is missing", entityRequest.getEntity(), is(notNullValue()));
    HttpEntity entity = entityRequest.getEntity();
    assertThat("contentLength is wrong", entity.getContentLength(), is(equalTo((long) entityValue.length())));
    assertThat("content is missing", entity.getContent(), is(notNullValue()));
    String string = StreamUtils.copyToString(entity.getContent(), Charset.forName("UTF-8"));
    assertThat("content is wrong", string, is(equalTo(entityValue)));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpEntity(org.apache.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) URI(java.net.URI) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

Example 18 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project spring-cloud-netflix by spring-cloud.

the class RibbonApacheHttpRequest method toRequest.

public HttpUriRequest toRequest(final RequestConfig requestConfig) {
    final RequestBuilder builder = RequestBuilder.create(this.context.getMethod());
    builder.setUri(this.uri);
    for (final String name : this.context.getHeaders().keySet()) {
        final List<String> values = this.context.getHeaders().get(name);
        for (final String value : values) {
            builder.addHeader(name, value);
        }
    }
    for (final String name : this.context.getParams().keySet()) {
        final List<String> values = this.context.getParams().get(name);
        for (final String value : values) {
            builder.addParameter(name, value);
        }
    }
    if (this.context.getRequestEntity() != null) {
        final BasicHttpEntity entity;
        entity = new BasicHttpEntity();
        entity.setContent(this.context.getRequestEntity());
        // if the entity contentLength isn't set, transfer-encoding will be set
        // to chunked in org.apache.http.protocol.RequestContent. See gh-1042
        Long contentLength = this.context.getContentLength();
        if ("GET".equals(this.context.getMethod()) && (contentLength == null || contentLength < 0)) {
            entity.setContentLength(0);
        } else if (contentLength != null) {
            entity.setContentLength(contentLength);
        }
        builder.setEntity(entity);
    }
    customize(this.context.getRequestCustomizers(), builder);
    builder.setConfig(requestConfig);
    return builder.build();
}
Also used : RequestBuilder(org.apache.http.client.methods.RequestBuilder) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 19 with RequestBuilder

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

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 20 with RequestBuilder

use of org.apache.http.client.methods.RequestBuilder in project core-ng-project by neowu.

the class HTTPClientImpl method httpRequest.

HttpUriRequest httpRequest(HTTPRequest request) {
    HTTPMethod method = request.method();
    String uri = request.uri();
    logger.debug("[request] method={}, uri={}", method, uri);
    RequestBuilder builder = RequestBuilder.create(method.name());
    try {
        builder.setUri(uri);
    } catch (IllegalArgumentException e) {
        throw new HTTPClientException("uri is invalid, uri=" + uri, "INVALID_URL", e);
    }
    request.headers().forEach((name, value) -> {
        logger.debug("[request:header] {}={}", name, new FieldParam(name, value));
        builder.setHeader(name, value);
    });
    request.params().forEach((name, value) -> {
        logger.debug("[request:param] {}={}", name, value);
        builder.addParameter(name, value);
    });
    byte[] body = request.body();
    if (body != null) {
        ContentType contentType = request.contentType();
        logRequestBody(request, contentType);
        org.apache.http.entity.ContentType type = org.apache.http.entity.ContentType.create(contentType.mediaType(), contentType.charset().orElse(null));
        builder.setEntity(new ByteArrayEntity(request.body(), type));
    }
    return builder.build();
}
Also used : RequestBuilder(org.apache.http.client.methods.RequestBuilder) FieldParam(core.framework.impl.log.filter.FieldParam) ContentType(core.framework.http.ContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HTTPMethod(core.framework.http.HTTPMethod) HTTPClientException(core.framework.http.HTTPClientException)

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