1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail.internet;
  6. import javax.mail.*;
  7. import javax.activation.*;
  8. import java.io.*;
  9. import java.net.UnknownServiceException;
  10. /**
  11. * A utility class that implements a DataSource out of
  12. * a MimePart. This class is primarily meant for service providers.
  13. *
  14. * @see javax.mail.internet.MimePart
  15. * @see javax.activation.DataSource
  16. * @author John Mani
  17. */
  18. public class MimePartDataSource implements DataSource, MessageAware {
  19. private MimePart part;
  20. private MessageContext context;
  21. /**
  22. * Constructor, that constructs a DataSource from a MimePart.
  23. */
  24. public MimePartDataSource(MimePart part) {
  25. this.part = part;
  26. }
  27. /**
  28. * Returns an input stream from this MimePart. <p>
  29. *
  30. * This method applies the appropriate transfer-decoding, based
  31. * on the Content-Transfer-Encoding attribute of this MimePart.
  32. * Thus the returned input stream is a decoded stream of bytes.<p>
  33. *
  34. * This implementation obtains the raw content from the Part
  35. * using the <code>getContentStream()</code> method and decodes
  36. * it using the <code>MimeUtility.decode()</code> method.
  37. *
  38. * @see javax.mail.internet.MimeMessage#getContentStream
  39. * @see javax.mail.internet.MimeBodyPart#getContentStream
  40. * @see javax.mail.internet.MimeUtility#decode
  41. * @return decoded input stream
  42. */
  43. public InputStream getInputStream() throws IOException {
  44. InputStream is;
  45. try {
  46. if (part instanceof MimeBodyPart)
  47. is = ((MimeBodyPart)part).getContentStream();
  48. else if (part instanceof MimeMessage)
  49. is = ((MimeMessage)part).getContentStream();
  50. else
  51. throw new MessagingException("Unknown part");
  52. String encoding = part.getEncoding();
  53. if (encoding != null)
  54. return MimeUtility.decode(is, encoding);
  55. else
  56. return is;
  57. } catch (MessagingException mex) {
  58. throw new IOException(mex.getMessage());
  59. }
  60. }
  61. /**
  62. * DataSource method to return an output stream. <p>
  63. *
  64. * This implementation throws the UnknownServiceException.
  65. */
  66. public OutputStream getOutputStream() throws IOException {
  67. throw new UnknownServiceException();
  68. }
  69. /**
  70. * Returns the content-type of this DataSource. <p>
  71. *
  72. * This implementation just invokes the <code>getContentType</code>
  73. * method on the MimePart.
  74. */
  75. public String getContentType() {
  76. try {
  77. return part.getContentType();
  78. } catch (MessagingException mex) {
  79. return null;
  80. }
  81. }
  82. /**
  83. * DataSource method to return a name. <p>
  84. *
  85. * This implementation just returns an empty string.
  86. */
  87. public String getName() {
  88. try {
  89. if (part instanceof MimeBodyPart)
  90. return ((MimeBodyPart)part).getFileName();
  91. } catch (MessagingException mex) {
  92. // ignore it
  93. }
  94. return "";
  95. }
  96. /**
  97. * Return the <code>MessageContext</code> for the current part.
  98. * @since JavaMail 1.1
  99. */
  100. public synchronized MessageContext getMessageContext() {
  101. if (context == null)
  102. context = new MessageContext(part);
  103. return context;
  104. }
  105. }