2012-04-30 56 views
6

Vorrei configurare un server proxy per il mio client Jersey.
Non voglio configurare il proxy per l'intera applicazione (usando argomenti JVM come http.proxyHost), e Id'e preferisco non usare il client Apache.
Ho letto here che esiste un'opzione per farlo fornendo HttpUrlConnection tramite HttpUrlConnectionFactory, ma non sono riuscito a trovare alcun esempio di codice.
Qualcuno sa come posso farlo?
Grazie!Configurare il proxy sul client Jersey

risposta

10

Con l'aiuto di Luca, ho capito fatto:

  1. Implementare HttpURLConnectionFactory, e l'override del metodo 'getHttpURLConnection', la mia implementazione è (grazie a Luca):
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128)); return new HttpURLConnection(url, proxy);

  2. Befo per creare un'istanza del client Jersey, creare un nuovo URLConnectionClientHandler e fornire il proprio HttpURLConnectionFactory nel suo costruttore. Quindi creare un nuovo Client e fornire il ClientHandler nel costruttore Client. Il mio codice:
    URLConnectionClientHandler urlConnectionClientHandler = new URLConnectionClientHandler(new MyHttpURLConnectionFactory());
    _client = new Client(urlConnectionClientHandler);

speranza di quell'aiuto.

+0

Si noti che questo funziona solo con Java 6 in poi - il costruttore HttpURLConnection con Proxy non è disponibile in Java 5. –

2

Prova

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); 
conn = new URL(url).openConnection(proxy); 
+5

Non so come, attualmente sto usando 'Client.create (config)'. Impossibile trovare alcuna opzione per usare 'UrlConnection' durante la creazione del client. – danieln

5

Prima di tutto ho creato questa classe

import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; 
    import java.io.IOException; 
    import java.net.HttpURLConnection; 
    import java.net.InetSocketAddress; 
    import java.net.Proxy; 
    import java.net.URL; 
    import java.security.KeyManagementException; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLContext; 
import javax.net.ssl.TrustManager; 

/** 
* 
* @author Aimable 
*/ 
public class ConnectionFactory implements HttpURLConnectionFactory { 

    Proxy proxy; 

    String proxyHost; 

    Integer proxyPort; 

    SSLContext sslContext; 

    public ConnectionFactory() { 
    } 

    public ConnectionFactory(String proxyHost, Integer proxyPort) { 
     this.proxyHost = proxyHost; 
     this.proxyPort = proxyPort; 
    } 

    private void initializeProxy() { 
     proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); 
    } 

    @Override 
    public HttpURLConnection getHttpURLConnection(URL url) throws IOException { 
     initializeProxy(); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy); 
     if (con instanceof HttpsURLConnection) { 
      System.out.println("The valus is...."); 
      HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(proxy); 
      httpsCon.setHostnameVerifier(getHostnameVerifier()); 
      httpsCon.setSSLSocketFactory(getSslContext().getSocketFactory()); 
      return httpsCon; 
     } else { 
      return con; 
     } 

    } 

    public SSLContext getSslContext() { 
     try { 
      sslContext = SSLContext.getInstance("SSL"); 
      sslContext.init(null, new TrustManager[]{new SecureTrustManager()}, new SecureRandom()); 
     } catch (NoSuchAlgorithmException ex) { 
      Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (KeyManagementException ex) { 
      Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return sslContext; 
    } 

    private HostnameVerifier getHostnameVerifier() { 
     return new HostnameVerifier() { 
      @Override 
      public boolean verify(String hostname, 
        javax.net.ssl.SSLSession sslSession) { 
       return true; 
      } 
     }; 
    } 

} 

poi ho anche creare un'altra classe chiamata SecureTrustManager

import java.security.cert.CertificateException; 
import java.security.cert.X509Certificate; 
import javax.net.ssl.X509TrustManager; 

/** 
* 
* @author Aimable 
*/ 
public class SecureTrustManager implements X509TrustManager { 

    @Override 
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) 
      throws CertificateException { 
    } 

    @Override 
    public void checkServerTrusted(X509Certificate[] arg0, String arg1) 
      throws CertificateException { 
    } 

    @Override 
    public X509Certificate[] getAcceptedIssuers() { 
     return new X509Certificate[0]; 
    } 

    public boolean isClientTrusted(X509Certificate[] arg0) { 
     return true; 
    } 

    public boolean isServerTrusted(X509Certificate[] arg0) { 
     return true; 
    } 

} 

poi, dopo la creazione di questa classe sto chiamando il cliente come questo

URLConnectionClientHandler cc = new URLConnectionClientHandler(new ConnectionFactory(webProxy.getWebserviceProxyHost(), webProxy.getWebserviceProxyPort())); 
    client = new Client(cc); 
    client.setConnectTimeout(2000000); 

sostituire webProxy.getWeserviceHost dal proxyHost e webProxy.getWebservi ceProxyPort() dalla porta proxy.

Questo ha funzionato per me e dovrebbe funzionare anche per voi. Nota che sto usando Jersey 1.8 ma dovrebbe funzionare anche per Jersey 2

+0

grazie, questo ha funzionato per me. – whoami

Problemi correlati