001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/ImportWebHandler.java,v 1.18 2007/10/09 11:09:13 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.18 $
005: * $Date: 2007/10/09 11:09:13 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Igor Manic
039: */
040: package com.mvnforum.admin;
041:
042: import java.io.*;
043: import java.util.*;
044:
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047:
048: import com.mvnforum.MVNForumConfig;
049: import com.mvnforum.MVNForumResourceBundle;
050: import com.mvnforum.admin.importexport.jive.ImportJive;
051: import com.mvnforum.admin.importexport.mvnforum.ImportMvnForum;
052: import com.mvnforum.auth.*;
053: import com.mvnforum.user.UserModuleConfig;
054: import net.myvietnam.mvncore.exception.*;
055: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
056: import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
057: import net.myvietnam.mvncore.service.FileUploadParserService;
058: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
059: import net.myvietnam.mvncore.util.*;
060: import net.myvietnam.mvncore.web.*;
061: import net.myvietnam.mvncore.web.fileupload.FileItem;
062: import net.myvietnam.mvncore.web.fileupload.FileUploadException;
063: import net.myvietnam.mvncore.web.impl.GenericRequestServletImpl;
064: import org.apache.commons.logging.Log;
065: import org.apache.commons.logging.LogFactory;
066:
067: /**
068: * @author Igor Manic
069: * @version $Revision: 1.18 $, $Date: 2007/10/09 11:09:13 $
070: * <br/>
071: * <code>ImportWebHandler</code> class implements methods that process HTTP
072: * requests for import. Data could be imported from MVN Forum XML file conforming
073: * <a href="http://www.mvnforum.com/mvn.dtd">http://www.mvnforum.com/mvn.dtd</a>,
074: * or from MVN Forum backup ZIP file. Data can also be migrated from other sources
075: * (for now, it's only available for <code>Jive Forums</code>).
076: *
077: */
078: public class ImportWebHandler {
079:
080: /** Message log. */
081: private static Log log = LogFactory.getLog(ImportWebHandler.class);
082:
083: private static PrintWriter outputHtml = null;
084:
085: /**
086: * All output messages (status, informational and error) will be written
087: * to <code>outHtml</code>.
088: *
089: * @param outHtml <code>PrintWriter</code> to write all output to. If it is
090: * <code>null</code>, there will be no output messages.
091: */
092: public static void setOutputHtmlWriter(PrintWriter outHtml) {
093: ImportWebHandler.outputHtml = outHtml;
094: }
095:
096: /** Cannot instantiate. */
097: private ImportWebHandler() {
098: }
099:
100: public static Vector getBackupFilesOnServer() {
101: Vector result = new Vector();
102: File dir = new File(MVNForumConfig.getBackupDir());
103: File[] files = dir.listFiles();
104: if (files != null) {
105: for (int i = 0; i < files.length; i++) {
106: File file = files[i];
107: if (file.isFile()) {
108: result.add(DisableHtmlTagFilter.filter(file
109: .getName()));
110: } //else ignore subdirs
111: }
112: }
113: return result;
114: }
115:
116: /**
117: * This is the main import request dispatcher. It parses request
118: * parameters, and decides what is the type of file used and what procedure
119: * to call (restore from mvnForum XML, or mvnForum ZIP, or migrate from Jive XML, ...).
120: * It also extracts additional request parameters, if they exist. Then it calls
121: * the desired procedure and gives it all parameters it extracted from the request.<br/>
122: * //todo Igor: add @see tags for all importXml/Zip methods
123: *
124: * @param request <code>HttpServletRequest</code> object of the request.
125: * @param response <code>HttpServletResponse</code> object of the request.
126: *
127: * @throws ImportException
128: * @throws AuthenticationException
129: * @throws DatabaseException
130: *
131: */
132: public static void importXmlZip(HttpServletRequest request,
133: HttpServletResponse response) throws ImportException,
134: AuthenticationException, DatabaseException {
135:
136: OnlineUserManager onlineUserManager = OnlineUserManager
137: .getInstance();
138: OnlineUser onlineUser = onlineUserManager
139: .getOnlineUser(request);
140: MVNForumPermission permission = onlineUser.getPermission();
141: permission.ensureCanAdminSystem();
142:
143: Locale locale = I18nUtil.getLocaleInRequest(request);
144:
145: int sizeMax = MVNForumConfig.getMaxImportSize();
146: int sizeThreshold = 100000;// max memory used = 100K
147: String tempDir = MVNForumConfig.getTempDir();
148: List fileItems;
149: try {
150: GenericRequest genericRequest = new GenericRequestServletImpl(
151: request);
152: FileUploadParserService fileUploadParserService = MvnCoreServiceFactory
153: .getMvnCoreService().getFileUploadParserService();
154: fileItems = fileUploadParserService.parseRequest(
155: genericRequest, sizeMax, sizeThreshold, tempDir,
156: "UTF-8");
157: } catch (FileUploadException ex) {
158: log.error("Cannot upload", ex);
159: String localizedMessage = MVNForumResourceBundle.getString(
160: locale, "java.io.IOException.cannot_upload",
161: new Object[] { ex.getMessage() });
162: throw new ImportException(localizedMessage);
163: //throw new ImportException("Cannot upload file. Detailed reason: " + ex.getMessage());
164: }
165:
166: // values that must get from the form
167: String serverImportFilename = null; //if importing from the server backup directory
168: String clientImportFilename = null; //if uploading import file
169: int clientImportFileSize = 0; //if uploading import file
170: FileItem clientImportFileItem = null; //if uploading import file
171:
172: int importType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default is MVN Forum XML
173: boolean clearIfError = true; //default is to reset database in case of error
174: int messageLevel = MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES;
175: Vector otherFieldValues = new Vector();
176:
177: int logonMemberID = onlineUser.getMemberID();
178: String logonMemberName = onlineUser.getMemberName();
179: Calendar importTime = Calendar.getInstance();
180: String importIP = request.getRemoteAddr();
181: File importFile = null;
182: boolean deleteAfterImport = false;
183:
184: try {
185: for (int i = 0; i < fileItems.size(); i++) {
186: FileItem currentFileItem = (FileItem) fileItems.get(i);
187: //content-type: currentFileItem.getContentType();
188: String fieldName = currentFileItem.getFieldName();
189: boolean isFormField = currentFileItem.isFormField();
190:
191: if ((fieldName.equals("ServerImportFile"))
192: && (isFormField)) {
193: serverImportFilename = EnableHtmlTagFilter
194: .filter(currentFileItem.getString());
195: log.debug("serverImportFilename = "
196: + serverImportFilename);
197: currentFileItem.delete();
198: currentFileItem = null;
199:
200: } else if (fieldName.equals("ClientImportFile")) {
201: if (isFormField) {
202: String localizedMessage = MVNForumResourceBundle
203: .getString(
204: locale,
205: "mvncore.exception.ImportException.cannot_process_uploaded_import_file_with_a_form_field");
206: throw new ImportException(localizedMessage);
207: //throw new ImportException("Cannot process uploaded import file with a form field.");
208: }
209: clientImportFileSize = (int) currentFileItem
210: .getSize();
211: String fullFilePath = currentFileItem.getName();
212: clientImportFilename = FileUtil
213: .getFileName(fullFilePath);
214: log.debug("clientImportFilename = "
215: + clientImportFilename);
216: // now save to clientImportFileItem
217: clientImportFileItem = currentFileItem;
218:
219: } else if ((fieldName.equals("ImportType"))
220: && (isFormField)) {
221: try {
222: importType = Integer.parseInt(currentFileItem
223: .getString());
224: } catch (NumberFormatException e) {
225: importType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default
226: }
227: currentFileItem.delete();
228: currentFileItem = null;
229: } else if ((fieldName.equals("ClearIfError"))
230: && (isFormField)) {
231: int clearInt = 1;
232: try {
233: clearInt = Integer.parseInt(currentFileItem
234: .getString());
235: } catch (NumberFormatException e) {
236: clearInt = 1; //default
237: }
238: if (clearInt == 0)
239: clearIfError = false;
240: else
241: clearIfError = true;
242: currentFileItem.delete();
243: currentFileItem = null;
244: } else if ((fieldName.equals("MessageLevel"))
245: && (isFormField)) {
246: try {
247: messageLevel = Integer.parseInt(currentFileItem
248: .getString());
249: } catch (NumberFormatException e) {
250: messageLevel = MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES; //default
251: }
252: currentFileItem.delete();
253: currentFileItem = null;
254:
255: } else { //other field values
256: if (isFormField) {
257: otherFieldValues.add(new String(fieldName));
258: otherFieldValues.add(currentFileItem
259: .getString());
260: }
261: currentFileItem.delete();
262: currentFileItem = null;
263: }
264: } //for (int i = 0; i < fileItems.size(); i++ )
265:
266: if ((serverImportFilename != null)
267: && (!serverImportFilename.equals(""))) {
268: //import from server backup repository
269: String filepath = MVNForumConfig.getBackupDir()
270: + File.separatorChar + serverImportFilename;
271: importFile = new File(filepath);
272: deleteAfterImport = false;
273:
274: } else {
275: //upload client import file
276: deleteAfterImport = true; //delete uploaded file
277: if ((clientImportFilename == null)
278: || (clientImportFilename.equals(""))) {
279: log
280: .error("Cannot import. Please choose either a file on server, or upload a file.");
281: String localizedMessage = MVNForumResourceBundle
282: .getString(
283: locale,
284: "mvncore.exception.ImportException.cannot_import.choose_a_file_on_server_or_upload_a_file");
285: throw new ImportException(localizedMessage);
286: //throw new ImportException("Cannot import. Please choose either a file on server, or upload a file.");
287: } else {
288: log
289: .debug("ImportWebHandler : process upload with temp dir = "
290: + MVNForumConfig.getTempDir());
291: if (clientImportFileSize == 0) {
292: String localizedMessage = MVNForumResourceBundle
293: .getString(locale,
294: "mvncore.exception.ImportException.cannot_process_import.file_size_is_zero");
295: throw new ImportException(localizedMessage);
296: //throw new ImportException("Cannot process an import file with size = 0. Please check the file size or check if your file is missing.");
297: }
298: }
299:
300: try {
301: String filepath = MVNForumConfig.getTempDir()
302: + File.separatorChar
303: + "import-"
304: + ((importType == MVNForumConfig.IMPORTEXPORT_TYPE_JIVE_XML) ? "jive-"
305: : "")
306: + importTime.get(Calendar.YEAR)
307: + "-"
308: + importTime.get(Calendar.MONTH)
309: + "-"
310: + importTime.get(Calendar.DAY_OF_MONTH)
311: + "-"
312: + importTime.get(Calendar.HOUR_OF_DAY)
313: + "-"
314: + importTime.get(Calendar.MINUTE)
315: + "-"
316: + importTime.get(Calendar.SECOND)
317: + ((importType == MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP) ? ".zip"
318: : ".xml");
319: log
320: .debug("Client import file to save to file system = "
321: + filepath);
322: clientImportFileItem.write(new File(filepath));
323: importFile = new File(filepath);
324: clientImportFileItem.delete();
325: clientImportFileItem = null;
326: } catch (Exception ex) {
327: log.error("Cannot save the import file.", ex);
328: String localizedMessage = MVNForumResourceBundle
329: .getString(locale,
330: "mvncore.exception.ImportException.cannot_import.cannot_save_import_file");
331: throw new ImportException(localizedMessage);
332: //throw new ImportException("Cannot save the import file to the file system.", ex);
333: }
334: }
335:
336: } finally {
337: for (int i = 0; i < fileItems.size(); i++) {
338: try {
339: ((FileItem) fileItems.get(i)).delete();
340: } catch (Exception e) {
341: }
342: }
343: fileItems.clear();
344: fileItems = null;
345: }
346:
347: if (!importFile.exists()) {
348: String localizedMessage = MVNForumResourceBundle
349: .getString(locale,
350: "mvncore.exception.ImportException.cannot_find_import_file");
351: throw new ImportException(localizedMessage);
352: //throw new ImportException("Can't find import file.");
353: } else if (!importFile.isFile()) {
354: String localizedMessage = MVNForumResourceBundle
355: .getString(locale,
356: "mvncore.exception.ImportException.import_file_is_not_a_file");
357: throw new ImportException(localizedMessage);
358: //throw new ImportException("Import \"file\" is actually not a file (probably a directory).");
359: } else if (!importFile.canRead()) {
360: String localizedMessage = MVNForumResourceBundle
361: .getString(locale,
362: "mvncore.exception.ImportException.dont_have_read_permission");
363: throw new ImportException(localizedMessage);
364: //throw new ImportException("Don't have a permission to read an import file from server.");
365: }
366:
367: try {
368: switch (importType) {
369: case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML:
370: ImportMvnForum.importXml(importFile, request, response,
371: logonMemberID, logonMemberName, importTime,
372: importIP, clearIfError, messageLevel);
373: break;
374: case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP:
375: ImportMvnForum.importZip(importFile, request, response,
376: logonMemberID, logonMemberName, importTime,
377: importIP, clearIfError, messageLevel);
378: break;
379: case MVNForumConfig.IMPORTEXPORT_TYPE_JIVE_XML:
380: ImportJive.importXml(importFile, request, response,
381: logonMemberID, logonMemberName, importTime,
382: importIP, clearIfError, otherFieldValues,
383: messageLevel);
384: break;
385:
386: default:
387: log.error("importXmlZip: invalid importType = "
388: + importType);
389: String localizedMessage = MVNForumResourceBundle
390: .getString(locale,
391: "mvncore.exception.ImportException.invalid_import_type_specified");
392: throw new ImportException(localizedMessage);
393: //throw new ImportException("Invalid import type specified.");
394: }
395: } catch (ImportException e) {
396: /* In case of this exception, I already printed error message.
397: * Now I should NOT allow this exception to propagate higher, since
398: * it will then initiate errorpage, which I don't want, and can't
399: * allow because already commited output.
400: * So, I don't do anything here, just catch it.
401: */
402: } finally {
403: /* Logout current user, even if the import raised an exception,
404: because database data are probably changed anyway. */
405: onlineUserManager.logout(request, response);
406: //this didn't work: ManagerFactory.getOnlineUserFactory().logout(request, response);
407:
408: //delete uploaded file, but only if it's not already deleted !
409: if (deleteAfterImport && (importFile.exists())) {
410: importFile.delete();
411: }
412: importFile = null;
413: }
414: }
415:
416: // =================================================================
417: // ===== PRINTING STATUS AND ERROR MESSAGES TO THE OUTPUT HTML =====
418: // =================================================================
419: private static boolean oddLine = true;
420: private static boolean outputHtmlNotEnded = false;
421: private static boolean outputNonImportantMessages = true;
422: private static boolean outputImportantMessages = true;
423:
424: //outputErrors is always true
425:
426: public static void setMessageOutputLevel(int messageLevel) {
427: switch (messageLevel) {
428: case MVNForumConfig.MESSAGE_LEVEL_ONLY_ERRORS:
429: outputImportantMessages = false;
430: outputNonImportantMessages = false;
431: break;
432: case MVNForumConfig.MESSAGE_LEVEL_IMPORTANT_MESSAGES:
433: outputImportantMessages = true;
434: outputNonImportantMessages = false;
435: break;
436: default: //MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES
437: outputNonImportantMessages = true;
438: outputImportantMessages = true;
439: }
440: }
441:
442: public static void startHtml(HttpServletRequest request) {
443: if (outputHtml != null) {
444: outputHtmlNotEnded = true;
445: outputHtml.println("<html>");
446: outputHtml.println("<head>");
447: outputHtml
448: .println(" <title>mvnForum - Import/Restore Process</title>");
449: outputHtml
450: .println(" <meta name=\"copyright\" content=\"This software is copyrighted by MyVietnam.net and released under the terms of the GNU General Public License GPL license. All rights reserved.\">");
451: outputHtml
452: .println(" <meta name=\"Keywords\" content=\"mvnForum, server side java, online meetings, collaboration, bulletin board, board, forum, discussion board, jsp, servlet, java, j2se, j2ee, database, jdbc, connection pool, mysql, MyVietnam, mvnForum, framework, web app, web application, servlet container, tomcat, jboss, weblogic, websphere\">");
453: outputHtml
454: .println(" <meta name=\"Description\" content=\"mvnForum is a powerful Jsp/Servlet forum (discussion board) - based on Java technology.\">");
455: outputHtml
456: .println(" <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
457: outputHtml
458: .println(" <meta http-equiv=\"Expires\" content=\"-1\">");
459: outputHtml
460: .println(" <link href=\""
461: + request.getContextPath()
462: + "/mvnplugin/mvnforum/css/style.css\" rel=\"stylesheet\" type=\"text/css\">");
463: outputHtml.println("</head>");
464: outputHtml
465: .println("<body leftmargin=\"0\" topmargin=\"0\">");
466: outputHtml.println("<br/>");
467: outputHtml.println("");
468: outputHtml
469: .println("<table class=\"tborder\" width=\"95%\" cellpadding=\"3\" align=\"center\">");
470: outputHtml.println("<tr class=\"theader\">");
471: outputHtml
472: .println(" <td align=\"center\">Messages during the process</td>");
473: outputHtml.println("</tr>");
474: outputHtml.flush();
475: oddLine = true;
476: }
477: }
478:
479: public static void endHtml() {
480: if (outputHtml != null) {
481: outputHtml.println("</table>");
482: outputHtml.println("");
483: outputHtml.println("<br/>");
484: outputHtml.println("</body>");
485: outputHtml.println("</html>");
486: outputHtml.flush();
487: outputHtmlNotEnded = false;
488: }
489: }
490:
491: public static void addMessage(String message) {
492: if (outputNonImportantMessages) {
493: outputHtmlTableRow(message, "messageText");
494: }
495: }
496:
497: public static void addErrorMessage(String message) {
498: outputHtmlTableRow(message, "messageTextBoldRed");
499: }
500:
501: /**
502: * Adds warning that the process contained some errors, and the database
503: * might be inconsistent now. It also tries to clear the database and
504: * create default entries, including "admin" member.
505: */
506: public static void addFinalErrorHandling(
507: HttpServletRequest request, boolean resetAllData) {
508: outputHtmlTableRow(" ", "messageTextBoldRed"); //add one empty row in output message table
509: String message = "There was an error during the import. DATA ARE NOW PROBABLY DAMAGED.<br/>";
510: if (resetAllData)
511: message += "Trying to clear the database and create \"admin\" member (password will be \"admin\")...<br/>";
512: outputHtmlTableRow(message, "messageTextBoldRed");
513:
514: String indexUrl = request.getContextPath()
515: + UserModuleConfig.getUrlPattern() + "/index";
516: String finalMessage = "If you still want to import these data that produced an error, "
517: + "please correct above errors first.<br/>"
518: + "Then proceed to: "
519: + "<a href=\"login\" class=\"menuLink\">Login</a> | "
520: + "<a href=\"index\" class=\"menuLink\">Admin Index</a> | "
521: + "<a href=\""
522: + indexUrl
523: + "\" class=\"menuLink\">Forum Index</a><br/>"
524: + "<span class=\"messageTextBoldRed\">"
525: + "Notice you should enter new username and password!"
526: + "</span>";
527: try {
528: if (resetAllData) {
529: ImportWebHelper.clearDatabase();
530: ImportWebHelper.clearFiles(request.getSession()
531: .getServletContext());
532: ImportWebHelper.createDefaultContents();
533: }
534: } catch (IOException e) {
535: addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
536: } catch (DatabaseException e) {
537: addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
538: } catch (CreateException e) {
539: addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
540: } catch (DuplicateKeyException e) {
541: addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
542: } catch (ObjectNotFoundException e) {
543: addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
544: } catch (ForeignKeyNotFoundException e) {
545: addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
546: } finally {
547: outputHtmlTableRow(finalMessage, "messageTextBoldRed");
548: }
549: }
550:
551: public static void addSuccessMessage(HttpServletRequest request) {
552: String indexUrl = request.getContextPath()
553: + UserModuleConfig.getUrlPattern() + "/index";
554: String message = "Successful. Proceed to: "
555: + "<a href=\"login\" class=\"menuLink\">Login</a> | "
556: + "<a href=\"index\" class=\"menuLink\">Admin Index</a> | "
557: + "<a href=\"" + indexUrl
558: + "\" class=\"menuLink\">Forum Index</a><br/>"
559: + "<span class=\"messageTextBoldRed\">"
560: + "Notice you should enter new username and password!"
561: + "</span>";
562: outputHtmlTableRow(message, "messageTextBold");
563: }
564:
565: public static void addImportantMessage(String message) {
566: if (outputImportantMessages) {
567: outputHtmlTableRow(message, "messageTextBoldRed");
568: }
569: }
570:
571: private static void outputHtmlTableRow(String message,
572: String cssClass) {
573: if (outputHtml != null) {
574: if (oddLine) {
575: outputHtml.println("<tr class=\"trow1\">");
576: } else {
577: outputHtml.println("<tr class=\"trow2\">");
578: }
579: outputHtml.println(" <td class=\"" + cssClass + "\">"
580: + message + "</td>");
581: outputHtml.println("</tr>");
582: outputHtml.flush();
583: oddLine = !oddLine;
584: }
585: }
586:
587: }
|