001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.encoding;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
041: import com.sun.xml.stream.buffer.XMLStreamBuffer;
042: import com.sun.xml.stream.buffer.XMLStreamBufferMark;
043: import com.sun.xml.stream.buffer.stax.StreamReaderBufferCreator;
044: import com.sun.xml.ws.api.SOAPVersion;
045: import com.sun.xml.ws.api.message.AttachmentSet;
046: import com.sun.xml.ws.api.message.HeaderList;
047: import com.sun.xml.ws.api.message.Message;
048: import com.sun.xml.ws.api.message.Packet;
049: import com.sun.xml.ws.api.pipe.ContentType;
050: import com.sun.xml.ws.api.streaming.XMLStreamReaderFactory;
051: import com.sun.xml.ws.api.streaming.XMLStreamWriterFactory;
052: import com.sun.xml.ws.message.AttachmentSetImpl;
053: import com.sun.xml.ws.message.stream.StreamHeader;
054: import com.sun.xml.ws.message.stream.StreamMessage;
055: import com.sun.xml.ws.protocol.soap.VersionMismatchException;
056: import com.sun.xml.ws.server.UnsupportedMediaException;
057: import com.sun.xml.ws.streaming.XMLStreamReaderUtil;
058: import com.sun.xml.ws.streaming.TidyXMLStreamReader;
059:
060: import javax.xml.stream.XMLStreamConstants;
061: import javax.xml.stream.XMLStreamException;
062: import javax.xml.stream.XMLStreamReader;
063: import javax.xml.stream.XMLStreamWriter;
064: import javax.xml.ws.WebServiceException;
065: import java.io.IOException;
066: import java.io.InputStream;
067: import java.io.OutputStream;
068: import java.nio.channels.ReadableByteChannel;
069: import java.nio.channels.WritableByteChannel;
070: import java.util.HashMap;
071: import java.util.List;
072: import java.util.Map;
073:
074: /**
075: * A stream SOAP codec.
076: *
077: * @author Paul Sandoz
078: */
079: @SuppressWarnings({"StringEquality"})
080: public abstract class StreamSOAPCodec implements
081: com.sun.xml.ws.api.pipe.StreamSOAPCodec, RootOnlyCodec {
082:
083: private static final String SOAP_ENVELOPE = "Envelope";
084: private static final String SOAP_HEADER = "Header";
085: private static final String SOAP_BODY = "Body";
086:
087: private final String SOAP_NAMESPACE_URI;
088: private final SOAPVersion soapVersion;
089:
090: /*package*/StreamSOAPCodec(SOAPVersion soapVersion) {
091: SOAP_NAMESPACE_URI = soapVersion.nsUri;
092: this .soapVersion = soapVersion;
093: }
094:
095: // consider caching
096: // private final XMLStreamReader reader;
097:
098: // consider caching
099: // private final MutableXMLStreamBuffer buffer;
100:
101: public ContentType getStaticContentType(Packet packet) {
102: return getContentType(packet.soapAction);
103: }
104:
105: public ContentType encode(Packet packet, OutputStream out) {
106: if (packet.getMessage() != null) {
107: XMLStreamWriter writer = XMLStreamWriterFactory.create(out);
108: try {
109: packet.getMessage().writeTo(writer);
110: writer.flush();
111: } catch (XMLStreamException e) {
112: throw new WebServiceException(e);
113: }
114: XMLStreamWriterFactory.recycle(writer);
115: }
116: return getContentType(packet.soapAction);
117: }
118:
119: protected abstract ContentType getContentType(String soapAction);
120:
121: public ContentType encode(Packet packet, WritableByteChannel buffer) {
122: //TODO: not yet implemented
123: throw new UnsupportedOperationException();
124: }
125:
126: protected abstract List<String> getExpectedContentTypes();
127:
128: public void decode(InputStream in, String contentType, Packet packet)
129: throws IOException {
130: decode(in, contentType, packet, new AttachmentSetImpl());
131: }
132:
133: /*
134: * Checks against expected Content-Type headers that is handled by a codec
135: *
136: * @param ct the Content-Type of the request
137: * @param expected expected Content-Types for a codec
138: * @return true if the codec supports this Content-Type
139: * false otherwise
140: */
141: private static boolean isContentTypeSupported(String ct,
142: List<String> expected) {
143: for (String contentType : expected) {
144: if (ct.indexOf(contentType) != -1) {
145: return true;
146: }
147: }
148: return false;
149: }
150:
151: /**
152: * Decodes a message from {@link XMLStreamReader} that points to
153: * the beginning of a SOAP infoset.
154: *
155: * @param reader
156: * can point to the start document or the start element.
157: */
158: public final @NotNull
159: Message decode(@NotNull
160: XMLStreamReader reader) {
161: return decode(reader, new AttachmentSetImpl());
162: }
163:
164: /**
165: * Decodes a message from {@link XMLStreamReader} that points to
166: * the beginning of a SOAP infoset.
167: *
168: * @param reader
169: * can point to the start document or the start element.
170: * @param attachmentSet
171: * {@link StreamSOAPCodec} can take attachments parsed outside,
172: * so that this codec can be used as a part of a biggre codec
173: * (like MIME multipart codec.)
174: */
175: public final Message decode(XMLStreamReader reader, @NotNull
176: AttachmentSet attachmentSet) {
177:
178: // Move to soap:Envelope and verify
179: if (reader.getEventType() != XMLStreamConstants.START_ELEMENT)
180: XMLStreamReaderUtil.nextElementContent(reader);
181: XMLStreamReaderUtil.verifyReaderState(reader,
182: XMLStreamConstants.START_ELEMENT);
183: if (SOAP_ENVELOPE.equals(reader.getLocalName())
184: && !SOAP_NAMESPACE_URI.equals(reader.getNamespaceURI())) {
185: throw new VersionMismatchException(soapVersion,
186: SOAP_NAMESPACE_URI, reader.getNamespaceURI());
187: }
188: XMLStreamReaderUtil.verifyTag(reader, SOAP_NAMESPACE_URI,
189: SOAP_ENVELOPE);
190:
191: TagInfoset envelopeTag = new TagInfoset(reader);
192:
193: // Collect namespaces on soap:Envelope
194: Map<String, String> namespaces = new HashMap<String, String>();
195:
196: // Move to next element
197: XMLStreamReaderUtil.nextElementContent(reader);
198: XMLStreamReaderUtil.verifyReaderState(reader,
199: javax.xml.stream.XMLStreamConstants.START_ELEMENT);
200:
201: HeaderList headers = null;
202: TagInfoset headerTag = null;
203:
204: if (reader.getLocalName().equals(SOAP_HEADER)
205: && reader.getNamespaceURI().equals(SOAP_NAMESPACE_URI)) {
206: headerTag = new TagInfoset(reader);
207:
208: // Collect namespaces on soap:Header
209: for (int i = 0; i < reader.getNamespaceCount(); i++) {
210: namespaces.put(reader.getNamespacePrefix(i), reader
211: .getNamespaceURI(i));
212: }
213: // skip <soap:Header>
214: XMLStreamReaderUtil.nextElementContent(reader);
215:
216: // If SOAP header blocks are present (i.e. not <soap:Header/>)
217: if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
218: headers = new HeaderList();
219:
220: try {
221: // Cache SOAP header blocks
222: cacheHeaders(reader, namespaces, headers);
223: } catch (XMLStreamException e) {
224: // TODO need to throw more meaningful exception
225: throw new WebServiceException(e);
226: }
227: }
228:
229: // Move to soap:Body
230: XMLStreamReaderUtil.nextElementContent(reader);
231: }
232:
233: // Verify that <soap:Body> is present
234: XMLStreamReaderUtil.verifyTag(reader, SOAP_NAMESPACE_URI,
235: SOAP_BODY);
236: TagInfoset bodyTag = new TagInfoset(reader);
237:
238: XMLStreamReaderUtil.nextElementContent(reader);
239: return new StreamMessage(envelopeTag, headerTag, attachmentSet,
240: headers, bodyTag, reader, soapVersion);
241: // when there's no payload,
242: // it's tempting to use EmptyMessageImpl, but it doesn't presere the infoset
243: // of <envelope>,<header>, and <body>, so we need to stick to StreamMessage.
244: }
245:
246: public void decode(ReadableByteChannel in, String contentType,
247: Packet packet) {
248: throw new UnsupportedOperationException();
249: }
250:
251: public final StreamSOAPCodec copy() {
252: return this ;
253: }
254:
255: private XMLStreamBuffer cacheHeaders(XMLStreamReader reader,
256: Map<String, String> namespaces, HeaderList headers)
257: throws XMLStreamException {
258: MutableXMLStreamBuffer buffer = createXMLStreamBuffer();
259: StreamReaderBufferCreator creator = new StreamReaderBufferCreator();
260: creator.setXMLStreamBuffer(buffer);
261:
262: // Reader is positioned at the first header block
263: while (reader.getEventType() == javax.xml.stream.XMLStreamConstants.START_ELEMENT) {
264: Map<String, String> headerBlockNamespaces = namespaces;
265:
266: // Collect namespaces on SOAP header block
267: if (reader.getNamespaceCount() > 0) {
268: headerBlockNamespaces = new HashMap<String, String>(
269: namespaces);
270: for (int i = 0; i < reader.getNamespaceCount(); i++) {
271: headerBlockNamespaces.put(reader
272: .getNamespacePrefix(i), reader
273: .getNamespaceURI(i));
274: }
275: }
276:
277: // Mark
278: XMLStreamBuffer mark = new XMLStreamBufferMark(
279: headerBlockNamespaces, creator);
280: // Create Header
281: headers.add(createHeader(reader, mark));
282:
283: // Cache the header block
284: // After caching Reader will be positioned at next header block or
285: // the end of the </soap:header>
286: creator.createElementFragment(reader, false);
287: if (reader.getEventType() != XMLStreamConstants.START_ELEMENT
288: && reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
289: XMLStreamReaderUtil.nextElementContent(reader);
290: }
291: }
292:
293: return buffer;
294: }
295:
296: protected abstract StreamHeader createHeader(
297: XMLStreamReader reader, XMLStreamBuffer mark);
298:
299: private MutableXMLStreamBuffer createXMLStreamBuffer() {
300: // TODO: Decode should own one MutableXMLStreamBuffer for reuse
301: // since it is more efficient. ISSUE: possible issue with
302: // lifetime of information in the buffer if accessed beyond
303: // the pipe line.
304: return new MutableXMLStreamBuffer();
305: }
306:
307: public void decode(InputStream in, String contentType,
308: Packet packet, AttachmentSet att) throws IOException {
309: List<String> expectedContentTypes = getExpectedContentTypes();
310: if (contentType != null
311: && !isContentTypeSupported(contentType,
312: expectedContentTypes)) {
313: throw new UnsupportedMediaException(contentType,
314: expectedContentTypes);
315: }
316: String charset = new ContentTypeImpl(contentType).getCharSet();
317: XMLStreamReader reader = XMLStreamReaderFactory.create(null,
318: in, charset, true);
319: reader = new TidyXMLStreamReader(reader, in);
320: packet.setMessage(decode(reader, att));
321: }
322:
323: public void decode(ReadableByteChannel in, String contentType,
324: Packet response, AttachmentSet att) {
325: throw new UnsupportedOperationException();
326: }
327:
328: /**
329: * Creates a new {@link StreamSOAPCodec} instance.
330: */
331: public static StreamSOAPCodec create(SOAPVersion version) {
332: if (version == null)
333: // this decoder is for SOAP, not for XML/HTTP
334: throw new IllegalArgumentException();
335: switch (version) {
336: case SOAP_11:
337: return new StreamSOAP11Codec();
338: case SOAP_12:
339: return new StreamSOAP12Codec();
340: default:
341: throw new AssertionError();
342: }
343: }
344:
345: }
|