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.istack.Nullable;
041: import com.sun.xml.ws.api.message.Attachment;
042: import com.sun.xml.ws.developer.StreamingAttachmentFeature;
043: import com.sun.xml.ws.developer.StreamingDataHandler;
044: import com.sun.xml.ws.util.ByteArrayBuffer;
045: import com.sun.xml.ws.util.ByteArrayDataSource;
046: import org.jvnet.mimepull.MIMEMessage;
047: import org.jvnet.mimepull.MIMEPart;
048:
049: import javax.activation.DataHandler;
050: import javax.xml.soap.SOAPException;
051: import javax.xml.soap.SOAPMessage;
052: import javax.xml.transform.Source;
053: import javax.xml.transform.stream.StreamSource;
054: import javax.xml.ws.WebServiceException;
055: import java.io.ByteArrayInputStream;
056: import java.io.IOException;
057: import java.io.InputStream;
058: import java.io.OutputStream;
059: import java.util.HashMap;
060: import java.util.List;
061: import java.util.Map;
062:
063: /**
064: * Parses Mime multipart message into primary part and attachment parts. It
065: * parses the stream lazily as and when required.
066: *
067: * @author Vivek Pandey
068: * @author Jitendra Kotamraju
069: */
070: public final class MimeMultipartParser {
071:
072: private final String start;
073: private final MIMEMessage message;
074: private Attachment root;
075:
076: // Attachments without root part
077: private final Map<String, Attachment> attachments = new HashMap<String, Attachment>();
078:
079: private boolean gotAll;
080:
081: public MimeMultipartParser(InputStream in, String contentType,
082: StreamingAttachmentFeature feature) {
083: ContentType ct = new ContentType(contentType);
084: String boundary = ct.getParameter("boundary");
085: if (boundary == null || boundary.equals("")) {
086: throw new WebServiceException(
087: "MIME boundary parameter not found" + contentType);
088: }
089: message = (feature != null) ? new MIMEMessage(in, boundary,
090: feature.getConfig()) : new MIMEMessage(in, boundary);
091: // Strip <...> from root part's Content-ID
092: String st = ct.getParameter("start");
093: if (st != null && st.length() > 2 && st.charAt(0) == '<'
094: && st.charAt(st.length() - 1) == '>') {
095: st = st.substring(1, st.length() - 1);
096: }
097: start = st;
098: }
099:
100: /**
101: * Parses the stream and returns the root part. If start parameter is
102: * present in Content-Type, it is used to determine the root part, otherwise
103: * root part is the first part.
104: *
105: * @return StreamAttachment for root part
106: * null if root part cannot be found
107: *
108: */
109: public @Nullable
110: Attachment getRootPart() {
111: if (root == null) {
112: root = new PartAttachment((start != null) ? message
113: .getPart(start) : message.getPart(0));
114: }
115: return root;
116: }
117:
118: /**
119: * Parses the entire stream and returns all MIME parts except root MIME part.
120: *
121: * @return Map<String, StreamAttachment> for all attachment parts
122: */
123: public @NotNull
124: Map<String, Attachment> getAttachmentParts() {
125: if (!gotAll) {
126: MIMEPart rootPart = (start != null) ? message
127: .getPart(start) : message.getPart(0);
128: List<MIMEPart> parts = message.getAttachments();
129: for (MIMEPart part : parts) {
130: if (part != rootPart) {
131: PartAttachment attach = new PartAttachment(part);
132: attachments.put(attach.getContentId(), attach);
133: }
134: }
135: gotAll = true;
136: }
137: return attachments;
138: }
139:
140: /**
141: * This method can be called to get a matching MIME attachment part for the
142: * given contentId. It parses the stream until it finds a matching part.
143: *
144: * @return StreamAttachment attachment for contentId
145: * null if there is no attachment for contentId
146: */
147: public @Nullable
148: Attachment getAttachmentPart(String contentId) throws IOException {
149: //first see if this attachment is already parsed, if so return it
150: Attachment attach = attachments.get(contentId);
151: if (attach == null) {
152: MIMEPart part = message.getPart(contentId);
153: attach = new PartAttachment(part);
154: attachments.put(contentId, attach);
155: }
156: return attach;
157: }
158:
159: static class PartAttachment implements Attachment {
160:
161: final MIMEPart part;
162: byte[] buf;
163:
164: PartAttachment(MIMEPart part) {
165: this .part = part;
166: }
167:
168: public @NotNull
169: String getContentId() {
170: return part.getContentId();
171: }
172:
173: public @NotNull
174: String getContentType() {
175: return part.getContentType();
176: }
177:
178: public byte[] asByteArray() {
179: if (buf == null) {
180: ByteArrayBuffer baf = new ByteArrayBuffer();
181: try {
182: baf.write(part.readOnce());
183: } catch (IOException ioe) {
184: throw new WebServiceException(ioe);
185: }
186: buf = baf.toByteArray();
187: }
188: return buf;
189: }
190:
191: public DataHandler asDataHandler() {
192: return (buf != null) ? new DataHandler(
193: new ByteArrayDataSource(buf, getContentType()))
194: : new StreamingDataHandler(part);
195: }
196:
197: public Source asSource() {
198: return (buf != null) ? new StreamSource(
199: new ByteArrayInputStream(buf)) : new StreamSource(
200: part.readOnce());
201: }
202:
203: public InputStream asInputStream() {
204: return (buf != null) ? new ByteArrayInputStream(buf) : part
205: .readOnce();
206: }
207:
208: public void writeTo(OutputStream os) throws IOException {
209: if (buf != null) {
210: os.write(buf);
211: } else {
212: InputStream in = part.readOnce();
213: byte[] temp = new byte[8192];
214: int len;
215: while ((len = in.read(temp)) != -1) {
216: os.write(temp, 0, len);
217: }
218: }
219: }
220:
221: public void writeTo(SOAPMessage saaj) throws SOAPException {
222: saaj.createAttachmentPart().setDataHandler(asDataHandler());
223: }
224: }
225:
226: }
|