it.openutils.mgnlspring.MagnoliaMultipartResolver Maven / Gradle / Ivy
/*
* Copyright 2007 Fabrizio Giustina.
*
* 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 it.openutils.mgnlspring;
import info.magnolia.cms.beans.runtime.Document;
import info.magnolia.cms.beans.runtime.MultipartForm;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsFileUploadSupport;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;
/**
* Spring MagnoliaMultipartResolver which interacts with magnolia.
* @author fgiust
* @version $Id: MagnoliaMultipartResolver.java 344 2007-06-30 15:31:28Z fgiust $
*/
public class MagnoliaMultipartResolver extends CommonsFileUploadSupport
implements
MultipartResolver,
ServletContextAware
{
/**
* Constructor for use as bean. Determines the servlet container's temporary directory via the ServletContext passed
* in as through the ServletContextAware interface (typically by a WebApplicationContext).
* @see #setServletContext
* @see org.springframework.web.context.ServletContextAware
* @see org.springframework.web.context.WebApplicationContext
*/
public MagnoliaMultipartResolver()
{
super();
}
/**
* Constructor for standalone usage. Determines the servlet container's temporary directory via the given
* ServletContext.
* @param servletContext the ServletContext to use
*/
public MagnoliaMultipartResolver(ServletContext servletContext)
{
this();
setServletContext(servletContext);
}
/**
* Initialize the underlying org.apache.commons.fileupload.servlet.ServletFileUpload
instance. Can be
* overridden to use a custom subclass, e.g. for testing purposes.
* @param fileItemFactory the Commons FileItemFactory to use
* @return the new ServletFileUpload instance
*/
@Override
protected FileUpload newFileUpload(FileItemFactory fileItemFactory)
{
return new ServletFileUpload(fileItemFactory);
}
public void setServletContext(ServletContext servletContext)
{
if (!isUploadTempDirSpecified())
{
getFileItemFactory().setRepository(WebUtils.getTempDir(servletContext));
}
}
public boolean isMultipart(HttpServletRequest request)
{
return FileUploadBase.isMultipartContent(new ServletRequestContext(request));
}
@SuppressWarnings("unchecked")
public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException
{
// already parsed by magnolia
MultipartForm form = (MultipartForm) request.getAttribute("multipartform");
Map documents = form.getDocuments();
Map files = new HashMap();
for (String key : documents.keySet())
{
Document doc = documents.get(key);
files.put(key, new DocWrapperMultipartFile(doc));
}
Map singleParamters = form.getParameters();
Map multiParamters = new HashMap();
for (String key : singleParamters.keySet())
{
multiParamters.put(key, new String[]{singleParamters.get(key) });
}
return new DefaultMultipartHttpServletRequest(request, files, multiParamters);
}
public static class DocWrapperMultipartFile implements MultipartFile
{
Document document;
public DocWrapperMultipartFile(Document document)
{
this.document = document;
}
/**
* {@inheritDoc}
*/
public byte[] getBytes() throws IOException
{
return IOUtils.toByteArray(getInputStream());
}
/**
* {@inheritDoc}
*/
public String getContentType()
{
return document.getType();
}
/**
* {@inheritDoc}
*/
public InputStream getInputStream() throws IOException
{
return document.getStream();
}
/**
* {@inheritDoc}
*/
public String getName()
{
return document.getFileNameWithExtension();
}
/**
* {@inheritDoc}
*/
public String getOriginalFilename()
{
return document.getFileNameWithExtension();
}
/**
* {@inheritDoc}
*/
public long getSize()
{
return document.getLength();
}
/**
* {@inheritDoc}
*/
public boolean isEmpty()
{
return document.getLength() == 0;
}
/**
* {@inheritDoc}
*/
public void transferTo(File dest) throws IOException, IllegalStateException
{
FileUtils.copyFile(document.getFile(), dest);
}
}
/**
* {@inheritDoc}
*/
public void cleanupMultipart(MultipartHttpServletRequest request)
{
// nothing to do
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy