From b56501e70d2d09c7157f9ffa33d8c6c9475e1234 Mon Sep 17 00:00:00 2001 From: jomu Date: Wed, 20 Nov 2013 21:06:07 +0000 Subject: [PATCH] fixed sonar findings --- .../shared/network/mail/MailMessageUtils.java | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/network/src/main/java/de/muehlencord/shared/network/mail/MailMessageUtils.java b/network/src/main/java/de/muehlencord/shared/network/mail/MailMessageUtils.java index b367202..0105d01 100644 --- a/network/src/main/java/de/muehlencord/shared/network/mail/MailMessageUtils.java +++ b/network/src/main/java/de/muehlencord/shared/network/mail/MailMessageUtils.java @@ -1,7 +1,6 @@ /** - * 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,13 +20,14 @@ import org.apache.log4j.Logger; /** * Util class to convert between javax.mail.Message and MailMessage objects + * * @author joern@muehlencord.de */ public abstract class MailMessageUtils { /** the logging object */ private static final Logger logger = Logger.getLogger(MailMessageUtils.class.getName()); - + /** content type of mutipart messages - e.g. multipart_alternative or _mixed */ private static final String CONTENTTYPE_MULTIPART = "multipart/"; /** content type of text messages - like text/plain or text/html */ @@ -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 */ @@ -256,9 +254,9 @@ public abstract class MailMessageUtils { } catch (java.io.UnsupportedEncodingException ex) { logger.debug(ex.getMessage()); } 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) { - throw new MessagingException("Cannot read content from bodypart.", mex); + throw new MessagingException("Cannot read content from bodypart.", mex); } 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 - * 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 */ private static String getContentByStream(BodyPart bp, String encoding) throws IOException, MessagingException { - String returnValue; + String returnValue; Reader input = null; StringWriter output = null; try { - input = new BufferedReader(new InputStreamReader(bp.getInputStream(), encoding)); + input = new BufferedReader(new InputStreamReader(bp.getInputStream(), encoding)); output = new StringWriter(); char[] b = new char[4096]; while (input.read(b) != -1) { @@ -291,16 +291,20 @@ public abstract class MailMessageUtils { } returnValue = output.toString(); } 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 { try { - output.close(); + if (output != null) { + output.close(); + } } catch (IOException ex) { logger.debug(ex.getMessage()); logger.debug(StringUtil.getStackTraceString(ex)); } try { - input.close(); + 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 {