Search in sources :

Example 86 with HttpContext

use of org.apache.http.protocol.HttpContext in project spring-framework by spring-projects.

the class HttpComponentsAsyncClientHttpRequestFactory method createAsyncRequest.

@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);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestConfig(org.apache.http.client.config.RequestConfig) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) Configurable(org.apache.http.client.methods.Configurable)

Example 87 with HttpContext

use of org.apache.http.protocol.HttpContext in project spring-security-oauth by spring-projects.

the class ServerRunning method createRestTemplate.

public RestOperations createRestTemplate() {
    RestTemplate client = new RestTemplate();
    client.setRequestFactory(new HttpComponentsClientHttpRequestFactory() {

        @Override
        protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
            HttpClientContext context = HttpClientContext.create();
            Builder builder = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).setAuthenticationEnabled(false).setRedirectsEnabled(false);
            context.setRequestConfig(builder.build());
            return context;
        }
    });
    client.setErrorHandler(new DefaultResponseErrorHandler() {

        @Override
        public boolean hasError(ClientHttpResponse response) throws IOException {
            return false;
        }
    });
    return client;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) Builder(org.apache.http.client.config.RequestConfig.Builder) RestTemplate(org.springframework.web.client.RestTemplate) HttpContext(org.apache.http.protocol.HttpContext) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) IOException(java.io.IOException) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpMethod(org.springframework.http.HttpMethod)

Example 88 with HttpContext

use of org.apache.http.protocol.HttpContext in project XobotOS by xamarin.

the class AbstractHttpClient method execute.

// non-javadoc, see interface HttpClient
public final HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
    if (request == null) {
        throw new IllegalArgumentException("Request must not be null.");
    }
    // a null target may be acceptable, this depends on the route planner
    // a null context is acceptable, default context created below
    HttpContext execContext = null;
    RequestDirector director = null;
    // all shared objects that are potentially threading unsafe.
    synchronized (this) {
        HttpContext defaultContext = createHttpContext();
        if (context == null) {
            execContext = defaultContext;
        } else {
            execContext = new DefaultedHttpContext(context, defaultContext);
        }
        // Create a director for this request
        director = createClientRequestDirector(getRequestExecutor(), getConnectionManager(), getConnectionReuseStrategy(), getConnectionKeepAliveStrategy(), getRoutePlanner(), getHttpProcessor().copy(), getHttpRequestRetryHandler(), getRedirectHandler(), getTargetAuthenticationHandler(), getProxyAuthenticationHandler(), getUserTokenHandler(), determineParams(request));
    }
    try {
        return director.execute(target, request, execContext);
    } catch (HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}
Also used : RequestDirector(org.apache.http.client.RequestDirector) DefaultedHttpContext(org.apache.http.protocol.DefaultedHttpContext) DefaultedHttpContext(org.apache.http.protocol.DefaultedHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 89 with HttpContext

use of org.apache.http.protocol.HttpContext in project camel by apache.

the class HttpProducerTwoHeadersWithSameKeyTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor()).setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory()).setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext()).registerHandler("/myapp", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            Header[] from = request.getHeaders("from");
            assertEquals("me", from[0].getValue());
            Header[] to = request.getHeaders("to");
            assertEquals("[foo, bar]", to[0].getValue());
            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    }).registerHandler("/myapp", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            Header[] from = request.getHeaders("from");
            assertEquals("me", from[0].getValue());
            Header[] to = request.getHeaders("to");
            assertEquals("[foo, bar]", to[0].getValue());
            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    }).create();
    localServer.start();
    super.setUp();
}
Also used : HttpRequest(org.apache.http.HttpRequest) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) HttpRequestHandler(org.apache.http.protocol.HttpRequestHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Before(org.junit.Before)

Example 90 with HttpContext

use of org.apache.http.protocol.HttpContext in project camel by apache.

the class HttpProducerTwoParametersWithSameKeyTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    localServer = ServerBootstrap.bootstrap().setHttpProcessor(getBasicHttpProcessor()).setConnectionReuseStrategy(getConnectionReuseStrategy()).setResponseFactory(getHttpResponseFactory()).setExpectationVerifier(getHttpExpectationVerifier()).setSslContext(getSSLContext()).registerHandler("/myapp", new HttpRequestHandler() {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            String uri = request.getRequestLine().getUri();
            assertEquals("/myapp?from=me&to=foo&to=bar", uri);
            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    }).create();
    localServer.start();
    super.setUp();
}
Also used : HttpRequest(org.apache.http.HttpRequest) StringEntity(org.apache.http.entity.StringEntity) HttpRequestHandler(org.apache.http.protocol.HttpRequestHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Before(org.junit.Before)

Aggregations

HttpContext (org.apache.http.protocol.HttpContext)169 HttpResponse (org.apache.http.HttpResponse)77 IOException (java.io.IOException)72 HttpRequest (org.apache.http.HttpRequest)55 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)47 HttpException (org.apache.http.HttpException)30 Test (org.junit.Test)29 HttpGet (org.apache.http.client.methods.HttpGet)27 HttpHost (org.apache.http.HttpHost)26 HttpPost (org.apache.http.client.methods.HttpPost)26 ArrayList (java.util.ArrayList)22 Header (org.apache.http.Header)21 StringEntity (org.apache.http.entity.StringEntity)21 NameValuePair (org.apache.http.NameValuePair)18 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)18 ProtocolException (org.apache.http.ProtocolException)17 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)17 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)17 URI (java.net.URI)16 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)16