FileDocCategorySizeDatePackage
MimePartDataSource.javaAPI DocJavaMail 1.4.36599Tue Nov 17 10:38:12 GMT 2009javax.mail.internet

MimePartDataSource

public class MimePartDataSource extends Object implements MessageAware, DataSource
A utility class that implements a DataSource out of a MimePart. This class is primarily meant for service providers.
see
javax.mail.internet.MimePart
see
javax.activation.DataSource
author
John Mani

Fields Summary
protected MimePart
part
The MimePart that provides the data for this DataSource.
private MessageContext
context
private static boolean
ignoreMultipartEncoding
Constructors Summary
public MimePartDataSource(MimePart part)
Constructor, that constructs a DataSource from a MimePart.


                 
       
	this.part = part;
    
Methods Summary
public java.lang.StringgetContentType()
Returns the content-type of this DataSource.

This implementation just invokes the getContentType method on the MimePart.

	try {
	    return part.getContentType();
	} catch (MessagingException mex) {
	    // would like to be able to reflect the exception to the
	    // application, but since we can't do that we return a
	    // generic "unknown" value here and hope for another
	    // exception later.
	    return "application/octet-stream";
	}
    
public java.io.InputStreamgetInputStream()
Returns an input stream from this MimePart.

This method applies the appropriate transfer-decoding, based on the Content-Transfer-Encoding attribute of this MimePart. Thus the returned input stream is a decoded stream of bytes.

This implementation obtains the raw content from the Part using the getContentStream() method and decodes it using the MimeUtility.decode() method.

see
javax.mail.internet.MimeMessage#getContentStream
see
javax.mail.internet.MimeBodyPart#getContentStream
see
javax.mail.internet.MimeUtility#decode
return
decoded input stream

	InputStream is;

	try {
	    if (part instanceof MimeBodyPart)
		is = ((MimeBodyPart)part).getContentStream();
	    else if (part instanceof MimeMessage)
		is = ((MimeMessage)part).getContentStream();
	    else
		throw new MessagingException("Unknown part");
	    
	    String encoding = restrictEncoding(part.getEncoding(), part);
	    if (encoding != null)
		return MimeUtility.decode(is, encoding);
	    else
		return is;
	} catch (MessagingException mex) {
	    throw new IOException(mex.getMessage());
	}
    
public synchronized javax.mail.MessageContextgetMessageContext()
Return the MessageContext for the current part.

since
JavaMail 1.1

	if (context == null)
	    context = new MessageContext(part);
	return context;
    
public java.lang.StringgetName()
DataSource method to return a name.

This implementation just returns an empty string.

	try {
	    if (part instanceof MimeBodyPart)
		return ((MimeBodyPart)part).getFileName();
	} catch (MessagingException mex) {
	    // ignore it
	}
	return "";
    
public java.io.OutputStreamgetOutputStream()
DataSource method to return an output stream.

This implementation throws the UnknownServiceException.

	throw new UnknownServiceException("Writing not supported");
    
private static java.lang.StringrestrictEncoding(java.lang.String encoding, javax.mail.internet.MimePart part)
Restrict the encoding to values allowed for the Content-Type of the specified MimePart. Returns either the original encoding or null.

	if (!ignoreMultipartEncoding || encoding == null)
	    return encoding;

	if (encoding.equalsIgnoreCase("7bit") ||
		encoding.equalsIgnoreCase("8bit") ||
		encoding.equalsIgnoreCase("binary"))
	    return encoding;	// these encodings are always valid

	String type = part.getContentType();
	if (type == null)
	    return encoding;

	try {
	    /*
	     * multipart and message types aren't allowed to have
	     * encodings except for the three mentioned above.
	     * If it's one of these types, ignore the encoding.
	     */
	    ContentType cType = new ContentType(type);
	    if (cType.match("multipart/*") || cType.match("message/*"))
		return null;
	} catch (ParseException pex) {
	    // ignore it
	}
	return encoding;