1
votes

Previously i did implement one handler for my web service using axis which will read the xml file and return it as a response. My code is as below:

try {
        Message msg = msgContext.getResponseMessage();
        MessageContext context = MessageContext.getCurrentContext();



        InputStream in = this.getClass().getClassLoader().getResourceAsStream("login_rsp_xml");
        if(in==null){

            System.out.println("in is empty!!!");
        }
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(in);
        msg.getSOAPPart().setContent(new DOMSource(doc));
        System.out.println("return file>>"+fileName);

    } catch (Exception e) {
        System.setProperty("req_no", null);
        e.printStackTrace();            
    }

Now, i need to do the same thing using axis2. Do anyone knew how to replace the outgoing response in axis2? I did try to convert the file to string and invoke envelope.setText(), but failed and get exception "org.apache.axiom.soap.SOAPProcessingException: Can not detach SOAP Body, SOAP Envelope must have a Body !!".

Thanks in advance for the help/advice.

My code in axis2:

   import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.jaxws.message.Message;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SamRspHandler extends AbstractHandler implements Handler {


    public String getFileContent(String path) throws Exception{
        InputStream in = this.getClass().getClassLoader()
                //.getResourceAsStream("/xmlFiles/loginRspHeader.xml");
                .getResourceAsStream(path);

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();

        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc2 = dBuilder.parse(in);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                "yes");
        StringWriter writer = new StringWriter();
        transformer
                .transform(new DOMSource(doc2), new StreamResult(writer));
        String output2 = writer.getBuffer().toString();

        return output2;
    }

    public InvocationResponse invoke(MessageContext msgContext)
            throws AxisFault {
        System.out.println("In Response");


        String content = msgContext.getEnvelope().toString();

        try {

            System.out.println("zzzz>>"+System.getProperty("abc"));         


            msgContext.getEnvelope().setText("abc");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return InvocationResponse.CONTINUE;
    }

    public void revoke(MessageContext msgContext) {

    }


}
1

1 Answers

0
votes

If you are setting an XML file inside of a SOAP Response, it must be enclosed inside of a <![CDATA[]> tag. This is to prevent that XML File being processed as part of the SOAP Response. Mind you SOAP also uses XML to process Request and Response.

<![CDATA[
    <?xml version="1.0"?>
    <!-- Your XML -->
]>

Create your XML file as above and set it envelope.setText()