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