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);
}
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();
}
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));
}
}
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));
}
}
Aggregations