Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Client + set proxy

Hi I've a jersey client which i use to upload a file. I tried using it locally and everything works fine. But in production environment i've to set proxy. I browsed thru few pages but could not get exact solution. Can someone pls help me with this?

here is my client code:

File file = new File("e:\\test.zip");
FormDataMultiPart part = new FormDataMultiPart();
part.bodyPart(new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
WebResource resource = null;

if (proxy.equals("yes")) {
    // How do i configure client in this case?
} else {
    // this uses system proxy i guess
    resource = Client.create().resource(url);
}

String response = (String) resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
System.out.println(response);
like image 711
user732362 Avatar asked May 02 '12 14:05

user732362


3 Answers

There is an easier approach, if you want to avoid more libraries in legacy projects and there is no need for Proxy Authentication:

First you need a class which implements HttpURLConnectionFactory:

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;


public class ConnectionFactory implements HttpURLConnectionFactory {

    Proxy proxy;

    private void initializeProxy() {
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxy.com", 3128));
    }

    public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
        initializeProxy();
        return (HttpURLConnection) url.openConnection(proxy);
    }
}

Second is to instantiate an com.sun.jersey.client.urlconnection.URLConnectionHandler

URLConnectionClientHandler ch  = new URLConnectionClientHandler(new ConnectionFactory());

and third is to use the Client Constructor instead of Client.create:

Client client = new Client(ch);

Of course you can customize the initializing of the Proxy in the ConnectionFactory.

like image 157
luckyluke Avatar answered Nov 06 '22 13:11

luckyluke


luckyluke answer shall work. Here my version:

ClientConfig config = new DefaultClientConfig();
Client client = new Client(new URLConnectionClientHandler(
        new HttpURLConnectionFactory() {
    Proxy p = null;
    @Override
    public HttpURLConnection getHttpURLConnection(URL url)
            throws IOException {
        if (p == null) {
            if (System.getProperties().containsKey("http.proxyHost")) {
                p = new Proxy(Proxy.Type.HTTP,
                        new InetSocketAddress(
                        System.getProperty("http.proxyHost"),
                        Integer.getInteger("http.proxyPort", 80)));
            } else {
                p = Proxy.NO_PROXY;
            }
        }
        return (HttpURLConnection) url.openConnection(p);
    }
}), config);
like image 20
sehari24jam Avatar answered Nov 06 '22 13:11

sehari24jam


Here you go:

 DefaultApacheHttpClient4Config config = new DefaultApacheHttpClient4Config();
      config.getProperties().put(
      ApacheHttpClient4Config.PROPERTY_PROXY_URI, 
      "PROXY_URL"
 );

 config.getProperties().put(
      ApacheHttpClient4Config.PROPERTY_PROXY_USERNAME, 
      "PROXY_USER"
 );

 config.getProperties().put(
      ApacheHttpClient4Config.PROPERTY_PROXY_PASSWORD, 
      "PROXY_PASS"
 );     

 Client c = ApacheHttpClient4.create(config);
 WebResource r = c.resource("https://www.google.com/");
like image 3
sdolgy Avatar answered Nov 06 '22 14:11

sdolgy