I have a simple servlet which reads in a soap message, parses the
XML payload and attempts to add a single element to the existing
(soap message just read in)soap message. I receive the soap message
(from client) and display the elements to log file - no problem.
However, when I add an element to the soap message and return the
message the change does not take place. It's as if I never changed
the message. Can I modify the soap message I just read? Do I have to
recreate a new message from scratch or is there something I'm missing?
The servlet code is below. Thanks for any help.

SLS

------------------

//
// ModifySoapMsgService.java
//
// This program reads a SOAP message and returns a modify version of
// the same
// message back to the client application (requester).
//
//
// Build Instructions:
// o set classpath= <tomcat path>\common\lib\servlet.jar
// o javac ModifySoapMsgService.java
// o Copy ModifySoapMsgService.class file to
// <your web apps path>\MyWsApps\WEB-INF\classes
// o Call from client application (ModifySoapMsgClient).
//



/* Servlet related packages. */
import javax.servlet.*;         // Generic servlet package.
import javax.servlet.http.*;    // Web server extensions to encapsulate HTTP
communication.

/* SOAP related packages. */
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;

/* Other needed packages. */
import java.io.*;
import java.util.Iterator;


public class ModifySoapMsgService extends HttpServlet{
        private static MessageFactory myMsgFactory;

        /* Create static instance of message factory. */
        static{
        try {
            /* Create a new message factory. */
            myMsgFactory = MessageFactory.newInstance();

        }catch(SOAPException e){
            System.out.println("Caught Exception: " + e.getMessage());
        }
    }


        public void init(ServletConfig servletConfig) throws ServletException{
        super.init(servletConfig);  // Initialize servlet w/config info
passed by Tomcat.
    }

    public void doPost(HttpServletRequest request,
                                HttpServletResponse response) throws IOException, 
ServletException{
        SOAPMessage mySoapMsg;

        System.out.println("\n\nModifySoapMsgSerice.doPost() - Received SOAP
message...");
        log("doPost() ...");
        try{
            InputStream myInputStream = request.getInputStream();
            OutputStream myOutputStream = response.getOutputStream();

                    log("o Reading SOAP message ...");
            /* Create SOAP message from SOAP content (sent by client) of
input stream. */
            mySoapMsg = myMsgFactory.createMessage(new
MimeHeaders(),myInputStream);

            /* Process SOAP message. */
            SOAPMessage reply=null;
            reply = onMessage(mySoapMsg);

            /* Return modified SOAP message. */
            System.out.println("ModifySoapMsgSerice.doPost() - Return
results...");
            reply.saveChanges();
            reply.writeTo(myOutputStream);
            myOutputStream.flush();
        }catch(Exception e){
            throw new ServletException("doPost failed: " + e.getMessage());
        }
    }


    public SOAPMessage onMessage(SOAPMessage inSoapMsg){
        SOAPPart        mySoapPart;
        SOAPEnvelope    mySoapEnvelope;
        SOAPHeader      mySoapHeader;
        SOAPBody        mySoapBody;
        SOAPBodyElement myAppFormElement;
        SOAPElement     myApplicantElement;
        SOAPElement     myNameElement;
        SOAPElement     myAddressElement;
        SOAPElement     myPhoneNoElement;
        SOAPElement     myAgeElement;
        SOAPElement     myBeneficiaryElement;
        Iterator myElementIterator;       // Create iterator for child
elements.
        Name myElementName;
        String nsUri = "http://ltree.com/app";;
        String nsPrefix = "lt";
            ByteArrayOutputStream myByteArray = new ByteArrayOutputStream();


        System.out.println("ModifySoapMsgSerice.onMessage() - Processing
SOAP message...");
                log("doMessage() ...");

        try{
            /* Log SOAP message just read in. */
            inSoapMsg.writeTo(myByteArray);
            log("o inSoapMsg: " + myByteArray.toString());

            /* Get SOAP part from message. */
            mySoapPart = inSoapMsg.getSOAPPart();

            /* Get envelope from SOAP part. */
            mySoapEnvelope = mySoapPart.getEnvelope();

            /* Get header from SOAP envelope. */
            mySoapHeader = mySoapEnvelope.getHeader();

            /* Get body from envelope. */
            mySoapBody = mySoapEnvelope.getBody();

            /*
             * Parse message body.
                 *
             * <lt:AppForm term="life" xmlns:lt="http://ltree.com/app";>
             *      <lt:Applicant>
             *          <lt:Name>Pat Brown</lt:Name>
             *          <lt:Address>123 Main St</lt:Address>
             *          <lt:PhoneNo>123-4567</lt:PhoneNo>
             *      </lt:Applicant>
             *      <lt:Beneficiary>Dale Brown</lt:Beneficiary>
             * </lt:AppForm>
             */
            /* - Get <AppForm> element. */
            myElementName =
mySoapEnvelope.createName("AppForm",nsPrefix,nsUri);    // Creates element
name.
            myElementIterator = mySoapBody.getChildElements(myElementName);
// Get child element iterator.
            myAppFormElement = (SOAPBodyElement)myElementIterator.next(); //
Get <AppForm> element.
                log("o Read <" + myAppFormElement.getElementName().getLocalName() + ">
element");

            /* - Get <Applicant> element of <AppForm>. */
            myElementName =
mySoapEnvelope.createName("Applicant",nsPrefix,nsUri);
            myElementIterator =
myAppFormElement.getChildElements(myElementName);   // Get child element
iterator.
            myApplicantElement = (SOAPElement)myElementIterator.next(); //
Get <Applicant> element.
                        log("o Read <" + 
myApplicantElement.getElementName().getLocalName() + ">
element");

            /* - Get <Name> element of <Applicant>. */
            myElementName =
mySoapEnvelope.createName("Name",nsPrefix,nsUri);
            myElementIterator =
myApplicantElement.getChildElements(myElementName); // Get child element
iterator.
            myNameElement = (SOAPElement)myElementIterator.next(); // Get
<Name> element.
                        log("o Read <" + myNameElement.getElementName().getLocalName() 
+ ">
element");
            log("  - <Name> value: " + myNameElement.getValue());

            /* - Get <Address> element of <Applicant>. */
            myElementName =
mySoapEnvelope.createName("Address",nsPrefix,nsUri);
            myElementIterator =
myApplicantElement.getChildElements(myElementName); // Get child element
iterator.
            myAddressElement = (SOAPElement)myElementIterator.next(); // Get
<Address> element.
                        log("o Read <" + 
myAddressElement.getElementName().getLocalName() + ">
element");
            log("  - <Address> value: " + myAddressElement.getValue());

            /* - Get <PhoneNo> element of <Applicant>. */
            myElementName =
mySoapEnvelope.createName("PhoneNo",nsPrefix,nsUri);
            myElementIterator =
myApplicantElement.getChildElements(myElementName); // Get child element
iterator.
            myPhoneNoElement = (SOAPElement)myElementIterator.next(); // Get
<PhoneNo> element.
                        log("o Read <" + 
myPhoneNoElement.getElementName().getLocalName() + ">
element");
            log("  - <PhoneNo> value: " + myPhoneNoElement.getValue());

            /* - Get <Beneficiary> element of <AppForm>. */
            myElementName =
mySoapEnvelope.createName("Beneficiary",nsPrefix,nsUri);
            myElementIterator =
myAppFormElement.getChildElements(myElementName);   // Get child element
iterator.
            myBeneficiaryElement = (SOAPElement)myElementIterator.next(); //
Get <Beneficiary> element.
                        log("o Read <" + 
myBeneficiaryElement.getElementName().getLocalName() +
"> element");
            log("  - <Beneficiary> value: " +
myBeneficiaryElement.getValue());



            /*
             * Modify message body.
             */
            /* - Add <Age> sub-element "<lt:Age>32</lt:Age>" to <Applicant>.
*/
            myElementName = mySoapEnvelope.createName("Age",nsPrefix,nsUri);
            myAgeElement =
myApplicantElement.addChildElement(myElementName);
            myAgeElement.addTextNode("32");

            inSoapMsg.saveChanges();

            log("o <Age> element: " + myAgeElement.getValue());
            myByteArray.reset();
            inSoapMsg.writeTo(myByteArray);
            log("o inSoapMsg: " + myByteArray.toString());

       }catch(Exception e){
            System.out.println("Caught Exception: " + e.getMessage());
        }

        return inSoapMsg;
    }

}

Reply via email to