org.apache.http.HttpEntityEnclosingRequest#setEntity ( )源码实例Demo

下面列出了org.apache.http.HttpEntityEnclosingRequest#setEntity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: micro-integrator   文件: SimpleHttpClient.java
/**
 * 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);
}
 
源代码2 项目: micro-integrator   文件: RESTClient.java
/**
 * 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);
}
 
@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);
}
 
@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);
}
 
源代码5 项目: product-ei   文件: SimpleHttpClient.java
/**
 * 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);
}
 
源代码6 项目: product-ei   文件: RESTClient.java
/**
 * 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);
}
 
源代码7 项目: atlas   文件: Solr6Index.java
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);
    }
}
 
@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());
}
 
源代码9 项目: product-ei   文件: SimpleHttpClient.java
/**
 * 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);
}
 
源代码10 项目: product-ei   文件: SimpleHttpClient.java
/**
 * 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);
}
 
@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);
}
 
@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);
}
 
@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());
}
 
@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);
}
 
@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);
}
 
@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);
}
 
源代码17 项目: ArgusAPM   文件: AopHttpClient.java
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;
}
 
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);
}
 
@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);
}
 
源代码20 项目: product-ei   文件: SimpleHttpClient.java
/**
 * 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);
}