Skip to main content

[fishcat] How to setup proxy in jax-ws client generated from wsdl without using System.properties ?

4 replies [Last post]
survivant
Offline
Joined: 2004-04-12
Points: 0

I'm trying to test jax-ws clients in my application with Glassfish. The application works fine at home, but at the office I have to use proxy.

My problem is that I have to use proxy for one client only and not all of them.

It works fine for this client if I use :

System.setProperty("https.proxyHost", util.getProperties("proxy.host"));
System.setProperty("https.proxyPort", util.getProperties("proxy.port"));

but the problem is that the others clients stopped working because they don't need a proxy.

I want to test it for Glassfish 4, but I have no idea how to do that.

PS. All the clients code are generated from wsdl.

thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
survivant
Offline
Joined: 2004-04-12
Points: 0

I found that solution. There is something better ?

import org.hibernate.validator.util.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
* So the way a ProxySelector works is that for all Connections made,
* it delegates to a proxySelector(There is a default we're going to
* override with this class) to know if it needs to use a proxy
* for the connection.
*

This class was specifically created with the intent to proxy connections
* going to the allegiance soap service.

*
* @author Nate
*/
class CustomProxySelector extends ProxySelector {

private final ProxySelector def;

private Proxy proxy;

private static final Logger logger = Logger.getLogger(CustomProxySelector.class.getName());

private List proxyList = new ArrayList();

/*
* We want to hang onto the default and delegate
* everything to it unless it's one of the url's
* we need proxied.
*/
CustomProxySelector(String proxyHost, String proxyPort) {
this.def = ProxySelector.getDefault();
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, (null == proxyPort) ? 80 : Integer.valueOf(proxyPort)));
proxyList.add(proxy);
ProxySelector.setDefault(this);
}

@Override
public List select(URI uri) {
logger.info("Trying to reach URL : " + uri);
if (uri == null) {
throw new IllegalArgumentException("URI can't be null.");
}
if (uri.getHost().contains("allegiancetech")) {
logger.info("We're trying to reach allegiance so we're going to use the extProxy.");
return proxyList;
}
return def.select(uri);
}

/*
* Method called by the handlers when it failed to connect
* to one of the proxies returned by select().
*/
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
logger.severe("Failed to connect to a proxy when connecting to " + uri.getHost());
if (uri == null || sa == null || ioe == null) {
throw new IllegalArgumentException("Arguments can't be null.");
}
def.connectFailed(uri, sa, ioe);
}

}

jungicz
Offline
Joined: 2004-08-17
Points: 0

have you tried setting 'https.nonProxyHosts' system property?

--lukas

survivant
Offline
Joined: 2004-04-12
Points: 0

thanks. I didn't know that one. That could probably works. I should works for most of the cases. but there is still the case if I have 2 clients that connect to the same host with different path. (I know that one is not common and probably doesn't make sens at all) But if I want to isolate one particular client, the example that I provided is the way to go ?

jungicz
Offline
Joined: 2004-08-17
Points: 0

yes, in that particular case you example should be a way to go

--lukas