Search in sources :

Example 26 with RequestLine

use of org.apache.http.RequestLine in project LogHub by fbacchella.

the class AbstractHttpSender method doRequest.

protected HttpResponse doRequest(HttpRequest therequest) {
    CloseableHttpResponse response = null;
    HttpClientContext context = HttpClientContext.create();
    if (credsProvider != null) {
        context.setCredentialsProvider(credsProvider);
    }
    HttpHost host;
    RequestLine requestLine = new BasicRequestLine(therequest.verb, therequest.url.getPath(), therequest.httpVersion);
    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(requestLine);
    if (therequest.content != null) {
        request.setEntity(therequest.content);
    }
    therequest.headers.forEach((i, j) -> request.addHeader(i, j));
    host = new HttpHost(therequest.url.getHost(), therequest.url.getPort(), therequest.url.getProtocol());
    try {
        response = client.execute(host, request, context);
    } catch (ConnectionPoolTimeoutException e) {
        logger.error("Connection to {} timed out", host);
        return new HttpResponse(host, null, e, null);
    } catch (HttpHostConnectException e) {
        String message = "";
        try {
            throw e.getCause();
        } catch (ConnectException e1) {
            message = String.format("Connection to %s refused", host);
        } catch (SocketTimeoutException e1) {
            message = String.format("Slow response from %s", host);
        } catch (Throwable e1) {
            message = String.format("Connection to %s failed: %s", host, e1.getMessage());
            logger.catching(Level.DEBUG, e1);
        }
        logger.error(message);
        logger.catching(Level.DEBUG, e.getCause());
        return new HttpResponse(host, null, e, null);
    } catch (IOException e) {
        Throwable rootCause = e;
        while (rootCause.getCause() != null) {
            rootCause = rootCause.getCause();
        }
        ;
        // A TLS exception, will not help to retry
        if (rootCause instanceof GeneralSecurityException) {
            logger.error("Secure comunication with {} failed: {}", host, rootCause.getMessage());
            logger.catching(Level.DEBUG, rootCause);
            return new HttpResponse(host, null, null, (GeneralSecurityException) rootCause);
        } else {
            logger.error("Comunication with {} failed: {}", host, e.getMessage());
            logger.catching(Level.DEBUG, e);
            return new HttpResponse(host, null, e, null);
        }
    }
    if (response == null) {
        logger.error("give up trying to connect to " + getPublishName());
        return null;
    }
    ;
    return new HttpResponse(host, response, null, null);
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ConnectionPoolTimeoutException(org.apache.http.conn.ConnectionPoolTimeoutException) RequestLine(org.apache.http.RequestLine) BasicRequestLine(org.apache.http.message.BasicRequestLine) SocketTimeoutException(java.net.SocketTimeoutException) HttpHost(org.apache.http.HttpHost) BasicRequestLine(org.apache.http.message.BasicRequestLine) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException)

Example 27 with RequestLine

use of org.apache.http.RequestLine in project wso2-synapse by wso2.

the class ClientConnectionDebug method recordRequestStartTime.

public void recordRequestStartTime(NHttpClientConnection conn, Axis2HttpRequest axis2Req) {
    if (conn != null) {
        this.connectionCreationTime = (Long) conn.getContext().getAttribute(ClientHandler.CONNECTION_CREATION_TIME);
        try {
            HttpRequest request = axis2Req.getRequest();
            RequestLine requestLine = request.getRequestLine();
            this.lastRequestProtocol = requestLine.getProtocolVersion().toString();
            this.lastRequestHTTPMethod = requestLine.getMethod();
            this.headers = request.getAllHeaders();
        } catch (IOException ignore) {
        } catch (HttpException ignore) {
        }
    }
    if (this.lastRequestStartTime != 0) {
        if (previousRequestAttempts == null) {
            previousRequestAttempts = new StringBuffer();
        } else {
            previousRequestAttempts.append(fieldSeparator);
        }
        previousRequestAttempts.append("Attempt-Info").append(keyValueSeparator).append("{");
        previousRequestAttempts.append("Req-Start-Time").append(keyValueSeparator).append(format(this.lastRequestStartTime));
        previousRequestAttempts.append(fieldSeparator);
        previousRequestAttempts.append("Req-URL").append(keyValueSeparator).append(this.lastRequestEPR).append("}");
    }
    this.lastRequestStartTime = System.currentTimeMillis();
    this.lastRequestEPR = axis2Req.getEpr().toString();
}
Also used : Axis2HttpRequest(org.apache.synapse.transport.nhttp.Axis2HttpRequest) HttpRequest(org.apache.http.HttpRequest) RequestLine(org.apache.http.RequestLine) HttpException(org.apache.http.HttpException) IOException(java.io.IOException)

Example 28 with RequestLine

use of org.apache.http.RequestLine in project cu-kfs by CU-CommunityApps.

the class MockServiceEndpointBase method getNonBlankValueFromRequestUrlIfPresent.

protected Optional<String> getNonBlankValueFromRequestUrlIfPresent(HttpRequest request, Pattern regex, int regexGroupIndex) {
    RequestLine requestLine = request.getRequestLine();
    String requestUrl = requestLine.getUri();
    Matcher urlMatcher = regex.matcher(requestUrl);
    if (regexGroupIndex < 0 || urlMatcher.groupCount() < regexGroupIndex) {
        throw new IllegalArgumentException("Regex does not have a capturing group with an index of " + regexGroupIndex);
    } else if (!urlMatcher.matches()) {
        return Optional.empty();
    } else {
        return defaultToEmptyOptionalIfBlank(urlMatcher.group(regexGroupIndex));
    }
}
Also used : RequestLine(org.apache.http.RequestLine) Matcher(java.util.regex.Matcher)

Example 29 with RequestLine

use of org.apache.http.RequestLine in project knox by apache.

the class DefaultDispatchTest method testJiraKnox58.

// Make sure Hadoop cluster topology isn't exposed to client when there is a connectivity issue.
@Test
public void testJiraKnox58() throws URISyntaxException, IOException {
    URI uri = new URI("http://unreachable-host.invalid");
    BasicHttpParams params = new BasicHttpParams();
    HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpUriRequest.class);
    EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
    EasyMock.expect(outboundRequest.getURI()).andReturn(uri).anyTimes();
    RequestLine requestLine = EasyMock.createNiceMock(RequestLine.class);
    EasyMock.expect(requestLine.getMethod()).andReturn("GET").anyTimes();
    EasyMock.expect(requestLine.getProtocolVersion()).andReturn(HttpVersion.HTTP_1_1).anyTimes();
    EasyMock.expect(outboundRequest.getRequestLine()).andReturn(requestLine).anyTimes();
    EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();
    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<SynchronousServletOutputStreamAdapter>() {

        @Override
        public SynchronousServletOutputStreamAdapter answer() throws Throwable {
            return new SynchronousServletOutputStreamAdapter() {

                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host.invalid");
                }
            };
        }
    });
    EasyMock.replay(outboundRequest, inboundRequest, outboundResponse, requestLine);
    DefaultDispatch dispatch = new DefaultDispatch();
    HttpClientBuilder builder = HttpClientBuilder.create();
    CloseableHttpClient client = builder.build();
    dispatch.setHttpClient(client);
    try {
        dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
        fail("Should have thrown IOException");
    } catch (IOException e) {
        assertThat(e.getMessage(), not(containsString("unreachable-host.invalid")));
        assertThat(e, not(instanceOf(UnknownHostException.class)));
        assertThat("Message needs meaningful content.", e.getMessage().trim().length(), greaterThan(12));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) UnknownHostException(java.net.UnknownHostException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URI(java.net.URI) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestLine(org.apache.http.RequestLine) SynchronousServletOutputStreamAdapter(org.apache.knox.gateway.servlet.SynchronousServletOutputStreamAdapter) BasicHttpParams(org.apache.http.params.BasicHttpParams) Test(org.junit.Test)

Aggregations

RequestLine (org.apache.http.RequestLine)29 IOException (java.io.IOException)9 HttpHost (org.apache.http.HttpHost)9 HttpEntity (org.apache.http.HttpEntity)7 ProtocolVersion (org.apache.http.ProtocolVersion)7 StatusLine (org.apache.http.StatusLine)6 BasicRequestLine (org.apache.http.message.BasicRequestLine)6 HttpRequest (org.apache.http.HttpRequest)5 HttpException (org.apache.http.HttpException)4 HttpResponse (org.apache.http.HttpResponse)4 ConnectionClosedException (org.apache.http.ConnectionClosedException)3 Header (org.apache.http.Header)3 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)3 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)3 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)3 BasicStatusLine (org.apache.http.message.BasicStatusLine)3 Before (org.junit.Before)3 Matchers.anyString (org.mockito.Matchers.anyString)3 SocketTimeoutException (java.net.SocketTimeoutException)2 URL (java.net.URL)2