You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Manjiri Karmarkar <ma...@googlemail.com> on 2012/01/22 07:08:33 UTC

HttpEntity and MimeMessage

Hello,

I am trying to create a "HttpEntity" from a MimeMessage
(javax.mail.internet.MimeMessage).
I was wondering if there is special implementation for the MimeMessage ?

The following code isnt working well for me because
mimeMessage.getInputStream only gives me the message body (and not the
headers ... i want to convert entire MimeMessage into HttpEntity):

BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
basicHttpEntity.setContent(mimeMessage.getInputStream());
request.setEntity(basicHttpEntity);

Note: for scalability reasons I want to avoid using the "ByteArrayEntity"
(I want to work with streams).

Thank You

Sincerely,
Manjiri

Re: HttpEntity and MimeMessage

Posted by Manjiri Karmarkar <ma...@googlemail.com>.
Hello Oleg,

thanks for your responses. I kind of went the custom implementation way.

Overwrote the "writeTo" method of BasicHttpEntity to first write the
MimeMessage headers and then the content.

BasicHttpEntity basicHttpEntity = new BasicHttpEntity() {
        // non-javadoc, see interface HttpEntity
        public void writeTo(final OutputStream outstream) throws
IOException {
            if (outstream == null) {
                throw new IllegalArgumentException("Output stream may not
be null");
            }

            /***** custom code start */

            // Else, the content is untouched, so we can just output it
            // First, write out the header
            Enumeration hdrLines;
            try {
                hdrLines = msg.getNonMatchingHeaderLines(null);
            } catch (MessagingException e) {
                throw new IllegalArgumentException(e);
            }
            LineOutputStream los = new LineOutputStream(outstream);
            while (hdrLines.hasMoreElements())
            los.writeln((String)hdrLines.nextElement());

            // The CRLF separator between header and content
            los.writeln();

            // Finally, the content.
            /***** custom code end */

            InputStream instream = getContent();
            int l;
            byte[] tmp = new byte[2048];
            while ((l = instream.read(tmp)) != -1) {
                outstream.write(tmp, 0, l);
            }
        }
      };
      basicHttpEntity.setContent(msg.getRawInputStream());
      request.setEntity(basicHttpEntity);


Thank You.

Sincerely,
Manjiri

On 22 January 2012 14:54, Oleg Kalnichevski <ol...@apache.org> wrote:

> On Sun, 2012-01-22 at 00:08 -0600, Manjiri Karmarkar wrote:
> > Hello,
> >
> > I am trying to create a "HttpEntity" from a MimeMessage
> > (javax.mail.internet.MimeMessage).
> > I was wondering if there is special implementation for the MimeMessage ?
> >
> > The following code isnt working well for me because
> > mimeMessage.getInputStream only gives me the message body (and not the
> > headers ... i want to convert entire MimeMessage into HttpEntity):
> >
> > BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
> > basicHttpEntity.setContent(mimeMessage.getInputStream());
> > request.setEntity(basicHttpEntity);
> >
> > Note: for scalability reasons I want to avoid using the "ByteArrayEntity"
> > (I want to work with streams).
> >
> > Thank You
> >
> > Sincerely,
> > Manjiri
>
> Manjiri
>
> If you can afford converting MimeMessage to another object you could use
> MultipartEntity shipped with HttpMime (a module of HttpClient). If you
> can't, your best option is to build a custom HttpEntity class along the
> lines of MultipartEntity
>
>
> http://hc.apache.org/httpcomponents-client-ga/httpmime/xref/org/apache/http/entity/mime/MultipartEntity.html
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: HttpEntity and MimeMessage

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Sun, 2012-01-22 at 00:08 -0600, Manjiri Karmarkar wrote:
> Hello,
> 
> I am trying to create a "HttpEntity" from a MimeMessage
> (javax.mail.internet.MimeMessage).
> I was wondering if there is special implementation for the MimeMessage ?
> 
> The following code isnt working well for me because
> mimeMessage.getInputStream only gives me the message body (and not the
> headers ... i want to convert entire MimeMessage into HttpEntity):
> 
> BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
> basicHttpEntity.setContent(mimeMessage.getInputStream());
> request.setEntity(basicHttpEntity);
> 
> Note: for scalability reasons I want to avoid using the "ByteArrayEntity"
> (I want to work with streams).
> 
> Thank You
> 
> Sincerely,
> Manjiri

Manjiri

If you can afford converting MimeMessage to another object you could use
MultipartEntity shipped with HttpMime (a module of HttpClient). If you
can't, your best option is to build a custom HttpEntity class along the
lines of MultipartEntity

http://hc.apache.org/httpcomponents-client-ga/httpmime/xref/org/apache/http/entity/mime/MultipartEntity.html

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org