001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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
018 package javax.mail.internet;
019
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.io.OutputStream;
023 import java.net.UnknownServiceException;
024 import javax.activation.DataSource;
025 import javax.mail.MessageAware;
026 import javax.mail.MessageContext;
027 import javax.mail.MessagingException;
028
029 /**
030 * @version $Rev: 149344 $ $Date: 2005-01-31 17:07:44 -0800 (Mon, 31 Jan 2005) $
031 */
032 public class MimePartDataSource implements DataSource, MessageAware {
033 private final MimePart part;
034
035 public MimePartDataSource(MimePart part) {
036 this.part = part;
037 }
038
039 public InputStream getInputStream() throws IOException {
040 try {
041 InputStream stream;
042 if (part instanceof MimeMessage) {
043 stream = ((MimeMessage) part).getContentStream();
044 } else if (part instanceof MimeBodyPart) {
045 stream = ((MimeBodyPart) part).getContentStream();
046 } else {
047 throw new MessagingException("Unknown part");
048 }
049 String encoding = part.getEncoding();
050 return encoding == null ? stream : MimeUtility.decode(stream, encoding);
051 } catch (MessagingException e) {
052 throw (IOException) new IOException(e.getMessage()).initCause(e);
053 }
054 }
055
056 public OutputStream getOutputStream() throws IOException {
057 throw new UnknownServiceException();
058 }
059
060 public String getContentType() {
061 try {
062 return part.getContentType();
063 } catch (MessagingException e) {
064 return null;
065 }
066 }
067
068 public String getName() {
069 return "";
070 }
071
072 public synchronized MessageContext getMessageContext() {
073 return new MessageContext(part);
074 }
075 }