Skip to content

Commit

Permalink
Merge branch 'essobedo-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilhcem committed May 5, 2014
2 parents aba865c + bee0380 commit de8d4e3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 75 deletions.
25 changes: 20 additions & 5 deletions src/main/java/com/nilhcem/fakesmtp/core/ArgsHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.nilhcem.fakesmtp.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.List;
import com.nilhcem.fakesmtp.model.UIModel;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
Expand All @@ -12,7 +9,10 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.nilhcem.fakesmtp.model.UIModel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

/**
* Handles command line arguments.
Expand Down Expand Up @@ -44,11 +44,16 @@ public enum ArgsHandler {
private static final String OPT_RELAYDOMAINS_DESC = "Comma separated email domain(s) for which relay is accepted. If not specified, relays to any domain. If specified, relays only emails matching these domain(s), dropping (not saving) others";
private static final String OPT_RELAYDOMAINS_SEPARATOR = ",";

private static final String OPT_MEMORYMODE_SHORT = "m";
private static final String OPT_MEMORYMODE_LONG = "memory-mode";
private static final String OPT_MEMORYMODE_DESC = "Disable the persistence in order to avoid the overhead that it adds";

private final Options options;

private String port;
private boolean backgroundStart;
private boolean startServerAtLaunch;
private boolean memoryModeEnabled;

/**
* Handles command line arguments.
Expand All @@ -60,6 +65,7 @@ private ArgsHandler() {
options.addOption(OPT_PORT_SHORT, OPT_PORT_LONG, true, OPT_PORT_DESC);
options.addOption(OPT_BACKGROUNDSTART_SHORT, OPT_BACKGROUNDSTART_LONG, false, OPT_BACKGROUNDSTART_DESC);
options.addOption(OPT_RELAYDOMAINS_SHORT, OPT_RELAYDOMAINS_LONG, true, OPT_RELAYDOMAINS_DESC);
options.addOption(OPT_MEMORYMODE_SHORT, OPT_MEMORYMODE_LONG, false, OPT_MEMORYMODE_DESC);
}

/**
Expand All @@ -80,6 +86,7 @@ public void handleArgs(String[] args) throws ParseException {
port = cmd.getOptionValue(OPT_PORT_SHORT);
startServerAtLaunch = cmd.hasOption(OPT_AUTOSTART_SHORT);
backgroundStart = cmd.hasOption(OPT_BACKGROUNDSTART_SHORT);
memoryModeEnabled = cmd.hasOption(OPT_MEMORYMODE_SHORT);

String relaydomains = cmd.getOptionValue(OPT_RELAYDOMAINS_SHORT);
if (relaydomains != null) {
Expand Down Expand Up @@ -121,6 +128,14 @@ public String getPort() {
return port;
}

/**
* @return whether or not the SMTP server should disable the persistence in order to avoid the overhead that it adds.
* This is particularly useful when we launch performance tests that massively send emails.
*/
public boolean memoryModeEnabled() {
return memoryModeEnabled;
}

/**
* @return the file name of the program.
*/
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/nilhcem/fakesmtp/gui/MainPanel.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.nilhcem.fakesmtp.gui;

import java.util.Observable;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import net.miginfocom.swing.MigLayout;

import com.nilhcem.fakesmtp.core.ArgsHandler;
import com.nilhcem.fakesmtp.core.I18n;
import com.nilhcem.fakesmtp.gui.info.ClearAllButton;
Expand All @@ -20,6 +12,13 @@
import com.nilhcem.fakesmtp.gui.tab.MailsListPane;
import com.nilhcem.fakesmtp.server.MailSaver;
import com.nilhcem.fakesmtp.server.SMTPServerHandler;
import net.miginfocom.swing.MigLayout;

import java.util.Observable;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
* Provides the main panel of the application, which will contain all the components.
Expand Down Expand Up @@ -176,6 +175,10 @@ private void checkArgs() {
if (args.shouldStartServerAtLaunch()) {
startServerBtn.toggleButton();
}

if (args.memoryModeEnabled()) {
saveMsgTextField.get().setEnabled(false);
}
}

/**
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/com/nilhcem/fakesmtp/gui/MenuBar.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.nilhcem.fakesmtp.gui;

import com.nilhcem.fakesmtp.core.ArgsHandler;
import com.nilhcem.fakesmtp.core.Configuration;
import com.nilhcem.fakesmtp.core.I18n;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.ActionEvent;
Expand All @@ -16,12 +23,6 @@
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nilhcem.fakesmtp.core.Configuration;
import com.nilhcem.fakesmtp.core.I18n;

/**
* Provides the menu bar of the application.
*
Expand Down Expand Up @@ -91,13 +92,17 @@ private JMenu createEditMenu() {

JMenuItem mailsLocation = new JMenuItem(i18n.get("menubar.messages.location"));
mailsLocation.setMnemonic(i18n.get("menubar.mnemo.msglocation").charAt(0));
mailsLocation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setChanged();
notifyObservers();
}
});
if (ArgsHandler.INSTANCE.memoryModeEnabled()) {
mailsLocation.setEnabled(false);
} else {
mailsLocation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setChanged();
notifyObservers();
}
});
}

editMenu.add(mailsLocation);
return editMenu;
Expand Down
57 changes: 31 additions & 26 deletions src/main/java/com/nilhcem/fakesmtp/gui/info/SaveMsgField.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.nilhcem.fakesmtp.gui.info;

import com.nilhcem.fakesmtp.core.ArgsHandler;
import com.nilhcem.fakesmtp.core.I18n;
import com.nilhcem.fakesmtp.gui.DirChooser;
import com.nilhcem.fakesmtp.model.UIModel;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JTextField;
import com.nilhcem.fakesmtp.core.I18n;
import com.nilhcem.fakesmtp.gui.DirChooser;
import com.nilhcem.fakesmtp.model.UIModel;

/**
* Text field in which will be written the path where emails will be automatically saved.
Expand All @@ -34,34 +37,36 @@ public SaveMsgField() {
saveMsgField.setEditable(false);
saveMsgField.setBackground(bg);

// Add a MouseListener
saveMsgField.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
if (!ArgsHandler.INSTANCE.memoryModeEnabled()) {
// Add a MouseListener
saveMsgField.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
openFolderSelection();
}
@Override
public void mousePressed(MouseEvent e) {
openFolderSelection();
}

@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}

private void openFolderSelection() {
setChanged();
notifyObservers();
}
});
private void openFolderSelection() {
setChanged();
notifyObservers();
}
});
}
}

/**
Expand Down
53 changes: 31 additions & 22 deletions src/main/java/com/nilhcem/fakesmtp/server/MailSaver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.nilhcem.fakesmtp.server;

import com.nilhcem.fakesmtp.core.ArgsHandler;
import com.nilhcem.fakesmtp.core.Configuration;
import com.nilhcem.fakesmtp.core.I18n;
import com.nilhcem.fakesmtp.model.EmailModel;
import com.nilhcem.fakesmtp.model.UIModel;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -15,15 +25,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.nilhcem.fakesmtp.core.Configuration;
import com.nilhcem.fakesmtp.core.I18n;
import com.nilhcem.fakesmtp.model.EmailModel;
import com.nilhcem.fakesmtp.model.UIModel;

/**
* Saves emails and notifies components so they can refresh their views with new data.
*
Expand All @@ -32,15 +33,18 @@
*/
public final class MailSaver extends Observable {
private static final Logger LOGGER = LoggerFactory.getLogger(MailSaver.class);
private final Pattern subjectPattern = Pattern.compile("^Subject: (.*)$");
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
// This can be a static variable since it is Thread Safe
private static final Pattern SUBJECT_PATTERN = Pattern.compile("^Subject: (.*)$");

private final SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyhhmmssSSS");

/**
* Saves incoming email in file system and notifies observers.
*
* @param from the user who send the email.
* @param to the recipient of the email.
* @param data an inputstream object containing the email.
* @param data an InputStream object containing the email.
* @see com.nilhcem.fakesmtp.gui.MainPanel#addObservers to see which observers will be notified
*/
public void saveEmailAndNotify(String from, String to, InputStream data) {
Expand All @@ -61,16 +65,18 @@ public void saveEmailAndNotify(String from, String to, InputStream data) {
}
}

// We move everything that we can move outside the synchronized block to limit the impact
EmailModel model = new EmailModel();
model.setFrom(from);
model.setTo(to);
String mailContent = convertStreamToString(data);
model.setSubject(getSubjectFromStr(mailContent));
model.setEmailStr(mailContent);

synchronized (getLock()) {
String mailContent = convertStreamToString(data);
String filePath = saveEmailToFile(mailContent);

EmailModel model = new EmailModel();
model.setReceivedDate(new Date());
model.setFrom(from);
model.setTo(to);
model.setSubject(getSubjectFromStr(mailContent));
model.setEmailStr(mailContent);
model.setFilePath(filePath);

setChanged();
Expand All @@ -83,7 +89,8 @@ public void saveEmailAndNotify(String from, String to, InputStream data) {
*/
public void deleteEmails() {
Map<Integer, String> mails = UIModel.INSTANCE.getListMailsMap();

if (ArgsHandler.INSTANCE.memoryModeEnabled())
return;
for (String value : mails.values()) {
File file = new File(value);
if (file.exists()) {
Expand Down Expand Up @@ -118,8 +125,8 @@ public Object getLock() {
* These 4 lines are SubEtha SMTP additional information.
* </p>
*
* @param is the inputstream to be converted.
* @return the converted string object, containing data from the inputstream passed in parameters.
* @param is the InputStream to be converted.
* @return the converted string object, containing data from the InputStream passed in parameters.
*/
private String convertStreamToString(InputStream is) {
final long lineNbToStartCopy = 4; // Do not copy the first 4 lines (received part)
Expand All @@ -131,7 +138,7 @@ private String convertStreamToString(InputStream is) {
try {
while ((line = reader.readLine()) != null) {
if (++lineNb > lineNbToStartCopy) {
sb.append(line + System.getProperty("line.separator"));
sb.append(line).append(LINE_SEPARATOR);
}
}
} catch (IOException e) {
Expand All @@ -147,6 +154,8 @@ private String convertStreamToString(InputStream is) {
* @return the path of the created file.
*/
private String saveEmailToFile(String mailContent) {
if (ArgsHandler.INSTANCE.memoryModeEnabled())
return null;
String filePath = String.format("%s%s%s", UIModel.INSTANCE.getSavePath(), File.separator,
dateFormat.format(new Date()));

Expand Down Expand Up @@ -186,7 +195,7 @@ private String getSubjectFromStr(String data) {

String line;
while ((line = reader.readLine()) != null) {
Matcher matcher = subjectPattern.matcher(line);
Matcher matcher = SUBJECT_PATTERN.matcher(line);
if (matcher.matches()) {
return matcher.group(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!-- Custom appender, for sending the SMTP logs in the swing application directly -->
<!-- If you modify this part, please change also the "logback.appender.name" key in the configuration.properties file -->
<appender name="SMTPLOGS" class="com.nilhcem.fakesmtp.log.SMTPLogsAppender" />
<logger name="org.subethamail.smtp.server.Session" level="DEBUG">
<logger name="org.subethamail.smtp.server.Session" level="INFO">
<appender-ref ref="SMTPLOGS" />
</logger>
<logger name="org.subethamail.smtp.server.ServerThread" level="INFO">
Expand Down

0 comments on commit de8d4e3

Please sign in to comment.