Tuesday, March 24, 2009

Invoking Web Services through a proxy using JAX-RPC and JAX-WS

Not very often, we face the possibility of invoking Web Services provided by external entities that are outside our network. Some companies solve this by configuring their network to allow some application servers to bypass proxy servers. Whatever be the case, when in development, developers have to to be able to invoke web services through proxies. This post will be describe how to
  • Invoking Web Services through a proxy using JAX-RPC
  • Invoking Web Services through a proxy using JAX-WS
The solutions provided here are specific to Oracle Weblogic Server 10.3. I would suggest that you try the solutions provided on "Java Networking and Proxies", and only if they don't work (which happened to me), try the following.


There's More ...
Invoking Web Services through a proxy using JAX-RPC

  1. Generate the Web Service Proxy classes using the following ant task.
    <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" />
    <target name="build-client">
    <clientgen
    wsdl="[path_to_WSDL]"
    destDir="./src"
    packageName="com.my.client"
    type="JAXRPC"/>
    </target>
  2. When the client classes are generated the type as JAXRPC, the generated classes will consist of Service, ServiceImpl, PortType and ProtTypeImpl etc files. To invoke the service you have to create the PortType from the Service. When working behind a proxy, you have to instantiate the ServiceImpl by using a constructor that takes in a weblogic.wsee.connection.transport.http.HttpTransportInfo.HttpTransportInfo object as a parameter as shown below.
      private HttpTransportInfo getHttpInfo() {
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyServer", 9090));
    HttpTransportInfo httpInfo = new HttpTransportInfo();
    httpInfo.setProxy(proxy);

    httpInfo.setProxyUsername("proxyuser".getBytes());
    httpInfo.setProxyPassword("proxypassword".getBytes());
    return httpInfo;
    }
    When using SSL, you can use the weblogic.wsee.connection.transport.https.HttpsTransportInfo class which can be created in the same way as above.
Invoking Web Services through a proxy using JAX-WS
  1. When using JAX-WS, you have to change the clientgen task's "type" attribute to JAXWS, as shown below
    <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" />
    <target name="build-client">
    <clientgen
    wsdl="[path_to_WSDL]"
    destDir="./src"
    packageName="com.my.client"
    type="JAXWS"/>
    </target>
    Make sure that the "path_to_WSDL" is to a local copy of the WSDL, as the properties which we set in the following steps are after the initialization of the service.
  2. Running clientgen with JAXWS will create classes of the type *Service and *ServiceSoap
  3. Setting up the client for proxy server involves setting a couple of request paramters: Username and password as shown below.
        MyService service = null;
    service = new MyService();

    MyServiceSoap port = service.getMyServiceSoap();
    BindingProvider bp = (BindingProvider) port;
    Binding binding = bp.getBinding();

    Map<String, Object> ctx = bp.getRequestContext();
    ctx.put(BindingProvider.USERNAME_PROPERTY, "proxyuser");
    ctx.put(BindingProvider.PASSWORD_PROPERTY, "proxypassword");
    Note that we don't specify the Proxy Server here. JAX-WS sends authentication information for the proxy in request headers.

Popular Posts