001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.soap.marshalers;
018    
019    import javax.xml.namespace.QName;
020    import javax.xml.stream.XMLInputFactory;
021    import javax.xml.stream.XMLOutputFactory;
022    
023    import org.apache.servicemix.jbi.jaxp.StAXSourceTransformer;
024    
025    /**
026     * 
027     * @author Guillaume Nodet
028     * @version $Revision: 359186 $
029     * @since 3.0 
030     */
031    public class SoapMarshaler {
032    
033        public static final String MIME_CONTENT_TYPE = "Content-Type";
034            public static final String MULTIPART_CONTENT = "multipart/";
035            public static final String SOAP_PART_ID = "soap-request";
036            public static final String SOAP_11_URI = "http://schemas.xmlsoap.org/soap/envelope/";
037            public static final String SOAP_12_URI = "http://www.w3.org/2003/05/soap-envelope";
038            public static final String SOAP_PREFIX = "env";
039            public static final String ENVELOPE = "Envelope";
040            public static final String HEADER = "Header";
041            public static final String BODY = "Body";
042            public static final String FAULT = "Fault";
043        
044        public static final QName SOAP_11_FAULTCODE = new QName("faultcode");
045        public static final QName SOAP_11_FAULTSTRING = new QName("faultstring");
046        public static final QName SOAP_11_FAULTACTOR = new QName("faultactor");
047        public static final QName SOAP_11_FAULTDETAIL = new QName("detail");
048        public static final QName SOAP_11_CODE_VERSIONMISMATCH = new QName(SOAP_11_URI, "VersionMismatch");
049        public static final QName SOAP_11_CODE_MUSTUNDERSTAND = new QName(SOAP_11_URI, "MustUnderstand");
050        public static final QName SOAP_11_CODE_CLIENT = new QName(SOAP_11_URI, "Client");
051        public static final QName SOAP_11_CODE_SERVER = new QName(SOAP_11_URI, "Server");
052        
053        public static final QName SOAP_12_FAULTCODE = new QName(SOAP_12_URI, "Code");
054        public static final QName SOAP_12_FAULTSUBCODE = new QName(SOAP_12_URI, "Subcode");
055        public static final QName SOAP_12_FAULTVALUE = new QName(SOAP_12_URI, "Value");
056        public static final QName SOAP_12_FAULTREASON = new QName(SOAP_12_URI, "Reason");
057        public static final QName SOAP_12_FAULTTEXT = new QName(SOAP_12_URI, "Text");
058        public static final QName SOAP_12_FAULTNODE = new QName(SOAP_12_URI, "Node");
059        public static final QName SOAP_12_FAULTROLE = new QName(SOAP_12_URI, "Role");
060        public static final QName SOAP_12_FAULTDETAIL = new QName(SOAP_12_URI, "Detail");
061        public static final QName SOAP_12_CODE_DATAENCODINGUNKNOWN = new QName(SOAP_12_URI, "DataEncodingUnknown");
062        public static final QName SOAP_12_CODE_VERSIONMISMATCH = new QName(SOAP_12_URI, "VersionMismatch");
063        public static final QName SOAP_12_CODE_MUSTUNDERSTAND = new QName(SOAP_12_URI, "MustUnderstand");
064        public static final QName SOAP_12_CODE_RECEIVER = new QName(SOAP_12_URI, "Receiver");
065        public static final QName SOAP_12_CODE_SENDER = new QName(SOAP_12_URI, "Sender");
066        
067            protected XMLInputFactory inputFactory;
068            protected XMLOutputFactory outputFactory;
069            protected StAXSourceTransformer  sourceTransformer;
070            protected boolean repairingNamespace;
071            protected String prefix = SOAP_PREFIX;
072            protected boolean soap = true;
073        protected boolean useDom = false;
074            protected String soapUri = SOAP_12_URI;
075        
076        public static final String MULTIPLE_DETAILS_NODE_WRAPPER = "multiple-details";
077    
078            public SoapMarshaler() {
079            }
080    
081        public SoapMarshaler(boolean soap) {
082            this.soap = soap;
083        }
084    
085            public SoapMarshaler(boolean soap, boolean useDom) {
086                    this.soap = soap;
087            this.useDom = useDom;
088            }
089    
090        public XMLInputFactory getInputFactory() {
091            if (inputFactory == null) {
092                inputFactory = XMLInputFactory.newInstance();
093                inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
094            }
095            return inputFactory;
096        }
097    
098        public XMLOutputFactory getOutputFactory() {
099            if (outputFactory == null) {
100                outputFactory = XMLOutputFactory.newInstance();
101                if (isRepairingNamespace()) {
102                    outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, 
103                                                                      Boolean.valueOf(isRepairingNamespace()));
104                }
105            }
106            return outputFactory;
107        }
108        
109        public StAXSourceTransformer getSourceTransformer() {
110            if (sourceTransformer == null) {
111                    sourceTransformer = new StAXSourceTransformer();
112            }
113            return sourceTransformer;
114        }
115    
116        public boolean isRepairingNamespace() {
117            return repairingNamespace;
118        }
119    
120        public void setRepairingNamespace(boolean repairingNamespace) {
121            this.repairingNamespace = repairingNamespace;
122        }
123        
124        public boolean isSoap() {
125            return soap;
126        }
127        
128        public void setSoap(boolean soap) {
129            this.soap = soap;
130        }
131        
132        /**
133         * @return the useDom
134         */
135        public boolean isUseDom() {
136            return useDom;
137        }
138    
139        /**
140         * @param useDom the useDom to set
141         */
142        public void setUseDom(boolean useDom) {
143            this.useDom = useDom;
144        }
145    
146        public String getPrefix() {
147            return prefix;
148        }
149        
150        public void setPrefix(String prefix) {
151            this.prefix = prefix;
152        }
153        
154        public String getSoapUri() {
155            return soapUri;
156        }
157        
158        public void setSoapUri(String soapUri) {
159            this.soapUri = soapUri;
160        }
161        
162        public SoapReader createReader() {
163            return new SoapReader(this);
164        }
165    
166            public SoapWriter createWriter(SoapMessage message) {
167                    return new SoapWriter(this, message);
168            }
169    }