2011-06-22 3 views
31

私はインターネット上のシステムにSOAPクライアント要求を行う必要があるアプリケーションを持っているので、HTTPプロキシを経由する必要があります。システム全体のプロパティを設定せずにJAX-WSリクエストにHTTPプロキシを使用するにはどうすればよいですか?

// Cowboy-style. Blow away anything any other part of the application has set. 
System.getProperties().put("proxySet", "true"); 
System.getProperties().put("https.proxyHost", HTTPS_PROXY_HOST); 
System.getProperties().put("https.proxyPort", HTTPS_PROXY_PORT); 

またはデフォルトのProxySelector(また、システム全体の設定)を設定することにより:

// More Cowboy-style! Every thing Google has found says to do it this way!?!?! 
ProxySelector.setDefault(new MyProxySelector(HTTPS_PROXY_HOST, HTTPS_PROXY_PORT)); 

どちらのを

一つは、システムプロパティとして、システム全体の値を設定することによってこれを行うことができ異なるHTTPプロキシを介して、またはプロキシを使用せずにWebサーバーにアクセスすることを望む他のサブシステムの可能性がある場合、これは賢明な選択です。 ProxySelectorを使用すると、どの接続がプロキシを使用するかを設定できるようになりますが、巨大なアプリケーションのすべてのものに対してそのことを理解する必要があります。

妥当なAPIは、java.net.Socket(java.net.Proxy proxy)のコンストラクタのように、java.net.Proxyオブジェクトを受け取るメソッドを持ちます。そうすることで、必要な設定は、システムの設定が必要な部分にとってローカルになります。 JAX-WSでこれを行う方法はありますか?

システム全体のプロキシ設定を設定したくありません。

答えて

5

JAX-WSを使用している場合は、基になるHttpURLConnectionで使用されるソケットファクトリを設定できます。私はこれがSSLのために可能であるという曖昧な兆候を見ています(HTTPS SSLSocketFactoryを参照)が、私はあなたが通常のHTTP接続に対してそれを行うことができるかどうかはっきりしません。 JDKクラス)。

ソケットファクトリを設定できる場合は、必要な特定のプロキシを使用するカスタムソケットファクトリをコンフィグレーションできます。

+1

'JAXWSProperties'が私のJDK 1.6に存在すると表示されません。 – jbindel

+0

他に何も現れなければ、私はこれを後で受け入れます。 – jbindel

+0

あなたのメソッドはうまくいきました。私たちはそれをうまく実装しましたが、サポートされているAPIと 'HttpsURLConnection.setDefaultSSLSocketFactory()'を使って 'SSLSocketFactory'のJVMワイドな設定を行うことに決めました。任意のキーと証明書を、デフォルトのキーストアとトラストストアの1つのバージョンに結合します。 – jbindel

7

カスタムProxySelectorの使用をお勧めします。私は同じ問題を抱えていました。それも簡単です。

ここに私のCustomProxySelectorです:

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. 
* <p>This class was specifically created with the intent to proxy connections 
* going to the allegiance soap service.</p> 
* 
* @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<Proxy> proxyList = new ArrayList<Proxy>(); 

/* 
* 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<Proxy> 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); 
} 

}

+2

私はこれが 'MyProxySelector'でのアプローチと似ていると思います。 – jbindel

関連する問題