commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Scott Walters <walt...@edge.net>
Subject Re: looking for multipart-mime-handling library
Date Thu, 26 Jun 2003 12:23:51 GMT
I found the javamail jars to be good for this but the missing piece that
I could never find in it was something to determine the boundary string
when parsing an existing document.  After digging throughly into the
javamail source code, I determined that it doesn't seem to do this at
all.  Once I wrote this and figured out how to plug it my own code to
the framework, everything worked great.  The class I wrtote for this is
called MimeStringDataSource and is attached below. It extends
javax.activation.DataSource and an instance of it is passed into the
MimeMultipart ctor like so...

MimeMultipart mm = new MimeMultipart(new MimeStringDataSource(new
String(mimeDocument)));


Here's the MimeStringDataSource class.  The getContentType method is
invoked by the mail framework and determines the start and end of the
boundary string.  Then it creates an instance of
javax.mail.internet.ContentType and sets the boundary parameter on it to
the newly determined boundary string.  The string representation of the
ContentType object that contains the correct boundary string is returned
to the MimeMultipart ctor.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;
import javax.mail.internet.ContentType;

/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable
"typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class MimeStringDataSource implements DataSource {

	private String _src;
	public MimeStringDataSource(String src) {
		_src = src;
	}

	/**
	 * @see javax.activation.DataSource#getContentType()
	 */
	public String getContentType() {
		ContentType cType = new ContentType("multipart", "mixed", null);
		String boundary = null;
		int curpos = 0, endpos, delimSize = 2;
		String delimCompare = "--";
		while(boundary == null) {
			endpos = _src.indexOf("\r\n", curpos);
			if (endpos < 0) {
				endpos = _src.indexOf("\n", curpos);
				delimSize = 1;
				delimCompare = "-";
				if (endpos < 0)
					endpos = _src.indexOf("\r", curpos);
				if (endpos < 0)
					return "unknown";
			}
			if (_src.substring(curpos, curpos+2).equals("--"))
				boundary = _src.substring(curpos+2, endpos);
			else
				curpos = endpos + 2;
		}
		cType.setParameter("boundary", boundary);
		return cType.toString();
	}

	/**
	 * @see javax.activation.DataSource#getInputStream()
	 */
	public InputStream getInputStream() throws IOException {
		return new ByteArrayInputStream(_src.getBytes());
	}

	/**
	 * @see javax.activation.DataSource#getName()
	 */
	public String getName() {
		return new String("MimeStringDataSource");
	}

	/**
	 * @see javax.activation.DataSource#getOutputStream()
	 */
	public OutputStream getOutputStream() throws IOException {
		return new ByteArrayOutputStream();
	}

}


Bill Keese wrote:
> For encoding multipart mime messages, there's httpclient's
> MultipartPostMethod, but maybe that's tied to http (and also I'm not sure
> where the decoding counterpart is)
> 
> ----- Original Message ----- 
> From: "Simon Kitching" <simon@ecnetwork.co.nz>
> To: <commons-user@jakarta.apache.org>
> Sent: Thursday, June 26, 2003 11:41 AM
> Subject: looking for multipart-mime-handling library
> 
> 
> 
>>Hi All,
>>
>>I'm looking for code which can handle multipart mime messages, both
>>building them and decoding them.
>>
>>Currently I am using the javax.mail apis, with the implementation
>>downloadable from Sun. However, frankly, the javax.mail API stinks. And
>>the implementation isn't much better, as well as being closed-source. In
>>fact I've run into a showstopper bug in it which means I'm now looking
>>around for any alternative implementations. As a last resort I can
>>roll-my-own but I'd rather not.
>>
>>Is there any project (apache or otherwise) which provides mime handling
>>utilities? I guess there is some in james.apache.org, but it doesn't
>>seem to be separated out (not that I've looked deeply yet).
>>
>>If there isn't anything around, how would people feel about adding it to
>>codec? I think it is a good fit for that project, with the major
>>question being whether there is enough demand to make it worth doing...
>>
>>
>>Cheers,
>>
>>Simon
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 



Mime
View raw message