Author: tho...@jb... Date: 2006-06-30 09:41:35 -0400 (Fri, 30 Jun 2006) New Revision: 534 Added: trunk/src/main/java/org/jboss/ws/binding/soap/JAXWSBindingProviderPayload.java trunk/src/main/java/org/jboss/ws/soap/JAXWSPayloadBuilder.java trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java trunk/src/main/java/org/jboss/ws/soap/PayloadBuilder.java trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderDOM.java trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderStax.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/ObjectFactory.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/ProviderBeanJAXB.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/ProviderJAXBTestCase.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/ProviderPayloadTestCase.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/package-info.java trunk/src/test/resources/jaxws/provider/jaxb/ trunk/src/test/resources/jaxws/provider/jaxb/WEB-INF/ trunk/src/test/resources/jaxws/provider/jaxb/WEB-INF/web.xml trunk/src/test/resources/jaxws/provider/jaxb/org/ trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/ trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/test/ trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/test/ws/ trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/test/ws/jaxws/ trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/test/ws/jaxws/provider/ trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/test/ws/jaxws/provider/ObjectFactory.java trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/test/ws/jaxws/provider/UserType.java trunk/src/test/resources/jaxws/provider/jaxb/org/jboss/test/ws/jaxws/provider/package-info.java trunk/src/test/resources/jaxws/provider/jaxb/usertype.xsd trunk/src/test/resources/jaxws/provider/payload/ trunk/src/test/resources/jaxws/provider/payload/WEB-INF/ Removed: trunk/src/main/java/org/jboss/ws/soap/DOMEnvelopeBuilder.java trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java trunk/src/main/java/org/jboss/ws/soap/SAAJEnvelopeBuilder.java trunk/src/main/java/org/jboss/ws/soap/STAXEnvelopeBuilder.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/ProviderBeanPayloadJAXB.java Modified: trunk/ant.properties.example trunk/src/main/java/org/jboss/ws/binding/BindingProvider.java trunk/src/main/java/org/jboss/ws/binding/BindingProviderRegistry.java trunk/src/main/java/org/jboss/ws/server/ServiceEndpoint.java trunk/src/main/java/org/jboss/ws/server/ServiceEndpointInvoker.java trunk/src/main/java/org/jboss/ws/soap/SOAPBodyImpl.java trunk/src/main/java/org/jboss/ws/soap/SOAPMessageImpl.java trunk/src/test/ant/build-jars.xml trunk/src/test/java/org/jboss/test/ws/jaxws/provider/ProviderBeanMessage.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/ProviderMessageTestCase.java trunk/src/test/java/org/jboss/test/ws/jaxws/provider/UserType.java Log: JBWS-1020: Implement javax.xml.ws.Provider<JAXBSource> Modified: trunk/ant.properties.example =================================================================== --- trunk/ant.properties.example 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/ant.properties.example 2006-06-30 13:41:35 UTC (rev 534) @@ -3,7 +3,7 @@ # # Required JBoss Home -#jboss.jdk15.home=d:/cvs/jboss-branch/jboss-4.0.x/build/output/jboss-4.0.5.CR1-ejb3 +#jboss.jdk15.home=d:/cvs/jboss-head/build/output/jboss-5.0.0.Alpha # Optional JBoss Home #jboss.jdk14.home=d:/cvs/jboss-archive/jboss-4.0.4/build/output/jboss-4.0.4.GA Modified: trunk/src/main/java/org/jboss/ws/binding/BindingProvider.java =================================================================== --- trunk/src/main/java/org/jboss/ws/binding/BindingProvider.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/binding/BindingProvider.java 2006-06-30 13:41:35 UTC (rev 534) @@ -39,6 +39,7 @@ { static String JAXRPC_SOAP11_BINDING = "org.jboss.ws.binding.jaxrpc.soap11"; static String JAXWS_MESSAGE_BINDING = "org.jboss.ws.binding.jaxws.message"; + static String JAXWS_PAYLOAD_BINDING = "org.jboss.ws.binding.jaxws.payload"; /** On the client side, generate the SOAPMessage from IN parameters. */ SOAPMessage bindRequestMessage(OperationMetaData opMetaData, EndpointInvocation epInv, Map<QName, UnboundHeader> unboundHeaders) throws BindingException; Modified: trunk/src/main/java/org/jboss/ws/binding/BindingProviderRegistry.java =================================================================== --- trunk/src/main/java/org/jboss/ws/binding/BindingProviderRegistry.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/binding/BindingProviderRegistry.java 2006-06-30 13:41:35 UTC (rev 534) @@ -28,6 +28,7 @@ import org.jboss.ws.WSException; import org.jboss.ws.binding.soap.JAXRPCBindingProviderSOAP11; import org.jboss.ws.binding.soap.JAXWSBindingProviderMessage; +import org.jboss.ws.binding.soap.JAXWSBindingProviderPayload; /** A registry of binding providers. * @@ -43,6 +44,7 @@ { providers.put(BindingProvider.JAXRPC_SOAP11_BINDING, defaultProvider); providers.put(BindingProvider.JAXWS_MESSAGE_BINDING, JAXWSBindingProviderMessage.class); + providers.put(BindingProvider.JAXWS_PAYLOAD_BINDING, JAXWSBindingProviderPayload.class); } /** Get a binding provider for a given URI Added: trunk/src/main/java/org/jboss/ws/binding/soap/JAXWSBindingProviderPayload.java =================================================================== --- trunk/src/main/java/org/jboss/ws/binding/soap/JAXWSBindingProviderPayload.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/binding/soap/JAXWSBindingProviderPayload.java 2006-06-30 13:41:35 UTC (rev 534) @@ -0,0 +1,141 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ws.binding.soap; + +// $Id$ + +import java.util.Map; + +import javax.xml.namespace.QName; +import javax.xml.soap.SOAPMessage; +import javax.xml.transform.Source; + +import org.jboss.logging.Logger; +import org.jboss.util.NotImplementedException; +import org.jboss.ws.Constants; +import org.jboss.ws.WSException; +import org.jboss.ws.binding.BindingException; +import org.jboss.ws.binding.BindingProvider; +import org.jboss.ws.binding.EndpointInvocation; +import org.jboss.ws.binding.UnboundHeader; +import org.jboss.ws.metadata.OperationMetaData; +import org.jboss.ws.metadata.ParameterMetaData; +import org.jboss.ws.soap.MessageContextAssociation; +import org.jboss.ws.soap.MessageFactoryImpl; +import org.jboss.ws.soap.SOAPBodyImpl; +import org.jboss.ws.soap.SOAPMessageContextImpl; +import org.jboss.ws.soap.SOAPMessageImpl; + +/** A BindingProvider for a JAXWS payload + * + * @author Tho...@jb... + * @since 27-Jun-2006 + */ +public class JAXWSBindingProviderPayload implements BindingProvider +{ + // provide logging + private static final Logger log = Logger.getLogger(JAXWSBindingProviderPayload.class); + + /** On the client side, generate the payload from IN parameters. */ + public SOAPMessage bindRequestMessage(OperationMetaData opMetaData, EndpointInvocation epInv, Map<QName, UnboundHeader> unboundHeaders) throws BindingException + { + throw new NotImplementedException(); + } + + /** On the server side, extract the IN parameters from the payload and populate an Invocation object */ + public EndpointInvocation unbindRequestMessage(OperationMetaData opMetaData, SOAPMessage reqMessage) throws BindingException + { + log.debug("unbindRequestMessage: " + opMetaData.getXmlName()); + + try + { + // Construct the endpoint invocation object + EndpointInvocation epInv = new EndpointInvocation(opMetaData); + + SOAPMessageContextImpl msgContext = MessageContextAssociation.peekMessageContext(); + if (msgContext == null) + throw new WSException("MessageContext not available"); + + ParameterMetaData paramMetaData = opMetaData.getParameters().get(0); + QName xmlName = paramMetaData.getXmlName(); + + SOAPBodyImpl soapBody = (SOAPBodyImpl)reqMessage.getSOAPBody(); + Source payload = soapBody.getPayload(); + epInv.setRequestParamValue(xmlName, payload); + + return epInv; + } + catch (Exception e) + { + handleException(e); + return null; + } + } + + /** On the server side, generate the payload from OUT parameters. */ + public SOAPMessage bindResponseMessage(OperationMetaData opMetaData, EndpointInvocation epInv) throws BindingException + { + log.debug("bindResponseMessage: " + opMetaData.getXmlName()); + + try + { + SOAPMessageContextImpl msgContext = MessageContextAssociation.peekMessageContext(); + if (msgContext == null) + throw new WSException("MessageContext not available"); + + // Associate current message with message context + MessageFactoryImpl factory = new MessageFactoryImpl(); + factory.setEnvelopeURI(Constants.NS_SOAP11_ENV); + SOAPMessageImpl resMessage = (SOAPMessageImpl)factory.createMessage(); + msgContext.setMessage(resMessage); + + Source payload = (Source)epInv.getReturnValue(); + SOAPBodyImpl soapBody = (SOAPBodyImpl)resMessage.getSOAPBody(); + soapBody.setPayload(payload); + + return resMessage; + } + catch (Exception e) + { + handleException(e); + return null; + } + } + + /** On the client side, extract the OUT parameters from the payload and return them to the client. */ + public void unbindResponseMessage(OperationMetaData opMetaData, SOAPMessage resMessage, EndpointInvocation epInv, Map<QName, UnboundHeader> unboundHeaders) + throws BindingException + { + throw new NotImplementedException(); + } + + private void handleException(Exception ex) throws BindingException + { + if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + + if (ex instanceof BindingException) + throw (BindingException)ex; + + throw new BindingException(ex); + } +} Property changes on: trunk/src/main/java/org/jboss/ws/binding/soap/JAXWSBindingProviderPayload.java ___________________________________________________________________ Name: svn:keywords + Id Revision Name: svn:eol-style + LF Modified: trunk/src/main/java/org/jboss/ws/server/ServiceEndpoint.java =================================================================== --- trunk/src/main/java/org/jboss/ws/server/ServiceEndpoint.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/server/ServiceEndpoint.java 2006-06-30 13:41:35 UTC (rev 534) @@ -194,6 +194,7 @@ } MessageFactoryImpl msgFactory = new MessageFactoryImpl(); + msgFactory.setServiceMode(epMetaData.getServiceMode()); msgFactory.setStyle(epMetaData.getStyle()); MimeHeaders headers = (headerSource != null ? headerSource.getMimeHeaders() : null); Modified: trunk/src/main/java/org/jboss/ws/server/ServiceEndpointInvoker.java =================================================================== --- trunk/src/main/java/org/jboss/ws/server/ServiceEndpointInvoker.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/server/ServiceEndpointInvoker.java 2006-06-30 13:41:35 UTC (rev 534) @@ -107,6 +107,10 @@ { bindingProvider = BindingProviderRegistry.getProvider(BindingProvider.JAXWS_MESSAGE_BINDING); } + else if (epMetaData.getServiceMode() == Mode.PAYLOAD) + { + bindingProvider = BindingProviderRegistry.getProvider(BindingProvider.JAXWS_PAYLOAD_BINDING); + } else { bindingProvider = BindingProviderRegistry.getDefaultProvider(); Deleted: trunk/src/main/java/org/jboss/ws/soap/DOMEnvelopeBuilder.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/DOMEnvelopeBuilder.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/soap/DOMEnvelopeBuilder.java 2006-06-30 13:41:35 UTC (rev 534) @@ -1,238 +0,0 @@ -package org.jboss.ws.soap; - -import org.w3c.dom.*; -import org.jboss.util.xml.DOMUtils; -import org.jboss.util.xml.DOMWriter; -import org.jboss.xb.binding.NamespaceRegistry; -import org.jboss.ws.jaxrpc.Style; -import org.jboss.ws.WSException; - -import javax.xml.soap.*; -import javax.xml.soap.Node; -import javax.xml.namespace.QName; -import java.io.InputStream; -import java.io.IOException; -import java.util.Iterator; - -/** - * @author Heiko Braun, <hei...@jb...> - * @since 19-Apr-2006 - */ -public class DOMEnvelopeBuilder implements SAAJEnvelopeBuilder { - - private SOAPMessage soapMessage; - private boolean ignoreParseException = false; - private Style style = Style.DOCUMENT; - - public void setSOAPMessage(SOAPMessage soapMessage) { - this.soapMessage = soapMessage; - } - - public void setIgnoreParseException(boolean ignoreParseException) { - this.ignoreParseException = ignoreParseException; - } - - public void setStyle(Style style) { - this.style = style; - } - - public SOAPEnvelope build(InputStream ins) throws IOException, SOAPException { - // Parse the XML input stream - Element domEnv = null; - try - { - domEnv = DOMUtils.parse(ins); - } - catch (IOException ex) - { - if (ignoreParseException) - { - return null; - } - throw ex; - } - - String envNS = domEnv.getNamespaceURI(); - String envPrefix = domEnv.getPrefix(); - - // Construct the envelope - SOAPFactoryImpl soapFactory = new SOAPFactoryImpl(); - SOAPPartImpl soapPart = (SOAPPartImpl)soapMessage.getSOAPPart(); - SOAPEnvelopeImpl soapEnv = new SOAPEnvelopeImpl(soapPart, soapFactory.createElement(domEnv, false)); - NamespaceRegistry namespaceRegistry = soapEnv.getNamespaceRegistry(); - - DOMUtils.copyAttributes(soapEnv, domEnv); - registerNamespaces(namespaceRegistry, soapEnv); - - // Add the header elements - Element domHeader = DOMUtils.getFirstChildElement(domEnv, new QName(envNS, "Header")); - if (domHeader != null) - { - SOAPHeader soapHeader = soapEnv.getHeader(); - - DOMUtils.copyAttributes(soapHeader, domHeader); - registerNamespaces(namespaceRegistry, soapHeader); - - Iterator it = DOMUtils.getChildElements(domHeader); - while (it.hasNext()) - { - Element srcElement = (Element)it.next(); - String xmlFragment = DOMWriter.printNode(srcElement, false); - - Name name = new NameImpl(srcElement.getLocalName(), srcElement.getPrefix(), srcElement.getNamespaceURI()); - SOAPContentElement destElement = new SOAPHeaderElementImpl(name); - soapHeader.addChildElement(destElement); - - DOMUtils.copyAttributes(destElement, srcElement); - destElement.setXMLFragment(xmlFragment); - } - } - - // Add the body elements - Element domBody = DOMUtils.getFirstChildElement(domEnv, new QName(envNS, "Body")); - SOAPBody soapBody = soapEnv.getBody(); - - DOMUtils.copyAttributes(soapBody, domBody); - registerNamespaces(namespaceRegistry, soapBody); - - Iterator itBody = DOMUtils.getChildElements(domBody); - if (itBody.hasNext()) - { - Element domBodyElement = (Element)itBody.next(); - - String localName = domBodyElement.getLocalName(); - String prefix = domBodyElement.getPrefix(); - String nsURI = domBodyElement.getNamespaceURI(); - Name beName = new NameImpl(localName, prefix, nsURI); - - // Process a <env:Fault> message - if (beName.equals(new NameImpl("Fault", envPrefix, envNS))) - { - SOAPFaultImpl soapFault = new SOAPFaultImpl(envNS); - soapBody.addChildElement(soapFault); - - DOMUtils.copyAttributes(soapFault, domBodyElement); - - Element domFaultCode = DOMUtils.getFirstChildElement(domBodyElement, new QName("faultcode")); - if (domFaultCode == null) - throw new SOAPException("SOAPFault does not contain a <faultcode> element"); - - Element domFaultString = DOMUtils.getFirstChildElement(domBodyElement, new QName("faultstring")); - if (domFaultString == null) - throw new SOAPException("SOAPFault does not contain a <faultstring> element"); - - String faultCode = DOMUtils.getTextContent(domFaultCode); - soapFault.setFaultCode(faultCode); - - String faultString = DOMUtils.getTextContent(domFaultString); - soapFault.setFaultString(faultString); - - Element domFaultActor = DOMUtils.getFirstChildElement(domBodyElement, new QName("faultactor")); - if (domFaultActor != null) - { - String faultActor = DOMUtils.getTextContent(domFaultActor); - soapFault.setFaultActor(faultActor); - } - - // Add the fault detail - Element domFaultDetail = DOMUtils.getFirstChildElement(domBodyElement, "detail"); - if (domFaultDetail != null) - { - Detail detail = soapFault.addDetail(); - Iterator it = DOMUtils.getChildElements(domFaultDetail); - while (it.hasNext()) - { - Element domElement = (Element)it.next(); - SOAPElement detailEntry = new DetailEntryImpl(soapFactory.createElement(domElement, true)); - detailEntry = detail.addChildElement(detailEntry); - } - } - } - - // Process and RPC or DOCUMENT style message - else - { - - if (style == Style.RPC) - { - SOAPBodyElementRpc soapBodyElement = new SOAPBodyElementRpc(beName); - soapBodyElement = (SOAPBodyElementRpc)soapBody.addChildElement(soapBodyElement); - - DOMUtils.copyAttributes(soapBodyElement, domBodyElement); - - Iterator itBodyElement = DOMUtils.getChildElements(domBodyElement); - while (itBodyElement.hasNext()) - { - Element srcElement = (Element)itBodyElement.next(); - - Name name = new NameImpl(srcElement.getLocalName(), srcElement.getPrefix(), srcElement.getNamespaceURI()); - SOAPContentElement destElement = new SOAPContentElement(name); - soapBodyElement.addChildElement(destElement); - - String xmlFragment = DOMWriter.printNode(srcElement, false); - destElement.setXMLFragment(xmlFragment); - } - } - else if (style == Style.DOCUMENT) - { - Element srcElement = (Element)domBodyElement; - SOAPBodyElementDoc destElement = new SOAPBodyElementDoc(beName); - destElement = (SOAPBodyElementDoc)soapBody.addChildElement(destElement); - - String xmlFragment = DOMWriter.printNode(srcElement, false); - destElement.setXMLFragment(xmlFragment); - } - else if (style == null) - { - SOAPBodyElementMessage soapBodyElement = new SOAPBodyElementMessage(beName); - soapBodyElement = (SOAPBodyElementMessage)soapBody.addChildElement(soapBodyElement); - - DOMUtils.copyAttributes(soapBodyElement, domBodyElement); - - NodeList nlist = domBodyElement.getChildNodes(); - for (int i = 0; i < nlist.getLength(); i++) - { - org.w3c.dom.Node child = nlist.item(i); - short childType = child.getNodeType(); - if (childType == org.w3c.dom.Node.ELEMENT_NODE) - { - SOAPElement soapElement = soapFactory.createElement((Element)child, true); - soapBodyElement.addChildElement(soapElement); - } - else if (childType == org.w3c.dom.Node.TEXT_NODE) - { - String nodeValue = child.getNodeValue(); - soapBodyElement.addTextNode(nodeValue); - } - else if (childType == org.w3c.dom.Node.CDATA_SECTION_NODE) - { - String nodeValue = child.getNodeValue(); - soapBodyElement.addTextNode(nodeValue); - } - else - { - System.out.println("Ignore child type: " + childType); - } - } - } - else - { - throw new WSException("Unsupported message style: " + style); - } - } - } - - return soapEnv; - } - - private void registerNamespaces(NamespaceRegistry namespaceRegistry, SOAPElement soapEl) - { - Iterator itNSPrefixes = soapEl.getNamespacePrefixes(); - while (itNSPrefixes.hasNext()) - { - String prefix = (String)itNSPrefixes.next(); - String nsURI = soapEl.getNamespaceURI(prefix); - namespaceRegistry.registerURI(nsURI, prefix); - } - } -} Added: trunk/src/main/java/org/jboss/ws/soap/JAXWSPayloadBuilder.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/JAXWSPayloadBuilder.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/soap/JAXWSPayloadBuilder.java 2006-06-30 13:41:35 UTC (rev 534) @@ -0,0 +1,130 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ws.soap; + +//$Id$ + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; + +import javax.xml.namespace.QName; +import javax.xml.soap.Name; +import javax.xml.soap.SOAPElement; +import javax.xml.soap.SOAPException; +import javax.xml.soap.SOAPHeader; +import javax.xml.transform.stream.StreamSource; + +import org.jboss.util.xml.DOMUtils; +import org.jboss.util.xml.DOMWriter; +import org.jboss.xb.binding.NamespaceRegistry; +import org.w3c.dom.Element; + +/** + * A SOAPEnvelope builder for JAXWS in service mode PAYLOAD + * + * @author Tho...@jb... + * @since 30-Jun-2006 + */ +public class JAXWSPayloadBuilder implements PayloadBuilder +{ + public void build(SOAPMessageImpl soapMessage, InputStream ins, boolean ignoreParseError) throws IOException, SOAPException + { + // Parse the XML input stream + Element domEnv = null; + try + { + domEnv = DOMUtils.parse(ins); + } + catch (IOException ex) + { + if (ignoreParseError) + { + return; + } + throw ex; + } + + String envNS = domEnv.getNamespaceURI(); + String envPrefix = domEnv.getPrefix(); + + // Construct the envelope + SOAPFactoryImpl soapFactory = new SOAPFactoryImpl(); + SOAPPartImpl soapPart = (SOAPPartImpl)soapMessage.getSOAPPart(); + SOAPEnvelopeImpl soapEnv = new SOAPEnvelopeImpl(soapPart, soapFactory.createElement(domEnv, false)); + NamespaceRegistry namespaceRegistry = soapEnv.getNamespaceRegistry(); + + DOMUtils.copyAttributes(soapEnv, domEnv); + registerNamespaces(namespaceRegistry, soapEnv); + + // Add the header elements + Element domHeader = DOMUtils.getFirstChildElement(domEnv, new QName(envNS, "Header")); + if (domHeader != null) + { + SOAPHeader soapHeader = soapEnv.getHeader(); + + DOMUtils.copyAttributes(soapHeader, domHeader); + registerNamespaces(namespaceRegistry, soapHeader); + + Iterator it = DOMUtils.getChildElements(domHeader); + while (it.hasNext()) + { + Element srcElement = (Element)it.next(); + String xmlFragment = DOMWriter.printNode(srcElement, false); + + Name name = new NameImpl(srcElement.getLocalName(), srcElement.getPrefix(), srcElement.getNamespaceURI()); + SOAPContentElement destElement = new SOAPHeaderElementImpl(name); + soapHeader.addChildElement(destElement); + + DOMUtils.copyAttributes(destElement, srcElement); + destElement.setXMLFragment(xmlFragment); + } + } + + // Add the body elements + Element domBody = DOMUtils.getFirstChildElement(domEnv, new QName(envNS, "Body")); + SOAPBodyImpl soapBody = (SOAPBodyImpl)soapEnv.getBody(); + + DOMUtils.copyAttributes(soapBody, domBody); + registerNamespaces(namespaceRegistry, soapBody); + + Iterator itBody = DOMUtils.getChildElements(domBody); + if (itBody.hasNext()) + { + Element domBodyElement = (Element)itBody.next(); + String xmlStr = DOMWriter.printNode(domBodyElement, false); + soapBody.setPayload(new StreamSource(new ByteArrayInputStream(xmlStr.getBytes()))); + } + } + + private void registerNamespaces(NamespaceRegistry namespaceRegistry, SOAPElement soapEl) + { + Iterator itNSPrefixes = soapEl.getNamespacePrefixes(); + while (itNSPrefixes.hasNext()) + { + String prefix = (String)itNSPrefixes.next(); + String nsURI = soapEl.getNamespaceURI(prefix); + namespaceRegistry.registerURI(nsURI, prefix); + } + } +} Property changes on: trunk/src/main/java/org/jboss/ws/soap/JAXWSPayloadBuilder.java ___________________________________________________________________ Name: svn:keywords + Id Revision Name: svn:eol-style + LF Deleted: trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java 2006-06-30 13:41:35 UTC (rev 534) @@ -1,257 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * Copyright 2005, JBoss Inc., and individual contributors as indicated - * by the @authors tag. See the copyright.txt in the distribution for a - * full listing of individual contributors. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ -package org.jboss.ws.soap; - -// $Id$ - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Collection; -import java.util.Iterator; - -import javax.mail.internet.ContentType; -import javax.mail.internet.ParseException; -import javax.xml.soap.AttachmentPart; -import javax.xml.soap.MessageFactory; -import javax.xml.soap.MimeHeader; -import javax.xml.soap.MimeHeaders; -import javax.xml.soap.SOAPException; -import javax.xml.soap.SOAPMessage; - -import org.jboss.logging.Logger; -import org.jboss.ws.Constants; -import org.jboss.ws.jaxrpc.Style; -import org.jboss.ws.soap.attachment.MimeConstants; -import org.jboss.ws.soap.attachment.MultipartRelatedDecoder; -import org.jboss.ws.utils.IOUtils; - -/** - * MessageFactory implementation - * - * @author Tho...@jb... - */ -public class MessageFactoryImpl extends MessageFactory -{ - private static Logger log = Logger.getLogger(MessageFactoryImpl.class); - - // The envelope URI used by the MessageFactory - private String envelopeURI = Constants.NS_SOAP11_ENV; - - // The style used by this MessageFactory - private Style style; - - /** - * Get the SOAP envelope URI this factory will use when creating envelopes. - */ - public String getEnvelopeURI() - { - return envelopeURI; - } - - /** - * Set the SOAP envelope URI this factory will use when creating envelopes. - */ - public void setEnvelopeURI(String envelopeURI) - { - this.envelopeURI = envelopeURI; - } - - /** - * Get the Style this message factory will use - */ - public Style getStyle() - { - if (style == null) - { - SOAPMessageContextImpl msgContext = MessageContextAssociation.peekMessageContext(); - if (msgContext != null && msgContext.getOperationMetaData() != null) - { - style = msgContext.getOperationMetaData().getStyle(); - } - log.trace("Using style: " + style); - } - return style; - } - - public void setStyle(Style style) - { - this.style = style; - } - - /** - * Creates a new SOAPMessage object with the default SOAPPart, SOAPEnvelope, - * SOAPBody, and SOAPHeader objects. Profile-specific message factories can - * choose to prepopulate the SOAPMessage object with profile-specific - * headers. - * - * Content can be added to this message's SOAPPart object, and the message - * can be sent "as is" when a message containing only a SOAP part is - * sufficient. Otherwise, the SOAPMessage object needs to create one or more - * AttachmentPart objects and add them to itself. Any content that is not in - * XML format must be in an AttachmentPart object. - * - * @return a new SOAPMessage object - * @throws javax.xml.soap.SOAPException - * if a SOAP error occurs - */ - public SOAPMessage createMessage() throws SOAPException - { - SOAPMessageImpl soapMessage = new SOAPMessageImpl(); - SOAPPartImpl soapPart = (SOAPPartImpl)soapMessage.getSOAPPart(); - new SOAPEnvelopeImpl(soapPart, envelopeURI); - return soapMessage; - } - - /** - * Internalizes the contents of the given InputStream object into a new - * SOAPMessage object and returns the SOAPMessage object. - * - * @param mimeHeaders - * the transport-specific headers passed to the message in a - * transport-independent fashion for creation of the message - * @param ins - * the InputStream object that contains the data for a message - * @return a new SOAPMessage object containing the data from the given - * InputStream object - * @throws java.io.IOException - * if there is a problem in reading data from the input stream - * @throws javax.xml.soap.SOAPException - * if the message is invalid - */ - public SOAPMessage createMessage(MimeHeaders mimeHeaders, InputStream ins) throws IOException, SOAPException - { - return createMessageInternal(mimeHeaders, ins, false); - } - - public SOAPMessage createMessageInternal(MimeHeaders mimeHeaders, InputStream ins, boolean ingnoreParseException) throws IOException, SOAPException - { - if (mimeHeaders == null) - { - mimeHeaders = new MimeHeaders(); - } - else if (log.isTraceEnabled()) - { - Iterator<MimeHeader> itMimeHeaders = mimeHeaders.getAllHeaders(); - while (itMimeHeaders.hasNext()) - { - MimeHeader mh = itMimeHeaders.next(); - log.trace(mh); - } - } - - ContentType contentType = getContentType(mimeHeaders); - log.debug("createMessage: [contentType=" + contentType + "]"); - - // Debug the incoming message - if (log.isTraceEnabled()) - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); - IOUtils.copyStream(baos, ins); - byte[] bytes = baos.toByteArray(); - - log.trace("createMessage\n" + new String(bytes)); - ins = new ByteArrayInputStream(bytes); - } - - Collection<AttachmentPart> attachments = null; - if (isMultipartRelatedContent(contentType)) - { - MultipartRelatedDecoder decoder; - try - { - decoder = new MultipartRelatedDecoder(contentType); - decoder.decodeMultipartRelatedMessage(ins); - } - catch (RuntimeException rte) - { - throw rte; - } - catch (IOException ex) - { - throw ex; - } - catch (Exception ex) - { - throw new SOAPException("Cannot decode multipart related message", ex); - } - - ins = decoder.getRootPart().getDataHandler().getInputStream(); - attachments = decoder.getRelatedParts(); - } - else if (isSoapContent(contentType) == false) - { - throw new SOAPException("Unsupported content type: " + contentType); - } - - SOAPMessageImpl soapMessage = new SOAPMessageImpl(); - if (mimeHeaders != null) - soapMessage.setMimeHeaders(mimeHeaders); - - if (attachments != null) - soapMessage.setAttachments(attachments); - - SAAJEnvelopeBuilder envelopeBuilder = new DOMEnvelopeBuilder(); - envelopeBuilder.setIgnoreParseException(ingnoreParseException); - envelopeBuilder.setStyle(getStyle()); - envelopeBuilder.setSOAPMessage(soapMessage); - envelopeBuilder.build(ins); - - return soapMessage; - } - - - private static ContentType getContentType(MimeHeaders headers) throws SOAPException - { - ContentType contentType = null; - try - { - String[] type = headers.getHeader(MimeConstants.CONTENT_TYPE); - if (type != null) - { - contentType = new ContentType(type[0]); - } - else - { - contentType = new ContentType(MimeConstants.TYPE_SOAP11); - } - return contentType; - } - catch (ParseException e) - { - throw new SOAPException("Could not parse content type:" + e); - } - } - - private boolean isSoapContent(ContentType type) - { - String baseType = type.getBaseType(); - return MimeConstants.TYPE_SOAP11.equals(baseType) || MimeConstants.TYPE_SOAP12.equals(baseType); - } - - private boolean isMultipartRelatedContent(ContentType type) - { - String baseType = type.getBaseType(); - return MimeConstants.TYPE_MULTIPART_RELATED.equals(baseType); - } -} Added: trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java 2006-06-30 13:41:35 UTC (rev 534) @@ -0,0 +1,279 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ws.soap; + +// $Id$ + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.Iterator; + +import javax.mail.internet.ContentType; +import javax.mail.internet.ParseException; +import javax.xml.soap.AttachmentPart; +import javax.xml.soap.MessageFactory; +import javax.xml.soap.MimeHeader; +import javax.xml.soap.MimeHeaders; +import javax.xml.soap.SOAPException; +import javax.xml.soap.SOAPMessage; +import javax.xml.ws.Service.Mode; + +import org.jboss.logging.Logger; +import org.jboss.ws.Constants; +import org.jboss.ws.jaxrpc.Style; +import org.jboss.ws.soap.attachment.MimeConstants; +import org.jboss.ws.soap.attachment.MultipartRelatedDecoder; +import org.jboss.ws.utils.IOUtils; + +/** + * MessageFactory implementation + * + * @author Tho...@jb... + */ +public class MessageFactoryImpl extends MessageFactory +{ + private static Logger log = Logger.getLogger(MessageFactoryImpl.class); + + // The envelope URI used by the MessageFactory + private String envelopeURI = Constants.NS_SOAP11_ENV; + + // The JAXWS ServiceMode + private Mode serviceMode; + // The style used by this MessageFactory + private Style style; + + /** + * Get the SOAP envelope URI this factory will use when creating envelopes. + */ + public String getEnvelopeURI() + { + return envelopeURI; + } + + /** + * Set the SOAP envelope URI this factory will use when creating envelopes. + */ + public void setEnvelopeURI(String envelopeURI) + { + this.envelopeURI = envelopeURI; + } + + /** + * Get the Style this message factory will use + */ + public Style getStyle() + { + if (style == null) + { + SOAPMessageContextImpl msgContext = MessageContextAssociation.peekMessageContext(); + if (msgContext != null && msgContext.getOperationMetaData() != null) + { + style = msgContext.getOperationMetaData().getStyle(); + } + log.trace("Using style: " + style); + } + return style; + } + + public void setStyle(Style style) + { + this.style = style; + } + + public Mode getServiceMode() + { + return serviceMode; + } + + public void setServiceMode(Mode serviceMode) + { + this.serviceMode = serviceMode; + } + + /** + * Creates a new SOAPMessage object with the default SOAPPart, SOAPEnvelope, + * SOAPBody, and SOAPHeader objects. Profile-specific message factories can + * choose to prepopulate the SOAPMessage object with profile-specific + * headers. + * + * Content can be added to this message's SOAPPart object, and the message + * can be sent "as is" when a message containing only a SOAP part is + * sufficient. Otherwise, the SOAPMessage object needs to create one or more + * AttachmentPart objects and add them to itself. Any content that is not in + * XML format must be in an AttachmentPart object. + * + * @return a new SOAPMessage object + * @throws javax.xml.soap.SOAPException + * if a SOAP error occurs + */ + public SOAPMessage createMessage() throws SOAPException + { + SOAPMessageImpl soapMessage = new SOAPMessageImpl(); + SOAPPartImpl soapPart = (SOAPPartImpl)soapMessage.getSOAPPart(); + new SOAPEnvelopeImpl(soapPart, envelopeURI); + return soapMessage; + } + + /** + * Internalizes the contents of the given InputStream object into a new + * SOAPMessage object and returns the SOAPMessage object. + * + * @param mimeHeaders + * the transport-specific headers passed to the message in a + * transport-independent fashion for creation of the message + * @param ins + * the InputStream object that contains the data for a message + * @return a new SOAPMessage object containing the data from the given + * InputStream object + * @throws java.io.IOException + * if there is a problem in reading data from the input stream + * @throws javax.xml.soap.SOAPException + * if the message is invalid + */ + public SOAPMessage createMessage(MimeHeaders mimeHeaders, InputStream ins) throws IOException, SOAPException + { + return createMessageInternal(mimeHeaders, ins, false); + } + + public SOAPMessage createMessageInternal(MimeHeaders mimeHeaders, InputStream ins, boolean ingnoreParseException) throws IOException, SOAPException + { + if (mimeHeaders == null) + { + mimeHeaders = new MimeHeaders(); + } + else if (log.isTraceEnabled()) + { + Iterator<MimeHeader> itMimeHeaders = mimeHeaders.getAllHeaders(); + while (itMimeHeaders.hasNext()) + { + MimeHeader mh = itMimeHeaders.next(); + log.trace(mh); + } + } + + ContentType contentType = getContentType(mimeHeaders); + log.debug("createMessage: [contentType=" + contentType + "]"); + + // Debug the incoming message + if (log.isTraceEnabled()) + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); + IOUtils.copyStream(baos, ins); + byte[] bytes = baos.toByteArray(); + + log.trace("createMessage\n" + new String(bytes)); + ins = new ByteArrayInputStream(bytes); + } + + Collection<AttachmentPart> attachments = null; + if (isMultipartRelatedContent(contentType)) + { + MultipartRelatedDecoder decoder; + try + { + decoder = new MultipartRelatedDecoder(contentType); + decoder.decodeMultipartRelatedMessage(ins); + } + catch (RuntimeException rte) + { + throw rte; + } + catch (IOException ex) + { + throw ex; + } + catch (Exception ex) + { + throw new SOAPException("Cannot decode multipart related message", ex); + } + + ins = decoder.getRootPart().getDataHandler().getInputStream(); + attachments = decoder.getRelatedParts(); + } + else if (isSoapContent(contentType) == false) + { + throw new SOAPException("Unsupported content type: " + contentType); + } + + SOAPMessageImpl soapMessage = new SOAPMessageImpl(); + if (mimeHeaders != null) + soapMessage.setMimeHeaders(mimeHeaders); + + if (attachments != null) + soapMessage.setAttachments(attachments); + + // Get the SOAPEnvelope builder + PayloadBuilder payloadBuilder; + if (serviceMode == Mode.PAYLOAD) + { + payloadBuilder = new JAXWSPayloadBuilder(); + } + else + { + SAAJPayloadBuilderDOM jaxrpcBuilder = new SAAJPayloadBuilderDOM(); + jaxrpcBuilder.setStyle(getStyle()); + payloadBuilder = jaxrpcBuilder; + } + + // Build the payload + payloadBuilder.build(soapMessage, ins, false); + + return soapMessage; + } + + private static ContentType getContentType(MimeHeaders headers) throws SOAPException + { + ContentType contentType = null; + try + { + String[] type = headers.getHeader(MimeConstants.CONTENT_TYPE); + if (type != null) + { + contentType = new ContentType(type[0]); + } + else + { + contentType = new ContentType(MimeConstants.TYPE_SOAP11); + } + return contentType; + } + catch (ParseException e) + { + throw new SOAPException("Could not parse content type:" + e); + } + } + + private boolean isSoapContent(ContentType type) + { + String baseType = type.getBaseType(); + return MimeConstants.TYPE_SOAP11.equals(baseType) || MimeConstants.TYPE_SOAP12.equals(baseType); + } + + private boolean isMultipartRelatedContent(ContentType type) + { + String baseType = type.getBaseType(); + return MimeConstants.TYPE_MULTIPART_RELATED.equals(baseType); + } +} Property changes on: trunk/src/main/java/org/jboss/ws/soap/MessageFactoryImpl.java ___________________________________________________________________ Name: svn:keywords + Id Revision Name: svn:eol-style + LF Added: trunk/src/main/java/org/jboss/ws/soap/PayloadBuilder.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/PayloadBuilder.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/soap/PayloadBuilder.java 2006-06-30 13:41:35 UTC (rev 534) @@ -0,0 +1,40 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ws.soap; + +// $Id$ + +import java.io.IOException; +import java.io.InputStream; + +import javax.xml.soap.SOAPException; +import javax.xml.soap.SOAPMessage; + +/** + * @author Heiko Braun, <hei...@jb...> + * @author Tho...@jb... + * @since 19-Apr-2006 + */ +public interface PayloadBuilder +{ + void build(SOAPMessageImpl soapMessage, InputStream in, boolean ingnoreParseError) throws IOException, SOAPException; +} Property changes on: trunk/src/main/java/org/jboss/ws/soap/PayloadBuilder.java ___________________________________________________________________ Name: svn:keywords + Id Revision Name: svn:eol-style + LF Deleted: trunk/src/main/java/org/jboss/ws/soap/SAAJEnvelopeBuilder.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/SAAJEnvelopeBuilder.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/soap/SAAJEnvelopeBuilder.java 2006-06-30 13:41:35 UTC (rev 534) @@ -1,20 +0,0 @@ -package org.jboss.ws.soap; - -import org.jboss.ws.jaxrpc.Style; - -import javax.xml.soap.SOAPEnvelope; -import javax.xml.soap.SOAPMessage; -import javax.xml.soap.SOAPException; -import java.io.InputStream; -import java.io.IOException; - -/** - * @author Heiko Braun, <hei...@jb...> - * @since 19-Apr-2006 - */ -public interface SAAJEnvelopeBuilder { - void setSOAPMessage(SOAPMessage soapMessage); - void setStyle(Style style); - SOAPEnvelope build(InputStream in) throws IOException, SOAPException; - void setIgnoreParseException(boolean b); -} Added: trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderDOM.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderDOM.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderDOM.java 2006-06-30 13:41:35 UTC (rev 534) @@ -0,0 +1,260 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.ws.soap; + +//$Id$ + +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; + +import javax.xml.namespace.QName; +import javax.xml.soap.Detail; +import javax.xml.soap.Name; +import javax.xml.soap.SOAPBody; +import javax.xml.soap.SOAPElement; +import javax.xml.soap.SOAPException; +import javax.xml.soap.SOAPHeader; + +import org.jboss.util.xml.DOMUtils; +import org.jboss.util.xml.DOMWriter; +import org.jboss.ws.WSException; +import org.jboss.ws.jaxrpc.Style; +import org.jboss.xb.binding.NamespaceRegistry; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * A SOAPEnvelope builder for JAXRPC based on DOM + * + * @author Heiko Braun, <hei...@jb...> + * @author Tho...@jb... + * @since 19-Apr-2006 + */ +public class SAAJPayloadBuilderDOM implements PayloadBuilder +{ + private Style style = Style.DOCUMENT; + + public void setStyle(Style style) + { + this.style = style; + } + + public void build(SOAPMessageImpl soapMessage, InputStream ins, boolean ignoreParseError) throws IOException, SOAPException + { + // Parse the XML input stream + Element domEnv = null; + try + { + domEnv = DOMUtils.parse(ins); + } + catch (IOException ex) + { + if (ignoreParseError) + { + return; + } + throw ex; + } + + String envNS = domEnv.getNamespaceURI(); + String envPrefix = domEnv.getPrefix(); + + // Construct the envelope + SOAPFactoryImpl soapFactory = new SOAPFactoryImpl(); + SOAPPartImpl soapPart = (SOAPPartImpl)soapMessage.getSOAPPart(); + SOAPEnvelopeImpl soapEnv = new SOAPEnvelopeImpl(soapPart, soapFactory.createElement(domEnv, false)); + NamespaceRegistry namespaceRegistry = soapEnv.getNamespaceRegistry(); + + DOMUtils.copyAttributes(soapEnv, domEnv); + registerNamespaces(namespaceRegistry, soapEnv); + + // Add the header elements + Element domHeader = DOMUtils.getFirstChildElement(domEnv, new QName(envNS, "Header")); + if (domHeader != null) + { + SOAPHeader soapHeader = soapEnv.getHeader(); + + DOMUtils.copyAttributes(soapHeader, domHeader); + registerNamespaces(namespaceRegistry, soapHeader); + + Iterator it = DOMUtils.getChildElements(domHeader); + while (it.hasNext()) + { + Element srcElement = (Element)it.next(); + String xmlFragment = DOMWriter.printNode(srcElement, false); + + Name name = new NameImpl(srcElement.getLocalName(), srcElement.getPrefix(), srcElement.getNamespaceURI()); + SOAPContentElement destElement = new SOAPHeaderElementImpl(name); + soapHeader.addChildElement(destElement); + + DOMUtils.copyAttributes(destElement, srcElement); + destElement.setXMLFragment(xmlFragment); + } + } + + // Add the body elements + Element domBody = DOMUtils.getFirstChildElement(domEnv, new QName(envNS, "Body")); + SOAPBody soapBody = soapEnv.getBody(); + + DOMUtils.copyAttributes(soapBody, domBody); + registerNamespaces(namespaceRegistry, soapBody); + + Iterator itBody = DOMUtils.getChildElements(domBody); + if (itBody.hasNext()) + { + Element domBodyElement = (Element)itBody.next(); + + String localName = domBodyElement.getLocalName(); + String prefix = domBodyElement.getPrefix(); + String nsURI = domBodyElement.getNamespaceURI(); + Name beName = new NameImpl(localName, prefix, nsURI); + + // Process a <env:Fault> message + if (beName.equals(new NameImpl("Fault", envPrefix, envNS))) + { + SOAPFaultImpl soapFault = new SOAPFaultImpl(envNS); + soapBody.addChildElement(soapFault); + + DOMUtils.copyAttributes(soapFault, domBodyElement); + + Element domFaultCode = DOMUtils.getFirstChildElement(domBodyElement, new QName("faultcode")); + if (domFaultCode == null) + throw new SOAPException("SOAPFault does not contain a <faultcode> element"); + + Element domFaultString = DOMUtils.getFirstChildElement(domBodyElement, new QName("faultstring")); + if (domFaultString == null) + throw new SOAPException("SOAPFault does not contain a <faultstring> element"); + + String faultCode = DOMUtils.getTextContent(domFaultCode); + soapFault.setFaultCode(faultCode); + + String faultString = DOMUtils.getTextContent(domFaultString); + soapFault.setFaultString(faultString); + + Element domFaultActor = DOMUtils.getFirstChildElement(domBodyElement, new QName("faultactor")); + if (domFaultActor != null) + { + String faultActor = DOMUtils.getTextContent(domFaultActor); + soapFault.setFaultActor(faultActor); + } + + // Add the fault detail + Element domFaultDetail = DOMUtils.getFirstChildElement(domBodyElement, "detail"); + if (domFaultDetail != null) + { + Detail detail = soapFault.addDetail(); + Iterator it = DOMUtils.getChildElements(domFaultDetail); + while (it.hasNext()) + { + Element domElement = (Element)it.next(); + SOAPElement detailEntry = new DetailEntryImpl(soapFactory.createElement(domElement, true)); + detailEntry = detail.addChildElement(detailEntry); + } + } + } + + // Process and RPC or DOCUMENT style message + else + { + + if (style == Style.RPC) + { + SOAPBodyElementRpc soapBodyElement = new SOAPBodyElementRpc(beName); + soapBodyElement = (SOAPBodyElementRpc)soapBody.addChildElement(soapBodyElement); + + DOMUtils.copyAttributes(soapBodyElement, domBodyElement); + + Iterator itBodyElement = DOMUtils.getChildElements(domBodyElement); + while (itBodyElement.hasNext()) + { + Element srcElement = (Element)itBodyElement.next(); + + Name name = new NameImpl(srcElement.getLocalName(), srcElement.getPrefix(), srcElement.getNamespaceURI()); + SOAPContentElement destElement = new SOAPContentElement(name); + soapBodyElement.addChildElement(destElement); + + String xmlFragment = DOMWriter.printNode(srcElement, false); + destElement.setXMLFragment(xmlFragment); + } + } + else if (style == Style.DOCUMENT) + { + Element srcElement = (Element)domBodyElement; + SOAPBodyElementDoc destElement = new SOAPBodyElementDoc(beName); + destElement = (SOAPBodyElementDoc)soapBody.addChildElement(destElement); + + String xmlFragment = DOMWriter.printNode(srcElement, false); + destElement.setXMLFragment(xmlFragment); + } + else if (style == null) + { + SOAPBodyElementMessage soapBodyElement = new SOAPBodyElementMessage(beName); + soapBodyElement = (SOAPBodyElementMessage)soapBody.addChildElement(soapBodyElement); + + DOMUtils.copyAttributes(soapBodyElement, domBodyElement); + + NodeList nlist = domBodyElement.getChildNodes(); + for (int i = 0; i < nlist.getLength(); i++) + { + org.w3c.dom.Node child = nlist.item(i); + short childType = child.getNodeType(); + if (childType == org.w3c.dom.Node.ELEMENT_NODE) + { + SOAPElement soapElement = soapFactory.createElement((Element)child, true); + soapBodyElement.addChildElement(soapElement); + } + else if (childType == org.w3c.dom.Node.TEXT_NODE) + { + String nodeValue = child.getNodeValue(); + soapBodyElement.addTextNode(nodeValue); + } + else if (childType == org.w3c.dom.Node.CDATA_SECTION_NODE) + { + String nodeValue = child.getNodeValue(); + soapBodyElement.addTextNode(nodeValue); + } + else + { + System.out.println("Ignore child type: " + childType); + } + } + } + else + { + throw new WSException("Unsupported message style: " + style); + } + } + } + } + + private void registerNamespaces(NamespaceRegistry namespaceRegistry, SOAPElement soapEl) + { + Iterator itNSPrefixes = soapEl.getNamespacePrefixes(); + while (itNSPrefixes.hasNext()) + { + String prefix = (String)itNSPrefixes.next(); + String nsURI = soapEl.getNamespaceURI(prefix); + namespaceRegistry.registerURI(nsURI, prefix); + } + } +} Property changes on: trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderDOM.java ___________________________________________________________________ Name: svn:keywords + Id Revision Name: svn:eol-style + LF Added: trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderStax.java =================================================================== --- trunk/src/main/java/org/jboss/ws/soap/SAAJPayloadBuilderStax.java 2006-06-30 10:19:14 UTC (rev 533) +++ trunk/src/main/java/org/jboss/ws/s... [truncated message content] |