Home > About Myself, Programming > SOAP message to run the service in server

SOAP message to run the service in server


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Chittampally Vasanth Raja
 */
public class SOAPMessage1 {

         //First create the connection
    public static void main(String[] args) throws IOException, TransformerConfigurationException, TransformerException {
        try {
            SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection connection = soapConnFactory.createConnection();
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage message = messageFactory.createMessage();
            //Create objects for the message parts
            SOAPPart soapPart = message.getSOAPPart();
            SOAPEnvelope envelope = soapPart.getEnvelope();
            SOAPBody body = envelope.getBody();
            //Populate the body
            //Create the main element and namespace
            SOAPElement bodyElement = body.addChildElement(envelope.createName("add", "ns2", "http://me.org/"));
            //Add content
            bodyElement.addChildElement("num1").addTextNode("12");
            bodyElement.addChildElement("num2").addTextNode("34");
            // bodyElement.setEncodingStyle("text/xml");
            //Save the message
            message.saveChanges();
            //Check the input
            System.out.println("\nREQUEST:\n");
            message.writeTo(System.out);
            System.out.println();
            //Populate the Message
            //        StreamSource preppedMsgSrc = new StreamSource(
            //                 new FileInputStream("./prepped.msg"));
            //        soapPart.setContent(preppedMsgSrc);
            //String destination = "http://localhost:8084/TestAT/HelloWorld";
            String destination="http://localhost:8080/GlassWS/HelloWorldService";
            //Send the message
            SOAPMessage reply = connection.call(message, destination);
            System.out.println("" + reply);
            System.out.println("\nRESPONSE:\n");
            //Create the transformer
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
            //Extract the content of the reply
            Source sourceContent = reply.getSOAPPart().getContent();
            //Set the output for the transformation
            StreamResult result = new StreamResult(System.out);
            transformer.transform(sourceContent, result);
            System.out.println();
            //Close the connection
            connection.close();
        } catch (SOAPException ex) {
            Logger.getLogger(SOAPMessage1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedOperationException ex) {
            Logger.getLogger(SOAPMessage1.class.getName()).log(Level.SEVERE, null, ex);
        }
}

}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: