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'http<br /
><hr />Hotmail: Powerful Free email with security by M=\ni");
byte[] bytes = s.getBytes();
bytes = decodeQuotedPrintable(bytes);
System.out.println(new String(bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note you will need a util to convert a stream to bytes[]
Also note i raised this issue some time back so not sure how new it
is, or if it is version specific. Seems to be an issue with the apache
library's dealings with Hotmail content. I suppose if that library
changed between versions could be a culprit otherwise could be the
product of changes in Hotmail. Either way the decoder should be more
lenient.
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.