[appengine-java] Re: GAE 1.3.1 java.io.IOException: Truncated quoted printable data when processing a Hotmail email

2010-03-23 Thread moca
Feel free to post back if you make any improvements. Code follows:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

public class MimeUtil {
private static final Logger LOGGER = Logger.getLogger("MimeUtil");

public static MimeMessage createMimeMessage(HttpServletRequest
request)
throws MessagingException, IOException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
MimeMessage message = new MimeMessage(session,
request.getInputStream());
return message;
}

// 
http://commons.apache.org/codec/xref/org/apache/commons/codec/net/QuotedPrintableCodec.html
private static final byte ESCAPE_CHAR = '=';

public static String decodeQuotedPrintable(byte[] bytes, String
charset)
throws IOException {
return new String(decodeQuotedPrintable(bytes), charset);
}

public static byte[] decodeQuotedPrintable(byte[] bytes) throws
IOException {
if (bytes == null) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b == ESCAPE_CHAR) {
try {
if (bytes[i + 1] == 10) {
// FIX skip newline, lenient
++i;
} else {
int u = digit16(bytes[++i]);
int l = digit16(bytes[++i]);
out.write((char) ((u << 4) + l));
}
} catch (Exception e) {
throw new IOException("Invalid quoted-printable
encoding", e);
}
} else {
out.write(b);
}
}
return out.toByteArray();
}

public static int digit16(byte b) throws IOException {
int i = Character.digit(b, 16);
if (i == -1) {
throw new IOException("Invalid encoding: not a valid digit
(radix 16): "
+ b);
}
return i;
}

public static Object getContent(MimeMessage message) throws
Exception {
String charset = contentType2Charset(message.getContentType(),
null);
Object content;
try {
content = message.getContent();
} catch (Exception e) {
try {
byte[] out =
IOUtil.getBytes(message.getRawInputStream());
out = decodeQuotedPrintable(out);
if (charset != null) {
content = new String(out, charset);
} else {
content = new String(out);
}
} catch (Exception e1) {
throw e;
}
}
return content;
}

public static Object getContent(MimeBodyPart part) throws
Exception {
String charset = contentType2Charset(part.getContentType(),
null);
Object content;
try {
content = part.getContent();
} catch (Exception e) {
try {
byte[] out =
IOUtil.getBytes(part.getRawInputStream());
out = decodeQuotedPrintable(out);
if (charset != null) {
content = new String(out, charset);
} else {
content = new String(out);
}
} catch (Exception e1) {
throw e;
}
}
return content;
}

public static String contentType2Charset(String contentType,
String defaultCharset) {
String charset = defaultCharset;
if (contentType.indexOf("charset=") != -1) {
String[] split = contentType.split("charset=");
if (split.length > 1) {
charset = split[1];
if (charset.indexOf(';') >= 0) {
charset = charset.substring(0,
charset.indexOf(';'));
}
charset = charset.replaceAll("\"", "");
charset = charset.trim();
}
}
return charset;
}

public static void main(String[] args) {
try {
System.out.println(Character.digit('0', 16));
System.out.println(Character.digit('9', 16));
System.out.println(Character.digit('a', 16));
System.out.println(Character.digit('f', 16));
// digit16((byte)10);

String s = new String("margin:0px=3B href=3D'httpHotmail: Powerful Free email with security by M=\ni");
byte[] bytes = s.getBytes();
bytes = decodeQuotedPrintable(bytes);

[appengine-java] Re: GAE 1.3.1 java.io.IOException: Truncated quoted printable data when processing a Hotmail email

2010-03-23 Thread Lucian Baciu
That would be great if you could post your workaround!

Thanks

On Mar 23, 6:13 pm, moca  wrote:
> I have a fix for this that i will share later this evening, but i also
> ran into this issue. It would be good to have this fixed.
>
> My fix basically entails parsing the native stream when i encounter
> this error. There is an apache library which is doing the parsing. I
> made a simple alteration to a piece of an apache quoted printable
> parser to make it work and extracted this into a utility. Looks
> something like
>
> getContent(msg) {
>  try {
>    return msg.getContent();
>  } catch (e) {
>    return FixedQuotedPrintableParser.parse(msg.getNativeStream)
>  }
>
> }
>
> I'll post the real code when i get a chance.
>
> On Mar 21, 3:48 pm, Lucian Baciu  wrote:
>
>
>
> > The mail processing API doesn't seem to work when processing a Hotmail
> > email message. I get:
>
> > java.io.IOException: Truncated quoted printable data
> >         at
> > org.apache.geronimo.mail.util.QuotedPrintableEncoder.decodeNonspaceChar(Quo 
> > tedPrintableEncoder.java:
> > 597)
> >         at
> > org.apache.geronimo.mail.util.QuotedPrintableEncoder.decode(QuotedPrintable 
> > Encoder.java:
> > 584)
> >         at
> > org.apache.geronimo.mail.util.QuotedPrintableDecoderStream.read(QuotedPrint 
> > ableDecoderStream.java:
> > 80)
> >         at
> > org.apache.geronimo.mail.handlers.TextHandler.getContent(TextHandler.java:
> > 107)
> >         at javax.activation.DataSourceDataContentHandler.getContent(Unknown
> > Source)
> >         at javax.activation.DataHandler.getContent(Unknown Source)
> >         at 
> > javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:392)
>
> > I am using the code that should work from 
> > here:http://groups.google.com/group/google-appengine-java/browse_thread/th...
>
> > The code fails on this line: Object bp = p.getContent();
>
> > Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



[appengine-java] Re: GAE 1.3.1 java.io.IOException: Truncated quoted printable data when processing a Hotmail email

2010-03-23 Thread moca
I have a fix for this that i will share later this evening, but i also
ran into this issue. It would be good to have this fixed.

My fix basically entails parsing the native stream when i encounter
this error. There is an apache library which is doing the parsing. I
made a simple alteration to a piece of an apache quoted printable
parser to make it work and extracted this into a utility. Looks
something like

getContent(msg) {
 try {
   return msg.getContent();
 } catch (e) {
   return FixedQuotedPrintableParser.parse(msg.getNativeStream)
 }
}

I'll post the real code when i get a chance.

On Mar 21, 3:48 pm, Lucian Baciu  wrote:
> The mail processing API doesn't seem to work when processing a Hotmail
> email message. I get:
>
> java.io.IOException: Truncated quoted printable data
>         at
> org.apache.geronimo.mail.util.QuotedPrintableEncoder.decodeNonspaceChar(Quo 
> tedPrintableEncoder.java:
> 597)
>         at
> org.apache.geronimo.mail.util.QuotedPrintableEncoder.decode(QuotedPrintable 
> Encoder.java:
> 584)
>         at
> org.apache.geronimo.mail.util.QuotedPrintableDecoderStream.read(QuotedPrint 
> ableDecoderStream.java:
> 80)
>         at
> org.apache.geronimo.mail.handlers.TextHandler.getContent(TextHandler.java:
> 107)
>         at javax.activation.DataSourceDataContentHandler.getContent(Unknown
> Source)
>         at javax.activation.DataHandler.getContent(Unknown Source)
>         at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:392)
>
> I am using the code that should work from 
> here:http://groups.google.com/group/google-appengine-java/browse_thread/th...
>
> The code fails on this line: Object bp = p.getContent();
>
> Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



[appengine-java] Re: GAE 1.3.1 java.io.IOException: Truncated quoted printable data when processing a Hotmail email

2010-03-22 Thread Peter Ondruska
Something has gotten broken with recent release because my code used
to work fine with Hotmail/Yahoo sent messages. Looks like I have to go
through release notes for 1.3.1 and fix processing incoming emails.. :-
(

On Mar 21, 11:48 pm, Lucian Baciu  wrote:
> The mail processing API doesn't seem to work when processing a Hotmail
> email message. I get:
>
> java.io.IOException: Truncated quoted printable data
>         at
> org.apache.geronimo.mail.util.QuotedPrintableEncoder.decodeNonspaceChar(Quo 
> tedPrintableEncoder.java:
> 597)
>         at
> org.apache.geronimo.mail.util.QuotedPrintableEncoder.decode(QuotedPrintable 
> Encoder.java:
> 584)
>         at
> org.apache.geronimo.mail.util.QuotedPrintableDecoderStream.read(QuotedPrint 
> ableDecoderStream.java:
> 80)
>         at
> org.apache.geronimo.mail.handlers.TextHandler.getContent(TextHandler.java:
> 107)
>         at javax.activation.DataSourceDataContentHandler.getContent(Unknown
> Source)
>         at javax.activation.DataHandler.getContent(Unknown Source)
>         at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:392)
>
> I am using the code that should work from 
> here:http://groups.google.com/group/google-appengine-java/browse_thread/th...
>
> The code fails on this line: Object bp = p.getContent();
>
> Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.