johvargas / sfdc-wsc

Automatically exported from code.google.com/p/sfdc-wsc
0 stars 0 forks source link

Support for Proxy with authentication #34

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.Generate jar from any  wsdl (I'm using the SFDC partners one)
2.Configure proxy parameters ( see code example below)
3. Call Connector.newConnection(config)

What is the expected output? What do you see instead?

Successful connection.
Instead I'm getting:
java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 
407 Proxy Authentication Required ( The ISA Server requires authorization to 
fulfill the request. Access to the Web Proxy filter is denied.  )"

What version of the product are you using? On what operating system?

I tested the issue on wsc-20.jar 

Please provide any additional information below.

I'm using the following code example:
***********************************************************************
package test.ws;

import com.sforce.soap.partner.*;
import com.sforce.soap.partner.sobject.*;
import com.sforce.ws.*;

public class simple {

    /**
     * @param args
     */

       public static void main(String[] args) {
                  ConnectorConfig config = new ConnectorConfig();
                  config.setUsername("myUser");
                  config.setPassword("mypass");
                  config.setProxy("10.1.1.15", 8080);
                  config.setProxyUsername("proxyUser");
                  config.setProxyPassword("ProxyPass");

                  PartnerConnection connection = null;
            try {
            connection = Connector.newConnection(config);

            } catch (ConnectionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                  SObject account = new SObject();
                  account.setType("Account");
                  account.setField("Name", "My Account1");
                  try {
                    connection.create(new SObject[]{account});
                } catch (ConnectionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
       }

}
**************************************************************************

I'm using Microsoft ISA as my proxy configured to allow Basic auth.

While tracing the network traffic using wireshark I can see that the request is 
sent without auth header, the response is the 407 message and there is no 
following CONNECT with authentication being sent.

The following code seems to work:

When tracing the following code with wireshark, I can see that the first 
CONNECT message is sent with no authentication headers, the same 407 message is 
returning from the server and the client respopnd by CONNECT that has the 
authentication header (Proxy-authorization: Basic......)

**********************************************************************
package test.ws;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

 class ProxyAuthenticator extends Authenticator {  

         private String user, password;  

         public ProxyAuthenticator(String user, String password) {  
             this.user = user;  
             this.password = password;  
         }  

         protected PasswordAuthentication getPasswordAuthentication() {  
             return new PasswordAuthentication(user, password.toCharArray());  
         }  
     }
**************************************************************************

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

public class TestProxy2 {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Authenticator.setDefault(new ProxyAuthenticator("proxyUser", "ProxyPass"));  
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.1.1.15", 8080));
        String line="";
        URL url = null;
        try {
            url = new URL("https://mail.google.com");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);

         uc.connect();
         StringBuffer page=new StringBuffer();
         StringBuffer tmp = new StringBuffer();
         BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        while ((line = in.readLine()) != null){
               page.append(line + "\n");
              System.out.println(page.toString());

        }
        System.out.println("page.toString()");

    }

}

**********************************************************************

Original issue reported on code.google.com by avi.han...@gmail.com on 5 Dec 2010 at 12:11

GoogleCodeExporter commented 8 years ago
One more issue that I forgot to mention is SSL tunneling, When using the proxy 
auth to connect to an HTTP URL everything work fine. Only when the URL points 
to an HTTPS I'm getting the error message.

Original comment by avi.han...@gmail.com on 5 Dec 2010 at 12:13

GoogleCodeExporter commented 8 years ago
This seems to work for me through a MS Proxy:

  ConnectorConfig partnerConfig = new ConnectorConfig();
  partnerConfig.setNtlmDomain("yourDomain");
  partnerConfig.setProxy("yourProxy", 80);
  partnerConfig.setProxyUsername("yourUsername");
  partnerConfig.setProxyPassword("yourPassword");
  partnerConfig.setAuthEndpoint("https://test.salesforce.com/services/Soap/u/21.0");
  partnerConfig.setConnectionTimeout(60 * 1000);
  partnerConnection = Connector.newConnection(partnerConfig);
  partnerConnection.describeGlobal().getSobjects()

Original comment by oscarm...@gmail.com on 3 Jun 2011 at 2:37

GoogleCodeExporter commented 8 years ago
I have the same problem with a non-MS proxy, and there have been no comments to 
this bug for over a year.

If the OP reads this, did you manage to solve it or found a work-around?

I'm using wsc-22-jdk-1-5.jar

Original comment by jose.cer...@gmail.com on 14 Jun 2012 at 8:24

GoogleCodeExporter commented 8 years ago
Well...

If you can get the TestProxy2 example to work - then try the ConnectorConfig 
method:

public void setProxy(java.net.Proxy proxy) ;

You won't see this method in Salesforce's documentation.

If that doesn't work - check that machine running the Java code and ping 
www.cnn.com.

If it can't ping that machine then it's likely that a firewall policy is 
restricting access to the internet (and you have change the policy).

Original comment by mclean.b...@gmail.com on 1 Aug 2013 at 12:13

GoogleCodeExporter commented 8 years ago
Hi,

The suggestion of Mclean Blades seems to do the trick. 
Thanks you very much.

Original comment by avi.han...@gmail.com on 9 Sep 2013 at 8:45

GoogleCodeExporter commented 8 years ago
Forgot to attach the code:

***********************************************************************
package test.ws;

import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.Proxy;

import com.sforce.soap.partner.*;
import com.sforce.soap.partner.sobject.*;
import com.sforce.ws.*;

public class simple {

       public static void main(String[] args) {

                  ConnectorConfig config = new ConnectorConfig();
                  config.setUsername("myuser");
                  config.setPassword("myPass");

                  Authenticator.setDefault(new ProxyAuthenticator("proxyUser", "proxyPass"));  
                  Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.1.1.15", 8080));
                  config.setProxy(proxy);

                  config.setAuthEndpoint("https://www.salesforce.com/services/Soap/u/23.0");

                  PartnerConnection connection = null;
                try {

                    connection = Connector.newConnection(config);

                    } catch (ConnectionException e) {
                     e.printStackTrace();
                    }
                  SObject account = new SObject();
                  account.setType("Account");
                  account.setField("Name", "My Account1");
                  try {
                    connection.create(new SObject[]{account});
                } catch (ConnectionException e) {
                    e.printStackTrace();
                }
       }

}

***********************************************************************

**********************************************************************
package test.ws;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

 class ProxyAuthenticator extends Authenticator {  

         private String user, password;  

         public ProxyAuthenticator(String user, String password) {  
             this.user = user;  
             this.password = password;  
         }  

         protected PasswordAuthentication getPasswordAuthentication() {  
             return new PasswordAuthentication(user, password.toCharArray());  
         }  
     }
**************************************************************************

Original comment by avi.han...@gmail.com on 9 Sep 2013 at 8:57

GoogleCodeExporter commented 8 years ago
thank you

Original comment by bharatsl...@gmail.com on 12 Jun 2014 at 1:30