public static boolean disableSocketProxy(HttpContext ctx) { Object v = ctx.getAttribute(DISABLE_SOCKET_PROXY_PROPERTY); return v != null && (Boolean) v; } }
public Object getAttribute(String name) { return this.delegate.getAttribute(name); }
public Object getAttribute(final String id) { Object obj = this.local.getAttribute(id); if (obj == null) { return this.defaults.getAttribute(id); } else { return obj; } }
@Override public Socket createSocket(final HttpContext context) throws IOException { InetSocketAddress socketAddress = (InetSocketAddress) context.getAttribute("socketAddress"); Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress); return new Socket(proxy); }
HttpGet httpget = new HttpGet(url); HttpContext context = new BasicHttpContext(); HttpResponse response = httpClient.execute(httpget, context); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) throw new IOException(response.getStatusLine().toString()); HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) context.getAttribute( ExecutionContext.HTTP_TARGET_HOST); String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
@Override public Socket createLayeredSocket(final Socket socket, final String target, final int port, final HttpContext context) throws IOException { Boolean enableSniValue = (Boolean) context.getAttribute(ENABLE_SNI); boolean enableSni = enableSniValue == null || enableSniValue; return super.createLayeredSocket(socket, enableSni ? target : "", port, context); } }
/** * Returns the CRC32 checksum calculated by the underlying CRC32ChecksumCalculatingInputStream. * * @return The CRC32 checksum. */ public long getCRC32Checksum() { if (context == null) { return 0L; } CRC32ChecksumCalculatingInputStream crc32ChecksumInputStream = (CRC32ChecksumCalculatingInputStream)context.getAttribute(CRC32ChecksumCalculatingInputStream.class.getName()); return crc32ChecksumInputStream == null ? 0L : crc32ChecksumInputStream.getCRC32Checksum(); }
@Override public void process(HttpRequest request, HttpContext context) { AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); Credentials credentials = credsProvider.getCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort())); if (credentials != null) { authState.update(new BasicScheme(), credentials); } } } }
@Override public void process(HttpResponse response, HttpContext context) { Span span = (Span) context.getAttribute(Span.class.getName()); if (span == null) return; handler.handleReceive(response, null, span); } }
/** * Return the cookies set by the server. * * Return values only when source is not from cache (source == NETWORK), returns empty list otherwise. * * @return cookies */ public List<Cookie> getCookies(){ if(context == null) return Collections.emptyList(); CookieStore store = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE); if(store == null) return Collections.emptyList(); return store.getCookies(); }
@Override public void process(HttpResponse response, HttpContext context) { String logId = (String) context.getAttribute(RestClients.LOG_ID_ATTRIBUTE); ClientLogger.logRawResponse(logId, HttpStatus.resolve(response.getStatusLine().getStatusCode())); } }
@Override public boolean retryRequest( final IOException exception, int executionCount, final HttpContext context) { boolean retry = super.retryRequest(exception, executionCount, context); if (retry) { AWSRequestMetrics awsRequestMetrics = (AWSRequestMetrics) context .getAttribute(AWSRequestMetrics.SIMPLE_NAME); if (awsRequestMetrics != null) { awsRequestMetrics.incrementCounter(Field.HttpClientRetryCount); } } return retry; } }
@Override public void failed(Exception ex) { Span currentSpan = (Span) context.getAttribute(Span.class.getName()); if (currentSpan != null) { context.removeAttribute(Span.class.getName()); handler.handleReceive(null, ex, currentSpan); } requestProducer.failed(ex); }
private RequestConfig retrieveRequestConfig(HttpComponentsClientHttpRequestFactory factory) throws Exception { URI uri = new URI(baseUrl + "/status/ok"); HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest) factory.createRequest(uri, HttpMethod.GET); return (RequestConfig) request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG); }
@Override public HttpResponse answer(InvocationOnMock invocation) { HttpContext context = (HttpContext) invocation.getArguments()[1]; if (context.getAttribute(ClientContext.COOKIE_STORE) != null) { BasicCookieStore cookieStore = (BasicCookieStore) context.getAttribute(ClientContext.COOKIE_STORE); BasicClientCookie cookie = new BasicClientCookie("cookie", "meLikeCookie"); cookieStore.addCookie(cookie); } return OK_200_RESPONSE; } };
@Override public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException { HttpAsyncClient client = startAsyncClient(); HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri); postProcessHttpRequest(httpRequest); HttpContext context = createHttpContext(httpMethod, uri); if (context == null) { context = HttpClientContext.create(); } // Request configuration not set in the context if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { // Use request configuration given by the user, when available RequestConfig config = null; if (httpRequest instanceof Configurable) { config = ((Configurable) httpRequest).getConfig(); } if (config == null) { config = createRequestConfig(client); } if (config != null) { context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); } } return new HttpComponentsAsyncClientHttpRequest(client, httpRequest, context); }
@Test public void customHttpAsyncClientUsesItsDefault() throws Exception { HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(); URI uri = new URI(baseUrl + "/status/ok"); HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest) factory.createAsyncRequest(uri, HttpMethod.GET); assertNull("No custom config should be set with a custom HttpAsyncClient", request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG)); }
@Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { HttpClient client = getHttpClient(); HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri); postProcessHttpRequest(httpRequest); HttpContext context = createHttpContext(httpMethod, uri); if (context == null) { context = HttpClientContext.create(); } // Request configuration not set in the context if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { // Use request configuration given by the user, when available RequestConfig config = null; if (httpRequest instanceof Configurable) { config = ((Configurable) httpRequest).getConfig(); } if (config == null) { config = createRequestConfig(client); } if (config != null) { context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); } } if (this.bufferRequestBody) { return new HttpComponentsClientHttpRequest(client, httpRequest, context); } else { return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context); } }
@Test public void assertCustomConfig() throws Exception { HttpClient httpClient = HttpClientBuilder.create().build(); HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(httpClient); hrf.setConnectTimeout(1234); hrf.setConnectionRequestTimeout(4321); hrf.setReadTimeout(4567); URI uri = new URI(baseUrl + "/status/ok"); HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest) hrf.createRequest(uri, HttpMethod.GET); Object config = request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG); assertNotNull("Request config should be set", config); assertTrue("Wrong request config type" + config.getClass().getName(), RequestConfig.class.isInstance(config)); RequestConfig requestConfig = (RequestConfig) config; assertEquals("Wrong custom connection timeout", 1234, requestConfig.getConnectTimeout()); assertEquals("Wrong custom connection request timeout", 4321, requestConfig.getConnectionRequestTimeout()); assertEquals("Wrong custom socket timeout", 4567, requestConfig.getSocketTimeout()); }
@Test public void defaultSettingsOfHttpAsyncClientLostOnExecutorCustomization() throws Exception { CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create() .setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1234).build()) .build(); HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(client); URI uri = new URI(baseUrl + "/status/ok"); HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest) factory.createAsyncRequest(uri, HttpMethod.GET); assertNull("No custom config should be set with a custom HttpClient", request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG)); factory.setConnectionRequestTimeout(4567); HttpComponentsAsyncClientHttpRequest request2 = (HttpComponentsAsyncClientHttpRequest) factory.createAsyncRequest(uri, HttpMethod.GET); Object requestConfigAttribute = request2.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG); assertNotNull(requestConfigAttribute); RequestConfig requestConfig = (RequestConfig) requestConfigAttribute; assertEquals(4567, requestConfig.getConnectionRequestTimeout()); // No way to access the request config of the HTTP client so no way to "merge" our customizations assertEquals(-1, requestConfig.getConnectTimeout()); }