use of org.apache.cxf.transport.http.Address 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.cxf.transport.http.Address in project cxf by apache.
the class NettyHttpConduit method setupConnection.
// Using Netty API directly
protected void setupConnection(Message message, Address address, HTTPClientPolicy csPolicy) throws IOException {
URI uri = address.getURI();
boolean addressChanged = false;
// need to do some clean up work on the URI address
String uriString = uri.toString();
if (uriString.startsWith("netty://")) {
try {
uriString = uriString.substring(8);
uri = new URI(uriString);
addressChanged = true;
} 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(NettyHttpConduitFactory.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;
}
message.put(USE_ASYNC, Boolean.TRUE);
if (StringUtils.isEmpty(uri.getPath())) {
// hc needs to have the path be "/"
uri = uri.resolve("/");
}
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);
}
// setup a new NettyHttpClientRequest
final NettyHttpClientRequest request = new NettyHttpClientRequest(uri, httpRequestMethod);
final int ctimeout = determineConnectionTimeout(message, csPolicy);
final int rtimeout = determineReceiveTimeout(message, csPolicy);
final int maxResponseContentLength = determineMaxResponseContentLength(message);
request.setConnectionTimeout(ctimeout);
request.setReceiveTimeout(rtimeout);
request.setMaxResponseContentLength(maxResponseContentLength);
message.put(NettyHttpClientRequest.class, request);
}
use of org.apache.cxf.transport.http.Address 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://")) {
try {
uriString = uriString.substring(5);
uri = new URI(uriString);
addressChanged = true;
} 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);
BasicHttpEntity entity = new BasicHttpEntity() {
public boolean isRepeatable() {
return e.getOutputStream().retransmitable();
}
};
entity.setChunked(true);
entity.setContentType((String) message.get(Message.CONTENT_TYPE));
e.setURI(uri);
e.setEntity(entity);
RequestConfig.Builder b = RequestConfig.custom().setConnectTimeout((int) csPolicy.getConnectionTimeout()).setSocketTimeout((int) csPolicy.getReceiveTimeout()).setConnectionRequestTimeout((int) csPolicy.getConnectionRequestTimeout());
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);
}
Aggregations