Search in sources :

Example 46 with HttpContext

use of org.apache.http.protocol.HttpContext in project epp.mpc by eclipse.

the class TransportFactoryTest method interceptRequest.

private static AbortRequestCustomizer interceptRequest(HttpClientCustomizer... customizers) throws Exception {
    AbortRequestCustomizer abortRequestCustomizer = new AbortRequestCustomizer();
    HttpClientCustomizer[] mergedCustomizers;
    if (customizers == null || customizers.length == 0) {
        mergedCustomizers = new HttpClientCustomizer[] { abortRequestCustomizer };
    } else {
        mergedCustomizers = new HttpClientCustomizer[customizers.length + 1];
        System.arraycopy(customizers, 0, mergedCustomizers, 0, customizers.length);
        mergedCustomizers[customizers.length] = abortRequestCustomizer;
    }
    HttpClientTransport httpClientTransport = createClient(mergedCustomizers);
    HttpClient client = httpClientTransport.getClient();
    HttpContext context = new BasicHttpContext();
    try {
        client.execute(new HttpGet("http://localhost/test"), context);
        fail("Expected request execution to fail");
    } catch (ConnectionClosedException ex) {
    // ignore expected exception
    }
    return abortRequestCustomizer;
}
Also used : HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) ConnectionClosedException(org.apache.http.ConnectionClosedException)

Example 47 with HttpContext

use of org.apache.http.protocol.HttpContext in project epp.mpc by eclipse.

the class TransportFactoryTest method testHttpClientCustomizer.

@Test
public void testHttpClientCustomizer() throws Exception {
    final HttpClientCustomizer customizer = Mockito.mock(HttpClientCustomizer.class);
    Mockito.when(customizer.customizeBuilder(Matchers.any())).thenAnswer(new Answer<HttpClientBuilder>() {

        public HttpClientBuilder answer(InvocationOnMock invocation) {
            HttpClientBuilder builder = (HttpClientBuilder) invocation.getArguments()[0];
            return builder == null ? null : builder.addInterceptorFirst(new HttpRequestInterceptor() {

                @Override
                public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                    request.addHeader("X-Customizer-Test", "true");
                }
            });
        }
    });
    Mockito.when(customizer.customizeCredentialsProvider(Matchers.any())).thenReturn(null);
    HttpRequest request = interceptRequest(customizer).getInterceptedRequest();
    Mockito.verify(customizer).customizeBuilder(Matchers.any());
    Mockito.verify(customizer).customizeCredentialsProvider(Matchers.any());
    assertThat(request.getFirstHeader("X-Customizer-Test"), LambdaMatchers.<Header, String>map(x -> x == null ? null : x.getValue()).matches("true"));
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) Header(org.apache.http.Header) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) Test(org.junit.Test)

Example 48 with HttpContext

use of org.apache.http.protocol.HttpContext in project epp.mpc by eclipse.

the class HttpClientFactory method createCredentialsProvider.

private static CredentialsProvider createCredentialsProvider(HttpClientBuilder clientBuilder) {
    // TODO we should handle configured proxy passwords and dialogs to prompt for unknown credentials on our own...
    CredentialsProvider credentialsProvider = new SystemCredentialsProvider();
    credentialsProvider = customizeCredentialsProvider(credentialsProvider);
    final CacheCredentialsProvider cacheProvider = new CacheCredentialsProvider();
    credentialsProvider = new ChainedCredentialsProvider(cacheProvider, credentialsProvider);
    credentialsProvider = new SynchronizedCredentialsProvider(credentialsProvider);
    clientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            context.setAttribute(CacheCredentialsAuthenticationStrategy.CREDENTIALS_CACHE_ATTRIBUTE, cacheProvider);
        }
    });
    return credentialsProvider;
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException)

Example 49 with HttpContext

use of org.apache.http.protocol.HttpContext in project service-proxy by membrane.

the class AssertUtils method getAuthenticatingHttpClient.

private static HttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) {
    Credentials defaultcreds = new UsernamePasswordCredentials(user, pass);
    BasicCredentialsProvider bcp = new BasicCredentialsProvider();
    bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {

        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            if (authState.getAuthScheme() == null) {
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                Credentials creds = credsProvider.getCredentials(authScope);
                if (creds != null) {
                    authState.update(new BasicScheme(), creds);
                }
            }
        }
    };
    HttpClient hc = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).setDefaultCredentialsProvider(bcp).addInterceptorFirst(preemptiveAuth).build();
    return hc;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpContext(org.apache.http.protocol.HttpContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 50 with HttpContext

use of org.apache.http.protocol.HttpContext in project docker-java-api by amihaiemil.

the class UnixHttpClientTestCase method executesRequestWithHostHandlerAndContext.

/**
 * 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);
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) HttpContext(org.apache.http.protocol.HttpContext) Test(org.junit.Test)

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