Skip to content
Snippets Groups Projects
Commit 529bb25f authored by kohleman's avatar kohleman
Browse files

SSDM-3474: Us a unique time stamp to create mail files

SVN: 36069
parent 8f10e91e
No related branches found
No related tags found
No related merge requests found
...@@ -35,10 +35,13 @@ import java.io.PrintWriter; ...@@ -35,10 +35,13 @@ import java.io.PrintWriter;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.io.Serializable; import java.io.Serializable;
import java.io.StringWriter; import java.io.StringWriter;
import java.text.DateFormat;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -86,7 +89,7 @@ import ch.systemsx.cisd.common.string.StringUtilities.IUniquenessChecker; ...@@ -86,7 +89,7 @@ import ch.systemsx.cisd.common.string.StringUtilities.IUniquenessChecker;
* <p> * <p>
* If you are tempted to add new functionality to this class, ensure that the new functionality does * If you are tempted to add new functionality to this class, ensure that the new functionality does
* not yet exist in <code>org.apache.common.io.FileUtils</code>, see <a * not yet exist in <code>org.apache.common.io.FileUtils</code>, see <a
* href="http://jakarta.apache.org/commons/io/api-release/org/apache/commons/io/FileUtils.html" * href="https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html"
* >javadoc</a>. * >javadoc</a>.
* *
* @author Bernd Rinn * @author Bernd Rinn
...@@ -94,12 +97,13 @@ import ch.systemsx.cisd.common.string.StringUtilities.IUniquenessChecker; ...@@ -94,12 +97,13 @@ import ch.systemsx.cisd.common.string.StringUtilities.IUniquenessChecker;
public final class FileUtilities public final class FileUtilities
{ {
public static final List<String> HDF5_FILE_TYPES = Arrays.asList("h5", "h5ar"); public static final List<String> HDF5_FILE_TYPES = Arrays.asList("h5", "h5ar");
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION,
FileUtilities.class); FileUtilities.class);
private static final Logger machineLog = LogFactory.getLogger(LogCategory.MACHINE, private static final Logger machineLog = LogFactory.getLogger(LogCategory.MACHINE,
FileUtilities.class); FileUtilities.class);
private FileUtilities() private FileUtilities()
{ {
// Can not be instantiated. // Can not be instantiated.
...@@ -1371,6 +1375,50 @@ public final class FileUtilities ...@@ -1371,6 +1375,50 @@ public final class FileUtilities
return new File(uniqueFilePath); return new File(uniqueFilePath);
} }
public final static File createTimestampUniqueFile(final File path, final Pattern regex)
{
return createTimestampUniqueFile(path);
}
/**
* Creates a time stamp numbered file in <var>path</var>.
*
*/
public final static File createTimestampUniqueFile(final File path)
{
assert path != null;
final String filePath = path.getPath();
String uniqueTimestamp = getUniqueTimestamp();
String uniqueFilePath = filePath + uniqueTimestamp;
while (new File(uniqueFilePath).exists() == true)
{
uniqueTimestamp = getUniqueTimestamp();
uniqueFilePath = filePath + uniqueTimestamp;
}
return new File(uniqueFilePath);
}
/**
* @return
* The format of the unique string consists of a time stamp and a unique random number:
* <var>_yyyy_MM_dd_HH_mm_ss_SSS_</var> + unique_number
*/
public final static String getUniqueTimestamp()
{
final String DATE_FORMAT = "_yyyy_MM_dd_HH_mm_ss_SSS_";
int random_int = (int) (Math.random() * 1000);
long timestamp = System.currentTimeMillis();
Date date = new Date(timestamp);
DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
return formatter.format(date) + random_int;
}
/** /**
* For given <var>root</var> and <var>file</var> extracts the relative path. * For given <var>root</var> and <var>file</var> extracts the relative path.
* <p> * <p>
...@@ -2303,7 +2351,7 @@ public final class FileUtilities ...@@ -2303,7 +2351,7 @@ public final class FileUtilities
if (file == null) if (file == null)
{ {
throw new IllegalArgumentException("Unspecified file."); throw new IllegalArgumentException("Unspecified file.");
} }
if (file.exists() == false) if (file.exists() == false)
{ {
throw new IllegalArgumentException("File does not exists: " + file); throw new IllegalArgumentException("File does not exists: " + file);
...@@ -2337,9 +2385,9 @@ public final class FileUtilities ...@@ -2337,9 +2385,9 @@ public final class FileUtilities
throw CheckedExceptionTunnel.wrapIfNecessary(exception); throw CheckedExceptionTunnel.wrapIfNecessary(exception);
} }
throw new EnvironmentFailureException("The size of the folder '" + file throw new EnvironmentFailureException("The size of the folder '" + file
+ "' couldn't be determined:\n command: " + result.getCommandName() + "' couldn't be determined:\n command: " + result.getCommandName()
+ "\n commad line arguments: " + result.getCommandLine() + "\n commad line arguments: " + result.getCommandLine()
+ "\n output: " + result.getOutput() + "\n output: " + result.getOutput()
+ "\n error output: "+ result.getErrorOutput()); + "\n error output: " + result.getErrorOutput());
} }
} }
...@@ -75,21 +75,21 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -75,21 +75,21 @@ public final class MailClient extends Authenticator implements IMailClient
private static final String UNICODE_CHARSET = "utf-8"; private static final String UNICODE_CHARSET = "utf-8";
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, MailClient.class); private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, MailClient.class);
private final Properties properties; private final Properties properties;
public MailClient(final String from, final String smtpHost) public MailClient(final String from, final String smtpHost)
{ {
this(from, smtpHost, null, null, null, null); this(from, smtpHost, null, null, null, null);
} }
public MailClient( public MailClient(
final String from, final String from,
final String smtpHost, final String smtpHost,
final String smtpPort, final String smtpPort,
final String smtpUsername, final String smtpUsername,
final String smtpPassword, final String smtpPassword,
final String testAddress) final String testAddress)
{ {
assert from != null; assert from != null;
assert smtpHost != null; assert smtpHost != null;
...@@ -97,33 +97,38 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -97,33 +97,38 @@ public final class MailClient extends Authenticator implements IMailClient
properties = new Properties(); properties = new Properties();
properties.put(JavaMailProperties.MAIL_FROM, from); properties.put(JavaMailProperties.MAIL_FROM, from);
properties.put(JavaMailProperties.MAIL_SMTP_HOST, smtpHost); properties.put(JavaMailProperties.MAIL_SMTP_HOST, smtpHost);
if(smtpPort != null) { if (smtpPort != null)
{
properties.put(JavaMailProperties.MAIL_SMTP_PORT, smtpPort); properties.put(JavaMailProperties.MAIL_SMTP_PORT, smtpPort);
} }
if(smtpUsername != null) { if (smtpUsername != null)
{
properties.put(JavaMailProperties.MAIL_SMTP_USER, smtpUsername); properties.put(JavaMailProperties.MAIL_SMTP_USER, smtpUsername);
} }
if(smtpPassword != null) { if (smtpPassword != null)
{
properties.put(MailClient.MAIL_SMTP_PASSWORD, smtpPassword); properties.put(MailClient.MAIL_SMTP_PASSWORD, smtpPassword);
} }
if(testAddress != null) { if (testAddress != null)
{
properties.put(MailClient.MAIL_TEST_ADDRESS, testAddress); properties.put(MailClient.MAIL_TEST_ADDRESS, testAddress);
} }
init(); init();
} }
public MailClient(MailClientParameters parameters) public MailClient(MailClientParameters parameters)
{ {
this(parameters.getPropertiesInstance()); this(parameters.getPropertiesInstance());
} }
public MailClient(Properties properties) public MailClient(Properties properties)
{ {
this.properties = ExtendedProperties.createWith(properties); this.properties = ExtendedProperties.createWith(properties);
init(); init();
} }
private void init() { private void init()
{
//Add some properties that don't need to come form the service //Add some properties that don't need to come form the service
String smtpUsername = properties.getProperty(JavaMailProperties.MAIL_SMTP_USER); String smtpUsername = properties.getProperty(JavaMailProperties.MAIL_SMTP_USER);
String smtpPassword = properties.getProperty(MailClient.MAIL_SMTP_PASSWORD); String smtpPassword = properties.getProperty(MailClient.MAIL_SMTP_PASSWORD);
...@@ -131,16 +136,18 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -131,16 +136,18 @@ public final class MailClient extends Authenticator implements IMailClient
{ {
properties.put(JavaMailProperties.MAIL_SMTP_AUTH, Boolean.TRUE.toString()); properties.put(JavaMailProperties.MAIL_SMTP_AUTH, Boolean.TRUE.toString());
} }
properties.put(JavaMailProperties.MAIL_DEBUG,operationLog.isDebugEnabled() ? Boolean.TRUE.toString() : Boolean.FALSE.toString()); properties.put(JavaMailProperties.MAIL_DEBUG, operationLog.isDebugEnabled() ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
properties.put(JavaMailProperties.MAIL_TRANSPORT_PROTOCOL, "smtp"); properties.put(JavaMailProperties.MAIL_TRANSPORT_PROTOCOL, "smtp");
//Get properties from System //Get properties from System
try try
{ {
Properties systemProperties = System.getProperties(); Properties systemProperties = System.getProperties();
for(String key:systemProperties.stringPropertyNames()) { for (String key : systemProperties.stringPropertyNames())
if(!properties.containsKey(key)) { {
if (!properties.containsKey(key))
{
properties.put(key, systemProperties.getProperty(key)); properties.put(key, systemProperties.getProperty(key));
} }
} }
...@@ -150,8 +157,10 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -150,8 +157,10 @@ public final class MailClient extends Authenticator implements IMailClient
} }
//Clear all properties not staring with mail. //Clear all properties not staring with mail.
for(String key:properties.stringPropertyNames()) { for (String key : properties.stringPropertyNames())
if(!key.startsWith("mail.")) { {
if (!key.startsWith("mail."))
{
properties.remove(key); properties.remove(key);
} }
} }
...@@ -168,8 +177,7 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -168,8 +177,7 @@ public final class MailClient extends Authenticator implements IMailClient
operationLog.info("Sending test email."); operationLog.info("Sending test email.");
} }
// subject, content, replyToOrNull, fromOrNull, recipients // subject, content, replyToOrNull, fromOrNull, recipients
sendEmailMessage("test", "", null, null, new EMailAddress[] sendEmailMessage("test", "", null, null, new EMailAddress[] { new EMailAddress(testAddress) });
{ new EMailAddress(testAddress) });
} else } else
{ {
if (operationLog.isInfoEnabled()) if (operationLog.isInfoEnabled())
...@@ -383,7 +391,7 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -383,7 +391,7 @@ public final class MailClient extends Authenticator implements IMailClient
throws EnvironmentFailureException throws EnvironmentFailureException
{ {
String from = properties.getProperty(JavaMailProperties.MAIL_FROM); String from = properties.getProperty(JavaMailProperties.MAIL_FROM);
final InternetAddress fromPerMail = final InternetAddress fromPerMail =
(fromOrNull != null) ? fromOrNull : createInternetAddress(from); (fromOrNull != null) ? fromOrNull : createInternetAddress(from);
if (operationLog.isInfoEnabled()) if (operationLog.isInfoEnabled())
...@@ -398,7 +406,7 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -398,7 +406,7 @@ public final class MailClient extends Authenticator implements IMailClient
if (replyTo != null) if (replyTo != null)
{ {
InternetAddress[] replyToAddress = InternetAddress[] replyToAddress =
{ replyTo }; { replyTo };
msg.setReplyTo(replyToAddress); msg.setReplyTo(replyToAddress);
} }
msg.addRecipients(Message.RecipientType.TO, recipients); msg.addRecipients(Message.RecipientType.TO, recipients);
...@@ -440,7 +448,7 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -440,7 +448,7 @@ public final class MailClient extends Authenticator implements IMailClient
private void send(MimeMessage msg) throws MessagingException private void send(MimeMessage msg) throws MessagingException
{ {
String smtpHost = properties.getProperty(JavaMailProperties.MAIL_SMTP_HOST); String smtpHost = properties.getProperty(JavaMailProperties.MAIL_SMTP_HOST);
if (smtpHost.startsWith(FILE_PREFIX)) if (smtpHost.startsWith(FILE_PREFIX))
{ {
// We don't have a real SMTP server // We don't have a real SMTP server
...@@ -456,7 +464,7 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -456,7 +464,7 @@ public final class MailClient extends Authenticator implements IMailClient
private void writeMessageToFile(MimeMessage msg) throws MessagingException private void writeMessageToFile(MimeMessage msg) throws MessagingException
{ {
String smtpHost = properties.getProperty(JavaMailProperties.MAIL_SMTP_HOST); String smtpHost = properties.getProperty(JavaMailProperties.MAIL_SMTP_HOST);
File emailFolder = new File(smtpHost.substring(FILE_PREFIX.length())); File emailFolder = new File(smtpHost.substring(FILE_PREFIX.length()));
if (emailFolder.exists()) if (emailFolder.exists())
{ {
...@@ -474,7 +482,7 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -474,7 +482,7 @@ public final class MailClient extends Authenticator implements IMailClient
+ emailFolder.getAbsolutePath() + "'."); + emailFolder.getAbsolutePath() + "'.");
} }
} }
File file = FileUtilities.createNextNumberedFile(new File(emailFolder, "email"), null); File file = FileUtilities.createTimestampUniqueFile(new File(emailFolder, "email"), null);
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
final Enumeration<String> headers = msg.getAllHeaderLines(); final Enumeration<String> headers = msg.getAllHeaderLines();
while (headers.hasMoreElements()) while (headers.hasMoreElements())
...@@ -512,7 +520,7 @@ public final class MailClient extends Authenticator implements IMailClient ...@@ -512,7 +520,7 @@ public final class MailClient extends Authenticator implements IMailClient
{ {
String smtpUsername = properties.getProperty(JavaMailProperties.MAIL_SMTP_USER); String smtpUsername = properties.getProperty(JavaMailProperties.MAIL_SMTP_USER);
String smtpPassword = properties.getProperty(MailClient.MAIL_SMTP_PASSWORD); String smtpPassword = properties.getProperty(MailClient.MAIL_SMTP_PASSWORD);
return new PasswordAuthentication(smtpUsername, smtpPassword); return new PasswordAuthentication(smtpUsername, smtpPassword);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment