diff --git a/openbis/resource/server/spring-servlet.xml b/openbis/resource/server/spring-servlet.xml index aab78500bbccbf7f8036c406a2539b58172b705b..4569725549987df91f322b709a1a259940c71c01 100644 --- a/openbis/resource/server/spring-servlet.xml +++ b/openbis/resource/server/spring-servlet.xml @@ -27,6 +27,7 @@ --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> - <property name="maxInMemorySize" value="10000000" /> + <!-- Default is 10240 (10kB). Set to 1MB (1048576). --> + <property name="maxInMemorySize" value="1048576" /> </bean> </beans> diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/UploadedFilesBean.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/UploadedFilesBean.java index 8e08d4f052241767113e34632d60f95d4d6e1cfe..e59993e7324b2671043eb03c25dbd1c84b3774d5 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/UploadedFilesBean.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/UploadedFilesBean.java @@ -16,11 +16,16 @@ package ch.systemsx.cisd.openbis.generic.client.web.server; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import org.apache.commons.io.FileUtils; import org.springframework.web.multipart.MultipartFile; +import ch.systemsx.cisd.common.exceptions.WrappedIOException; import ch.systemsx.cisd.common.spring.IUncheckedMultipartFile; import ch.systemsx.cisd.common.spring.MultipartFileAdapter; @@ -31,6 +36,8 @@ import ch.systemsx.cisd.common.spring.MultipartFileAdapter; */ public final class UploadedFilesBean { + private static final String CLASS_SIMPLE_NAME = UploadedFilesBean.class.getSimpleName(); + private List<IUncheckedMultipartFile> multipartFiles = new ArrayList<IUncheckedMultipartFile>(); /** @@ -39,10 +46,27 @@ public final class UploadedFilesBean */ private String sessionKey; + private final File createTempFile() throws IOException + { + final File tempFile = File.createTempFile(CLASS_SIMPLE_NAME, null); + tempFile.deleteOnExit(); + return tempFile; + } + public final void addMultipartFile(final MultipartFile multipartFile) { assert multipartFile != null : "Unspecified multipart file."; - multipartFiles.add(new MultipartFileAdapter(multipartFile)); + try + { + final File tempFile = createTempFile(); + multipartFile.transferTo(tempFile); + final FileMultipartFileAdapter multipartFileAdapter = + new FileMultipartFileAdapter(multipartFile, tempFile); + multipartFiles.add(multipartFileAdapter); + } catch (final IOException ex) + { + throw new WrappedIOException(ex); + } } public final Iterable<IUncheckedMultipartFile> iterable() @@ -50,11 +74,25 @@ public final class UploadedFilesBean return multipartFiles; } + /** + * Returns the number of files uploaded. + */ public final int size() { return multipartFiles.size(); } + /** + * Deletes the transferred files. + */ + public final void deleteTransferredFiles() + { + for (final IUncheckedMultipartFile multipartFile : iterable()) + { + ((FileMultipartFileAdapter) multipartFile).destFile.delete(); + } + } + public final String getSessionKey() { return sessionKey; @@ -64,4 +102,53 @@ public final class UploadedFilesBean { this.sessionKey = sessionKey; } + + // + // Helper classes + // + + private final static class FileMultipartFileAdapter extends MultipartFileAdapter + { + private final File destFile; + + FileMultipartFileAdapter(final MultipartFile multipartFile, final File destFile) + { + super(multipartFile); + this.destFile = destFile; + } + + // + // MultipartFileAdapter + // + + @Override + public final byte[] getBytes() + { + try + { + return FileUtils.readFileToByteArray(destFile); + } catch (final IOException ex) + { + throw new WrappedIOException(ex); + } + } + + @Override + public final InputStream getInputStream() + { + try + { + return FileUtils.openInputStream(destFile); + } catch (final IOException ex) + { + throw new WrappedIOException(ex); + } + } + + @Override + public final void transferTo(final File dest) + { + throw new UnsupportedOperationException(); + } + } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java index 8cbc487726bb9861b454f4f7f103d9e78564218f..f9e03e79156f61e83987dfbe96bab90bac57f069 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java @@ -130,6 +130,7 @@ public final class GenericClientService extends AbstractClientService implements throws ch.systemsx.cisd.openbis.generic.client.web.client.exception.UserFailureException { HttpSession session = null; + UploadedFilesBean uploadedFiles = null; try { final String sessionToken = getSessionToken(); @@ -138,8 +139,7 @@ public final class GenericClientService extends AbstractClientService implements && session.getAttribute(sessionKey) instanceof UploadedFilesBean : String .format("No UploadedFilesBean object as session attribute '%s' found.", sessionKey); - final UploadedFilesBean uploadedFiles = - (UploadedFilesBean) session.getAttribute(sessionKey); + uploadedFiles = (UploadedFilesBean) session.getAttribute(sessionKey); final BisTabFileLoader<NewSample> tabFileLoader = new BisTabFileLoader<NewSample>(new IParserObjectFactoryFactory<NewSample>() { @@ -174,6 +174,10 @@ public final class GenericClientService extends AbstractClientService implements throw UserFailureExceptionTranslator.translate(e); } finally { + if (uploadedFiles != null) + { + uploadedFiles.deleteTransferredFiles(); + } if (session != null) { session.removeAttribute(sessionKey);