The Source for Java Technology Collaboration
User: Password:



Jitendra Kotamraju's Blog

January 2006 Archives


Accessing Google Web Service using JAX-WS

Posted by jitu on January 28, 2006 at 10:57 PM | Permalink | Comments (3)

Google has a rpc/enc Web Service for searching and fetching cached pages. There are many ways to access these services(for e.g. Google API). Can it be accessed using JAX-WS? JAX-WS doesn't support rpc/enc Web Services so you don't have nice typed objects to work with. But one can use javax.xml.ws.Dispatch to send and receive SOAP messages. The advantages of using JAXWS for this purpose is that you could use other JAX-WS infrastructure components like handlers. The following program is used to access the spelling suggestion service. The same program can be used to access other services as well by passing a different SOAP message.

                                                                                
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import java.net.URL;
import java.io.FileInputStream;
                                                                                
public class GoogleClient {
    public static void main(String[] args) throws Exception {
        String wsdl = "http://api.google.com/GoogleSearch.wsdl";
        URL url = new URL(wsdl);
        QName serviceName = new QName("urn:GoogleSearch", "GoogleSearchService");
        QName portName = new QName("urn:GoogleSearch", "GoogleSearchPort");
        Service service = Service.create(url, serviceName);
        Dispatch dispatch = service.createDispatch(portName,
            SOAPMessage.class, Service.Mode.MESSAGE);
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage req = mf.createMessage(null, new FileInputStream(args[0]));
        SOAPMessage res = dispatch.invoke(req);
        res.writeTo(System.out);
    }
}
You need to insert your own key in the file doSpellingSuggestion.xml. I am not going to give mine !

You can use mustang to compile and run to see the spelling suggestion.

$ java GoogleClient doSpellingSuggestion.xml

Web Service endpoints in Mustang

Posted by jitu on January 18, 2006 at 05:17 PM | Permalink | Comments (26)

Mustang has a very good support for Web Services. One can create, publish a Web Service very easily. First write the Web Service endpoint implementation, and then use javax.xml.ws.Endpoint API to create and publish the Web Service.
package myws;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class Implementor {
    public int add(int a, int b) {
        return a+b;
    }
}

package myws;

import javax.xml.ws.Endpoint;

public class WSWrapper {
    public static void main(String[] args) {
        Implementor impl = new Implementor();
        // Create and publish the endpoint at the given address
        Endpoint endpoint = Endpoint.publish("http://localhost:8080/add", impl);
    }
}
Compile both the java files and run the java class myws.WSWrapper (Typically one needs to run wsgen before running the program but in this case it is not required). JAXWS runtime created and published a Web Service in publish() method. The WSDL of the Web Service is published at http://localhost:8080/add?wsdl and can be accessed from the browser. Now write a client to access this Web Service !!



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds