Skip to content

Commit

Permalink
Fixed MIME-Type bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasmiosczka committed Mar 12, 2019
1 parent 8df498f commit 91e2855
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeUtility;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.Base64;
Expand Down Expand Up @@ -61,22 +63,15 @@ private static KDM getKdmFromInputStream(IUpdateProgress iUpdateProgress, InputS
}

private static String decode(String s) {
if (s == null)
if(s == null) {
return null;
}
try {
return MimeUtility.decodeText(s);
} catch (UnsupportedEncodingException e) {
return null;
if (s.matches("^=\\?.*\\?.*\\?.*\\?=$")) {
s = s.substring(2, s.length() - 2);
String[] strings = s.split("\\?"); //[0]=charset, [1]=cypher, [2]=text
switch (strings[1]) {
case "Q": //Type is quoted printable
return strings[2];
case "B": //Type is base64
return new String(Base64.getDecoder().decode(strings[2]));
default: //Type is unknown
return strings[2];
}
} else {
return s;
}

}

private static Collection<KDM> handleMessages(Message[] messages, IUpdateProgress iUpdateProgress) throws IOException, JDOMException, ParseException, MessagingException {
Expand All @@ -90,6 +85,9 @@ private static Collection<KDM> handleMessages(Message[] messages, IUpdateProgres
BodyPart bodyPart = multipart.getBodyPart(part);
String fileName = decode(bodyPart.getFileName());
if (fileName != null && (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) || !StringHelper.isBlank(fileName))) {
System.out.println(fileName);
System.out.println(bodyPart.getFileName());
System.out.println();
if (fileName.endsWith(".zip"))
kdms.addAll(unzip(iUpdateProgress, bodyPart.getInputStream()));
else if (fileName.endsWith(".xml"))
Expand Down

0 comments on commit 91e2855

Please sign in to comment.