18 August 2009

a SOAP message using SAAJ

--> doGoogleSearch WebService with JAVA

----------------------Client.java----------------------
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.MimeHeaders;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;

import javax.xml.transform.stream.StreamResult;

public class Client {

public static void main(String[] args) {

try {
// Create the connection
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();

// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();

// Add eventually a SoapAction header if necessary
MimeHeaders hd = msg.getMimeHeaders();
hd.addHeader("SOAPAction", "\"urn:GoogleSearchAction\"");

// Object for message parts
SOAPPart sp = msg.getSOAPPart();

SOAPEnvelope env = sp.getEnvelope();
env.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
env.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
env.addNamespaceDeclaration("enc","http://schemas.xmlsoap.org/soap/encoding/");
env.addNamespaceDeclaration("env","http://schemas.xmlsoap.org/soap/envelop/");
env.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");

SOAPBody bd = env.getBody();

// Populate body
// Main element and namespace
SOAPElement be = bd.addChildElement(env.createName("doGoogleSearch",
"namesp1",
"urn:GoogleSearch"));
be.addChildElement("key").addTextNode("OS7mOjxQFHIztxIYU9yb8y3ibYgY4w2o").setAttribute("xsi:type","xsd:string");
be.addChildElement("q").addTextNode("ls").setAttribute("xsi:type","xsd:string");
be.addChildElement("start").addTextNode("0").setAttribute("xsi:type","xsd:int");
be.addChildElement("maxResults").addTextNode("5").setAttribute("xsi:type","xsd:int");
be.addChildElement("filter").addTextNode("0").setAttribute("xsi:type","xsd:boolean");
be.addChildElement("restrict").setAttribute("xsi:type","xsd:string");
be.addChildElement("safeSearch").addTextNode("0").setAttribute("xsi:type","xsd:boolean");
be.addChildElement("lr").setAttribute("xsi:type","xsd:string");
be.addChildElement("ie").addTextNode("latin1").setAttribute("xsi:type","xsd:string");
be.addChildElement("oe").addTextNode("latin1").setAttribute("xsi:type","xsd:string");

// Save message
msg.saveChanges();

// View input
System.out.println("\n Soap request:\n");
msg.writeTo(System.out);
System.out.println();

// Send
String urlval = "http://api.google.com/search/beta2";
SOAPMessage rp = conn.call(msg, urlval);

// View the output
System.out.println("\nXML response\n");

// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();

// Get reply content
Source sc = rp.getSOAPPart().getContent();

// Set output transformation
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println();

// Close connection
conn.close();

}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
-------------------------------------------------------

1. get the latest java sdk (1.6.0_16)
2. javac Client.java
3. java Client

Reference:
http://www.pascalbotte.be
http://users.skynet.be/pascalbotte/rcx-ws-doc/present.htm
http://users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm

No comments:

Post a Comment