This discussion is archived
14 Replies Latest reply: Nov 20, 2012 5:02 AM by GopalGajarao RSS

weblogic does not call the ProxySelector class for http url

646689 Newbie
Currently Being Moderated
We are using weblogic server for our project. I have created a ProxySelector class and made it the default in my code. When my code tried to access the external url, the ProxySelector is called for socket url but not http url. Because of this, I am not able to return any HTTP proxy object from the ProxySelector and it fails.

Note: The code works fine if I execute it from tomcat.
  • 1. Re: weblogic does not call the ProxySelector class for http url
    user161771 - oracle Expert
    Currently Being Moderated
    Have you tried checking whether or not a different approach works like:
    -Dhttp.proxyHost=proxyhostURL
    -Dhttp.proxyPort=proxyPortNumber
    -Dhttp.proxyUser=someUserName
    -Dhttp.proxyPassword=somePassword
    I also recommend working with support.
  • 2. Re: weblogic does not call the ProxySelector class for http url
    646689 Newbie
    Currently Being Moderated
    I cannot try system properties or jvm startup parameters. I needed a per connection approach. I am using webservices which does not do any url connection opening. Instead it uses a dispatch object to access a service. So the only thing I have is ProxySelector.

    Also My application is vast and it tries to access external urls at lots of places, we want to use this as this is a centralized approach.
  • 3. Re: weblogic does not call the ProxySelector class for http url
    user161771 - oracle Expert
    Currently Being Moderated
    Can you attached a super simple reproducer java snippet that you expect to work that does not?
  • 4. Re: weblogic does not call the ProxySelector class for http url
    646689 Newbie
    Currently Being Moderated
    Hi This is the method that I have for invoking webservices. It uses a Dispatch API. The myProxySelector is an instance of MyProxySelector which is already made defult in the appication startup.
    This MyProxySelector has a threadlocal variable useProxy to indicate whether the proxy should be used or not. so I am setting the variable just before reply = dispatch.invoke(sm);


    public class WebServiceDispatch {
    .............
    .............
    .............

    public Object invoke() throws GLException {
    Object result = null;
    try {
    Service service = Service.create(getOperation().getServiceName());
    service.addPort(getOperation().getOperationName(), SOAPBinding.SOAP11HTTP_BINDING, call.getServiceEndpoint());
    Dispatch<SOAPMessage> dispatch = service.createDispatch(getOperation().getOperationName(), SOAPMessage.class, Service.Mode.MESSAGE);
    initHandlers(dispatch);
    Map<String, Object> ctx = dispatch.getRequestContext();
    if (call.useWSS()) {
    ctx.put(SOAPConstants.WSSE_SECURITY, new WSSHeader(getCredentials().getUsername(), getCredentials().getPassword(), call.sendEncryptedPassword()));
    } else if (call.getCredentials() != null ) {
    String user = getCredentials().getUsername();
    if ( user != null ) {
    ctx.put(Dispatch.USERNAME_PROPERTY, user);
    if (getCredentials().getPassword() != null ) {
    if (call.sendEncryptedPassword()) {
    ctx.put(Dispatch.PASSWORD_PROPERTY, getCredentials().getPassword().getEncryption());
    } else {
    ctx.put(Dispatch.PASSWORD_PROPERTY, getCredentials().getPassword().getText());
    }
    }
    }
    }
    MessageFactory mf = MessageFactory.newInstance();
    SOAPMessage sm = mf.createMessage();
    SOAPHeader sh = sm.getSOAPHeader();
    SOAPBody sb = sm.getSOAPBody();
    for ( Iterator<?> it = call.getOperation().getOrderedParameters().iterator(); it.hasNext(); ) {
    WebServiceParameter parameter = (WebServiceParameter) it.next();
    if (parameter.isBodyParam()) {
    sb.addChildElement((SOAPElement)call.getParameterValue(parameter));
    } else {
    sh.addChildElement((SOAPElement)call.getParameterValue(parameter));
    }
    }

    String endPoint = call.getServiceEndpoint();
    myProxySelector.setUseProxy(useProxyServer);

    if (getOperation().getReturn() != null) {
    reply = dispatch.invoke(sm);
    result = getReplyElement(reply);
    call.setParameterValue(getOperation().getReturn(), result);
    } else {
    dispatch.invokeOneWay(sm);
    }

    } catch (Throwable t) {
    throw GLException.factory(new CausedBy("Error Invoking Web Service Dispatch"),t);
    }
    return result;
    }
    ........
    .........// more code to get webservice details from db and load useProxyServer flag value
    }

    Here is the ProxySelector code

    public class MyProxySelector extends ProxySelector {
    protected ThreadLocal<Boolean> useProxy = new ThreadLocal<Boolean>();

         public ThreadLocal<Boolean> getUseProxy() {
              return useProxy;
         }

         public void setUseProxy(boolean useProxy) {
              setUseProxy(useProxy? Boolean.TRUE: Boolean.FALSE);
         }

         public void setUseProxy(Boolean useProxy) {
              this.useProxy.set(useProxy);
         }

         private ProxySelector jvmDefaultSelector = null;
         public ProxySelector getJVMDefaultSelector() {
              return jvmDefaultSelector;
         }

         private static MyProxySelector myProxySelector = null;

         private MyProxySelector(ProxySelector jvmDefaultSelector) {
              this.jvmDefaultSelector = jvmDefaultSelector;
         }

         public static MyProxySelector getInstance() {
              if(myProxySelector==null) {
                   myProxySelector = new MyProxySelector(ProxySelector.getDefault());
              }
              return myProxySelector;
         }
         @Override
         public List<Proxy> select(URI uri) {
              try {
                   if (uri == null) {
                        throw new IllegalArgumentException("URI can't be null.");
                   }

                   Boolean useProxyObject = useProxy.get();
                   if(useProxyObject == null) {
                        String protocol = uri.getScheme();
                        if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
                             return getProxyList();
                        } else if(jvmDefaultSelector != null) {
                             return jvmDefaultSelector.select(uri);;
                        }
                   }

                   if(useProxyObject.booleanValue()) {
                        String protocol = uri.getScheme();
                        if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
                             return getProxyList();
                        }
                   }

              } catch(Exception e) {
              } finally {
                   if (useProxy != null) {
                        useProxy.remove();
                   }
              }

              return getNoProxyList();
         }

         @Override
         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
              if (uri == null || sa == null || ioe == null) {
                   throw new IllegalArgumentException("Arguments can't be null.");
              }
              if (jvmDefaultSelector != null) {
                   jvmDefaultSelector.connectFailed(uri, sa, ioe);
              }
         }

    private static final String PROXY_HOST = "glog.integration.http.proxyHost";
    private static String httpProxyHost = GLProperties.get().getProperty(PROXY_HOST);
    private static final String PROXY_PORT = "glog.integration.http.proxyPort";
    private static String httpProxyPort = GLProperties.get().getProperty(PROXY_PORT);

    private static ArrayList getNoProxyList() {
              ArrayList noProxyList = new ArrayList<Proxy>();
              noProxyList.add(Proxy.NO_PROXY);
              return noProxyList;
         }

    private static ArrayList getProxyList() {
         Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxyHost, Integer.parseInt(httpProxyPort)));
              ArrayList proxyList = new ArrayList<Proxy>();
              proxyList.add(proxy);
              return proxyList;
         }


    public static class Startup implements StartupShutdown {
    public void load(T2SharedConnection conn) throws GLException {}
    public void activate(T2SharedConnection conn) throws GLException {
         ProxySelector.setDefault(getInstance());
    }
    public void unload() throws GLException {}
    }
    }

    Edited by: pshivrat on Sep 3, 2010 10:09 PM
  • 5. Re: weblogic does not call the ProxySelector class for http url
    646689 Newbie
    Currently Being Moderated
    I am checking for HTTP protocol before returning the HTTP Proxy. In case of other protocol ftp or socket, I just pass the call to JVM provided default selector.

    When I execute this code on tomcat, it works fine. Here the proxyselector is invoked with the same http url and so I am able to return proxy object.
    When I execute this code on weblogic, the ProxySelector is not invoked with http url but invoked with the same url but the "socket" protocol or scheme.
    For eg, if I access http://www.yahoo.com, the weblogic invokes the ProxySelector with socket://www.yahoo.com
  • 6. Re: weblogic does not call the ProxySelector class for http url
    646689 Newbie
    Currently Being Moderated
    If you need a simple test case to check whether it really works or not, Add the below test method to your some session bean running on weblogic and get it invoked.

         public void test(String[] args) throws IOException {
              MyProxySelector p = MyProxySelector.getInstance();
    OutputStream outStream = null;
         URL url = new URL("http://otmtest.x10hosting.com/otmtest/testTransmission.php");
         p.setUseProxy(true);
              HttpURLConnection conn1 = (HttpURLConnection) url.openConnection();
              conn1.setDoOutput(true);
         outStream = conn1.getOutputStream();
         String s = "<a>f</a>";
         outStream.write(s.getBytes());
         outStream.flush();
         outStream.close();

    }

    Also add the below MyProxySelector class on your code base.
    package proxy;

    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.ProxySelector;
    import java.net.SocketAddress;
    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List;

    public class MyProxySelector extends ProxySelector {
    protected ThreadLocal<Boolean> useProxy = new ThreadLocal<Boolean>();

         public ThreadLocal<Boolean> getUseProxy() {
              return useProxy;
         }

         public void setUseProxy(boolean useProxy) {
              setUseProxy(useProxy? Boolean.TRUE: Boolean.FALSE);
         }

         public void setUseProxy(Boolean useProxy) {
              this.useProxy.set(useProxy);
         }

         private ProxySelector jvmDefaultSelector = null;
         public ProxySelector getJVMDefaultSelector() {
              return jvmDefaultSelector;
         }

         private static MyProxySelector myProxySelector = null;

         private MyProxySelector(ProxySelector jvmDefaultSelector) {
              this.jvmDefaultSelector = jvmDefaultSelector;
         }

         public static MyProxySelector getInstance() {
              if(myProxySelector==null) {
                   myProxySelector = new MyProxySelector(ProxySelector.getDefault());
              }
              return myProxySelector;
         }
         @Override
         public List<Proxy> select(URI uri) {
              try {
                   if (uri == null) {
                        throw new IllegalArgumentException("URI can't be null.");
                   }

                   Boolean useProxyObject = useProxy.get();
                   if(useProxyObject == null) {
                        String protocol = uri.getScheme();
                        if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
                             return getProxyList();
                        } else if(jvmDefaultSelector != null) {
                             return jvmDefaultSelector.select(uri);
                        }
                   }

                   if(useProxyObject.booleanValue()) {
                        String protocol = uri.getScheme();
                        if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
                             return getProxyList();
                        }
                   }

              } catch(Exception e) {
              } finally {
                   if (useProxy != null) {
                        useProxy.remove();
                   }
              }

              return getNoProxyList();
         }

         @Override
         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
              if (uri == null || sa == null || ioe == null) {
                   throw new IllegalArgumentException("Arguments can't be null.");
              }
              if (jvmDefaultSelector != null) {
                   jvmDefaultSelector.connectFailed(uri, sa, ioe);
              }
         }

    private static ArrayList getNoProxyList() {
              ArrayList noProxyList = new ArrayList<Proxy>();
              noProxyList.add(Proxy.NO_PROXY);
              return noProxyList;
         }

    private static ArrayList getProxyList() {
         Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("www.proxy1.com", 80));
              ArrayList proxyList = new ArrayList<Proxy>();
              proxyList.add(proxy);
              return proxyList;
         }
    }

    The test method should able to invoke the proxyselector.select method internally with a http url and so the proxyselector will be able to return the http proxy object.
  • 7. Re: weblogic does not call the ProxySelector class for http url
    646689 Newbie
    Currently Being Moderated
    Is there any update on this?
  • 8. Re: weblogic does not call the ProxySelector class for http url
    user161771 - oracle Expert
    Currently Being Moderated
    I asked someone from the web services team to take a look, but they are very busy and I'm not sure how much time they have to investigate this. I recommend opening a support case to get someone that can be dedicated to researching this issue.
  • 9. Re: weblogic does not call the ProxySelector class for http url
    646689 Newbie
    Currently Being Moderated
    The second code I have presented is http case which also fails with weblogic. Its not specific to webservice and so other team may be able to look at this.
    1. You can enclose the http url invocation code in normal java main method and check it will invoke the ProxySelector with http url.
    2. You can put the http url invocation code in some servlet and run it on tomcat, it will invoke the ProxySelector with http url.
    3. But when you put the same code on session bean running on weblogic, the ProxySelector is invoked with socket url. No invocation is done for http url.
  • 10. Re: weblogic does not call the ProxySelector class for http url
    646689 Newbie
    Currently Being Moderated
    Is there any update?
  • 11. Re: weblogic does not call the ProxySelector class for http url
    user161771 - oracle Expert
    Currently Being Moderated
    As I responded before, if you have not had another response here, then it's unlikely that the WLS team responsible for this feature will be able to investigate this outside of support channels and therefore you should open a service request with support.oracle.com to get someone with a dedicated SLA to track this down.
  • 12. Re: weblogic does not call the ProxySelector class for http url
    user161771 - oracle Expert
    Currently Being Moderated
    As a shot in the dark, on an internal mailing list someone recommended this option for someone having an issue with getting the java.net.Authenticator to work as expected due to WLS using it's own URL stream handler. I'm not sure if the same machinery is used for ProxySelectors or not, but if you could try adding -DUseSunHttpHandler=true to Weblogic Startup.
  • 13. Re: weblogic does not call the ProxySelector class for http url
    835121 Newbie
    Currently Being Moderated
    Paresh,
    Were you able to resolve the issue?
  • 14. Re: weblogic does not call the ProxySelector class for http url
    GopalGajarao Newbie
    Currently Being Moderated
    Facing the same issue, did any of you guys have a resolution for this issue ?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points