001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import java.io.File;
019: import java.util.Iterator;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022: import org.apache.commons.fileupload.FileItem;
023: import org.apache.commons.lang.StringUtils;
024: import org.jamwiki.utils.WikiLogger;
025: import org.jamwiki.Environment;
026: import org.jamwiki.WikiException;
027: import org.jamwiki.WikiMessage;
028: import org.jamwiki.model.WikiUser;
029: import org.jamwiki.utils.WikiUtil;
030: import org.springframework.web.servlet.ModelAndView;
031:
032: /**
033: * Used to import an XML file (in MediaWiki format), creating or updating a
034: * topic as a result.
035: */
036: public class ImportServlet extends JAMWikiServlet {
037:
038: private static final WikiLogger logger = WikiLogger
039: .getLogger(ImportServlet.class.getName());
040: protected static final String JSP_IMPORT = "import.jsp";
041:
042: /**
043: * This method handles the request after its parent class receives control.
044: *
045: * @param request - Standard HttpServletRequest object.
046: * @param response - Standard HttpServletResponse object.
047: * @return A <code>ModelAndView</code> object to be handled by the rest of the Spring framework.
048: */
049: protected ModelAndView handleJAMWikiRequest(
050: HttpServletRequest request, HttpServletResponse response,
051: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
052: String contentType = ((request.getContentType() == null) ? ""
053: : request.getContentType().toLowerCase());
054: if (contentType.indexOf("multipart") == -1) {
055: view(request, next, pageInfo);
056: } else {
057: importFile(request, next, pageInfo);
058: }
059: return next;
060: }
061:
062: /**
063: *
064: */
065: private void importFile(HttpServletRequest request,
066: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
067: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
068: Iterator iterator = ServletUtil
069: .processMultipartRequest(
070: request,
071: Environment
072: .getValue(Environment.PROP_FILE_DIR_FULL_PATH),
073: Environment
074: .getLongValue(Environment.PROP_FILE_MAX_FILE_SIZE));
075: WikiUser user = ServletUtil.currentUser();
076: XMLTopicFactory importer = new XMLTopicFactory(virtualWiki,
077: user, ServletUtil.getIpAddress(request));
078: String topicName = null;
079: while (iterator.hasNext()) {
080: FileItem item = (FileItem) iterator.next();
081: if (item.isFormField()) {
082: continue;
083: }
084: File xmlFile = saveFileItem(item);
085: // FIXME - breaks if more than one topic imported
086: topicName = importer.importWikiXml(xmlFile);
087: xmlFile.delete();
088: }
089: if (!StringUtils.isBlank(topicName)) {
090: ServletUtil.redirect(next, virtualWiki, topicName);
091: } else {
092: next.addObject("error", new WikiMessage(
093: "import.caption.failure"));
094: view(request, next, pageInfo);
095: }
096: }
097:
098: /**
099: *
100: */
101: private void view(HttpServletRequest request, ModelAndView next,
102: WikiPageInfo pageInfo) throws Exception {
103: pageInfo.setContentJsp(JSP_IMPORT);
104: pageInfo.setPageTitle(new WikiMessage("import.title"));
105: pageInfo.setSpecial(true);
106: }
107:
108: /**
109: *
110: */
111: private File saveFileItem(FileItem item) throws Exception {
112: // upload user file to the server
113: String subdirectory = "tmp";
114: File directory = new File(Environment
115: .getValue(Environment.PROP_BASE_FILE_DIR), subdirectory);
116: if (!directory.exists() && !directory.mkdirs()) {
117: throw new WikiException(new WikiMessage(
118: "upload.error.directorycreate", directory
119: .getAbsolutePath()));
120: }
121: // use current timestamp as unique file name
122: String filename = System.currentTimeMillis() + ".xml";
123: File xmlFile = new File(directory, filename);
124: // transfer remote file
125: item.write(xmlFile);
126: return xmlFile;
127: }
128: }
|