Search in sources :

Example 26 with HttpRequest

use of org.apache.http.HttpRequest in project incubator-skywalking by apache.

the class HttpHostInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    HttpHost producer = (HttpHost) allArguments[0];
    String uri = producer.toURI();
    AbstractSpan span = ContextManager.createLocalSpan("httpasyncclient/" + method.getName());
    span.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT).setLayer(SpanLayer.HTTP);
    Tags.HTTP.METHOD.set(span, ((HttpRequest) allArguments[1]).getRequestLine().getMethod());
    Tags.URL.set(span, uri);
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpHost(org.apache.http.HttpHost) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 27 with HttpRequest

use of org.apache.http.HttpRequest in project connect-sdk-java by Ingenico-ePayments.

the class DefaultConnectionIdempotenceTest method setResponse.

private Answer<Void> setResponse(final String body, final int statusCode, final Map<String, String> responseHeaders, final Map<String, String> requestHeaders) {
    return new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            HttpRequest request = invocation.getArgumentAt(0, HttpRequest.class);
            HttpResponse response = invocation.getArgumentAt(1, HttpResponse.class);
            if (requestHeaders != null) {
                for (Header header : request.getAllHeaders()) {
                    requestHeaders.put(header.getName(), header.getValue());
                }
            }
            response.setStatusCode(statusCode);
            response.setHeader("Content-Type", "application/json");
            for (Map.Entry<String, String> entry : responseHeaders.entrySet()) {
                response.setHeader(entry.getKey(), entry.getValue());
            }
            response.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON));
            return null;
        }
    };
}
Also used : HttpRequest(org.apache.http.HttpRequest) Answer(org.mockito.stubbing.Answer) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpResponse(org.apache.http.HttpResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with HttpRequest

use of org.apache.http.HttpRequest in project connect-sdk-java by Ingenico-ePayments.

the class DefaultConnection method createHttpClient.

private CloseableHttpClient createHttpClient(ProxyConfiguration proxyConfiguration) {
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager);
    HttpRoutePlanner routePlanner;
    CredentialsProvider credentialsProvider;
    if (proxyConfiguration != null) {
        HttpHost proxy = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort(), proxyConfiguration.getScheme());
        routePlanner = new DefaultProxyRoutePlanner(proxy, DefaultSchemePortResolver.INSTANCE);
        credentialsProvider = new BasicCredentialsProvider();
        if (proxyConfiguration.getUsername() != null) {
            AuthScope authscope = new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort());
            final Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
            credentialsProvider.setCredentials(authscope, credentials);
            // enable preemptive authentication
            HttpRequestInterceptor proxyAuthenticationInterceptor = new HttpRequestInterceptor() {

                @Override
                public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                    Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
                    if (header == null) {
                        header = new BasicScheme((Charset) null).authenticate(credentials, request, context);
                        if (!AUTH.PROXY_AUTH_RESP.equals(header.getName())) {
                            header = new BasicHeader(AUTH.PROXY_AUTH_RESP, header.getValue());
                        }
                        request.setHeader(header);
                    }
                }
            };
            builder = builder.addInterceptorLast(proxyAuthenticationInterceptor);
        }
    } else {
        // add support for system properties
        routePlanner = new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault());
        credentialsProvider = new SystemDefaultCredentialsProvider();
    }
    // add logging - last for requests, first for responses
    LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
    builder = builder.addInterceptorLast((HttpRequestInterceptor) loggingInterceptor);
    builder = builder.addInterceptorFirst((HttpResponseInterceptor) loggingInterceptor);
    return builder.setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) ResponseHeader(com.ingenico.connect.gateway.sdk.java.ResponseHeader) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) BasicHeader(org.apache.http.message.BasicHeader) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) AuthScope(org.apache.http.auth.AuthScope) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicHeader(org.apache.http.message.BasicHeader) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 29 with HttpRequest

use of org.apache.http.HttpRequest in project spring-cloud-netflix by spring-cloud.

the class GZIPCompression method httpClientDoesNotDecompressEncodedData.

@Test
public void httpClientDoesNotDecompressEncodedData() throws Exception {
    setupContext();
    InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[] { 1 }));
    HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/compressed/get/1", inputStreamEntity, new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest());
    CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest);
    assertEquals(200, response.getStatusLine().getStatusCode());
    byte[] responseBytes = copyToByteArray(response.getEntity().getContent());
    assertTrue(Arrays.equals(GZIPCompression.compress("Get 1"), responseBytes));
}
Also used : HttpRequest(org.apache.http.HttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) StreamUtils.copyToString(org.springframework.util.StreamUtils.copyToString) InputStreamEntity(org.apache.http.entity.InputStreamEntity) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 30 with HttpRequest

use of org.apache.http.HttpRequest in project spring-cloud-netflix by spring-cloud.

the class GZIPCompression method putRequestBuiltWithBody.

@Test
public void putRequestBuiltWithBody() throws Exception {
    setupContext();
    InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[] { 1 }));
    HttpRequest httpRequest = getFilter().buildHttpRequest("PUT", "uri", inputStreamEntity, new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest());
    assertTrue(httpRequest instanceof HttpEntityEnclosingRequest);
    HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
    assertTrue(httpEntityEnclosingRequest.getEntity() != null);
}
Also used : HttpRequest(org.apache.http.HttpRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) StreamUtils.copyToString(org.springframework.util.StreamUtils.copyToString) InputStreamEntity(org.apache.http.entity.InputStreamEntity) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

HttpRequest (org.apache.http.HttpRequest)155 HttpResponse (org.apache.http.HttpResponse)57 HttpContext (org.apache.http.protocol.HttpContext)56 HttpHost (org.apache.http.HttpHost)52 Test (org.junit.Test)40 IOException (java.io.IOException)36 Header (org.apache.http.Header)33 HttpException (org.apache.http.HttpException)33 HttpEntity (org.apache.http.HttpEntity)27 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)25 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)22 HttpGet (org.apache.http.client.methods.HttpGet)21 HttpPost (org.apache.http.client.methods.HttpPost)21 URI (java.net.URI)19 ProtocolException (org.apache.http.ProtocolException)18 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)16 StringEntity (org.apache.http.entity.StringEntity)16 ArrayList (java.util.ArrayList)14 NameValuePair (org.apache.http.NameValuePair)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14