本文整理匯總了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);
}