Java 类org.apache.http.client.methods.Configurable 实例源码

项目:lams    文件:HttpComponentsAsyncClientHttpRequestFactory.java   
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
    HttpAsyncClient asyncClient = getHttpAsyncClient();
    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 = RequestConfig.DEFAULT;
           }
           context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
       }
    return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
}
项目:spring4-understanding    文件:HttpComponentsAsyncClientHttpRequestFactory.java   
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
    HttpAsyncClient asyncClient = getHttpAsyncClient();
    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(asyncClient);
        }
        if (config != null) {
            context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
        }
    }
    return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
}
项目:lams    文件:HttpComponentsClientHttpRequestFactory.java   
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
    CloseableHttpClient client = (CloseableHttpClient) getHttpClient();
    Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");
    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) {
               if (this.socketTimeout > 0 || this.connectTimeout > 0) {
                   config = RequestConfig.custom()
                           .setConnectTimeout(this.connectTimeout)
                           .setSocketTimeout(this.socketTimeout)
                           .build();
               }
            else {
                   config = RequestConfig.DEFAULT;
               }
           }
           context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
       }
    if (this.bufferRequestBody) {
        return new HttpComponentsClientHttpRequest(client, httpRequest, context);
    }
    else {
        return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
    }
}
项目:spring4-understanding    文件:HttpComponentsHttpInvokerRequestExecutorTests.java   
@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());
}
项目:spring4-understanding    文件:HttpComponentsHttpInvokerRequestExecutorTests.java   
@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());
}
项目:spring4-understanding    文件:HttpComponentsClientHttpRequestFactoryTests.java   
@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());
}
项目:spring4-understanding    文件:HttpComponentsClientHttpRequestFactoryTests.java   
@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());
}
项目:purecloud-iot    文件:Request.java   
HttpResponse internalExecute(
        final HttpClient client,
        final HttpContext localContext) throws ClientProtocolException, IOException {
    final RequestConfig.Builder builder;
    if (client instanceof Configurable) {
        builder = RequestConfig.copy(((Configurable) client).getConfig());
    } else {
        builder = RequestConfig.custom();
    }
    if (this.useExpectContinue != null) {
        builder.setExpectContinueEnabled(this.useExpectContinue);
    }
    if (this.socketTmeout != null) {
        builder.setSocketTimeout(this.socketTmeout);
    }
    if (this.connectTimeout != null) {
        builder.setConnectTimeout(this.connectTimeout);
    }
    if (this.proxy != null) {
        builder.setProxy(this.proxy);
    }
    final RequestConfig config = builder.build();
    this.request.setConfig(config);
    return client.execute(this.request, localContext);
}
项目:spring4-understanding    文件:HttpComponentsClientHttpRequestFactory.java   
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
    HttpClient client = getHttpClient();
    Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");

    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);
    }
}
项目:spring4-understanding    文件:HttpComponentsHttpInvokerRequestExecutorTests.java   
@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);

    HttpComponentsHttpInvokerRequestExecutor executor =
            new HttpComponentsHttpInvokerRequestExecutor() {
                @Override
                public HttpClient getHttpClient() {
                    return client;
                }
            };
    executor.setReadTimeout(5000);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    RequestConfig requestConfig = httpPost.getConfig();
    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);
    executor.setReadTimeout(7000);
    HttpPost httpPost2 = executor.createHttpPost(config);
    RequestConfig requestConfig2 = httpPost2.getConfig();
    assertEquals(1234, requestConfig2.getConnectTimeout());
    assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
    assertEquals(7000, requestConfig2.getSocketTimeout());
}
项目:spring4-understanding    文件:HttpComponentsClientHttpRequestFactoryTests.java   
@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());
}
项目:spring-data-jest    文件:ElasticsearchJestAutoConfigurationWithCustomHttpConfigurerTest.java   
@Test
public void should_jest_auto_configuration_have_custom_httpbuilder() {
    RequestConfig config = ((Configurable) ((JestHttpClient) jestClient).getHttpClient()).getConfig();

    int connectTimeout = config.getConnectTimeout();

    assertThat(connectTimeout, is(3551));
}
项目:put-it-to-rest    文件:ClientConfigurationTest.java   
@Test
public void shouldApplyTimeouts() throws Exception {
    assertThat("Configurable http client expected", exampleHttpClient, is(instanceOf(Configurable.class)));

    final RequestConfig config = ((Configurable) exampleHttpClient).getConfig();

    assertThat(config.getSocketTimeout(), is(34 * 60 * 60 * 1000));
    assertThat(config.getConnectTimeout(), is(12 * 60 * 1000));
}
项目:riptide    文件:RestAsyncClientHttpRequestFactory.java   
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;
}
项目:riptide    文件:ClientConfigurationTest.java   
@Test
public void shouldApplyTimeouts() throws Exception {
    assertThat("Configurable http client expected", exampleHttpClient, is(instanceOf(Configurable.class)));

    final RequestConfig config = ((Configurable) exampleHttpClient).getConfig();

    assertThat(config.getSocketTimeout(), is(34 * 60 * 60 * 1000));
    assertThat(config.getConnectTimeout(), is(12 * 60 * 1000));
}
项目:Visit    文件:MinimalHttpClient.java   
@Override
protected CloseableHttpResponse doExecute(
        final HttpHost target,
        final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException {
    Args.notNull(target, "Target host");
    Args.notNull(request, "HTTP request");
    HttpExecutionAware execAware = null;
    if (request instanceof HttpExecutionAware) {
        execAware = (HttpExecutionAware) request;
    }
    try {
        final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
        final HttpClientContext localcontext = HttpClientContext.adapt(
            context != null ? context : new BasicHttpContextHC4());
        final HttpRoute route = new HttpRoute(target);
        RequestConfig config = null;
        if (request instanceof Configurable) {
            config = ((Configurable) request).getConfig();
        }
        if (config != null) {
            localcontext.setRequestConfig(config);
        }
        return this.requestExecutor.execute(route, wrapper, localcontext, execAware);
    } catch (final HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}
项目:Visit    文件:InternalHttpClient.java   
@Override
protected CloseableHttpResponse doExecute(
        final HttpHost target,
        final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException {
    Args.notNull(request, "HTTP request");
    HttpExecutionAware execAware = null;
    if (request instanceof HttpExecutionAware) {
        execAware = (HttpExecutionAware) request;
    }
    try {
        final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
        final HttpClientContext localcontext = HttpClientContext.adapt(
                context != null ? context : new BasicHttpContextHC4());
        RequestConfig config = null;
        if (request instanceof Configurable) {
            config = ((Configurable) request).getConfig();
        }
        if (config == null) {
            final HttpParams params = request.getParams();
            if (params instanceof HttpParamsNames) {
                if (!((HttpParamsNames) params).getNames().isEmpty()) {
                    config = HttpClientParamConfig.getRequestConfig(params);
                }
            } else {
                config = HttpClientParamConfig.getRequestConfig(params);
            }
        }
        if (config != null) {
            localcontext.setRequestConfig(config);
        }
        setupContext(localcontext);
        final HttpRoute route = determineRoute(target, wrapper, localcontext);
        return this.execChain.execute(route, wrapper, localcontext, execAware);
    } catch (final HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}
项目:spring-cloud-commons    文件:DefaultApacheHttpClientFactoryTests.java   
@Test
public void createClient() throws Exception {
    final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100)
            .setConnectTimeout(200).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    CloseableHttpClient httpClient = new DefaultApacheHttpClientFactory(HttpClientBuilder.create()).createBuilder().
            setConnectionManager(mock(HttpClientConnectionManager.class)).
            setDefaultRequestConfig(requestConfig).build();
    Assertions.assertThat(httpClient).isInstanceOf(Configurable.class);
    RequestConfig config = ((Configurable) httpClient).getConfig();
    assertEquals(100, config.getSocketTimeout());
    assertEquals(200, config.getConnectTimeout());
    assertEquals(CookieSpecs.IGNORE_COOKIES, config.getCookieSpec());
}
项目:spring-cloud-netflix    文件:SimpleHostRoutingFilterTests.java   
@Test
public void timeoutPropertiesAreApplied() {
    addEnvironment(this.context, "zuul.host.socket-timeout-millis=11000",
            "zuul.host.connect-timeout-millis=2100");
    setupContext();
    CloseableHttpClient httpClient = getFilter().newClient();
    Assertions.assertThat(httpClient).isInstanceOf(Configurable.class);
    RequestConfig config = ((Configurable) httpClient).getConfig();
    assertEquals(11000, config.getSocketTimeout());
    assertEquals(2100, config.getConnectTimeout());
}
项目:ZTLib    文件:MinimalHttpClient.java   
@Override
protected CloseableHttpResponse doExecute(
        final HttpHost target,
        final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException {
    Args.notNull(target, "Target host");
    Args.notNull(request, "HTTP request");
    HttpExecutionAware execAware = null;
    if (request instanceof HttpExecutionAware) {
        execAware = (HttpExecutionAware) request;
    }
    try {
        final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
        final HttpClientContext localcontext = HttpClientContext.adapt(
            context != null ? context : new BasicHttpContextHC4());
        final HttpRoute route = new HttpRoute(target);
        RequestConfig config = null;
        if (request instanceof Configurable) {
            config = ((Configurable) request).getConfig();
        }
        if (config != null) {
            localcontext.setRequestConfig(config);
        }
        return this.requestExecutor.execute(route, wrapper, localcontext, execAware);
    } catch (final HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}
项目:ZTLib    文件:InternalHttpClient.java   
@Override
protected CloseableHttpResponse doExecute(
        final HttpHost target,
        final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException {
    Args.notNull(request, "HTTP request");
    HttpExecutionAware execAware = null;
    if (request instanceof HttpExecutionAware) {
        execAware = (HttpExecutionAware) request;
    }
    try {
        final HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
        final HttpClientContext localcontext = HttpClientContext.adapt(
                context != null ? context : new BasicHttpContextHC4());
        RequestConfig config = null;
        if (request instanceof Configurable) {
            config = ((Configurable) request).getConfig();
        }
        if (config == null) {
            final HttpParams params = request.getParams();
            if (params instanceof HttpParamsNames) {
                if (!((HttpParamsNames) params).getNames().isEmpty()) {
                    config = HttpClientParamConfig.getRequestConfig(params);
                }
            } else {
                config = HttpClientParamConfig.getRequestConfig(params);
            }
        }
        if (config != null) {
            localcontext.setRequestConfig(config);
        }
        setupContext(localcontext);
        final HttpRoute route = determineRoute(target, wrapper, localcontext);
        return this.execChain.execute(route, wrapper, localcontext, execAware);
    } catch (final HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}
项目:put-it-to-rest    文件:HttpAsyncClientOverrideTest.java   
@Bean
@Qualifier("example")
public HttpClient exampleHttpClient() {
    return mock(HttpClient.class, withSettings().extraInterfaces(Configurable.class));
}
项目:spring4-understanding    文件:HttpComponentsHttpInvokerRequestExecutor.java   
/**
 * 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
 */
protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) {
    HttpClient client = getHttpClient();
    if (client instanceof Configurable) {
        RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
        return mergeRequestConfig(clientRequestConfig);
    }
    return this.requestConfig;
}
项目:spring4-understanding    文件:HttpComponentsClientHttpRequestFactory.java   
/**
 * 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)
 */
protected RequestConfig createRequestConfig(Object client) {
    if (client instanceof Configurable) {
        RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
        return mergeRequestConfig(clientRequestConfig);
    }
    return this.requestConfig;
}