use of org.apache.hc.core5.http.HttpHost in project cxf by apache.
the class AsyncHTTPConduit method setupConnection.
@Override
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
if (factory.isShutdown()) {
message.put(USE_ASYNC, Boolean.FALSE);
super.setupConnection(message, address, csPolicy);
return;
}
propagateJaxwsSpecTimeoutSettings(message, csPolicy);
boolean addressChanged = false;
// need to do some clean up work on the URI address
URI uri = address.getURI();
String uriString = uri.toString();
if (uriString.startsWith("hc://")) {
uriString = uriString.substring(5);
addressChanged = true;
} else if (uriString.startsWith("hc5://")) {
uriString = uriString.substring(6);
addressChanged = true;
}
if (addressChanged) {
try {
uri = new URI(uriString);
} catch (URISyntaxException ex) {
throw new MalformedURLException("unsupport uri: " + uriString);
}
}
String s = uri.getScheme();
if (!"http".equals(s) && !"https".equals(s)) {
throw new MalformedURLException("unknown protocol: " + s);
}
Object o = message.getContextualProperty(USE_ASYNC);
if (o == null) {
o = factory.getUseAsyncPolicy();
}
switch(UseAsyncPolicy.getPolicy(o)) {
case ALWAYS:
o = true;
break;
case NEVER:
o = false;
break;
case ASYNC_ONLY:
default:
o = !message.getExchange().isSynchronous();
break;
}
// check tlsClientParameters from message header
TLSClientParameters clientParameters = message.get(TLSClientParameters.class);
if (clientParameters == null) {
clientParameters = tlsClientParameters;
}
if ("https".equals(uri.getScheme()) && clientParameters != null && clientParameters.getSSLSocketFactory() != null) {
// if they configured in an SSLSocketFactory, we cannot do anything
// with it as the NIO based transport cannot use socket created from
// the SSLSocketFactory.
o = false;
}
if (!PropertyUtils.isTrue(o)) {
message.put(USE_ASYNC, Boolean.FALSE);
super.setupConnection(message, addressChanged ? new Address(uriString, uri) : address, csPolicy);
return;
}
if (StringUtils.isEmpty(uri.getPath())) {
// hc needs to have the path be "/"
uri = uri.resolve("/");
}
message.put(USE_ASYNC, Boolean.TRUE);
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Asynchronous connection to " + uri.toString() + " has been set up");
}
message.put("http.scheme", uri.getScheme());
String httpRequestMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
if (httpRequestMethod == null) {
httpRequestMethod = "POST";
message.put(Message.HTTP_REQUEST_METHOD, httpRequestMethod);
}
final CXFHttpRequest e = new CXFHttpRequest(httpRequestMethod, uri);
final String contentType = (String) message.get(Message.CONTENT_TYPE);
final MutableHttpEntity entity = new MutableHttpEntity(contentType, null, true) {
public boolean isRepeatable() {
return e.getOutputStream().retransmitable();
}
};
e.setEntity(entity);
final RequestConfig.Builder b = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(csPolicy.getConnectionTimeout())).setResponseTimeout(Timeout.ofMilliseconds(csPolicy.getReceiveTimeout())).setConnectionRequestTimeout(Timeout.ofMilliseconds(csPolicy.getConnectionRequestTimeout()));
final Proxy p = proxyFactory.createProxy(csPolicy, uri);
if (p != null && p.type() != Proxy.Type.DIRECT) {
InetSocketAddress isa = (InetSocketAddress) p.address();
HttpHost proxy = new HttpHost(isa.getHostString(), isa.getPort());
b.setProxy(proxy);
}
e.setConfig(b.build());
message.put(CXFHttpRequest.class, e);
}
use of org.apache.hc.core5.http.HttpHost in project ksql by confluentinc.
the class WebClient method send.
/**
* Sends a POST request to a web server
* This method requires a pre-configured http client instance
*
* @param customerId customer Id on behalf of which the request is sent
* @param bytes request payload
* @param httpPost A POST request structure
* @param proxy a http (passive) proxy
* @param httpClient http client instance configured by caller
* @return an HTTP Status code
* @see #send(String, byte[], HttpPost, ResponseHandler)
*/
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:FinalParameters" })
protected static int send(final String customerId, final byte[] bytes, final HttpPost httpPost, final HttpHost proxy, CloseableHttpClient httpClient, final ResponseHandler responseHandler) {
int statusCode = DEFAULT_STATUS_CODE;
if (bytes != null && bytes.length > 0 && httpPost != null && customerId != null) {
// add the body to the request
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.LEGACY);
builder.addTextBody("cid", customerId);
builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, "filename");
httpPost.setEntity(builder.build());
httpPost.addHeader("api-version", "phone-home-v1");
// set the HTTP config
RequestConfig config = RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).setConnectionRequestTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).setResponseTimeout(Timeout.ofMilliseconds(REQUEST_TIMEOUT_MS)).build();
CloseableHttpResponse response = null;
try {
if (proxy != null) {
log.debug("setting proxy to {}", proxy);
config = RequestConfig.copy(config).setProxy(proxy).build();
httpPost.setConfig(config);
final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
if (httpClient == null) {
httpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).setDefaultRequestConfig(config).build();
}
} else {
if (httpClient == null) {
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
}
}
response = httpClient.execute(httpPost);
if (responseHandler != null) {
responseHandler.handle(response);
}
// send request
log.debug("POST request returned {}", new StatusLine(response).toString());
statusCode = response.getCode();
} catch (IOException e) {
log.error("Could not submit metrics to Confluent: {}", e.getMessage());
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
log.warn("could not close http client", e);
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.warn("could not close http response", e);
}
}
}
} else {
statusCode = HttpStatus.SC_BAD_REQUEST;
}
return statusCode;
}
Aggregations