Java Code Examples for org.apache.http.HttpEntityEnclosingRequest#setEntity()

The following examples show how to use org.apache.http.HttpEntityEnclosingRequest#setEntity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RESTClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a HTTP DELETE request with entity body to the specified URL.
 *
 * @param url     Target endpoint URL
 * @param headers Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
                                        final String payload, String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    HTTPUtils.setHTTPHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return httpclient.execute(request);
}
 
Example 2
Source File: HttpComponentsClientHttpRequest.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
	addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
		entityEnclosingRequest.setEntity(requestEntity);
	}
	HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
	return new HttpComponentsClientHttpResponse(httpResponse);
}
 
Example 3
Source File: HttpComponentsAsyncClientHttpRequest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
		throws IOException {

	HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
		entityEnclosingRequest.setEntity(requestEntity);
	}

	HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
	Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
	return new ClientHttpResponseFuture(futureResponse, callback);
}
 
Example 4
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP DELETE request with entity body to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
        final String payload, String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 5
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP PUT request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doPut(String url, final Map<String, String> headers,
                          final String payload, String contentType) throws IOException {
    HttpUriRequest request = new HttpPut(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes());
            out.flush();
            out.close();
        }
    });
    ent.setContentType(contentType);
    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 6
Source File: AWSRequestSigningApacheInterceptorTest.java    From aws-request-signing-apache-interceptor with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodedUriSigner() throws Exception {
    HttpEntityEnclosingRequest request =
            new BasicHttpEntityEnclosingRequest("GET", "/foo-2017-02-25%2Cfoo-2017-02-26/_search?a=b");
    request.setEntity(new StringEntity("I'm an entity"));
    request.addHeader("foo", "bar");
    request.addHeader("content-length", "0");

    HttpCoreContext context = new HttpCoreContext();
    context.setTargetHost(HttpHost.create("localhost"));

    createInterceptor().process(request, context);

    assertEquals("bar", request.getFirstHeader("foo").getValue());
    assertEquals("wuzzle", request.getFirstHeader("Signature").getValue());
    assertNull(request.getFirstHeader("content-length"));
    assertEquals("/foo-2017-02-25%2Cfoo-2017-02-26/_search", request.getFirstHeader("resourcePath").getValue());
}
 
Example 7
Source File: Solr6Index.java    From atlas with Apache License 2.0 6 votes vote down vote up
private void configureSolrClientsForKerberos() throws PermanentBackendException {
    String kerberosConfig = System.getProperty("java.security.auth.login.config");
    if(kerberosConfig == null) {
        throw new PermanentBackendException("Unable to configure kerberos for solr client. System property 'java.security.auth.login.config' is not set.");
    }
    logger.debug("Using kerberos configuration file located at '{}'.", kerberosConfig);
    try(Krb5HttpClientBuilder krbBuild = new Krb5HttpClientBuilder()) {

        SolrHttpClientBuilder kb = krbBuild.getBuilder();
        HttpClientUtil.setHttpClientBuilder(kb);
        HttpRequestInterceptor bufferedEntityInterceptor = new HttpRequestInterceptor() {
            @Override
            public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                if(request instanceof HttpEntityEnclosingRequest) {
                    HttpEntityEnclosingRequest enclosingRequest = ((HttpEntityEnclosingRequest) request);
                    HttpEntity requestEntity = enclosingRequest.getEntity();
                    enclosingRequest.setEntity(new BufferedHttpEntity(requestEntity));
                }
            }
        };
        HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor);

        HttpRequestInterceptor preemptiveAuth = new PreemptiveAuth(new KerberosScheme());
        HttpClientUtil.addRequestInterceptor(preemptiveAuth);
    }
}
 
Example 8
Source File: RESTClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a HTTP DELETE request with entity body to the specified URL.
 *
 * @param url     Target endpoint URL
 * @param headers Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
                                        final String payload, String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    HTTPUtils.setHTTPHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return httpclient.execute(request);
}
 
Example 9
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP PATCH request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doPatch(String url, final Map<String, String> headers,
                            final String payload, String contentType) throws IOException {
    HttpUriRequest request = new HttpPatch(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes());
            out.flush();
            out.close();
        }
    });
    ent.setContentType(contentType);
    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 10
Source File: HttpComponentsAsyncClientHttpRequest.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
		throws IOException {

	HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
		entityEnclosingRequest.setEntity(requestEntity);
	}

	HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
	Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
	return new ClientHttpResponseFuture(futureResponse, callback);
}
 
Example 11
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
		throws IOException {

	HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
		entityEnclosingRequest.setEntity(requestEntity);
	}

	HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
	Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
	return new ClientHttpResponseFuture(futureResponse, callback);
}
 
Example 12
Source File: SimpleHttpClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP DELETE request with entity body to the specified URL
 *
 * @param url     Target endpoint URL
 * @param headers Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers, final String payload,
                                        String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 13
Source File: AopHttpClient.java    From ArgusAPM with Apache License 2.0 5 votes vote down vote up
private static HttpUriRequest handleRequest(HttpUriRequest request, NetInfo data) {
    data.setURL(request.getURI().toString());
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        if (entityRequest.getEntity() != null) {
            entityRequest.setEntity(new AopHttpRequestEntity(entityRequest.getEntity(), data));
        }
        return (HttpUriRequest) entityRequest;
    }
    return request;
}
 
Example 14
Source File: HttpComponentsClientHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
	addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
		entityEnclosingRequest.setEntity(requestEntity);
	}
	HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
	return new HttpComponentsClientHttpResponse(httpResponse);
}
 
Example 15
Source File: PreemptiveAuthHttpClientConnection.java    From git-client-plugin with MIT License 5 votes vote down vote up
private void execute() throws IOException, ClientProtocolException {
    if (resp == null)
        if (entity != null) {
            if (req instanceof HttpEntityEnclosingRequest) {
                HttpEntityEnclosingRequest eReq = (HttpEntityEnclosingRequest) req;
                eReq.setEntity(entity);
            }
            resp = getClient().execute(req);
            entity.getBuffer().close();
            entity = null;
        } else
            resp = getClient().execute(req);
}
 
Example 16
Source File: HttpComponentsStreamingClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
	HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest && this.body != null) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new StreamingHttpEntity(getHeaders(), this.body);
		entityEnclosingRequest.setEntity(requestEntity);
	}

	HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
	return new HttpComponentsClientHttpResponse(httpResponse);
}
 
Example 17
Source File: HttpComponentsClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
	addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
		entityEnclosingRequest.setEntity(requestEntity);
	}
	HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
	return new HttpComponentsClientHttpResponse(httpResponse);
}
 
Example 18
Source File: EntityForwardingRedirectStrategyTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetRedirect() throws ProtocolException
{
    mockHeader();
    when(statusLine.getStatusCode()).thenReturn(STATUS_CODE);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    HttpEntityEnclosingRequest httpRequest = new HttpPost(URI);
    StringEntity requestStringEntity = new StringEntity("{key:value}", StandardCharsets.UTF_8);
    httpRequest.setEntity(requestStringEntity);
    HttpEntityEnclosingRequest actualRedirect = (HttpEntityEnclosingRequest) redirectStrategy
            .getRedirect(httpRequest, httpResponse, httpContext);
    assertEquals(REDIRECT_URI, actualRedirect.getRequestLine().getUri());
    assertEquals(requestStringEntity, actualRedirect.getEntity());
}
 
Example 19
Source File: HttpComponentsStreamingClientHttpRequest.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
	HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);

	if (this.httpRequest instanceof HttpEntityEnclosingRequest && this.body != null) {
		HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
		HttpEntity requestEntity = new StreamingHttpEntity(getHeaders(), this.body);
		entityEnclosingRequest.setEntity(requestEntity);
	}

	HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
	return new HttpComponentsClientHttpResponse(httpResponse);
}
 
Example 20
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Send a HTTP OPTIONS request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doOptions(String url, final Map<String, String> headers,
                              final String payload, String contentType) throws IOException {
    HttpUriRequest request = new HttpOptions(url);
    setHeaders(headers, request);
    if(payload != null) {
        HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
        final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

        EntityTemplate ent = new EntityTemplate(new ContentProducer() {
            public void writeTo(OutputStream outputStream) throws IOException {
                OutputStream out = outputStream;
                if (zip) {
                    out = new GZIPOutputStream(outputStream);
                }
                out.write(payload.getBytes());
                out.flush();
                out.close();
            }
        });
        ent.setContentType(contentType);
        if (zip) {
            ent.setContentEncoding("gzip");
        }
        entityEncReq.setEntity(ent);
    }
    return client.execute(request);
}