001 /*
002 *
003 * $Revision: 15202 $ $Date: 2009-05-15 17:00:44 +0200 (Fri, 15 May 2009) $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.frontend.wcms;
025
026 import java.io.File;
027 import java.io.IOException;
028 import java.util.Iterator;
029 import java.util.List;
030
031 import javax.servlet.http.HttpServletRequest;
032 import javax.servlet.http.HttpServletResponse;
033
034 import org.apache.commons.fileupload.FileItem;
035 import org.apache.commons.fileupload.FileItemFactory;
036 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
037 import org.apache.commons.fileupload.servlet.ServletFileUpload;
038 import org.apache.commons.io.FilenameUtils;
039 import org.apache.log4j.Logger;
040 import org.jdom.Document;
041 import org.jdom.Element;
042
043 import org.mycore.common.MCRConfiguration;
044 import org.mycore.common.MCRSession;
045 import org.mycore.common.MCRSessionMgr;
046
047 public class MCRWCMSFileUploadServlet extends MCRWCMSServlet {
048 private static final long serialVersionUID = 1L;
049
050 private static File DOCUMENT_DIR = new File(MCRConfiguration.instance().getString("MCR.WCMS.documentPath").replace('/', File.separatorChar));
051
052 private static File IMAGE_DIR = new File(MCRConfiguration.instance().getString("MCR.WCMS.imagePath").replace('/', File.separatorChar));
053
054 private static Logger LOGGER = Logger.getLogger(MCRWCMSFileUploadServlet.class);
055
056 /**
057 * Main program called by doGet and doPost.
058 */
059 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
060 boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
061
062 if (isMultiPart) {
063 int fileMaxSize = MCRConfiguration.instance().getInt("MCR.WCMS.maxUploadFileSize");
064
065 FileItemFactory factory = new DiskFileItemFactory();
066 ServletFileUpload upload = new ServletFileUpload(factory);
067 upload.setSizeMax(fileMaxSize);
068
069 // parse the request
070 try {
071 List items = upload.parseRequest(request);
072 for (Iterator it = items.iterator(); it.hasNext();) {
073 FileItem item = (FileItem) it.next();
074 if (item.isFormField()) {
075 processFormField(item);
076 } else {
077 processUploadedFile(item);
078 }
079 }
080 } catch (Exception e) {
081 LOGGER.error("Error while getting uploaded file.", e);
082 generateStatusPage(request, response, false, e.getLocalizedMessage());
083 }
084 generateStatusPage(request, response, true, null);
085 } else {
086 generateUploadPage(request, response);
087 }
088 }
089
090 private void generateStatusPage(HttpServletRequest request, HttpServletResponse response, boolean success, String message) throws IOException {
091 String msg = (message != null) ? message : "";
092 String status = (success) ? "done" : "failed";
093 forwardPage(request, response, status, msg);
094 }
095
096 private void generateUploadPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
097 forwardPage(request, response, "upload", "2");
098 }
099
100 private void forwardPage(HttpServletRequest request, HttpServletResponse response, String status, String error) throws IOException {
101 MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
102 Element rootOut = new Element("cms");
103 Document jdom = new Document(rootOut);
104 rootOut.addContent(new Element("session").setText("fileUpload"));
105 rootOut.addContent(new Element("userID").setText(mcrSession.get("userID").toString()));
106 rootOut.addContent(new Element("userClass").setText(mcrSession.get("userClass").toString()));
107 rootOut.addContent(new Element("status").setText(status));
108 rootOut.addContent(new Element("error").setText(error));
109 getLayoutService().doLayout(request,response,jdom);
110 }
111
112 private void processFormField(FileItem item) {
113 String name = item.getFieldName();
114 String value = item.getString();
115 LOGGER.info("Got form field " + name + "=" + value);
116 }
117
118 private void processUploadedFile(FileItem item) throws Exception {
119 String fileName = FilenameUtils.getName(item.getName());
120 String contentType = item.getContentType();
121 if (contentType.startsWith("image")) {
122 File imageFile = new File(IMAGE_DIR, fileName);
123 item.write(imageFile);
124 } else {
125 File docFile = new File(DOCUMENT_DIR, fileName);
126 item.write(docFile);
127 }
128 LOGGER.info("Got file " + fileName);
129 }
130 }