CXF client uses java.net.URLConnection
to connect to a service. The URLConnection can be configured to select a local IP address in this way (see How can I specify the local address on a java.net.URLConnection?)
URL url = new URL(yourUrlHere);
Proxy proxy = new Proxy(Proxy.Type.DIRECT,
new InetSocketAddress(
InetAddress.getByAddress(
new byte[]{your, ip, interface, here}), yourTcpPortHere));
URLConnection conn = url.openConnection(proxy);
I have inspected the code of artifacts cxf-rt-rs-client
and cxf-rt-transports-http
to see how CXF is creating the connection. In ProxyFactory is the code to create the Proxy
object needed for the UrlConnection
private Proxy createProxy(final HTTPClientPolicy policy) {
return new Proxy(Proxy.Type.valueOf(policy.getProxyServerType().toString()),
new InetSocketAddress(policy.getProxyServer(),
policy.getProxyServerPort()));
}
As you can see, there is no way to configure the IP address, so i am afraid that the answer to the question is you can not configure source IP address with CXF
But, I think it would not be difficult to modify the source code to allow setting the source IP address
HTTPClientPolicy
Add the following code to org.apache.cxf.transports.http.configuration.HTTPClientPolicy
at cxf-rt-transports-http
public class HTTPClientPolicy {
protected byte[] sourceIPAddress;
protected int port;
public boolean isSetSourceIPAddress(){
return (this.sourceIPAddress != null);
}
ProxyFactory
Modify the following code to org.apache.cxf.transport.http.ProxyFactory
at cxf-rt-transports-http
//added || policy.isSetSourceIPAddress()
//getProxy() calls finally to createProxy
public Proxy createProxy(HTTPClientPolicy policy, URI currentUrl) {
if (policy != null) {
// Maybe the user has provided some proxy information
if (policy.isSetProxyServer() || policy.isSetSourceIPAddress())
&& !StringUtils.isEmpty(policy.getProxyServer())) {
return getProxy(policy, currentUrl.getHost());
} else {
// There is a policy but no Proxy configuration,
// fallback on the system proxy configuration
return getSystemProxy(currentUrl.getHost());
}
} else {
// Use system proxy configuration
return getSystemProxy(currentUrl.getHost());
}
}
//Added condition to set the source IP address (is set)
//Will work also with a proxy
private Proxy createProxy(final HTTPClientPolicy policy) {
if (policy.isSetSourceIPAddress()){
Proxy proxy = new Proxy(Proxy.Type.DIRECT,
new InetSocketAddress(
InetAddress.getByAddress(
policy.getSourceIPAddress(), policy.getPort()));
} else {
return new Proxy(Proxy.Type.valueOf(policy.getProxyServerType().toString()),
new InetSocketAddress(policy.getProxyServer(),
policy.getProxyServerPort()));
}
}
Usage
Client client = ClientProxy.getClient(service);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setSourceIPAddress(new byte[]{your, ip, interface, here}));
httpClientPolicy.setPort(yourTcpPortHere);
http.setClient(httpClientPolicy);