1 2 Previous Next 16 Replies Latest reply on Feb 1, 2005 6:28 AM by Thomas Diesler

    Is there a way to get the soap body as raw XML?

    Mette Hedin Newbie

      I have been trying to use custom types for my web service, but have been running into problems with it, and I have been reflecting on how a far easier thing for me would be to simply get access to the raw XML contained in the SOAP message. This way I can unmarshal the XML document myself in the manner I see best fitting, rather than having to try to jump through hoops to get the document into my classes.

      Is there any way of doing this? I have not found any obvious solution...

        • 1. Re: Is there a way to get the soap body as raw XML?
          Thomas Diesler Master

          You can access the SOAPEnvelope from handlers and java service endpoints (JSE) there is no equivalent for EJB endpoints.

          • 2. Re: Is there a way to get the soap body as raw XML?
            Mette Hedin Newbie

            I am using a JSE, so that part is not a problem. However, it is not obvious to me how to access the SOAPEnvelope. I have looked around based on some of the key words that you provided in your reply, but was unable to find anything. Is there any documentation for how to do this?

            • 3. Re: Is there a way to get the soap body as raw XML?
              Ole Matzura Novice

              Hi!

              The below article describes some techniques although I am not sure if jboss supports nodatabinding webservices.. alternatively you could create custom xml serializer/deserializers which would be specified in ws4ee-deployment.xml.

              anyway, check out http://java.sun.com/developer/technicalArticles/xml/jaxrpcpatterns/index.html

              good luck, hope this helps a bit!

              regards
              Ole

              • 4. Re: Is there a way to get the soap body as raw XML?
                Mette Hedin Newbie

                Thank you for forwarding the article, it looks really interesting, and I will be sure to give it a thorough read.

                However, I was more interested in how to specifically accomplish what Thomas suggested above above, as I assume there must be a way to do what he suggests. The reason I ask the question is that I have been trying for some time to get my custom deserialization to work, but to no avail.

                • 5. Re: Is there a way to get the soap body as raw XML?
                  Thomas Diesler Master

                  How to access the SOAPEnvelope from an JSE endpoint is described in section

                  10.1.3 ServletEndpointContext

                  of the JAXRPC-1.1 spec. Links to relevant specs are on the wiki.

                  http://www.jcp.org/en/jsr/detail?id=101

                  • 6. Re: Is there a way to get the soap body as raw XML?
                    Alejandro Guizar Master

                    I'm working in the BPEL module within the jBPM project. Process data in a bpel document is defined in terms of variables, some of which hold WSDL messages. A process can exchange messages with various partner links and expose a different interface (port type) to each.
                    I'll be deploying a port component for each partner link. I'd like to use a SEI with only one method that received all messages for that partner link regardless of the WSDL operation, instead of a separate method for each operation.

                    public interface BpelPartnerLink extends Remote {
                     public SOAPElement receive(SOAPElement message);
                    }
                    
                    public class BpelPartnerLinkService implements BpelProcess, ServiceLifecycle {
                     public SOAPElement deliverMessage(SOAPElement message) {
                     // figure out the involved partner link and the operation using
                     // the servlet endpoint context
                     }
                    }

                    I checked out the article suggested by Ole. Switching off data binding seems to be part of the solution.
                    1) Is this supported by JBossWS?
                    2) Can I route all WSDL operations to a single method?

                    Thanks for your help,
                    Alejandro Guízar

                    • 7. Re: Is there a way to get the soap body as raw XML?
                      Thomas Diesler Master

                      Have a look at

                      /**
                       * Test unstructured message processing
                       *
                       * @author Thomas.Diesler@jboss.org
                       * @since 26-Nov-2004
                       */
                      public class MessageEndpointTestCase extends JBossTestCase
                      


                      The wiki page has yet to be written

                      • 8. Re: Is there a way to get the soap body as raw XML?
                        Ole Matzura Novice

                        Hi!

                        We have nodatabinding webservices working under jboss 4.0.1. Regarding mapping of all operations to one method you could probably do this by handcoding operation to method mapping under <service-endpoint-method-mapping> in the jaxrpx-mapping.xml file

                        good luck!

                        /Ole

                        • 9. Re: Is there a way to get the soap body as raw XML?
                          Alejandro Guizar Master

                          Thomas, Ole:

                          Thanks for your answer! But, as expected, I ran into trouble again...

                          1) I've checked out the webservice module from both head and branch_4_0, but I can't find the MessageEndpointTestCase, where it is?

                          2) I mapped all my operations to the same method with <service-endpoint-method-mapping>. But it didn't work. I took a look at the generated WSDD file in the /data directory. It only described the last operation in document order, which makes me believe that, somewhere, a single operation is assumed to be mapped to a method, and such mapping is being overwritten. Can you help me out with this issue?

                          If I can't map all operations to the same method, I'd have to generate a distinct interface and implementation (and compile them) for each partner link at deployment time, resulting in a considerably more complex deployment model.

                          Thanks again!
                          -Alex

                          • 10. Re: Is there a way to get the soap body as raw XML?
                            Mette Hedin Newbie

                             

                            "omatzura" wrote:
                            Hi!

                            We have nodatabinding webservices working under jboss 4.0.1. Regarding mapping of all operations to one method you could probably do this by handcoding operation to method mapping under <service-endpoint-method-mapping> in the jaxrpx-mapping.xml file

                            good luck!

                            /Ole


                            When you say you have it working under 4.0.1, does that mean the 12-23 release, and does it mean it was not working in 4.0.0?

                            • 11. Re: Is there a way to get the soap body as raw XML?
                              Mette Hedin Newbie

                              Since noone has respnded to Alex's questions, and I am somewhat in the same boat, and still stuck.

                              I too have looked for MessageEndpointTestCase in the latest CV Head snapshot, and it isn't in there.

                              Also, I have read and re-read the section in the JAX-RPC spec, but I am very unclear of exactly how this fits into solving the problem. I can use the context to get to the document in my Service endpoint implementation, but at that point the document would already have been de-serialized into some class (my custom or not) and thus this approach would seem quite inefficient at best. Perhaps I do not understand in what context this API should be used, or perhaps there is no way of getting to the document without using custom serailizers/deserializers?

                              • 12. Re: Is there a way to get the soap body as raw XML?
                                Scott Stark Master

                                Browse under:
                                http://cvs.sourceforge.net/viewcvs.py/jboss/jbosstest/src/main/org/jboss/test/webservice/

                                /*
                                 * JBoss, the OpenSource J2EE webOS
                                 *
                                 * Distributable under LGPL license.
                                 * See terms of license at gnu.org.
                                 */
                                package org.jboss.test.webservice.message;
                                
                                // $Id: MessageEndpointTestCase.java,v 1.2 2005/01/19 13:25:08 tdiesler Exp $
                                
                                import junit.framework.Test;
                                import org.jboss.test.JBossTestCase;
                                import org.w3c.dom.Document;
                                import org.w3c.dom.Element;
                                
                                import javax.naming.Context;
                                import javax.naming.InitialContext;
                                import javax.naming.NamingException;
                                import javax.xml.parsers.DocumentBuilder;
                                import javax.xml.parsers.DocumentBuilderFactory;
                                import javax.xml.parsers.ParserConfigurationException;
                                import javax.xml.rpc.Service;
                                import javax.xml.soap.MessageFactory;
                                import javax.xml.soap.Name;
                                import javax.xml.soap.SOAPBody;
                                import javax.xml.soap.SOAPConnection;
                                import javax.xml.soap.SOAPConnectionFactory;
                                import javax.xml.soap.SOAPElement;
                                import javax.xml.soap.SOAPException;
                                import javax.xml.soap.SOAPFactory;
                                import javax.xml.soap.SOAPMessage;
                                import java.io.ByteArrayInputStream;
                                import java.net.URL;
                                import java.util.Properties;
                                
                                /**
                                 * Test unstructured message processing
                                 *
                                 * @author Thomas.Diesler@jboss.org
                                 * @since 26-Nov-2004
                                 */
                                public class MessageEndpointTestCase extends JBossTestCase
                                {
                                 private final String TARGET_ENDPOINT = "http://" + getServerHost() + ":8080/ws4ee-message";
                                
                                 public MessageEndpointTestCase(String name)
                                 {
                                 super(name);
                                 }
                                
                                 /** Use the SAAJ API to send the SOAP message.
                                 * This simulates an external client and tests server side message handling.
                                 */
                                 public void testSAAJClientFromEnvelope() throws Exception
                                 {
                                 String soapEnv =
                                 "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
                                 "<soapenv:Body>" +
                                 MessageEndpoint.request +
                                 "</soapenv:Body>" +
                                 "</soapenv:Envelope>";
                                
                                 MessageFactory mf = MessageFactory.newInstance();
                                 SOAPMessage reqMsg = mf.createMessage(null, new ByteArrayInputStream(soapEnv.getBytes()));
                                
                                 SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
                                 SOAPConnection con = conFactory.createConnection();
                                 SOAPMessage resMsg = con.call(reqMsg, new URL(TARGET_ENDPOINT));
                                
                                 SOAPBody soapBody = resMsg.getSOAPBody();
                                 SOAPElement soapElement = (SOAPElement)soapBody.getChildElements().next();
                                
                                 validateResponse(soapElement);
                                 }
                                
                                 /** Use the SAAJ API to send the SOAP message.
                                 * This simulates an external client and tests server side message handling.
                                 */
                                 public void testSAAJClientFromBody() throws Exception
                                 {
                                 MessageFactory mf = MessageFactory.newInstance();
                                 SOAPMessage reqMsg = mf.createMessage();
                                
                                 DocumentBuilder builder = getDocumentBuilder();
                                 Document doc = builder.parse(new ByteArrayInputStream(MessageEndpoint.request.getBytes()));
                                 reqMsg.getSOAPBody().addDocument(doc);
                                
                                 SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
                                 SOAPConnection con = conFactory.createConnection();
                                 SOAPMessage resMsg = con.call(reqMsg, new URL(TARGET_ENDPOINT));
                                
                                 SOAPBody soapBody = resMsg.getSOAPBody();
                                 SOAPElement soapElement = (SOAPElement)soapBody.getChildElements().next();
                                
                                 validateResponse(soapElement);
                                 }
                                
                                 /** Use the JBoss generated dynamic proxy send the SOAP message.
                                 * This tests server/client side message handling.
                                 */
                                 public void testProxyClient() throws Exception
                                 {
                                 InitialContext iniCtx = getInitialContext();
                                 Service service = (Service)iniCtx.lookup("java:comp/env/service/MessageService");
                                 MessageEndpoint port = (MessageEndpoint)service.getPort(MessageEndpoint.class);
                                
                                 DocumentBuilder builder = getDocumentBuilder();
                                 Document doc = builder.parse(new ByteArrayInputStream(MessageEndpoint.request.getBytes()));
                                 Element element = doc.getDocumentElement();
                                
                                 SOAPElement soapElement = (SOAPElement)port.processElement(element);
                                 validateResponse(soapElement);
                                 }
                                
                                 private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException
                                 {
                                 // Setup document builder
                                 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                                 docBuilderFactory.setNamespaceAware(true);
                                
                                 DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
                                 return builder;
                                 }
                                
                                 private void validateResponse(SOAPElement soapElement)
                                 throws SOAPException
                                 {
                                 SOAPFactory factory = SOAPFactory.newInstance();
                                
                                 Name expName = factory.createName("Response", MessageEndpoint.PREFIX, MessageEndpoint.NAMESPACE_URI);
                                 Name elementName = soapElement.getElementName();
                                 assertEquals(expName, elementName);
                                
                                 expName = factory.createName("POID");
                                 soapElement = (SOAPElement)soapElement.getChildElements(expName).next();
                                 elementName = soapElement.getElementName();
                                 assertEquals(expName, elementName);
                                
                                 String elementValue = soapElement.getValue();
                                 assertEquals("12345", elementValue);
                                
                                 expName = factory.createName("Status");
                                 soapElement = (SOAPElement)soapElement.getNextSibling();
                                 elementName = soapElement.getElementName();
                                 assertEquals(expName, elementName);
                                
                                 elementValue = soapElement.getValue();
                                 assertEquals("ok", elementValue);
                                 }
                                
                                 /** Deploy the test ear */
                                 public static Test suite() throws Exception
                                 {
                                 return getDeploySetup(MessageEndpointTestCase.class, "ws4ee-message.war, ws4ee-message-client.jar");
                                 }
                                
                                 /**
                                 * Get the client's env context, see tracker [840598] for details
                                 */
                                 protected InitialContext getInitialContext() throws NamingException
                                 {
                                 Properties env = new Properties();
                                 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                                 env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
                                 env.setProperty(Context.PROVIDER_URL, "jnp://" + getServerHost() + ":1099");
                                 env.setProperty("j2ee.clientName", "ws4ee-client");
                                 return new InitialContext(env);
                                 }
                                }
                                



                                • 13. Re: Is there a way to get the soap body as raw XML?
                                  Mette Hedin Newbie

                                  I apologize if I keep asking more questions, but I am still not able to connect the dots.

                                  The code you sent me (and the class was not in the snapsot, which is odd) seems to me to be a demonstration of how a client can construct a message in several different ways. i can use that information, so that is great, however my question in this topic was relating to how to process the document n the web service. Unless there is something I am missing, I don't see how this answers that question?

                                  • 14. Re: Is there a way to get the soap body as raw XML?
                                    Scott Stark Master

                                    If you drill down into the message package:

                                    jboss/jbosstest/src/main/org/jboss/test/webservice/message

                                    you will find the server side implementation:

                                    /*
                                     * JBoss, the OpenSource J2EE webOS
                                     *
                                     * Distributable under LGPL license.
                                     * See terms of license at gnu.org.
                                     */
                                    package org.jboss.test.webservice.message;
                                    
                                    // $Id: MessageEndpointImpl.java,v 1.1.2.2 2004/12/08 15:43:46 tdiesler Exp $
                                    
                                    import com.ibm.wsdl.util.xml.DOM2Writer;
                                    import org.jboss.logging.Logger;
                                    import org.w3c.dom.Document;
                                    import org.w3c.dom.Element;
                                    
                                    import javax.xml.parsers.DocumentBuilder;
                                    import javax.xml.parsers.DocumentBuilderFactory;
                                    import javax.xml.soap.Name;
                                    import javax.xml.soap.SOAPElement;
                                    import javax.xml.soap.SOAPFactory;
                                    import java.io.ByteArrayInputStream;
                                    import java.io.StringWriter;
                                    import java.rmi.RemoteException;
                                    
                                    /**
                                     * @author Thomas.Diesler@jboss.org
                                     * @since 26-Nov-2004
                                     */
                                    public class MessageEndpointImpl implements MessageEndpoint
                                    {
                                     // provide logging
                                     private final Logger log = Logger.getLogger(MessageEndpointImpl.class);
                                    
                                     public Element processElement(Element msg) throws RemoteException
                                     {
                                     StringWriter swr = new StringWriter();
                                     DOM2Writer.serializeAsXML(msg, swr);
                                     log.info("req: " + swr);
                                    
                                     SOAPElement soapElement = (SOAPElement)msg;
                                    
                                     try
                                     {
                                     SOAPFactory factory = SOAPFactory.newInstance();
                                    
                                     Name name = factory.createName("Order", PREFIX, NAMESPACE_URI);
                                     Name elementName = soapElement.getElementName();
                                     if (name.equals(elementName) == false)
                                     throw new IllegalArgumentException("Unexpected element: " + elementName);
                                    
                                     name = factory.createName("Customer");
                                     soapElement = (SOAPElement)soapElement.getChildElements(name).next();
                                     String elementValue = soapElement.getValue();
                                     if ("Customer".equals(soapElement.getLocalName()) && "Kermit".equals(elementValue) == false)
                                     throw new IllegalArgumentException("Unexpected element value: " + elementValue);
                                    
                                     soapElement = (SOAPElement)soapElement.getNextSibling();
                                     elementValue = soapElement.getValue();
                                     if ("Item".equals(soapElement.getLocalName()) && "Ferrari".equals(elementValue) == false)
                                     throw new IllegalArgumentException("Unexpected element value: " + elementValue);
                                    
                                     // Setup document builder
                                     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                                     docBuilderFactory.setNamespaceAware(true);
                                    
                                     // Prepare response
                                     DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
                                     Document doc = builder.parse(new ByteArrayInputStream(response.getBytes()));
                                     log.info("res: " + response);
                                     return doc.getDocumentElement();
                                     }
                                     catch (RuntimeException e)
                                     {
                                     throw e;
                                     }
                                     catch (Exception e)
                                     {
                                     throw new RemoteException(e.toString(), e);
                                     }
                                     }
                                    }
                                    


                                    The associated deployment descriptors may be found under:

                                    http://cvs.sourceforge.net/viewcvs.py/jboss/jbosstest/src/resources/webservice/message/


                                    1 2 Previous Next