fixed sonar findings
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* wrapper classes around javax.mail package
|
* wrapper classes around javax.mail package written by Joern Muehlencord
|
||||||
* written by Joern Muehlencord
|
*
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
package de.muehlencord.shared.network.mail;
|
package de.muehlencord.shared.network.mail;
|
||||||
|
|
||||||
@ -21,13 +20,14 @@ import org.apache.log4j.Logger;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Util class to convert between javax.mail.Message and MailMessage objects
|
* Util class to convert between javax.mail.Message and MailMessage objects
|
||||||
|
*
|
||||||
* @author joern@muehlencord.de
|
* @author joern@muehlencord.de
|
||||||
*/
|
*/
|
||||||
public abstract class MailMessageUtils {
|
public abstract class MailMessageUtils {
|
||||||
|
|
||||||
/** the logging object */
|
/** the logging object */
|
||||||
private static final Logger logger = Logger.getLogger(MailMessageUtils.class.getName());
|
private static final Logger logger = Logger.getLogger(MailMessageUtils.class.getName());
|
||||||
|
|
||||||
/** content type of mutipart messages - e.g. multipart_alternative or _mixed */
|
/** content type of mutipart messages - e.g. multipart_alternative or _mixed */
|
||||||
private static final String CONTENTTYPE_MULTIPART = "multipart/";
|
private static final String CONTENTTYPE_MULTIPART = "multipart/";
|
||||||
/** content type of text messages - like text/plain or text/html */
|
/** content type of text messages - like text/plain or text/html */
|
||||||
@ -93,7 +93,6 @@ public abstract class MailMessageUtils {
|
|||||||
String bodyContentType = getContentType(bp.getContentType());
|
String bodyContentType = getContentType(bp.getContentType());
|
||||||
logger.debug("contentType = " + bodyContentType);
|
logger.debug("contentType = " + bodyContentType);
|
||||||
|
|
||||||
|
|
||||||
if (bodyContentType.contains(CONTENTTYPE_MULTIPART)) {
|
if (bodyContentType.contains(CONTENTTYPE_MULTIPART)) {
|
||||||
MimeMultipart bodyMultiPart = (MimeMultipart) bp.getContent();
|
MimeMultipart bodyMultiPart = (MimeMultipart) bp.getContent();
|
||||||
int bodyPartCount = bodyMultiPart.getCount();
|
int bodyPartCount = bodyMultiPart.getCount();
|
||||||
@ -145,15 +144,14 @@ public abstract class MailMessageUtils {
|
|||||||
throw new MailMessageException("Neither plain/text nor text/html content found. Unsupported message format");
|
throw new MailMessageException("Neither plain/text nor text/html content found. Unsupported message format");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (MailMessageException | IOException | MessagingException ex) {
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new MailMessageException("Error while converting multipart mime message. Reason: " + ex.getMessage(), ex);
|
throw new MailMessageException("Error while converting multipart mime message. Reason: " + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
} else if (contentType.equals(CONTENTTYPE_TEXT_PLAIN)) {
|
} else if (contentType.equals(CONTENTTYPE_TEXT_PLAIN)) {
|
||||||
// plain/text mimmessage
|
// plain/text mimmessage
|
||||||
try {
|
try {
|
||||||
returnMessage = new MailMessage(subject, mm.getContent().toString(), messageId);
|
returnMessage = new MailMessage(subject, mm.getContent().toString(), messageId);
|
||||||
} catch (Exception ex) {
|
} catch (IOException | MessagingException ex) {
|
||||||
throw new MailMessageException("Error while converting plain/text mime message. Reason: " + ex.getMessage(), ex);
|
throw new MailMessageException("Error while converting plain/text mime message. Reason: " + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,14 +162,14 @@ public abstract class MailMessageUtils {
|
|||||||
returnMessage = new MailMessage(subject, null, messageId);
|
returnMessage = new MailMessage(subject, null, messageId);
|
||||||
returnMessage.setContent(contentString, true);
|
returnMessage.setContent(contentString, true);
|
||||||
// returnMessage.setContent(mm.getContent().toString(), false);
|
// returnMessage.setContent(mm.getContent().toString(), false);
|
||||||
} catch (Exception ex) {
|
} catch (IOException | MessagingException ex) {
|
||||||
throw new MailMessageException("Error while converting plain/text mime message. Reason: " + ex.getMessage(), ex);
|
throw new MailMessageException("Error while converting plain/text mime message. Reason: " + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
} else if (contentType.contains(CONTENTTYPE_MESSAGE)) {
|
} else if (contentType.contains(CONTENTTYPE_MESSAGE)) {
|
||||||
try {
|
try {
|
||||||
Message nestedMessage = (Message) mm.getContent();
|
Message nestedMessage = (Message) mm.getContent();
|
||||||
returnMessage = getInstance(nestedMessage);
|
returnMessage = getInstance(nestedMessage);
|
||||||
} catch (Exception ex) {
|
} catch (MailMessageException | IOException | MessagingException ex) {
|
||||||
throw new MailMessageException("Error while converting nested message. Reason: " + ex.getMessage(), ex);
|
throw new MailMessageException("Error while converting nested message. Reason: " + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
} else if (contentType.contains(CONTENTTYPE_APPLICATION)) {
|
} else if (contentType.contains(CONTENTTYPE_APPLICATION)) {
|
||||||
@ -188,22 +186,20 @@ public abstract class MailMessageUtils {
|
|||||||
logger.debug(mm.getContent().getClass());
|
logger.debug(mm.getContent().getClass());
|
||||||
logger.debug(mm.getContent());
|
logger.debug(mm.getContent());
|
||||||
logger.error(hint);
|
logger.error(hint);
|
||||||
} catch (Exception ex) {
|
} catch (IOException | MessagingException ex) {
|
||||||
logger.error(hint);
|
logger.error(hint);
|
||||||
}
|
}
|
||||||
throw new MailMessageException(hint);
|
throw new MailMessageException(hint);
|
||||||
}
|
}
|
||||||
} else if (message instanceof Message) {
|
} else {
|
||||||
try {
|
try {
|
||||||
String content = message.getContent().toString();
|
String content = message.getContent().toString();
|
||||||
String subject = message.getSubject();
|
String subject = message.getSubject();
|
||||||
returnMessage = new MailMessage(subject, content);
|
returnMessage = new MailMessage(subject, content);
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (IOException | MessagingException ex) {
|
||||||
throw new MailMessageException("Error whilec converting email. Reason: " + ex.getMessage(), ex);
|
throw new MailMessageException("Error whilec converting email. Reason: " + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw new MailMessageException("Unsupported messagetype " + message.getClass().toString() + ". Cannot create message");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add missing parameters
|
// TODO add missing parameters
|
||||||
@ -216,35 +212,37 @@ public abstract class MailMessageUtils {
|
|||||||
returnMessage.setSender(message.getFrom());
|
returnMessage.setSender(message.getFrom());
|
||||||
returnMessage.setSentDate(message.getSentDate());
|
returnMessage.setSentDate(message.getSentDate());
|
||||||
returnMessage.setSize(message.getSize());
|
returnMessage.setSize(message.getSize());
|
||||||
} catch (Exception ex) {
|
} catch (MessagingException ex) {
|
||||||
throw new MailMessageException("Cannot read email. Reason: " + ex.getMessage(), ex);
|
throw new MailMessageException("Cannot read email. Reason: " + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
return returnMessage;
|
return returnMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns the content type based on the content-type header. The messad asures the contentype
|
* returns the content type based on the content-type header. The messad asures the contentype is selected correctly from the string, including trim and
|
||||||
* is selected correctly from the string, including trim and tolower case
|
* tolower case
|
||||||
|
*
|
||||||
* @param contentType the contenttype string read from email header
|
* @param contentType the contenttype string read from email header
|
||||||
* @return the content type of the email (small letters)
|
* @return the content type of the email (small letters)
|
||||||
*/
|
*/
|
||||||
private static String getContentType(String contentType) {
|
private static String getContentType(final String contentType) {
|
||||||
|
|
||||||
|
String returnValue = contentType;
|
||||||
if (contentType.contains(";")) {
|
if (contentType.contains(";")) {
|
||||||
contentType = contentType.substring(0, contentType.indexOf(';'));
|
returnValue = contentType.substring(0, contentType.indexOf(';'));
|
||||||
}
|
}
|
||||||
contentType = contentType.toLowerCase();
|
returnValue = returnValue.toLowerCase();
|
||||||
contentType = contentType.trim();
|
returnValue = returnValue.trim();
|
||||||
return contentType;
|
return returnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns the content of the given bodypart which needs to be a text based bodypart
|
* returns the content of the given bodypart which needs to be a text based bodypart The method tries to read the messages as it is. In case of an
|
||||||
* The method tries to read the messages as it is. In case of an unsupported encoding it reads the message with
|
* unsupported encoding it reads the message with the default encoding (accepting "ugly" characters)
|
||||||
* the default encoding (accepting "ugly" characters)
|
*
|
||||||
* @param bp the body part to read the content from
|
* @param bp the body part to read the content from
|
||||||
* @return the content of the body part
|
* @return the content of the body part
|
||||||
|
*
|
||||||
* @throws IOException if the content cannot be read
|
* @throws IOException if the content cannot be read
|
||||||
* @throws MessagingException if the message cannot be processed
|
* @throws MessagingException if the message cannot be processed
|
||||||
*/
|
*/
|
||||||
@ -256,9 +254,9 @@ public abstract class MailMessageUtils {
|
|||||||
} catch (java.io.UnsupportedEncodingException ex) {
|
} catch (java.io.UnsupportedEncodingException ex) {
|
||||||
logger.debug(ex.getMessage());
|
logger.debug(ex.getMessage());
|
||||||
} catch (java.io.IOException ioex) {
|
} catch (java.io.IOException ioex) {
|
||||||
throw new IOException("Cannot read content from bodypart.", ioex);
|
throw new IOException("Cannot read content from bodypart.", ioex);
|
||||||
} catch (MessagingException mex) {
|
} catch (MessagingException mex) {
|
||||||
throw new MessagingException("Cannot read content from bodypart.", mex);
|
throw new MessagingException("Cannot read content from bodypart.", mex);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (returnValue == null) {
|
if (returnValue == null) {
|
||||||
@ -270,20 +268,22 @@ public abstract class MailMessageUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns the content of the given body part by reading the stream directly. This ommits getting unsupported encoding exceptions, as the content
|
* returns the content of the given body part by reading the stream directly. This ommits getting unsupported encoding exceptions, as the content is read
|
||||||
* is read with the given encoding
|
* with the given encoding
|
||||||
|
*
|
||||||
* @param bp the bodypart to read the content from
|
* @param bp the bodypart to read the content from
|
||||||
* @param encoding the encoding to force
|
* @param encoding the encoding to force
|
||||||
* @return the content string read with the given encoding
|
* @return the content string read with the given encoding
|
||||||
|
*
|
||||||
* @throws IOException if the content cannot be read
|
* @throws IOException if the content cannot be read
|
||||||
* @throws MessagingException if the message cannot be processed
|
* @throws MessagingException if the message cannot be processed
|
||||||
*/
|
*/
|
||||||
private static String getContentByStream(BodyPart bp, String encoding) throws IOException, MessagingException {
|
private static String getContentByStream(BodyPart bp, String encoding) throws IOException, MessagingException {
|
||||||
String returnValue;
|
String returnValue;
|
||||||
Reader input = null;
|
Reader input = null;
|
||||||
StringWriter output = null;
|
StringWriter output = null;
|
||||||
try {
|
try {
|
||||||
input = new BufferedReader(new InputStreamReader(bp.getInputStream(), encoding));
|
input = new BufferedReader(new InputStreamReader(bp.getInputStream(), encoding));
|
||||||
output = new StringWriter();
|
output = new StringWriter();
|
||||||
char[] b = new char[4096];
|
char[] b = new char[4096];
|
||||||
while (input.read(b) != -1) {
|
while (input.read(b) != -1) {
|
||||||
@ -291,16 +291,20 @@ public abstract class MailMessageUtils {
|
|||||||
}
|
}
|
||||||
returnValue = output.toString();
|
returnValue = output.toString();
|
||||||
} catch (IOException ioex) {
|
} catch (IOException ioex) {
|
||||||
throw new IOException("Cannot read content from bodypart by reading content stream.", ioex);
|
throw new IOException("Cannot read content from bodypart by reading content stream.", ioex);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
output.close();
|
if (output != null) {
|
||||||
|
output.close();
|
||||||
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
logger.debug(ex.getMessage());
|
logger.debug(ex.getMessage());
|
||||||
logger.debug(StringUtil.getStackTraceString(ex));
|
logger.debug(StringUtil.getStackTraceString(ex));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
input.close();
|
if (input != null) {
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
logger.debug(ex.getMessage());
|
logger.debug(ex.getMessage());
|
||||||
logger.debug(StringUtil.getStackTraceString(ex));
|
logger.debug(StringUtil.getStackTraceString(ex));
|
||||||
@ -311,8 +315,10 @@ public abstract class MailMessageUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* returns the message id from the header of the given message
|
* returns the message id from the header of the given message
|
||||||
|
*
|
||||||
* @param message the message to return the message id from
|
* @param message the message to return the message id from
|
||||||
* @return either the message id or null if the message id cannot be read
|
* @return either the message id or null if the message id cannot be read
|
||||||
|
*
|
||||||
* @throws MessagingException if the message cannot be processed
|
* @throws MessagingException if the message cannot be processed
|
||||||
*/
|
*/
|
||||||
private static String getMessageId(Message message) throws MessagingException {
|
private static String getMessageId(Message message) throws MessagingException {
|
||||||
|
|||||||
Reference in New Issue
Block a user