fixed sonar findings

This commit is contained in:
jomu
2013-11-20 21:06:07 +00:00
parent 56280ba6be
commit b56501e70d

View File

@ -1,6 +1,5 @@
/**
* wrapper classes around javax.mail package
* written by Joern Muehlencord
* wrapper classes around javax.mail package written by Joern Muehlencord
*
*/
package de.muehlencord.shared.network.mail;
@ -21,6 +20,7 @@ import org.apache.log4j.Logger;
/**
* Util class to convert between javax.mail.Message and MailMessage objects
*
* @author joern@muehlencord.de
*/
public abstract class MailMessageUtils {
@ -93,7 +93,6 @@ public abstract class MailMessageUtils {
String bodyContentType = getContentType(bp.getContentType());
logger.debug("contentType = " + bodyContentType);
if (bodyContentType.contains(CONTENTTYPE_MULTIPART)) {
MimeMultipart bodyMultiPart = (MimeMultipart) bp.getContent();
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");
}
} catch (Exception ex) {
} catch (MailMessageException | IOException | MessagingException ex) {
throw new MailMessageException("Error while converting multipart mime message. Reason: " + ex.getMessage(), ex);
}
} else if (contentType.equals(CONTENTTYPE_TEXT_PLAIN)) {
// plain/text mimmessage
try {
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);
}
@ -164,14 +162,14 @@ public abstract class MailMessageUtils {
returnMessage = new MailMessage(subject, null, messageId);
returnMessage.setContent(contentString, true);
// 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);
}
} else if (contentType.contains(CONTENTTYPE_MESSAGE)) {
try {
Message nestedMessage = (Message) mm.getContent();
returnMessage = getInstance(nestedMessage);
} catch (Exception ex) {
} catch (MailMessageException | IOException | MessagingException ex) {
throw new MailMessageException("Error while converting nested message. Reason: " + ex.getMessage(), ex);
}
} else if (contentType.contains(CONTENTTYPE_APPLICATION)) {
@ -188,22 +186,20 @@ public abstract class MailMessageUtils {
logger.debug(mm.getContent().getClass());
logger.debug(mm.getContent());
logger.error(hint);
} catch (Exception ex) {
} catch (IOException | MessagingException ex) {
logger.error(hint);
}
throw new MailMessageException(hint);
}
} else if (message instanceof Message) {
} else {
try {
String content = message.getContent().toString();
String subject = message.getSubject();
returnMessage = new MailMessage(subject, content);
} catch (Exception ex) {
} catch (IOException | MessagingException 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
@ -216,35 +212,37 @@ public abstract class MailMessageUtils {
returnMessage.setSender(message.getFrom());
returnMessage.setSentDate(message.getSentDate());
returnMessage.setSize(message.getSize());
} catch (Exception ex) {
} catch (MessagingException ex) {
throw new MailMessageException("Cannot read email. Reason: " + ex.getMessage(), ex);
}
return returnMessage;
}
/**
* returns the content type based on the content-type header. The messad asures the contentype
* is selected correctly from the string, including trim and tolower case
* returns the content type based on the content-type header. The messad asures the contentype is selected correctly from the string, including trim and
* tolower case
*
* @param contentType the contenttype string read from email header
* @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(";")) {
contentType = contentType.substring(0, contentType.indexOf(';'));
returnValue = contentType.substring(0, contentType.indexOf(';'));
}
contentType = contentType.toLowerCase();
contentType = contentType.trim();
return contentType;
returnValue = returnValue.toLowerCase();
returnValue = returnValue.trim();
return returnValue;
}
/**
* 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 unsupported encoding it reads the message with
* the default encoding (accepting "ugly" characters)
* 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
* unsupported encoding it reads the message with the default encoding (accepting "ugly" characters)
*
* @param bp the body part to read the content from
* @return the content of the body part
*
* @throws IOException if the content cannot be read
* @throws MessagingException if the message cannot be processed
*/
@ -270,11 +268,13 @@ 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
* is read with the given encoding
* returns the content of the given body part by reading the stream directly. This ommits getting unsupported encoding exceptions, as the content is read
* with the given encoding
*
* @param bp the bodypart to read the content from
* @param encoding the encoding to force
* @return the content string read with the given encoding
*
* @throws IOException if the content cannot be read
* @throws MessagingException if the message cannot be processed
*/
@ -294,13 +294,17 @@ public abstract class MailMessageUtils {
throw new IOException("Cannot read content from bodypart by reading content stream.", ioex);
} finally {
try {
if (output != null) {
output.close();
}
} catch (IOException ex) {
logger.debug(ex.getMessage());
logger.debug(StringUtil.getStackTraceString(ex));
}
try {
if (input != null) {
input.close();
}
} catch (IOException ex) {
logger.debug(ex.getMessage());
logger.debug(StringUtil.getStackTraceString(ex));
@ -311,8 +315,10 @@ public abstract class MailMessageUtils {
/**
* returns the message id from the header of the given message
*
* @param message the message to return the message id from
* @return either the message id or null if the message id cannot be read
*
* @throws MessagingException if the message cannot be processed
*/
private static String getMessageId(Message message) throws MessagingException {