diff --git a/datastore_server/source/java/applicationContext.xml b/datastore_server/source/java/applicationContext.xml index b0b9320176905673fb7e61cff907a77a0b8847f6..39f4e82c0da3e2dde2f4b7e664da562971d1d7f2 100644 --- a/datastore_server/source/java/applicationContext.xml +++ b/datastore_server/source/java/applicationContext.xml @@ -32,6 +32,14 @@ <property name="service"> <bean class="ch.systemsx.cisd.openbis.dss.generic.server.DataStoreService"> <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}"/> </bean> </property> diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/CIFEXRPCServiceFactory.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/CIFEXRPCServiceFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..27b38f585f62a385a762b015c5c8025ce305f2ee --- /dev/null +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/CIFEXRPCServiceFactory.java @@ -0,0 +1,51 @@ +/* + * 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 diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java index f17ab8f445a7c3489da103d8618859d2a66ec4a9..ad4cfe4c46f907f5fe9e15db045caa57267f5f7a 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreService.java @@ -24,9 +24,6 @@ import java.util.List; import org.springframework.beans.factory.InitializingBean; 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.InvalidSessionException; import ch.systemsx.cisd.common.exceptions.WrappedIOException; @@ -47,11 +44,13 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic private final IDataSetCommandExecutorFactory commandExecutorFactory; + private final MailClientParameters mailClientParameters; + private File storeRoot; private IDataSetCommandExecutor commandExecuter; - public DataStoreService(SessionTokenManager sessionTokenManager) + public DataStoreService(SessionTokenManager sessionTokenManager, MailClientParameters mailClientParameters) { this(sessionTokenManager, new IDataSetCommandExecutorFactory() { @@ -59,14 +58,15 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic { return new DataSetCommandExecuter(store); } - }); + }, mailClientParameters); } DataStoreService(SessionTokenManager sessionTokenManager, - IDataSetCommandExecutorFactory commandExecutorFactory) + IDataSetCommandExecutorFactory commandExecutorFactory, MailClientParameters mailClientParameters) { this.sessionTokenManager = sessionTokenManager; this.commandExecutorFactory = commandExecutorFactory; + this.mailClientParameters = mailClientParameters; } public final void setStoreRoot(File storeRoot) @@ -158,36 +158,8 @@ public class DataStoreService extends AbstractServiceWithLogger<IDataStoreServic { throw new InvalidSessionException("User couldn't be authenticated at CIFEX."); } - commandExecuter.scheduleCommand(new UploadingCommand(serviceFactory, 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; - } + commandExecuter.scheduleCommand(new UploadingCommand(serviceFactory, mailClientParameters, + dataSetLocations, context)); } } diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceLogger.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceLogger.java index f4b7ca8cba3e31e0182b889cbb57c9933bc46b85..17a618694d63d719a0ead33514918252ce8a9692 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceLogger.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/DataStoreServiceLogger.java @@ -61,7 +61,7 @@ class DataStoreServiceLogger implements IDataStoreService final String invocationStatusMessage = invocationSuccessful ? RESULT_SUCCESS : RESULT_FAILURE; // We put on purpose 2 spaces between the command and the message derived from the // 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) @@ -86,7 +86,7 @@ class DataStoreServiceLogger implements IDataStoreService public void uploadDataSetsToCIFEX(String sessionToken, List<String> dataSetLocations, 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); } } diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/MailClientParameters.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/MailClientParameters.java new file mode 100644 index 0000000000000000000000000000000000000000..2313681a71d6cb6c073dade7b9d06fb8cb845784 --- /dev/null +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/MailClientParameters.java @@ -0,0 +1,77 @@ +/* + * 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; + } +} diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/UploadingCommand.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/UploadingCommand.java index 026a74554c21a17871b2263effaa8fe7743befe5..c2f10063a36d7b37deb2ae77f47ccd142ae613d1 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/UploadingCommand.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/UploadingCommand.java @@ -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.common.logging.LogCategory; 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.openbis.generic.shared.dto.DataSetUploadContext; @@ -45,7 +47,15 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetUploadContext; */ 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; @@ -61,9 +71,9 @@ class UploadingCommand implements IDataSetCommand 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 } } 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) { - 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 List<String> dataSetLocations; private final String comment; private final String userID; private final String password; + private final String userEMail; + private final MailClientParameters mailClientParameters; private final TokenGenerator tokenGenerator; - UploadingCommand(ICIFEXRPCServiceFactory cifexServiceFactory, List<String> dataSetLocations, + UploadingCommand(ICIFEXRPCServiceFactory cifexServiceFactory, + MailClientParameters mailClientParameters, List<String> dataSetLocations, DataSetUploadContext context) { this.cifexServiceFactory = cifexServiceFactory; + this.mailClientParameters = mailClientParameters; this.dataSetLocations = dataSetLocations; this.userID = context.getUserID(); this.password = context.getPassword(); + userEMail = context.getUserEMail(); this.comment = context.getComment(); tokenGenerator = new TokenGenerator(); } @@ -136,7 +147,11 @@ class UploadingCommand implements IDataSetCommand Uploader uploader = new Uploader(cifexService, sessionToken); uploader.addProgressListener(new ProgressListener(zipFile)); 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) @@ -145,13 +160,14 @@ class UploadingCommand implements IDataSetCommand ZipOutputStream zipOutputStream = null; try { + String rootPath = store.getCanonicalPath(); outputStream = new FileOutputStream(zipFile); zipOutputStream = new ZipOutputStream(outputStream); for (String location : dataSetLocations) { try { - addTo(zipOutputStream, new File(store, location)); + addTo(zipOutputStream, rootPath, new File(store, location)); } catch (IOException ex) { notificationLog.error("Couldn't add data set '" + location + "' to zip file.", @@ -173,15 +189,13 @@ class UploadingCommand implements IDataSetCommand zipOutputStream.close(); } 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()) { @@ -189,7 +203,11 @@ class UploadingCommand implements IDataSetCommand try { 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; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) @@ -206,9 +224,19 @@ class UploadingCommand implements IDataSetCommand File[] files = file.listFiles(); 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); + } + }