View Javadoc

1   /*
2    * Copyright 2005-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.axiom.support;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.Iterator;
24  import java.util.Locale;
25  import javax.xml.namespace.QName;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.stream.XMLInputFactory;
28  
29  import org.springframework.util.Assert;
30  import org.springframework.util.StringUtils;
31  import org.springframework.xml.namespace.QNameUtils;
32  
33  import org.apache.axiom.om.OMContainer;
34  import org.apache.axiom.om.OMElement;
35  import org.apache.axiom.om.OMException;
36  import org.apache.axiom.om.OMNamespace;
37  import org.apache.axiom.soap.SOAPEnvelope;
38  import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
39  import org.w3c.dom.DOMImplementation;
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Element;
42  import org.w3c.dom.ls.DOMImplementationLS;
43  import org.w3c.dom.ls.LSOutput;
44  import org.w3c.dom.ls.LSSerializer;
45  
46  /**
47   * Collection of generic utility methods to work with Axiom. Includes conversion from <code>OMNamespace</code>s to
48   * <code>QName</code>s.
49   *
50   * @author Arjen Poutsma
51   * @author Tareq Abed Rabbo
52   * @see org.apache.axiom.om.OMNamespace
53   * @see javax.xml.namespace.QName
54   * @since 1.0.0
55   */
56  @SuppressWarnings("Since15")
57  public abstract class AxiomUtils {
58  
59      /**
60       * Converts a <code>javax.xml.namespace.QName</code> to a <code>org.apache.axiom.om.OMNamespace</code>. A
61       * <code>OMElement</code> is used to resolve the namespace, or to declare a new one.
62       *
63       * @param qName          the <code>QName</code> to convert
64       * @param resolveElement the element used to resolve the Q
65       * @return the converted SAAJ Name
66       * @throws OMException              if conversion is unsuccessful
67       * @throws IllegalArgumentException if <code>qName</code> is not fully qualified
68       */
69      public static OMNamespace toNamespace(QName qName, OMElement resolveElement) throws OMException {
70          String prefix = QNameUtils.getPrefix(qName);
71          if (StringUtils.hasLength(qName.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
72              return resolveElement.declareNamespace(qName.getNamespaceURI(), prefix);
73          }
74          else if (StringUtils.hasLength(qName.getNamespaceURI())) {
75              // check for existing namespace, and declare if necessary
76              return resolveElement.declareNamespace(qName.getNamespaceURI(), "");
77          }
78          else {
79              throw new IllegalArgumentException("qName [" + qName + "] does not contain a namespace");
80          }
81      }
82  
83      /**
84       * Converts the given locale to a <code>xml:lang</code> string, as used in Axiom Faults.
85       *
86       * @param locale the locale
87       * @return the language string
88       */
89      public static String toLanguage(Locale locale) {
90          return locale.toString().replace('_', '-');
91      }
92  
93      /**
94       * Converts the given locale to a <code>xml:lang</code> string, as used in Axiom Faults.
95       *
96       * @param language the language string
97       * @return the locale
98       */
99      public static Locale toLocale(String language) {
100         language = language.replace('-', '_');
101         return StringUtils.parseLocaleString(language);
102     }
103 
104     /** Removes the contents (i.e. children) of the container. */
105     public static void removeContents(OMContainer container) {
106         for (Iterator<?> iterator = container.getChildren(); iterator.hasNext();) {
107             iterator.next();
108             iterator.remove();
109         }
110     }
111 
112     /**
113      * Converts a given AXIOM {@link org.apache.axiom.soap.SOAPEnvelope} to a {@link Document}.
114      *
115      * @param envelope the SOAP envelope to be converted
116      * @return the converted document
117      * @throws IllegalArgumentException in case of errors
118      * @see org.apache.rampart.util.Axis2Util.getDocumentFromSOAPEnvelope(SOAPEnvelope, boolean)
119      */
120     public static Document toDocument(SOAPEnvelope envelope) {
121         try {
122             if (envelope instanceof Element) {
123                 return ((Element) envelope).getOwnerDocument();
124             }
125             else {
126                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
127                 envelope.build();
128                 envelope.serialize(bos);
129 
130                 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
131                 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
132                 documentBuilderFactory.setNamespaceAware(true);
133                 return documentBuilderFactory.newDocumentBuilder().parse(bis);
134             }
135         }
136         catch (Exception ex) {
137             throw new IllegalArgumentException("Error in converting SOAP Envelope to Document", ex);
138         }
139     }
140 
141     /**
142      * Converts a given {@link Document} to an AXIOM {@link org.apache.axiom.soap.SOAPEnvelope}.
143      *
144      * @param document the document to be converted
145      * @return the converted envelope
146      * @throws IllegalArgumentException in case of errors
147      * @see org.apache.rampart.util.Axis2Util.getSOAPEnvelopeFromDOMDocument(Document, boolean)
148      */
149     public static SOAPEnvelope toEnvelope(Document document) {
150         try {
151             DOMImplementation implementation = document.getImplementation();
152             Assert.isInstanceOf(DOMImplementationLS.class, implementation);
153 
154             DOMImplementationLS loadSaveImplementation = (DOMImplementationLS) implementation;
155             LSOutput output = loadSaveImplementation.createLSOutput();
156             ByteArrayOutputStream bos = new ByteArrayOutputStream();
157             output.setByteStream(bos);
158 
159             LSSerializer serializer = loadSaveImplementation.createLSSerializer();
160             serializer.write(document, output);
161 
162             ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
163 
164             StAXSOAPModelBuilder stAXSOAPModelBuilder =
165                     new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(bis), null);
166             SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();
167 
168             // Necessary to build a correct Axiom tree, see SWS-483
169             envelope.serialize(new NullOutputStream());
170 
171             return envelope;
172         }
173         catch (Exception ex) {
174             IllegalArgumentException iaex =
175                     new IllegalArgumentException("Error in converting Document to SOAP Envelope");
176             iaex.initCause(ex);
177             throw iaex;
178         }
179     }
180 
181     /** OutputStream that does nothing. */
182     private static class NullOutputStream extends OutputStream {
183 
184         @Override
185         public void write(int b) throws IOException {
186         }
187 
188         @Override
189         public void write(byte[] b) throws IOException {
190         }
191 
192         @Override
193         public void write(byte[] b, int off, int len) throws IOException {
194         }
195     }
196 
197 }