fixed sonar bugs

This commit is contained in:
jomu
2015-02-21 14:57:12 +00:00
parent de31fa5898
commit f8fb86003b
13 changed files with 149 additions and 138 deletions

View File

@ -1,7 +1,3 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.muehlencord.shared.network; package de.muehlencord.shared.network;
import static java.lang.Long.valueOf; import static java.lang.Long.valueOf;
@ -47,7 +43,7 @@ public class CidrTool {
} }
public static final int[] CIDR2MASK = new int[]{0x00000000, 0x80000000, protected static final int[] CIDR2MASK = new int[]{0x00000000, 0x80000000,
0xC0000000, 0xE0000000, 0xF0000000, 0xF8000000, 0xFC000000, 0xC0000000, 0xE0000000, 0xF0000000, 0xF8000000, 0xFC000000,
0xFE000000, 0xFF000000, 0xFF800000, 0xFFC00000, 0xFFE00000, 0xFE000000, 0xFF000000, 0xFF800000, 0xFFC00000, 0xFFE00000,
0xFFF00000, 0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000, 0xFFF00000, 0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000,

View File

@ -4,6 +4,7 @@ import de.muehlencord.shared.network.whois.NetworkInformation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.net.util.SubnetUtils; import org.apache.commons.net.util.SubnetUtils;
import org.apache.log4j.Logger;
/** /**
* *
@ -11,6 +12,8 @@ import org.apache.commons.net.util.SubnetUtils;
*/ */
public class NetworkScanner { public class NetworkScanner {
private final static Logger LOGGER = Logger.getLogger(NetworkScanner.class);
private String networkString; private String networkString;
private NetworkInformation networkInformation = null; // TODO add scan for information private NetworkInformation networkInformation = null; // TODO add scan for information
private List<IpInformation> ipinformationList = new ArrayList<>(); private List<IpInformation> ipinformationList = new ArrayList<>();
@ -25,15 +28,15 @@ public class NetworkScanner {
PortScanner portScanner = new PortScanner(20,1,1024); PortScanner portScanner = new PortScanner(20,1,1024);
for (String currentAddress : addresses) { for (String currentAddress : addresses) {
System.out.print ("Scanning "+currentAddress+"...."); LOGGER.info("Scanning "+currentAddress+"....");
List<PortInformation> portInformation = portScanner.scan(currentAddress); List<PortInformation> portInformation = portScanner.scan(currentAddress);
if (atLeastOnePortOpen(portInformation)) { if (atLeastOnePortOpen(portInformation)) {
IpInformation ipInformation = new IpInformation(currentAddress); IpInformation ipInformation = new IpInformation(currentAddress);
ipInformation.setPortInformation(portInformation); ipInformation.setPortInformation(portInformation);
ipinformationList.add (ipInformation); ipinformationList.add (ipInformation);
System.out.println ("open ports found"); LOGGER.info ("open ports found");
} else { } else {
System.out.println ("not found"); LOGGER.info ("not found");
} }
} }
} }

View File

@ -5,6 +5,7 @@ import java.net.Socket;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.apache.log4j.Logger;
/** /**
* *
@ -12,6 +13,8 @@ import java.util.concurrent.Future;
*/ */
public class PortScan { public class PortScan {
private final static Logger LOGGER = Logger.getLogger(PortScan.class);
private PortScan() { private PortScan() {
// hide constructor // hide constructor
} }
@ -25,16 +28,13 @@ public class PortScan {
Socket socket = new Socket(); Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout); socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close(); socket.close();
return new PortInformation (port, true); return new PortInformation(port, true);
} catch (Exception ex) { } catch (Exception ex) {
return new PortInformation (port, false); LOGGER.debug(ex.toString(), ex);
return new PortInformation(port, false);
} }
} }
}); });
} }
} }

View File

@ -7,6 +7,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.apache.log4j.Logger;
/** /**
* *
@ -14,6 +15,8 @@ import java.util.concurrent.Future;
*/ */
public class PortScanner { public class PortScanner {
private final static Logger LOGGER = Logger.getLogger(PortScanner.class);
private final int startPort; private final int startPort;
private final int endPort; private final int endPort;
private final int timeout; private final int timeout;
@ -42,9 +45,8 @@ public class PortScanner {
for (final Future<PortInformation> f : futures) { for (final Future<PortInformation> f : futures) {
resultList.add(f.get()); resultList.add(f.get());
} }
} catch (InterruptedException ex) { } catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace(); // FIXME add error handling LOGGER.error(ex.toString(), ex);
} catch (ExecutionException ex) {
} }
return resultList; return resultList;

View File

@ -5,15 +5,12 @@ import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException; import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile; import com.enterprisedt.net.ftp.FTPFile;
import de.muehlencord.shared.util.StringUtil; import de.muehlencord.shared.util.StringUtil;
import static de.muehlencord.shared.util.StringUtil.getStackTraceString;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import static java.util.Locale.getDefault;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
/** /**
* *
@ -24,7 +21,7 @@ public class FTPConnection {
/** the default timeout in ms */ /** the default timeout in ms */
public static final int DEFAULTTIMEOUT = 30000; public static final int DEFAULTTIMEOUT = 30000;
/** the logger object */ /** the logger object */
private final Logger logger = getLogger(FTPConnection.class); private final Logger LOGGER = Logger.getLogger(FTPConnection.class);
/** the username to connect with */ /** the username to connect with */
private String userName; private String userName;
/** the password to connect with */ /** the password to connect with */
@ -44,7 +41,7 @@ public class FTPConnection {
* @param password the password to connect with * @param password the password to connect with
*/ */
public FTPConnection(String remoteHost, String userName, String password) { public FTPConnection(String remoteHost, String userName, String password) {
this(remoteHost, userName, password, getDefault()); this(remoteHost, userName, password, Locale.getDefault());
} }
/** /**
@ -86,8 +83,8 @@ public class FTPConnection {
try { try {
client.quit(); client.quit();
} catch (IOException | FTPException ex) { } catch (IOException | FTPException ex) {
logger.error(ex.getMessage()); LOGGER.error(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
} }
} }
@ -107,8 +104,8 @@ public class FTPConnection {
returnValue.add(file.getName()); returnValue.add(file.getName());
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.error(ex.getMessage()); LOGGER.error(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex); throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex);
} }
@ -133,8 +130,8 @@ public class FTPConnection {
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.error(ex.getMessage()); LOGGER.error(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex); throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex);
} }
@ -160,8 +157,8 @@ public class FTPConnection {
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.error(ex.getMessage()); LOGGER.error(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex); throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex);
} }
@ -187,8 +184,8 @@ public class FTPConnection {
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.error(ex.getMessage()); LOGGER.error(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex); throw new FTPConnectionException("Error while getting diretoy listing. Reason: " + ex.getMessage(), ex);
} }
@ -215,8 +212,8 @@ public class FTPConnection {
client.put(localFilename, remoteName, false); client.put(localFilename, remoteName, false);
} }
} catch (Exception ex) { } catch (Exception ex) {
logger.error(ex.getMessage()); LOGGER.error(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
throw new FTPConnectionException("Error while uploading file. Reason: " + ex.getMessage(), ex); throw new FTPConnectionException("Error while uploading file. Reason: " + ex.getMessage(), ex);
} }
@ -233,8 +230,8 @@ public class FTPConnection {
try { try {
client.rename(remoteOldName, remoteNewName); client.rename(remoteOldName, remoteNewName);
} catch (Exception ex) { } catch (Exception ex) {
logger.error(ex.getMessage()); LOGGER.error(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
throw new FTPConnectionException("Error while renaming file. Reason: " + ex.getMessage(), ex); throw new FTPConnectionException("Error while renaming file. Reason: " + ex.getMessage(), ex);
} }
} }

View File

@ -193,11 +193,13 @@ public abstract class DefaultMailReader implements MailReader {
*/ */
@Override @Override
public String getFolder(String folderPath) throws MailReaderException { public String getFolder(String folderPath) throws MailReaderException {
String returnValue = ""; StringBuilder sb = new StringBuilder();
List<Folder> folderList = getFolderPath(folderPath); List<Folder> folderList = getFolderPath(folderPath);
for (Folder folder : folderList) { for (Folder folder : folderList) {
returnValue += folder.getName() + "/"; sb.append (folder.getName());
sb.append ("/");
} }
String returnValue = sb.toString();
if (returnValue.endsWith("/")) { if (returnValue.endsWith("/")) {
returnValue = returnValue.substring(0, returnValue.length() - 1); returnValue = returnValue.substring(0, returnValue.length() - 1);
} }
@ -407,7 +409,8 @@ public abstract class DefaultMailReader implements MailReader {
message.setFlag(Flags.Flag.DELETED, true); message.setFlag(Flags.Flag.DELETED, true);
} }
} }
} catch (Exception ex) { } catch (MessagingException | MailReaderException ex) {
LOGGER.debug(ex.getMessage(), ex);
} finally { } finally {
if ((df != null) && (df.isOpen())) { if ((df != null) && (df.isOpen())) {
try { try {
@ -458,7 +461,7 @@ public abstract class DefaultMailReader implements MailReader {
cmsg.getContentType(); cmsg.getContentType();
return cmsg; return cmsg;
} catch (MessagingException ex) { } catch (MessagingException ex) {
LOGGER.debug("Access via copy constructor failed, trying byte array constructor"); LOGGER.debug("Access via copy constructor failed, trying byte array constructor. "+ex.getMessage(), ex);
} }
try { try {
@ -476,7 +479,7 @@ public abstract class DefaultMailReader implements MailReader {
return cmsg; return cmsg;
} catch (Exception ex) { } catch (Exception ex) {
LOGGER.debug("Also fallback solution did not work"); LOGGER.debug("Also fallback solution did not work. "+ex.getMessage(), ex);
throw initialException; throw initialException;
} }
} }

View File

@ -23,7 +23,7 @@ public class MailMessage {
/** the html content of the message if avaialble, null else */ /** the html content of the message if avaialble, null else */
private List<String> htmlContent; private List<String> htmlContent;
/** true, if content is html message body */ /** true, if content is html message body */
private boolean isHtmlMessage; private boolean htmlMessage;
/** the message id if available, null otherwise */ /** the message id if available, null otherwise */
private String messageId; private String messageId;
/** the size of the message in bytes */ /** the size of the message in bytes */
@ -53,7 +53,7 @@ public class MailMessage {
content.add(contentString); content.add(contentString);
} }
this.htmlContent = new LinkedList<>(); this.htmlContent = new LinkedList<>();
this.isHtmlMessage = false; this.htmlMessage = false;
this.messageId = null; this.messageId = null;
this.sender = null; this.sender = null;
this.receiver = new LinkedList<>(); this.receiver = new LinkedList<>();
@ -88,12 +88,13 @@ public class MailMessage {
} }
private String getContent(List<String> contentList) { private String getContent(List<String> contentList) {
String returnValue = ""; StringBuilder sb = new StringBuilder();
String contentSeparator = "--------"; String contentSeparator = "--------";
for (String currentContent : contentList) { for (String currentContent : contentList) {
returnValue += currentContent; sb.append (currentContent);
returnValue += contentSeparator; // FIXME make configurable sb.append (contentSeparator); // FIXME make configurable
} }
String returnValue = sb.toString();
if (returnValue.endsWith(contentSeparator)) { if (returnValue.endsWith(contentSeparator)) {
returnValue = returnValue.substring(0, returnValue.length() - contentSeparator.length()); returnValue = returnValue.substring(0, returnValue.length() - contentSeparator.length());
} }
@ -111,7 +112,7 @@ public class MailMessage {
public void setContent(String c, boolean isHtmlContent) { public void setContent(String c, boolean isHtmlContent) {
if (c != null) { if (c != null) {
content.add(c); content.add(c);
this.isHtmlMessage = isHtmlContent; this.htmlMessage = isHtmlContent;
if (isHtmlContent) { if (isHtmlContent) {
this.htmlContent.add(c); this.htmlContent.add(c);
} }
@ -140,7 +141,7 @@ public class MailMessage {
* @return the isHtmlMessage * @return the isHtmlMessage
*/ */
public boolean isHtmlMessage() { public boolean isHtmlMessage() {
return isHtmlMessage; return htmlMessage;
} }
/** /**

View File

@ -4,13 +4,13 @@
*/ */
package de.muehlencord.shared.network.mail; package de.muehlencord.shared.network.mail;
import de.muehlencord.shared.util.StringUtil;
import static de.muehlencord.shared.util.StringUtil.getStackTraceString; import static de.muehlencord.shared.util.StringUtil.getStackTraceString;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Locale;
import javax.mail.BodyPart; import javax.mail.BodyPart;
import javax.mail.Message; import javax.mail.Message;
import javax.mail.MessagingException; import javax.mail.MessagingException;
@ -18,7 +18,6 @@ import javax.mail.Multipart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
/** /**
* Util class to convert between javax.mail.Message and MailMessage objects * Util class to convert between javax.mail.Message and MailMessage objects
@ -28,7 +27,7 @@ import static org.apache.log4j.Logger.getLogger;
public abstract class MailMessageUtils { public abstract class MailMessageUtils {
/** the logging object */ /** the logging object */
private static final 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/";
@ -70,16 +69,16 @@ public abstract class MailMessageUtils {
try { try {
contentType = getContentType(mm.getContentType()); contentType = getContentType(mm.getContentType());
subject = mm.getSubject(); subject = mm.getSubject();
logger.debug("message subject = " + subject); LOGGER.debug("message subject = " + subject);
messageId = getMessageId(message); messageId = getMessageId(message);
logger.debug("messageid = " + messageId); LOGGER.debug("messageid = " + messageId);
logger.debug(mm.getSentDate()); LOGGER.debug(mm.getSentDate());
} catch (MessagingException ex) { } catch (MessagingException ex) {
throw new MailMessageException("Error while converting mime message. Reason: " + ex.getMessage(), ex); throw new MailMessageException("Error while converting mime message. Reason: " + ex.getMessage(), ex);
} }
logger.debug("messageContentType = " + contentType); LOGGER.debug("messageContentType = " + contentType);
// handling multipart messages // handling multipart messages
if (contentType.contains(CONTENTTYPE_MULTIPART)) { if (contentType.contains(CONTENTTYPE_MULTIPART)) {
@ -88,23 +87,23 @@ public abstract class MailMessageUtils {
try { try {
Multipart mp = (Multipart) mm.getContent(); Multipart mp = (Multipart) mm.getContent();
int partsFound = mp.getCount(); int partsFound = mp.getCount();
logger.debug(partsFound + " parts found"); LOGGER.debug(partsFound + " parts found");
for (int i = 0; i < partsFound; i++) { for (int i = 0; i < partsFound; i++) {
logger.debug("Working on multiPart " + (i + 1)); LOGGER.debug("Working on multiPart " + (i + 1));
BodyPart bp = mp.getBodyPart(i); BodyPart bp = mp.getBodyPart(i);
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();
logger.debug(bodyPartCount + " bodyMultiParts found"); LOGGER.debug(bodyPartCount + " bodyMultiParts found");
for (int currentBodyPart = 0; currentBodyPart < bodyPartCount; currentBodyPart++) { for (int currentBodyPart = 0; currentBodyPart < bodyPartCount; currentBodyPart++) {
logger.debug("Working on bodypart " + (currentBodyPart + 1)); LOGGER.debug("Working on bodypart " + (currentBodyPart + 1));
BodyPart bodyPart = bodyMultiPart.getBodyPart(currentBodyPart); BodyPart bodyPart = bodyMultiPart.getBodyPart(currentBodyPart);
String bodyPartContentType = getContentType(bodyPart.getContentType()); String bodyPartContentType = getContentType(bodyPart.getContentType());
logger.debug("bodyPart contentType = " + bodyPartContentType); LOGGER.debug("bodyPart contentType = " + bodyPartContentType);
if (bodyPartContentType.contains(CONTENTTYPE_TEXT_PLAIN)) { if (bodyPartContentType.contains(CONTENTTYPE_TEXT_PLAIN)) {
returnMessage.setContent(getContent(bodyPart), false); returnMessage.setContent(getContent(bodyPart), false);
@ -117,20 +116,18 @@ public abstract class MailMessageUtils {
if (bp.getFileName() == null) { if (bp.getFileName() == null) {
returnMessage.setContent(getContent(bp), false); returnMessage.setContent(getContent(bp), false);
} else { } else {
logger.debug("Found base64 coded attachment - not yet supported, skipping"); LOGGER.debug("Found base64 coded attachment - not yet supported, skipping");
} }
} else if (bodyContentType.equals(CONTENTTYPE_TEXT_HTML)) { } else if (bodyContentType.equals(CONTENTTYPE_TEXT_HTML)) {
returnMessage.setHtmlContent(getContent(bp)); returnMessage.setHtmlContent(getContent(bp));
} else if (bodyContentType.equals(CONTENTTYPE_APPLICATION_PGPSIGNATURE)) { } else if (bodyContentType.equals(CONTENTTYPE_APPLICATION_PGPSIGNATURE)) {
logger.debug("Found pgp signature - not yet supported, skipping"); LOGGER.debug("Found pgp signature - not yet supported, skipping");
// TODO add pgp/signature support // TODO add pgp/signature support
} else if ((bodyContentType.contains(CONTENTTYPE_APPLICATION) || (bodyContentType.contains(CONTENTTYPE_MESSAGE)) } else if (isSupportedAttachmentType(bodyContentType)) {
|| (bodyContentType.contains(CONTENTTYPE_TEXT)) || (bodyContentType.contains(CONTENTTYPE_IMAGE)) LOGGER.debug("Found attachment - not yet supported, skipping");
|| (bodyContentType.contains(CONTENTTYPE_VIDEO)) || (bodyContentType.contains(CONTENTTYPE_AUDIO)))) {
logger.debug("Found attachment - not yet supported, skipping");
// TODO add attachment support // TODO add attachment support
} else { } else {
throw new MailMessageException("Reading bodyContentType " + bodyContentType + " not yet implemented"); throw new MailMessageException("Reading bodyContentType " + bodyContentType + " not yet implemented");
@ -177,19 +174,20 @@ public abstract class MailMessageUtils {
} else if (contentType.contains(CONTENTTYPE_APPLICATION)) { } else if (contentType.contains(CONTENTTYPE_APPLICATION)) {
// message with only attachment // message with only attachment
returnMessage = new MailMessage(subject, "", messageId); returnMessage = new MailMessage(subject, "", messageId);
logger.debug("Found attachment - not yet supported, skipping"); // TODO add attachment LOGGER.debug("Found attachment - not yet supported, skipping"); // TODO add attachment
} else if (contentType.contains(CONTENTTYPE_TEXT)) { } else if (contentType.contains(CONTENTTYPE_TEXT)) {
returnMessage = new MailMessage(subject, "", messageId); returnMessage = new MailMessage(subject, "", messageId);
logger.debug("Found attachment - not yet supported, skipping"); // TODO add attachment LOGGER.debug("Found attachment - not yet supported, skipping"); // TODO add attachment
} else { } else {
String hint = "Unkonwn message format Converting MimeMessage of contentType " + contentType + " not yet implemented."; String hint = "Unkonwn message format Converting MimeMessage of contentType " + contentType + " not yet implemented.";
try { try {
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 (IOException | MessagingException ex) { } catch (IOException | MessagingException ex) {
logger.error(hint); LOGGER.debug (ex.toString(), ex);
LOGGER.error(hint);
} }
throw new MailMessageException(hint); throw new MailMessageException(hint);
} }
@ -233,7 +231,7 @@ public abstract class MailMessageUtils {
if (contentType.contains(";")) { if (contentType.contains(";")) {
returnValue = contentType.substring(0, contentType.indexOf(';')); returnValue = contentType.substring(0, contentType.indexOf(';'));
} }
returnValue = returnValue.toLowerCase(); returnValue = returnValue.toLowerCase(Locale.US);
returnValue = returnValue.trim(); returnValue = returnValue.trim();
return returnValue; return returnValue;
} }
@ -254,7 +252,7 @@ public abstract class MailMessageUtils {
try { try {
returnValue = bp.getContent().toString(); returnValue = bp.getContent().toString();
} catch (java.io.UnsupportedEncodingException ex) { } catch (java.io.UnsupportedEncodingException ex) {
logger.debug(ex.getMessage()); throw new MessagingException("Cannot read content from bodypart. "+ex.getMessage(), ex);
} 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) {
@ -300,16 +298,16 @@ public abstract class MailMessageUtils {
output.close(); output.close();
} }
} catch (IOException ex) { } catch (IOException ex) {
logger.debug(ex.getMessage()); LOGGER.debug(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(getStackTraceString(ex));
} }
try { try {
if (input != null) { if (input != null) {
input.close(); input.close();
} }
} catch (IOException ex) { } catch (IOException ex) {
logger.debug(ex.getMessage()); LOGGER.debug(ex.getMessage());
logger.debug(getStackTraceString(ex)); LOGGER.debug(getStackTraceString(ex));
} }
} }
return returnValue; return returnValue;
@ -331,4 +329,28 @@ public abstract class MailMessageUtils {
return null; return null;
} }
} }
/**
* returns true, if the contentType string is one of the known attachment types.
*
* @param contentType the contentType to check
* @return true, if the contentType string is one of the known attachment types. False otherwise.
*/
private static boolean isSupportedAttachmentType(String contentType) {
if (contentType.contains(CONTENTTYPE_APPLICATION)) {
return true;
} else if (contentType.contains(CONTENTTYPE_MESSAGE)) {
return true;
} else if (contentType.contains(CONTENTTYPE_TEXT)) {
return true;
} else if (contentType.contains(CONTENTTYPE_IMAGE)) {
return true;
} else if (contentType.contains(CONTENTTYPE_VIDEO)) {
return true;
} else if (contentType.contains(CONTENTTYPE_AUDIO)) {
return true;
} else {
return false;
}
}
} }

View File

@ -1,13 +1,11 @@
package de.muehlencord.shared.network.mail; package de.muehlencord.shared.network.mail;
import de.muehlencord.shared.util.StringUtil; import de.muehlencord.shared.util.StringUtil;
import static de.muehlencord.shared.util.StringUtil.getStackTraceString;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
/** /**
* *
@ -16,7 +14,7 @@ import static org.apache.log4j.Logger.getLogger;
public abstract class MailReaderConfigurationFactory { public abstract class MailReaderConfigurationFactory {
/** the logging object */ /** the logging object */
private final static Logger logger = getLogger(MailReaderConfigurationFactory.class.getName()); private final static Logger LOGGER = Logger.getLogger(MailReaderConfigurationFactory.class);
/** /**
* reads the config file and creates a configured mailreader configuration * reads the config file and creates a configured mailreader configuration
@ -43,8 +41,8 @@ public abstract class MailReaderConfigurationFactory {
try { try {
is.close(); is.close();
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Error while reading input file."); LOGGER.error("Error while reading input file.");
logger.debug(getStackTraceString(ex)); LOGGER.debug(StringUtil.getStackTraceString(ex));
} }
} }
@ -53,15 +51,15 @@ public abstract class MailReaderConfigurationFactory {
throw new MailReaderConfigurationException("property smtp.server not specified. Please check config"); throw new MailReaderConfigurationException("property smtp.server not specified. Please check config");
} }
String host = p.getProperty("imap.server"); String host = p.getProperty("imap.server");
if ((smtpServer == null) || (smtpServer.isEmpty())) { if ((host == null) || (host.isEmpty())) {
throw new MailReaderConfigurationException("property imap.server not specified. Please check config"); throw new MailReaderConfigurationException("property imap.server not specified. Please check config");
} }
String userName = p.getProperty("imap.username"); String userName = p.getProperty("imap.username");
if ((smtpServer == null) || (smtpServer.isEmpty())) { if ((userName == null) || (userName.isEmpty())) {
throw new MailReaderConfigurationException("property imap.username not specified. Please check config"); throw new MailReaderConfigurationException("property imap.username not specified. Please check config");
} }
String password = p.getProperty("imap.password"); String password = p.getProperty("imap.password");
if ((smtpServer == null) || (smtpServer.isEmpty())) { if ((password == null) || (password.isEmpty())) {
throw new MailReaderConfigurationException("property imap.password not specified. Please check config"); throw new MailReaderConfigurationException("property imap.password not specified. Please check config");
} }

View File

@ -5,7 +5,6 @@ import de.muehlencord.shared.network.mail.MailReaderConfiguration;
import de.muehlencord.shared.network.mail.MailReaderConnectionException; import de.muehlencord.shared.network.mail.MailReaderConnectionException;
import javax.mail.Session; import javax.mail.Session;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
/** /**
* Implementation of MaiLReader to connect to an IMAP server * Implementation of MaiLReader to connect to an IMAP server
@ -15,7 +14,7 @@ import static org.apache.log4j.Logger.getLogger;
public class ImapMailReader extends DefaultMailReader { public class ImapMailReader extends DefaultMailReader {
/** the logger object */ /** the logger object */
private final static Logger logger = getLogger(ImapMailReader.class); private final static Logger LOGGER = Logger.getLogger(ImapMailReader.class);
/** /**
* creates a new instance to connect to an IMAP (or MS Exchange) server * creates a new instance to connect to an IMAP (or MS Exchange) server
@ -41,7 +40,7 @@ public class ImapMailReader extends DefaultMailReader {
hint += ". Reason: " + ex.getMessage(); hint += ". Reason: " + ex.getMessage();
throw new MailReaderConnectionException(hint, ex); throw new MailReaderConnectionException(hint, ex);
} }
logger.info("Connected to " + getConnectionShortCut()); LOGGER.info("Connected to " + getConnectionShortCut());
} }
/** /**

View File

@ -1,20 +1,11 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.muehlencord.shared.network.whois; package de.muehlencord.shared.network.whois;
import de.muehlencord.shared.network.whois.DefaultWhoisParser;
import de.muehlencord.shared.network.whois.WhoisInformation;
import de.muehlencord.shared.network.whois.WhoisException;
import de.muehlencord.shared.network.whois.WhoisParser;
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList; import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
import de.muehlencord.shared.util.StringUtil;
import static de.muehlencord.shared.util.StringUtil.getValueBetweenKeywords; import static de.muehlencord.shared.util.StringUtil.getValueBetweenKeywords;
import java.text.ParseException; import java.text.ParseException;
import java.util.Map; import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
/** /**
* *
@ -23,7 +14,7 @@ import static org.apache.log4j.Logger.getLogger;
public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser { public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser {
/** logger object */ /** logger object */
private final static Logger logger = getLogger(DefaultWhoisParser.class); private final static Logger LOGGER = Logger.getLogger(DefaultWhoisParser.class);
/** information to be returned */ /** information to be returned */
private WhoisInformation whoisInformation; private WhoisInformation whoisInformation;
@ -69,11 +60,11 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
} }
private void analyseBlock(String block) throws WhoisException { private void analyseBlock(String block) throws WhoisException {
logger.debug("Start analysing block"); LOGGER.debug("Start analysing block");
logger.debug("\n---\n" + block + "\n---\n"); LOGGER.debug("\n---\n" + block + "\n---\n");
if ((block == null) || (block.equals("")) || (!block.contains(" "))) { if ((block == null) || (block.equals("")) || (!block.contains(" "))) {
logger.debug("Skippig empty block"); LOGGER.debug("Skippig empty block");
return; return;
} }
@ -96,7 +87,7 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
String startIpAddress = ipAddresses[0]; String startIpAddress = ipAddresses[0];
String endIPAddress = ipAddresses[2]; String endIPAddress = ipAddresses[2];
whoisInformation.getNetwork().addAll(rangeToCidrList(startIpAddress, endIPAddress)); whoisInformation.getNetwork().addAll(rangeToCidrList(startIpAddress, endIPAddress));
logger.info("Network:" + whoisInformation.getNetwork().toString()); LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
} else { } else {
throw new WhoisException("Cannot identify netrange value"); throw new WhoisException("Cannot identify netrange value");
} }
@ -114,24 +105,24 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
orgId = orgName; orgId = orgName;
} }
whoisInformation.setNetworkInformation(new NetworkInformation(orgName, orgId, country)); whoisInformation.setNetworkInformation(new NetworkInformation(orgName, orgId, country));
logger.info("Networkinformation:" + whoisInformation.getNetworkInformation().toString()); LOGGER.info("Networkinformation:" + whoisInformation.getNetworkInformation().toString());
break; break;
case "OrgAbuseHandle:": case "OrgAbuseHandle:":
// TODO add abuse handler // TODO add abuse handler
logger.info("Skipping OrgAbuseHandle block"); LOGGER.info("Skipping OrgAbuseHandle block");
break; break;
case "OrgTechHandle:": case "OrgTechHandle:":
// admin person of network server belongs to // admin person of network server belongs to
logger.info("Skipping OrgTechHandle block"); LOGGER.info("Skipping OrgTechHandle block");
break; break;
case "#": case "#":
logger.info("Skipping comment block"); LOGGER.info("Skipping comment block");
break; break;
default: default:
logger.info("Unknown block found"); LOGGER.info("Unknown block found");
} }
} }
} }

View File

@ -3,7 +3,6 @@ package de.muehlencord.shared.network.whois;
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList; import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
import java.util.Map; import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
/** /**
* *
@ -12,7 +11,7 @@ import static org.apache.log4j.Logger.getLogger;
public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisParser { public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisParser {
/** logger object */ /** logger object */
private final static Logger logger = getLogger(DefaultWhoisParser.class); private final static Logger LOGGER = Logger.getLogger(DefaultWhoisParser.class);
/** information to be returned */ /** information to be returned */
private WhoisInformation whoisInformation; private WhoisInformation whoisInformation;
@ -36,11 +35,11 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
} }
private void analyseBlock(String block) throws WhoisException { private void analyseBlock(String block) throws WhoisException {
logger.debug("Start analysing block"); LOGGER.debug("Start analysing block");
logger.debug("\n---\n" + block + "\n---\n"); LOGGER.debug("\n---\n" + block + "\n---\n");
if ((block == null) || (block.equals("")) || (!block.contains(" "))) { if ((block == null) || (block.equals("")) || (!block.contains(" "))) {
logger.debug("Skippig empty block"); LOGGER.debug("Skippig empty block");
return; return;
} }
@ -75,7 +74,7 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
whoisInformation.getNetwork().add(inetnum); whoisInformation.getNetwork().add(inetnum);
} }
logger.info("Network:" + whoisInformation.getNetwork().toString()); LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
whoisInformation.setNetworkInformation(new NetworkInformation(netname, descr, country)); whoisInformation.setNetworkInformation(new NetworkInformation(netname, descr, country));
break; break;
@ -90,26 +89,26 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
} }
*/ */
whoisInformation.getRootNetwork().add(route); whoisInformation.getRootNetwork().add(route);
logger.info("Root network " + whoisInformation.getRootNetwork().toString()); LOGGER.info("Root network " + whoisInformation.getRootNetwork().toString());
break; break;
case "role:": case "role:":
// admin company of network server belongs to // admin company of network server belongs to
logger.info("Skipping role block"); LOGGER.info("Skipping role block");
break; break;
case "person:": case "person:":
// admin person of network server belongs to // admin person of network server belongs to
logger.info("Skipping person block"); LOGGER.info("Skipping person block");
break; break;
case "% Information": case "% Information":
case "% Note:": case "% Note:":
case "% This": case "% This":
case "%": case "%":
logger.info("Skipping comment block"); LOGGER.info("Skipping comment block");
break; break;
default: default:
logger.info("Unknown block found"); LOGGER.info("Unknown block found");
} }
} }
} }

View File

@ -1,8 +1,8 @@
package de.muehlencord.shared.network.whois; package de.muehlencord.shared.network.whois;
import java.util.Locale;
import org.apache.commons.net.whois.WhoisClient; import org.apache.commons.net.whois.WhoisClient;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
/** /**
* *
@ -11,25 +11,25 @@ import static org.apache.log4j.Logger.getLogger;
public class Whois { public class Whois {
/** logger object */ /** logger object */
private final static Logger logger = getLogger(Whois.class); private final static Logger LOGGER = Logger.getLogger(Whois.class);
/** whois client from commons-net package to execute commands with */ /** whoisClient client from commons-net package to execute commands with */
WhoisClient whois; WhoisClient whoisClient;
/** the complete output */ /** the complete output */
String whoisString = null; String whoisString = null;
/** /**
* creates a new instance of the whois client * creates a new instance of the whoisClient client
*/ */
public Whois() { public Whois() {
whois = new WhoisClient(); whoisClient = new WhoisClient();
whois.setDefaultTimeout(60000); whoisClient.setDefaultTimeout(60000);
} }
/** /**
* reads and parses the inforomation for the given ip address from the given whoisserver * reads and parses the inforomation for the given ip address from the given whoisserver
* @param whoIsServer the whois server to use * @param whoIsServer the whoisClient server to use
* @param ip the ip to query whois data for * @param ip the ip to query whoisClient data for
* @return whoisInformation parsed from whois string * @return whoisInformation parsed from whoisClient string
* @throws WhoisException if the parsing fails * @throws WhoisException if the parsing fails
*/ */
public WhoisInformation execute(String whoIsServer, String ip) throws WhoisException { public WhoisInformation execute(String whoIsServer, String ip) throws WhoisException {
@ -39,18 +39,18 @@ public class Whois {
parameter = "n + "; parameter = "n + ";
} }
// get whois information // get whoisClient information
try { try {
whois.connect(whoIsServer); whoisClient.connect(whoIsServer);
whoisString = whois.query(parameter + ip); whoisString = whoisClient.query(parameter + ip);
whois.disconnect(); whoisClient.disconnect();
} catch (Exception ex) { } catch (Exception ex) {
throw new WhoisException("Error while reading whois data from " + whoIsServer + ". Reason: " + ex.getMessage(), ex); throw new WhoisException("Error while reading whois data from " + whoIsServer + ". Reason: " + ex.getMessage(), ex);
} }
// identify RIR and select correct parser // identify RIR and select correct parser
WhoisParser parser; WhoisParser parser;
if (whoisString.toLowerCase().contains("arin.net")) { if (whoisString.toLowerCase(Locale.US).contains("arin.net")) {
parser = new ArinWhoisParser(); parser = new ArinWhoisParser();
} else { } else {
parser = new DefaultWhoisParser(); parser = new DefaultWhoisParser();