Skip to content
Snippets Groups Projects
Commit 98f0ffa5 authored by felmer's avatar felmer
Browse files

LMS-779 sending e-mail if uploading fails.

SVN: 10351
parent 25c80f5d
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,14 @@ ...@@ -32,6 +32,14 @@
<property name="service"> <property name="service">
<bean class="ch.systemsx.cisd.openbis.dss.generic.server.DataStoreService"> <bean class="ch.systemsx.cisd.openbis.dss.generic.server.DataStoreService">
<constructor-arg ref="session-token-manager" /> <constructor-arg ref="session-token-manager" />
<constructor-arg>
<bean class="ch.systemsx.cisd.openbis.dss.generic.server.MailClientParameters">
<property name="from" value="${mail.from}"/>
<property name="smtpHost" value="${mail.smtp.host}"/>
<property name="smtpUser" value="${mail.smtp.user}"/>
<property name="smtpPassword" value="${mail.smtp.password}"/>
</bean>
</constructor-arg>
<property name="storeRoot" value="${storeroot-dir}"/> <property name="storeRoot" value="${storeroot-dir}"/>
</bean> </bean>
</property> </property>
......
/*
* Copyright 2009 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.openbis.dss.generic.server;
import ch.systemsx.cisd.cifex.rpc.ICIFEXRPCService;
import ch.systemsx.cisd.cifex.rpc.client.RPCServiceFactory;
import ch.systemsx.cisd.cifex.shared.basic.Constants;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
final class CIFEXRPCServiceFactory implements ICIFEXRPCServiceFactory
{
private static final long serialVersionUID = 1L;
private final String cifexURL;
private transient ICIFEXRPCService service;
CIFEXRPCServiceFactory(String cifexURL)
{
this.cifexURL = cifexURL;
}
public ICIFEXRPCService createService()
{
if (service == null)
{
final String serviceURL = cifexURL + Constants.CIFEX_RPC_PATH;
service = RPCServiceFactory.createServiceProxy(serviceURL, true);
final int serverVersion = service.getVersion();
if (ICIFEXRPCService.VERSION != serverVersion)
{
throw new EnvironmentFailureException("The version of the CIFEX server is not "
+ ICIFEXRPCService.VERSION + " but " + serverVersion + ".");
}
}
return service;
}
}
\ No newline at end of file
...@@ -24,9 +24,6 @@ import java.util.List; ...@@ -24,9 +24,6 @@ import java.util.List;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import ch.systemsx.cisd.cifex.rpc.ICIFEXRPCService; import ch.systemsx.cisd.cifex.rpc.ICIFEXRPCService;
import ch.systemsx.cisd.cifex.rpc.client.RPCServiceFactory;
import ch.systemsx.cisd.cifex.shared.basic.Constants;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.exceptions.InvalidAuthenticationException; import ch.systemsx.cisd.common.exceptions.InvalidAuthenticationException;
import ch.systemsx.cisd.common.exceptions.InvalidSessionException; import ch.systemsx.cisd.common.exceptions.InvalidSessionException;
import ch.systemsx.cisd.common.exceptions.WrappedIOException; import ch.systemsx.cisd.common.exceptions.WrappedIOException;
...@@ -47,11 +44,13 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic ...@@ -47,11 +44,13 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic
private final IDataSetCommandExecutorFactory commandExecutorFactory; private final IDataSetCommandExecutorFactory commandExecutorFactory;
private final MailClientParameters mailClientParameters;
private File storeRoot; private File storeRoot;
private IDataSetCommandExecutor commandExecuter; private IDataSetCommandExecutor commandExecuter;
public DataStoreService(SessionTokenManager sessionTokenManager) public DataStoreService(SessionTokenManager sessionTokenManager, MailClientParameters mailClientParameters)
{ {
this(sessionTokenManager, new IDataSetCommandExecutorFactory() this(sessionTokenManager, new IDataSetCommandExecutorFactory()
{ {
...@@ -59,14 +58,15 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic ...@@ -59,14 +58,15 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic
{ {
return new DataSetCommandExecuter(store); return new DataSetCommandExecuter(store);
} }
}); }, mailClientParameters);
} }
DataStoreService(SessionTokenManager sessionTokenManager, DataStoreService(SessionTokenManager sessionTokenManager,
IDataSetCommandExecutorFactory commandExecutorFactory) IDataSetCommandExecutorFactory commandExecutorFactory, MailClientParameters mailClientParameters)
{ {
this.sessionTokenManager = sessionTokenManager; this.sessionTokenManager = sessionTokenManager;
this.commandExecutorFactory = commandExecutorFactory; this.commandExecutorFactory = commandExecutorFactory;
this.mailClientParameters = mailClientParameters;
} }
public final void setStoreRoot(File storeRoot) public final void setStoreRoot(File storeRoot)
...@@ -158,36 +158,8 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic ...@@ -158,36 +158,8 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic
{ {
throw new InvalidSessionException("User couldn't be authenticated at CIFEX."); throw new InvalidSessionException("User couldn't be authenticated at CIFEX.");
} }
commandExecuter.scheduleCommand(new UploadingCommand(serviceFactory, dataSetLocations, context)); commandExecuter.scheduleCommand(new UploadingCommand(serviceFactory, mailClientParameters,
} dataSetLocations, context));
private static final class CIFEXRPCServiceFactory implements ICIFEXRPCServiceFactory
{
private static final long serialVersionUID = 1L;
private final String cifexURL;
private transient ICIFEXRPCService service;
CIFEXRPCServiceFactory(String cifexURL)
{
this.cifexURL = cifexURL;
}
public ICIFEXRPCService createService()
{
if (service == null)
{
final String serviceURL = cifexURL + Constants.CIFEX_RPC_PATH;
service = RPCServiceFactory.createServiceProxy(serviceURL, true);
final int serverVersion = service.getVersion();
if (ICIFEXRPCService.VERSION != serverVersion)
{
throw new EnvironmentFailureException("The version of the CIFEX server is not "
+ ICIFEXRPCService.VERSION + " but " + serverVersion + ".");
}
}
return service;
}
} }
} }
...@@ -61,7 +61,7 @@ class DataStoreServiceLogger implements IDataStoreService ...@@ -61,7 +61,7 @@ class DataStoreServiceLogger implements IDataStoreService
final String invocationStatusMessage = invocationSuccessful ? RESULT_SUCCESS : RESULT_FAILURE; final String invocationStatusMessage = invocationSuccessful ? RESULT_SUCCESS : RESULT_FAILURE;
// We put on purpose 2 spaces between the command and the message derived from the // We put on purpose 2 spaces between the command and the message derived from the
// parameters. // parameters.
operationLog.info(String.format(": %s %s%s", commandName, message, invocationStatusMessage)); operationLog.info(String.format("%s %s%s", commandName, message, invocationStatusMessage));
} }
public int getVersion(String sessionToken) public int getVersion(String sessionToken)
...@@ -86,7 +86,7 @@ class DataStoreServiceLogger implements IDataStoreService ...@@ -86,7 +86,7 @@ class DataStoreServiceLogger implements IDataStoreService
public void uploadDataSetsToCIFEX(String sessionToken, List<String> dataSetLocations, public void uploadDataSetsToCIFEX(String sessionToken, List<String> dataSetLocations,
DataSetUploadContext context) throws InvalidAuthenticationException DataSetUploadContext context) throws InvalidAuthenticationException
{ {
log("upload_data_sets", "LOCATIONS(%s) USER(%s)", dataSetLocations, context.getUserID()); log("upload_data_sets", "USER(%s) LOCATIONS(%s)", context.getUserID(), dataSetLocations);
} }
} }
/*
* Copyright 2009 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.openbis.dss.generic.server;
import java.io.Serializable;
/**
*
*
* @author Franz-Josef Elmer
*/
public class MailClientParameters implements Serializable
{
private static final long serialVersionUID = 1L;
private String from;
private String smtpUser;
private String smtpPassword;
private String smtpHost;
public final String getFrom()
{
return from;
}
public final void setFrom(String from)
{
this.from = from;
}
public final String getSmtpUser()
{
return smtpUser;
}
public final void setSmtpUser(String smtpUser)
{
this.smtpUser = smtpUser;
}
public final String getSmtpPassword()
{
return smtpPassword;
}
public final void setSmtpPassword(String smtpPassword)
{
this.smtpPassword = smtpPassword;
}
public final String getSmtpHost()
{
return smtpHost;
}
public final void setSmtpHost(String smtpHost)
{
this.smtpHost = smtpHost;
}
}
...@@ -35,6 +35,8 @@ import ch.systemsx.cisd.cifex.rpc.client.gui.IProgressListener; ...@@ -35,6 +35,8 @@ import ch.systemsx.cisd.cifex.rpc.client.gui.IProgressListener;
import ch.systemsx.cisd.cifex.shared.basic.Constants; import ch.systemsx.cisd.cifex.shared.basic.Constants;
import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.mail.IMailClient;
import ch.systemsx.cisd.common.mail.MailClient;
import ch.systemsx.cisd.common.utilities.TokenGenerator; import ch.systemsx.cisd.common.utilities.TokenGenerator;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext; import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext;
...@@ -45,7 +47,15 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext; ...@@ -45,7 +47,15 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext;
*/ */
class UploadingCommand implements IDataSetCommand class UploadingCommand implements IDataSetCommand
{ {
private static final class ProgressListener implements IProgressListener private static final long serialVersionUID = 1L;
private static final Logger operationLog =
LogFactory.getLogger(LogCategory.OPERATION, UploadingCommand.class);
private static final Logger notificationLog =
LogFactory.getLogger(LogCategory.NOTIFY, UploadingCommand.class);
private final class ProgressListener implements IProgressListener
{ {
private final File zipFile; private final File zipFile;
...@@ -61,9 +71,9 @@ class UploadingCommand implements IDataSetCommand ...@@ -61,9 +71,9 @@ class UploadingCommand implements IDataSetCommand
public void start(File file, long fileSize) public void start(File file, long fileSize)
{ {
if (notificationLog.isInfoEnabled()) if (operationLog.isInfoEnabled())
{ {
notificationLog.info("Start uploading of zip file " + file); operationLog.info("Start uploading of zip file " + file);
} }
} }
...@@ -81,38 +91,39 @@ class UploadingCommand implements IDataSetCommand ...@@ -81,38 +91,39 @@ class UploadingCommand implements IDataSetCommand
} }
} else } else
{ {
operationLog.warn("Uploading of zip file " + zipFile + " has been aborted or failed."); operationLog.warn("Uploading of zip file " + zipFile
+ " has been aborted or failed.");
sendEMail("Uploading of zip file " + zipFile.getName()
+ " with requested data sets failed.");
} }
} }
public void exceptionOccured(Throwable throwable) public void exceptionOccured(Throwable throwable)
{ {
notificationLog.error("An error occured during uploading of zip file " + zipFile + ".", throwable); notificationLog.error("An error occured during uploading of zip file " + zipFile + ".",
throwable);
} }
} }
private static final long serialVersionUID = 1L;
private static final Logger operationLog =
LogFactory.getLogger(LogCategory.OPERATION, UploadingCommand.class);
private static final Logger notificationLog =
LogFactory.getLogger(LogCategory.NOTIFY, UploadingCommand.class);
private final ICIFEXRPCServiceFactory cifexServiceFactory; private final ICIFEXRPCServiceFactory cifexServiceFactory;
private final List<String> dataSetLocations; private final List<String> dataSetLocations;
private final String comment; private final String comment;
private final String userID; private final String userID;
private final String password; private final String password;
private final String userEMail;
private final MailClientParameters mailClientParameters;
private final TokenGenerator tokenGenerator; private final TokenGenerator tokenGenerator;
UploadingCommand(ICIFEXRPCServiceFactory cifexServiceFactory, List<String> dataSetLocations, UploadingCommand(ICIFEXRPCServiceFactory cifexServiceFactory,
MailClientParameters mailClientParameters, List<String> dataSetLocations,
DataSetUploadContext context) DataSetUploadContext context)
{ {
this.cifexServiceFactory = cifexServiceFactory; this.cifexServiceFactory = cifexServiceFactory;
this.mailClientParameters = mailClientParameters;
this.dataSetLocations = dataSetLocations; this.dataSetLocations = dataSetLocations;
this.userID = context.getUserID(); this.userID = context.getUserID();
this.password = context.getPassword(); this.password = context.getPassword();
userEMail = context.getUserEMail();
this.comment = context.getComment(); this.comment = context.getComment();
tokenGenerator = new TokenGenerator(); tokenGenerator = new TokenGenerator();
} }
...@@ -136,7 +147,11 @@ class UploadingCommand implements IDataSetCommand ...@@ -136,7 +147,11 @@ class UploadingCommand implements IDataSetCommand
Uploader uploader = new Uploader(cifexService, sessionToken); Uploader uploader = new Uploader(cifexService, sessionToken);
uploader.addProgressListener(new ProgressListener(zipFile)); uploader.addProgressListener(new ProgressListener(zipFile));
uploader.upload(Arrays.asList(zipFile), Constants.USER_ID_PREFIX + userID, comment); uploader.upload(Arrays.asList(zipFile), Constants.USER_ID_PREFIX + userID, comment);
} else
{
sendEMail("Couldn't create zip file " + zipFile.getName() + " with requested data sets");
} }
zipFile.delete();
} }
private boolean fillZipFile(File store, File zipFile) private boolean fillZipFile(File store, File zipFile)
...@@ -145,13 +160,14 @@ class UploadingCommand implements IDataSetCommand ...@@ -145,13 +160,14 @@ class UploadingCommand implements IDataSetCommand
ZipOutputStream zipOutputStream = null; ZipOutputStream zipOutputStream = null;
try try
{ {
String rootPath = store.getCanonicalPath();
outputStream = new FileOutputStream(zipFile); outputStream = new FileOutputStream(zipFile);
zipOutputStream = new ZipOutputStream(outputStream); zipOutputStream = new ZipOutputStream(outputStream);
for (String location : dataSetLocations) for (String location : dataSetLocations)
{ {
try try
{ {
addTo(zipOutputStream, new File(store, location)); addTo(zipOutputStream, rootPath, new File(store, location));
} catch (IOException ex) } catch (IOException ex)
{ {
notificationLog.error("Couldn't add data set '" + location + "' to zip file.", notificationLog.error("Couldn't add data set '" + location + "' to zip file.",
...@@ -173,15 +189,13 @@ class UploadingCommand implements IDataSetCommand ...@@ -173,15 +189,13 @@ class UploadingCommand implements IDataSetCommand
zipOutputStream.close(); zipOutputStream.close();
} catch (IOException ex) } catch (IOException ex)
{ {
// ignored notificationLog.error("Couldn't close zip file", ex);
} }
} }
IOUtils.closeQuietly(outputStream);
} }
} }
private void addTo(ZipOutputStream zipOutputStream, File file) throws IOException private void addTo(ZipOutputStream zipOutputStream, String rootPath, File file) throws IOException
{ {
if (file.isFile()) if (file.isFile())
{ {
...@@ -189,7 +203,11 @@ class UploadingCommand implements IDataSetCommand ...@@ -189,7 +203,11 @@ class UploadingCommand implements IDataSetCommand
try try
{ {
in = new FileInputStream(file); in = new FileInputStream(file);
zipOutputStream.putNextEntry(new ZipEntry(file.getPath())); String path = file.getCanonicalPath().substring(rootPath.length() + 1);
ZipEntry zipEntry = new ZipEntry(path);
zipEntry.setTime(file.lastModified());
zipEntry.setMethod(ZipEntry.DEFLATED);
zipOutputStream.putNextEntry(zipEntry);
int len; int len;
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) while ((len = in.read(buffer)) > 0)
...@@ -206,9 +224,19 @@ class UploadingCommand implements IDataSetCommand ...@@ -206,9 +224,19 @@ class UploadingCommand implements IDataSetCommand
File[] files = file.listFiles(); File[] files = file.listFiles();
for (File childFile : files) for (File childFile : files)
{ {
addTo(zipOutputStream, childFile); addTo(zipOutputStream, rootPath, childFile);
} }
} }
} }
private void sendEMail(String message)
{
String from = mailClientParameters.getFrom();
String smtpHost = mailClientParameters.getSmtpHost();
String smtpUser = mailClientParameters.getSmtpUser();
String smtpPassword = mailClientParameters.getSmtpPassword();
IMailClient mailClient = new MailClient(from, smtpHost, smtpUser, smtpPassword);
mailClient.sendMessage("[Data Set Server] Uploading failed", message, "", userEMail);
}
} }
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