001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * @(#)MimePartDataSource.java 1.9 02/03/27
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: package com.sun.xml.messaging.saaj.packaging.mime.internet;
061:
062: import java.io.*;
063: import java.net.UnknownServiceException;
064:
065: import javax.activation.DataSource;
066:
067: import com.sun.xml.messaging.saaj.packaging.mime.MessagingException;
068:
069: /**
070: * A utility class that implements a DataSource out of
071: * a MimeBodyPart. This class is primarily meant for service providers.
072: *
073: * @author John Mani
074: */
075:
076: public final class MimePartDataSource implements DataSource {
077: private final MimeBodyPart part;
078:
079: /**
080: * Constructor, that constructs a DataSource from a MimeBodyPart.
081: */
082: public MimePartDataSource(MimeBodyPart part) {
083: this .part = part;
084: }
085:
086: /**
087: * Returns an input stream from this MimeBodyPart. <p>
088: *
089: * This method applies the appropriate transfer-decoding, based
090: * on the Content-Transfer-Encoding attribute of this MimeBodyPart.
091: * Thus the returned input stream is a decoded stream of bytes.<p>
092: *
093: * This implementation obtains the raw content from the MimeBodyPart
094: * using the <code>getContentStream()</code> method and decodes
095: * it using the <code>MimeUtility.decode()</code> method.
096: *
097: * @return decoded input stream
098: */
099: public InputStream getInputStream() throws IOException {
100:
101: try {
102: InputStream is = part.getContentStream();
103:
104: String encoding = part.getEncoding();
105: if (encoding != null)
106: return MimeUtility.decode(is, encoding);
107: else
108: return is;
109: } catch (MessagingException mex) {
110: throw new IOException(mex.getMessage());
111: }
112: }
113:
114: /**
115: * DataSource method to return an output stream. <p>
116: *
117: * This implementation throws the UnknownServiceException.
118: */
119: public OutputStream getOutputStream() throws IOException {
120: throw new UnknownServiceException();
121: }
122:
123: /**
124: * Returns the content-type of this DataSource. <p>
125: *
126: * This implementation just invokes the <code>getContentType</code>
127: * method on the MimeBodyPart.
128: */
129: public String getContentType() {
130: return part.getContentType();
131: }
132:
133: /**
134: * DataSource method to return a name. <p>
135: *
136: * This implementation just returns an empty string.
137: */
138: public String getName() {
139: try {
140: return part.getFileName();
141: } catch (MessagingException mex) {
142: return "";
143: }
144: }
145: }
|