org.apache.http.protocol.HttpContext Java Examples
The following examples show how to use
org.apache.http.protocol.HttpContext.
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: RedirectStrategy.java From NetDiscovery with Apache License 2.0 | 7 votes |
@Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { URI uri = getLocationURI(request, response, context); String method = request.getRequestLine().getMethod(); if ("post".equalsIgnoreCase(method)) { try { HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request; httpRequestWrapper.setURI(uri); httpRequestWrapper.removeHeaders("Content-Length"); return httpRequestWrapper; } catch (Exception e) { log.error("强转为HttpRequestWrapper出错"); } return new HttpPost(uri); } else { return new HttpGet(uri); } }
Example #2
Source File: HttpClientTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@Test public void executeAsyncDelegatesToConfiguredAsyncClient() { // given HttpClient client = Mockito.spy(createDefaultTestObject()); CloseableHttpAsyncClient asyncClient = mockAsyncClient(client); BatchRequest request = createDefaultTestBatchRequest(); // when client.executeAsync(request, createMockTestResultHandler()); // then verify(client).getAsyncClient(); verify(asyncClient).execute( any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), any(HttpContext.class), any(FutureCallback.class)); }
Example #3
Source File: DefaultHttpClient.java From aws-xray-sdk-java with Apache License 2.0 | 6 votes |
@Override public CloseableHttpResponse execute( HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException { Subsegment subsegment = getRecorder().beginSubsegment(target.getHostName()); try { if (null != subsegment) { TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(target, request)); } CloseableHttpResponse response = super.execute(target, request, context); if (null != subsegment) { TracedResponseHandler.addResponseInformation(subsegment, response); } return response; } catch (Exception e) { if (null != subsegment) { subsegment.addException(e); } throw e; } finally { if (null != subsegment) { getRecorder().endSubsegment(); } } }
Example #4
Source File: AbstractHttpClientGenerator.java From cetty with Apache License 2.0 | 6 votes |
@Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { URI uri = getLocationURI(request, response, context); String method = request.getRequestLine().getMethod(); if (HttpConstants.POST.equalsIgnoreCase(method)) { try { HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request; httpRequestWrapper.setURI(uri); httpRequestWrapper.removeHeaders("Content-Length"); return httpRequestWrapper; } catch (Exception e) { e.printStackTrace(); } return new HttpPost(uri); } else { return new HttpGet(uri); } }
Example #5
Source File: HTTPTestValidationHandler.java From camel-kafka-connector with Apache License 2.0 | 6 votes |
@Override public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws IOException { lock.lock(); try { HttpEntity entity = ((HttpEntityEnclosingRequest) httpRequest).getEntity(); String content = EntityUtils.toString(entity); replies.add(content); if (replies.size() == expected) { receivedExpectedMessages.signal(); } httpResponse.setStatusCode(HttpStatus.SC_OK); } finally { lock.unlock(); } }
Example #6
Source File: UnixHttpClientTestCase.java From docker-java-api with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * UnixHttpClient can execute the HttpRequest with the given host * and context. * @throws IOException If something goes wrong. */ @Test public void executesRequestWithHostAndContext() throws IOException { final HttpHost host = new HttpHost("127.0.0.1"); final HttpRequest req = Mockito.mock(HttpRequest.class); final HttpContext context = Mockito.mock(HttpContext.class); final HttpResponse resp = Mockito.mock(HttpResponse.class); final HttpClient decorated = Mockito.mock(HttpClient.class); Mockito.when( decorated.execute(host, req, context) ).thenReturn(resp); final HttpClient unix = new UnixHttpClient(() -> decorated); MatcherAssert.assertThat( unix.execute(host, req, context), Matchers.is(resp) ); Mockito.verify( decorated, Mockito.times(1) ).execute(host, req, context); }
Example #7
Source File: EntityForwardingRedirectStrategy.java From vividus with Apache License 2.0 | 6 votes |
@Override public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { final String method = request.getRequestLine().getMethod(); HttpUriRequest redirect = super.getRedirect(request, response, context); if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)) { HttpPost httpPostRequest = new HttpPost(redirect.getURI()); if (request instanceof HttpEntityEnclosingRequest) { httpPostRequest.setEntity(((HttpEntityEnclosingRequest) request).getEntity()); } return httpPostRequest; } return redirect; }
Example #8
Source File: AbortedExceptionClientExecutionTimerIntegrationTest.java From ibm-cos-sdk-java with Apache License 2.0 | 6 votes |
@Test(expected = AbortedException.class) public void clientExecutionTimeoutEnabled_aborted_exception_occurs_timeout_not_expired() throws Exception { ClientConfiguration config = new ClientConfiguration() .withClientExecutionTimeout(CLIENT_EXECUTION_TIMEOUT) .withMaxErrorRetry(0); ConnectionManagerAwareHttpClient rawHttpClient = createRawHttpClientSpy(config); doThrow(new AbortedException()).when(rawHttpClient).execute(any (HttpRequestBase.class), any(HttpContext.class)); httpClient = new AmazonHttpClient(config, rawHttpClient, null); execute(httpClient, createMockGetRequest()); }
Example #9
Source File: MockedClientTests.java From ibm-cos-sdk-java with Apache License 2.0 | 6 votes |
@Test public void requestTimeoutEnabled_RequestCompletesWithinTimeout_EntityNotBufferedForStreamedResponse() throws Exception { ClientConfiguration config = new ClientConfiguration().withRequestTimeout(5 * 1000); ConnectionManagerAwareHttpClient rawHttpClient = createRawHttpClientSpy(config); HttpResponseProxy responseProxy = createHttpResponseProxySpy(); doReturn(responseProxy).when(rawHttpClient).execute(any(HttpRequestBase.class), any(HttpContext.class)); httpClient = new AmazonHttpClient(config, rawHttpClient, null); try { httpClient.requestExecutionBuilder().request(createMockGetRequest()).execute(new ErrorDuringUnmarshallingResponseHandler().leaveConnectionOpen()); fail("Exception expected"); } catch (AmazonClientException e) { } assertResponseWasNotBuffered(responseProxy); }
Example #10
Source File: UnixHttpClientTestCase.java From docker-java-api with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * UnixHttpClient can execute the HttpRequest with the given host, * response handler and context. * @throws IOException If something goes wrong. */ @Test public void executesRequestWithHostHandlerAndContext() throws IOException { final HttpHost host = new HttpHost("127.0.0.1"); final HttpRequest req = Mockito.mock(HttpRequest.class); final ResponseHandler<String> handler = Mockito.mock( ResponseHandler.class ); final HttpContext context = Mockito.mock(HttpContext.class); final HttpClient decorated = Mockito.mock(HttpClient.class); Mockito.when( decorated.execute(host, req, handler, context) ).thenReturn("executed"); final HttpClient unix = new UnixHttpClient(() -> decorated); MatcherAssert.assertThat( unix.execute(host, req, handler, context), Matchers.equalTo("executed") ); Mockito.verify( decorated, Mockito.times(1) ).execute(host, req, handler, context); }
Example #11
Source File: HttpClientTests.java From vividus with Apache License 2.0 | 6 votes |
@Test void testDoHttpHead() throws Exception { CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); HttpContext context = null; when(closeableHttpResponse.getEntity()).thenReturn(null); StatusLine statusLine = mock(StatusLine.class); int statusCode = HttpStatus.SC_MOVED_PERMANENTLY; Header[] headers = { header }; when(closeableHttpResponse.getAllHeaders()).thenReturn(headers); when(statusLine.getStatusCode()).thenReturn(statusCode); when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine); when(closeableHttpClient.execute(isA(HttpHead.class), eq(context))) .thenAnswer(getAnswerWithSleep(closeableHttpResponse)); HttpResponse httpResponse = httpClient.doHttpHead(URI_TO_GO); assertEquals(HEAD, httpResponse.getMethod()); assertEquals(URI_TO_GO, httpResponse.getFrom()); assertEquals(statusCode, httpResponse.getStatusCode()); assertThat(httpResponse.getResponseTimeInMs(), greaterThan(0L)); assertThat(httpResponse.getResponseHeaders(), is(equalTo(headers))); }
Example #12
Source File: HttpClientAdapter.java From uyuni with GNU General Public License v2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException { Boolean ignoreNoProxy = (Boolean) context.getAttribute(IGNORE_NO_PROXY); URI requestUri = (URI) context.getAttribute(REQUEST_URI); if (proxyHost != null && (Boolean.TRUE.equals(ignoreNoProxy) || useProxyFor(requestUri))) { if (log.isDebugEnabled()) { log.debug("Using proxy: " + proxyHost); } return super.determineRoute(host, request, context); } if (log.isDebugEnabled()) { log.debug("Using a direct connection (no proxy)"); } // Return direct route return new HttpRoute(host); }
Example #13
Source File: SavingConnectionDetailsHttpResponseInterceptorTests.java From vividus with Apache License 2.0 | 6 votes |
@Test void shouldSaveConnectionDetailsForSecuredConnectionAndDoNotCheckStalenessForResponsesWithEntity() { String protocol = "TLSv1.3"; SSLSession sslSession = mock(SSLSession.class); when(sslSession.getProtocol()).thenReturn(protocol); HttpContext context = mock(HttpContext.class); ManagedHttpClientConnection connection = mock(ManagedHttpClientConnection.class); when(context.getAttribute(HttpCoreContext.HTTP_CONNECTION)).thenReturn(connection); when(connection.isOpen()).thenReturn(true); when(connection.getSSLSession()).thenReturn(sslSession); HttpResponse response = mock(HttpResponse.class); when(response.getEntity()).thenReturn(mock(HttpEntity.class)); interceptor.process(response, context); verify(httpTestContext).putConnectionDetails(argThat(connectionDetails -> connectionDetails.isSecure() && protocol.equals(connectionDetails.getSecurityProtocol()))); verify(connection, never()).isStale(); }
Example #14
Source File: ExtendedHttpRequestRetryHandler.java From cyberduck with GNU General Public License v3.0 | 6 votes |
@Override public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) { final Throwable cause = ExceptionUtils.getRootCause(exception); if(cause instanceof RuntimeException) { log.error(String.format("Cancel retry request with execution count %d for failure %s", executionCount, cause)); return false; } final boolean retry = super.retryRequest(exception, executionCount, context); if(retry) { log.info(String.format("Retry request with failure %s", exception)); } else { log.warn(String.format("Cancel retry request with execution count %d for failure %s", executionCount, exception)); } return retry; }
Example #15
Source File: PreemptiveAuth.java From jenkins-client-java with MIT License | 6 votes |
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authScheme != null) { Credentials creds = credsProvider .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (creds == null) { throw new HttpException("No credentials for preemptive authentication"); } authState.update(authScheme, creds); } } }
Example #16
Source File: B2ErrorResponseInterceptor.java From cyberduck with GNU General Public License v3.0 | 6 votes |
@Override public void process(final HttpRequest request, final HttpContext context) { if(StringUtils.contains(request.getRequestLine().getUri(), "b2_authorize_account")) { // Skip setting token for if(log.isDebugEnabled()) { log.debug("Skip setting token in b2_authorize_account"); } return; } switch(request.getRequestLine().getMethod()) { case "POST": // Do not override Authorization header for upload requests with upload URL token if(StringUtils.contains(request.getRequestLine().getUri(), "b2_upload_part") || StringUtils.contains(request.getRequestLine().getUri(), "b2_upload_file")) { break; } default: if(StringUtils.isNotBlank(authorizationToken)) { request.removeHeaders(HttpHeaders.AUTHORIZATION); request.addHeader(HttpHeaders.AUTHORIZATION, authorizationToken); } } }
Example #17
Source File: MockedClientTests.java From ibm-cos-sdk-java with Apache License 2.0 | 5 votes |
@Test public void clientExecutionTimeoutEnabled_RequestCompletesWithinTimeout_TaskCanceledAndEntityBuffered() throws Exception { ClientConfiguration config = new ClientConfiguration().withClientExecutionTimeout(CLIENT_EXECUTION_TIMEOUT) .withMaxErrorRetry(0); ConnectionManagerAwareHttpClient rawHttpClient = createRawHttpClientSpy(config); HttpResponseProxy responseProxy = createHttpResponseProxySpy(); doReturn(responseProxy).when(rawHttpClient).execute(any(HttpRequestBase.class), any(HttpContext.class)); httpClient = new AmazonHttpClient(config, rawHttpClient, null); try { execute(httpClient, createMockGetRequest()); fail("Exception expected"); } catch (AmazonClientException e) { NullResponseHandler.assertIsUnmarshallingException(e); } assertResponseIsBuffered(responseProxy); ScheduledThreadPoolExecutor requestTimerExecutor = httpClient.getClientExecutionTimer().getExecutor(); assertTimerNeverTriggered(requestTimerExecutor); assertCanceledTasksRemoved(requestTimerExecutor); // Core threads should be spun up on demand. Since only one task was submitted only one // thread should exist assertEquals(1, requestTimerExecutor.getPoolSize()); assertCoreThreadsShutDownAfterBeingIdle(requestTimerExecutor); }
Example #18
Source File: SofaTracerHttpInterceptor.java From sofa-tracer with Apache License 2.0 | 5 votes |
@Override public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException { SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext(); SofaTracerSpan httpClientSpan = sofaTraceContext.getCurrentSpan(); //tag append super.appendHttpClientResponseSpanTags(httpResponse, httpClientSpan); //finish int statusCode = httpResponse.getStatusLine().getStatusCode(); httpClientTracer.clientReceive(String.valueOf(statusCode)); }
Example #19
Source File: HttpClientConfigurer.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void configureRetryHandler(DefaultHttpClient httpClient) { httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return false; } }); }
Example #20
Source File: SocksConnectionSocketFactory.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public Socket createSocket(final HttpContext context) throws IOException { final HttpHost socksProxy = getSocksProxy(context); if (socksProxy != null) { return createSocketWithSocksProxy(socksProxy); } return super.createSocket(context); }
Example #21
Source File: EveryRequestPlanner.java From vscrawler with Apache License 2.0 | 5 votes |
@Override public Proxy determineProxy(HttpHost host, HttpRequest request, HttpContext context, IPPool ipPool, CrawlerSession crawlerSession) { HttpClientContext httpClientContext = HttpClientContext.adapt(context); Proxy bind = (Proxy) context.getAttribute(VSCrawlerConstant.VSCRAWLER_AVPROXY_KEY); String accessUrl = null; if (request instanceof HttpRequestWrapper || request instanceof HttpGet) { accessUrl = HttpUriRequest.class.cast(request).getURI().toString(); } if (!PoolUtil.isDungProxyEnabled(httpClientContext)) { log.info("{}不会被代理", accessUrl); return null; } if (bind == null || bind.isDisable()) { bind = ipPool.getIP(host.getHostName(), accessUrl); } if (bind == null) { return null; } log.info("{} 当前使用IP为:{}:{}", host.getHostName(), bind.getIp(), bind.getPort()); // 将绑定IP放置到context,用于后置拦截器统计这个IP的使用情况 return bind; }
Example #22
Source File: AuthHttpClient.java From docker-java-api with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public HttpResponse execute( final HttpHost target, final HttpRequest request, final HttpContext context ) throws IOException { throw new UnsupportedOperationException(); }
Example #23
Source File: SofaTracerAsyncHttpInterceptor.java From sofa-tracer with Apache License 2.0 | 5 votes |
@Override public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException { SofaTracerSpan httpClientSpan = (SofaTracerSpan) httpContext .getAttribute(CURRENT_ASYNC_HTTP_SPAN_KEY); //tag append super.appendHttpClientResponseSpanTags(httpResponse, httpClientSpan); //finish int statusCode = httpResponse.getStatusLine().getStatusCode(); httpClientTracer.clientReceiveTagFinish(httpClientSpan, String.valueOf(statusCode)); }
Example #24
Source File: CsrfHttpClientTest.java From multiapps-controller with Apache License 2.0 | 5 votes |
@Test public void testExecuteWithContext() throws IOException { HttpClient httpClient = HttpMocks.mockClient(builder -> builder.addResponse(OK_RESPONSE)); HttpRequest request = Mockito.spy(new HttpGet()); HttpResponse response = getResultFromExecution(httpClient, "dummy", client -> client.execute(null, request, (HttpContext) null)); Mockito.verify(request, Mockito.never()) .setHeader(Mockito.anyString(), Mockito.anyString()); Assertions.assertEquals(200, response.getStatusLine() .getStatusCode()); }
Example #25
Source File: HTTPSession.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement h : codecs) { if (h.getName().equalsIgnoreCase("deflate")) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); return; } } } } }
Example #26
Source File: RetryHandler.java From Mobike with Apache License 2.0 | 5 votes |
@Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { boolean retry = true; Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT); boolean sent = (b != null && b); if (executionCount > maxRetries) { // Do not retry if over max retry count retry = false; } else if (isInList(exceptionWhitelist, exception)) { // immediately retry if error is whitelisted retry = true; } else if (isInList(exceptionBlacklist, exception)) { // immediately cancel retry if the error is blacklisted retry = false; } else if (!sent) { // for most other errors, retry only if request hasn't been fully sent yet retry = true; } if (retry) { // resend all idempotent requests HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); if (currentReq == null) { return false; } } if (retry) { SystemClock.sleep(retrySleepTimeMS); } else { exception.printStackTrace(); } return retry; }
Example #27
Source File: SofaTracerHttpInterceptor.java From sofa-tracer with Apache License 2.0 | 5 votes |
@Override public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException { //lazy init RequestLine requestLine = httpRequest.getRequestLine(); String methodName = requestLine.getMethod(); //span generated SofaTracerSpan httpClientSpan = httpClientTracer.clientSend(methodName); super.appendHttpClientRequestSpanTags(httpRequest, httpClientSpan); }
Example #28
Source File: AmazonHttpClientTest.java From ibm-cos-sdk-java with Apache License 2.0 | 5 votes |
@Test public void testUserAgentPrefixAndSuffixAreAdded() throws Exception { String prefix = "somePrefix", suffix = "someSuffix"; Request<?> request = mockRequest(SERVER_NAME, HttpMethodName.PUT, URI_NAME, true); HttpResponseHandler<AmazonWebServiceResponse<Object>> handler = createStubResponseHandler(); EasyMock.replay(handler); ClientConfiguration config = new ClientConfiguration().withUserAgentPrefix(prefix).withUserAgentSuffix(suffix); Capture<HttpRequestBase> capturedRequest = new Capture<HttpRequestBase>(); EasyMock.reset(httpClient); EasyMock .expect(httpClient.execute( EasyMock.capture(capturedRequest), EasyMock.<HttpContext>anyObject())) .andReturn(createBasicHttpResponse()) .once(); EasyMock.replay(httpClient); AmazonHttpClient client = new AmazonHttpClient(config, httpClient, null); client.requestExecutionBuilder().request(request).execute(handler); String userAgent = capturedRequest.getValue().getFirstHeader("User-Agent").getValue(); Assert.assertTrue(userAgent.startsWith(prefix)); Assert.assertTrue(userAgent.endsWith(suffix)); }
Example #29
Source File: ResourceValidatorTests.java From vividus with Apache License 2.0 | 5 votes |
@Test void shouldMarkValidationAsBrokenIfExceptionOccurs() throws IOException { IOException ioException = new IOException(); when(httpClient.execute(argThat(r -> HEAD.equals(r.getMethod())), any(HttpContext.class))) .thenThrow(ioException); ResourceValidation resourceValidation = new ResourceValidation(FIRST, CSS_SELECTOR); ResourceValidation result = resourceValidator.perform(resourceValidation); assertEquals(CheckStatus.BROKEN, result.getCheckStatus()); verify(httpClient).execute(any(HttpUriRequest.class), any(HttpContext.class)); verify(softAssert).recordFailedAssertion(eq("Exception occured during check of: https://vividus.org"), eq(ioException)); }
Example #30
Source File: AbstractHttpClient.java From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 | 5 votes |
public final HttpContext createContext(URI uri, UsernamePasswordCredentials creds) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(uri.getHost(), uri.getPort()), creds); org.apache.http.HttpHost host = new org.apache.http.HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(host, basicAuth); HttpClientContext context1 = HttpClientContext.create(); context1.setCredentialsProvider(credsProvider); context1.setAuthCache(authCache); return context1; }