/** * Create a default {@link RequestConfig} to use with the given client. * Can return {@code null} to indicate that no custom request config should * be set and the defaults of the {@link HttpClient} should be used. * <p>The default implementation tries to merge the defaults of the client * with the local customizations of this factory instance, if any. * @param client the {@link HttpClient} (or {@code HttpAsyncClient}) to check * @return the actual RequestConfig to use (may be {@code null}) * @since 4.2 * @see #mergeRequestConfig(RequestConfig) */ @Nullable protected RequestConfig createRequestConfig(Object client) { if (client instanceof Configurable) { RequestConfig clientRequestConfig = ((Configurable) client).getConfig(); return mergeRequestConfig(clientRequestConfig); } return this.requestConfig; }
/** * Create a {@link RequestConfig} for the given configuration. Can return {@code null} * to indicate that no custom request config should be set and the defaults of the * {@link HttpClient} should be used. * <p>The default implementation tries to merge the defaults of the client with the * local customizations of the instance, if any. * @param config the HTTP invoker configuration that specifies the * target service * @return the RequestConfig to use */ @Nullable protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) { HttpClient client = getHttpClient(); if (client instanceof Configurable) { RequestConfig clientRequestConfig = ((Configurable) client).getConfig(); return mergeRequestConfig(clientRequestConfig); } return this.requestConfig; }
/** * Create a default {@link RequestConfig} to use with the given client. * Can return {@code null} to indicate that no custom request config should * be set and the defaults of the {@link HttpClient} should be used. * <p>The default implementation tries to merge the defaults of the client * with the local customizations of this factory instance, if any. * @param client the {@link HttpClient} (or {@code HttpAsyncClient}) to check * @return the actual RequestConfig to use (may be {@code null}) * @since 4.2 * @see #mergeRequestConfig(RequestConfig) */ @Nullable protected RequestConfig createRequestConfig(Object client) { if (client instanceof Configurable) { RequestConfig clientRequestConfig = ((Configurable) client).getConfig(); return mergeRequestConfig(clientRequestConfig); } return this.requestConfig; }
/** * Create a {@link RequestConfig} for the given configuration. Can return {@code null} * to indicate that no custom request config should be set and the defaults of the * {@link HttpClient} should be used. * <p>The default implementation tries to merge the defaults of the client with the * local customizations of the instance, if any. * @param config the HTTP invoker configuration that specifies the * target service * @return the RequestConfig to use */ @Nullable protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) { HttpClient client = getHttpClient(); if (client instanceof Configurable) { RequestConfig clientRequestConfig = ((Configurable) client).getConfig(); return mergeRequestConfig(clientRequestConfig); } return this.requestConfig; }
@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); }
@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 localSettingsOverrideClientDefaultSettings() throws Exception { RequestConfig defaultConfig = RequestConfig.custom() .setConnectTimeout(1234).setConnectionRequestTimeout(6789).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client); hrf.setConnectTimeout(5000); RequestConfig requestConfig = retrieveRequestConfig(hrf); assertEquals(5000, requestConfig.getConnectTimeout()); assertEquals(6789, requestConfig.getConnectionRequestTimeout()); assertEquals(-1, requestConfig.getSocketTimeout()); }
@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 defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception { RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client); assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf)); hrf.setConnectionRequestTimeout(4567); RequestConfig requestConfig = retrieveRequestConfig(hrf); assertNotNull(requestConfig); assertEquals(4567, requestConfig.getConnectionRequestTimeout()); // Default connection timeout merged assertEquals(1234, requestConfig.getConnectTimeout()); }
@Test public void localSettingsOverrideClientDefaultSettings() throws Exception { RequestConfig defaultConfig = RequestConfig.custom() .setConnectTimeout(1234).setConnectionRequestTimeout(6789).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client); executor.setConnectTimeout(5000); HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service"); HttpPost httpPost = executor.createHttpPost(config); RequestConfig requestConfig = httpPost.getConfig(); assertEquals(5000, requestConfig.getConnectTimeout()); assertEquals(6789, requestConfig.getConnectionRequestTimeout()); assertEquals(-1, requestConfig.getSocketTimeout()); }
@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 mergeBasedOnCurrentHttpClient() throws Exception { RequestConfig defaultConfig = RequestConfig.custom() .setSocketTimeout(1234).build(); final CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() { @Override public HttpClient getHttpClient() { return client; } }; hrf.setReadTimeout(5000); RequestConfig requestConfig = retrieveRequestConfig(hrf); assertEquals(-1, requestConfig.getConnectTimeout()); assertEquals(-1, requestConfig.getConnectionRequestTimeout()); assertEquals(5000, requestConfig.getSocketTimeout()); // Update the Http client so that it returns an updated config RequestConfig updatedDefaultConfig = RequestConfig.custom() .setConnectTimeout(1234).build(); when(configurable.getConfig()).thenReturn(updatedDefaultConfig); hrf.setReadTimeout(7000); RequestConfig requestConfig2 = retrieveRequestConfig(hrf); assertEquals(1234, requestConfig2.getConnectTimeout()); assertEquals(-1, requestConfig2.getConnectionRequestTimeout()); assertEquals(7000, requestConfig2.getSocketTimeout()); }
withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); when(configurable.getConfig()).thenReturn(updatedDefaultConfig); executor.setReadTimeout(7000); HttpPost httpPost2 = executor.createHttpPost(config);
@Test public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException { RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client); HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service"); HttpPost httpPost = executor.createHttpPost(config); assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig()); executor.setConnectionRequestTimeout(4567); HttpPost httpPost2 = executor.createHttpPost(config); assertNotNull(httpPost2.getConfig()); assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout()); // Default connection timeout merged assertEquals(1234, httpPost2.getConfig().getConnectTimeout()); }
public HttpHost getDefaultProxy() { Configurable clientConfiguration = (Configurable) httpClient; return clientConfiguration.getConfig().getProxy(); }
private RequestConfig getCurrentConfiguration(final ClientInvocation request, final HttpRequestBase httpMethod) { RequestConfig baseConfig; if (httpMethod != null && httpMethod.getConfig() != null) { baseConfig = httpMethod.getConfig(); } else { ApacheHttpClient43Engine engine = ((ApacheHttpClient43Engine) request.getClient().httpEngine()); baseConfig = ((Configurable) engine.getHttpClient()).getConfig(); if (baseConfig == null) { Configurable clientConfiguration = (Configurable) httpClient; baseConfig = clientConfiguration.getConfig(); } } return baseConfig; }
public RestAsyncClientHttpRequestFactory(final HttpClient client, final AsyncListenableTaskExecutor executor) { final RequestConfig config = Configurable.class.cast(client).getConfig(); this.factory = new HttpComponentsClientHttpRequestFactory(client) { @Override protected void postProcessHttpRequest(final HttpUriRequest request) { // restore the client's request settings that are incorrectly handled by spring HttpRequestBase.class.cast(request).setConfig(config); } }; this.executor = executor; }
@Override public HttpHost getDefaultProxy() { Configurable clientConfiguration = (Configurable) httpClient; return clientConfiguration.getConfig().getProxy(); }
@Override public HttpHost getDefaultProxy() { Configurable clientConfiguration = (Configurable) httpClient; return clientConfiguration.getConfig().getProxy(); }
public static RequestConfig getRequestConfig(Object client) { if (client == null) return null; if (client instanceof Configurable) return ((Configurable) client).getConfig(); return getRequestConfigFromField(client); }