本文整理汇总了Java中org.apache.http.HttpEntityEnclosingRequest类的典型用法代码示例。如果您正苦于以下问题:Java HttpEntityEnclosingRequest类的具体用法?Java HttpEntityEnclosingRequest怎么用?Java HttpEntityEnclosingRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpEntityEnclosingRequest类属于org.apache.http包,在下文中一共展示了HttpEntityEnclosingRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: retryRequest
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Decide about retry #" + executionCount + " for exception " + exception.getMessage());
}
if (executionCount >= _maxRetryCount) {
// Do not retry if over max retry count
return false;
} else if (exception instanceof NoHttpResponseException) {
// Retry if the server dropped connection on us
return true;
} else if (exception instanceof SSLHandshakeException) {
// Do not retry on SSL handshake exception
return false;
}
HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
// Retry if the request is considered idempotent
return idempotent;
}
示例2: retryRequest
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 5) {// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof InterruptedIOException) {// 超时
return false;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {// SSL握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
示例3: testSimpleSigner
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@Test
public void testSimpleSigner() throws Exception {
HttpEntityEnclosingRequest request =
new BasicHttpEntityEnclosingRequest("GET", "/query?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"));
}
开发者ID:awslabs,项目名称:aws-request-signing-apache-interceptor,代码行数:18,代码来源:AWSRequestSigningApacheInterceptorTest.java
示例4: testEncodedUriSigner
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@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());
}
开发者ID:awslabs,项目名称:aws-request-signing-apache-interceptor,代码行数:19,代码来源:AWSRequestSigningApacheInterceptorTest.java
示例5: process
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@Override
public void process(final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
final HttpRequestAttachment.Builder builder = create("Request", request.getRequestLine().getUri())
.withMethod(request.getRequestLine().getMethod());
Stream.of(request.getAllHeaders())
.forEach(header -> builder.withHeader(header.getName(), header.getValue()));
if (request instanceof HttpEntityEnclosingRequest) {
final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
entity.writeTo(os);
final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
builder.withBody(body);
}
final HttpRequestAttachment requestAttachment = builder.build();
processor.addAttachment(requestAttachment, renderer);
}
示例6: executeInternal
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@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);
}
final HttpResponseFutureCallback callback = new HttpResponseFutureCallback();
final Future<HttpResponse> futureResponse =
this.httpClient.execute(this.httpRequest, this.httpContext, callback);
return new ClientHttpResponseFuture(futureResponse, callback);
}
示例7: process
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException
("HTTP request may not be null.");
}
if ((request instanceof HttpEntityEnclosingRequest) &&
!request.containsHeader(HTTP.DATE_HEADER)) {
String httpdate = DATE_GENERATOR.getCurrentDate();
request.setHeader(HTTP.DATE_HEADER, httpdate);
}
}
示例8: process
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
// Do not send the expect header if request body is known to be empty
if (entity != null && entity.getContentLength() != 0) {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (HttpProtocolParams.useExpectContinue(request.getParams())
&& !ver.lessEquals(HttpVersion.HTTP_1_0)) {
request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
}
}
}
}
示例9: retryRequest
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 3) {// 如果已经重试了3次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return true;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {// ssl握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
示例10: pushContent
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
private void pushContent(HttpUriRequest request, String contentType, String contentEncoding, byte[] content) {
// TODO: check other preconditions?
if (contentType != null && content != null && request instanceof HttpEntityEnclosingRequest) {
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(content));
entity.setContentLength(content.length);
entity.setChunked(false);
if (contentEncoding != null)
entity.setContentEncoding(contentEncoding);
entity.setContentType(contentType);
HttpEntityEnclosingRequest rr = (HttpEntityEnclosingRequest) request;
rr.setEntity(entity);
}
}
示例11: retryRequest
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
public boolean retryRequest(
IOException exception,
int executionCount,
HttpContext context) {
if (executionCount >= 5) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
return false;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof ConnectTimeoutException) {
// Connection refused
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
return true;
}
return false;
}
示例12: retryRequest
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
/**
* 自定义的恢复策略
*/
public boolean retryRequest(IOException exception, int exceptionCount,
HttpContext context) {
if (exceptionCount >= 3)
return false;
if (exception instanceof InterruptedIOException) {
return false;
}
if (exception instanceof UnknownHostException) {
return false;
}
if (exception instanceof ConnectTimeoutException) {
return false;
}
if (exception instanceof SSLException) {
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
return true;
}
return false;
}
示例13: process
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
Args.notNull(request, "HTTP request");
if (!request.containsHeader(HTTP.EXPECT_DIRECTIVE)) {
if (request instanceof HttpEntityEnclosingRequest) {
final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
final HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
// Do not send the expect header if request body is known to be empty
if (entity != null
&& entity.getContentLength() != 0 && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig config = clientContext.getRequestConfig();
if (config.isExpectContinueEnabled()) {
request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
}
}
}
}
}
示例14: HttpMethodReleaseInputStream
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
/**
* Constructs an input stream based on an {@link HttpMethod} object
* representing an HTTP connection. If a connection input stream is
* available, this constructor wraps the underlying input stream and makes
* that stream available. If no underlying connection is available, an empty
* {@link ByteArrayInputStream} is made available.
*
* @param httpMethod
* The HTTP method being executed, whose response content is to
* be wrapped.
*/
public HttpMethodReleaseInputStream(HttpEntityEnclosingRequest httpMethod) {
this.httpRequest = httpMethod;
try {
this.inputStream = httpMethod.getEntity().getContent();
} catch (IOException e) {
if (log.isWarnEnabled()) {
log.warn("Unable to obtain HttpMethod's response data stream", e);
}
try {
httpMethod.getEntity().getContent().close();
} catch (Exception ex) {}
this.inputStream = new ByteArrayInputStream(new byte[] {}); // Empty input stream;
}
}
示例15: createPrimeZombieWithFileRequest
import org.apache.http.HttpEntityEnclosingRequest; //导入依赖的package包/类
@Test
public void createPrimeZombieWithFileRequest() throws Exception {
final File file = new File(getClass().getClassLoader().getResource("example-priming.json").getFile());
final HttpUriRequest primeZombieRequest = requestFactory.createPrimeZombieWithFileRequest(file);
assertThat(primeZombieRequest.getMethod()).isEqualTo("POST");
assertThat(primeZombieRequest.getURI().toString()).isEqualTo(zombieBaseUrl);
assertZombieHeader(primeZombieRequest, "zombie", "priming-file");
final HttpEntityEnclosingRequest httpPost = (HttpEntityEnclosingRequest)primeZombieRequest;
final String entityString = IOUtils.toString(httpPost.getEntity().getContent(), defaultCharset());
final String fileString = IOUtils.toString(new FileInputStream(file), defaultCharset());
assertThat(entityString).contains(fileString);
}