?? importwebhandler.java
字號:
/*
* $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/ImportWebHandler.java,v 1.2 2004/01/18 19:13:10 minhnn Exp $
* $Author: minhnn $
* $Revision: 1.2 $
* $Date: 2004/01/18 19:13:10 $
*
* ====================================================================
*
* Copyright (C) 2002-2004 by MyVietnam.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* All copyright notices regarding mvnForum MUST remain intact
* in the scripts and in the outputted HTML.
* The "powered by" text/logo with a link back to
* http://www.mvnForum.com and http://www.MyVietnam.net in the
* footer of the pages MUST remain visible when the pages
* are viewed on the internet or intranet.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Support can be obtained from support forums at:
* http://www.mvnForum.com/mvnforum/index
*
* Correspondence and Marketing Questions can be sent to:
* info@MyVietnam.net
*
* @author: Igor Manic imanic@users.sourceforge.net
*/
package com.mvnforum.admin;
import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mvnforum.admin.importexport.jive.ImportJive;
import com.mvnforum.admin.importexport.mvnforum.ImportMvnForum;
import com.mvnforum.auth.*;
import com.mvnforum.user.UserModuleConfig;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.fileupload.*;
import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
import net.myvietnam.mvncore.util.FileUtil;
import net.myvietnam.mvncore.util.ParamUtil;
/**
* @author <a href="mailto:imanic@users.sourceforge.net">Igor Manic</a>
* @version $Revision: 1.2 $, $Date: 2004/01/18 19:13:10 $
* <br/>
* <code>ImportWebHandler</code> class implements methods that process HTTP
* requests for import. Data could be imported from MVN Forum XML file conforming
* <a href="http://www.mvnforum.com/mvn.dtd">http://www.mvnforum.com/mvn.dtd</a>,
* or from MVN Forum backup ZIP file. Data can also be migrated from other sources
* (for now, it's only available for <code>Jive Forums</code>).
*
*/
class ImportWebHandler {
/** Message log. */
private static Log log = LogFactory.getLog(ImportWebHandler.class);
private static PrintWriter outputHtml=null;
/**
* All output messages (status, informational and error) will be written
* to <code>outHtml</code>.
*
* @param outHtml <code>PrintWriter</code> to write all output to. If it is
* <code>null</code>, there will be no output messages.
*/
public static void setOutputHtmlWriter(PrintWriter outHtml) {
ImportWebHandler.outputHtml = outHtml;
}
/** Cannot instantiate. */
private ImportWebHandler() {
}
public static Vector getBackupFilesOnServer() {
Vector result = new Vector();
File dir = new File(MVNForumConfig.getBackupDir());
File[] files = dir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()) {
result.add(DisableHtmlTagFilter.filter(file.getName()));
} //else ignore subdirs
}
}
return result;
}
/**
* This is the main import request dispatcher. It parses request
* parameters, and decides what is the type of file used and what procedure
* to call (restore from mvnForum XML, or mvnForum ZIP, or migrate from Jive XML, ...).
* It also extracts additional request parameters, if they exist. Then it calls
* the desired procedure and gives it all parameters it extracted from the request.<br/>
* //todo Igor: add @see tags for all importXml/Zip methods
*
* @param request <code>HttpServletRequest</code> object of the request.
* @param response <code>HttpServletResponse</code> object of the request.
*
* @throws ImportException
* @throws AuthenticationException
* @throws DatabaseException
* @throws AssertionException
*
*/
public static void importXmlZip(HttpServletRequest request, HttpServletResponse response)
throws ImportException, AuthenticationException, DatabaseException, AssertionException {
OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
FileUpload fileUpload = new FileUpload();
fileUpload.setSizeMax(MVNForumConfig.getMaxImportSize());
fileUpload.setSizeThreshold(100000);// max memory used = 100K
fileUpload.setRepositoryPath(MVNForumConfig.getTempDir());
List fileItems;
try {
fileItems = fileUpload.parseRequest(request);
} catch (FileUploadException ex) {
log.error("Cannot upload", ex);
throw new ImportException("Cannot upload file. Detailed reason: " + ex.getMessage());
}
// values that must get from the form
String serverImportFilename = null; //if importing from the server backup directory
String clientImportFilename = null; //if uploading import file
int clientImportFileSize = 0; //if uploading import file
FileItem clientImportFileItem = null; //if uploading import file
int importType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default is MVN Forum XML
boolean clearIfError = true; //default is to reset database in case of error
int messageLevel = MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES;
Vector otherFieldValues=new Vector();
int logonMemberID = onlineUser.getMemberID();
String logonMemberName = onlineUser.getMemberName();
Calendar importTime = Calendar.getInstance();
String importIP = request.getRemoteAddr();
File importFile = null;
boolean deleteAfterImport = false;
try {
for (int i = 0; i < fileItems.size(); i++ ) {
FileItem currentFileItem = (FileItem)fileItems.get(i);
//content-type: currentFileItem.getContentType();
String fieldName = currentFileItem.getFieldName();
boolean isFormField = currentFileItem.isFormField();
if ( (fieldName.equals("ServerImportFile")) && (isFormField) ) {
serverImportFilename=EnableHtmlTagFilter.filter(currentFileItem.getString());
log.debug("serverImportFilename = " + serverImportFilename);
currentFileItem.delete(); currentFileItem=null;
} else if (fieldName.equals("ClientImportFile")) {
if (isFormField) {
throw new AssertionException("Cannot process uploaded import file with a form field.");
}
clientImportFileSize = (int)currentFileItem.getSize();
String fullFilePath = currentFileItem.getName();
clientImportFilename = FileUtil.getFileName(fullFilePath);
log.debug("clientImportFilename = " + clientImportFilename);
// now save to clientImportFileItem
clientImportFileItem = currentFileItem;
} else if ( (fieldName.equals("ImportType")) && (isFormField) ) {
try {
importType=Integer.parseInt(currentFileItem.getString());
} catch (NumberFormatException e) {
importType=MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default
}
currentFileItem.delete(); currentFileItem=null;
} else if ( (fieldName.equals("ClearIfError")) && (isFormField) ) {
int clearInt=1;
try {
clearInt=Integer.parseInt(currentFileItem.getString());
} catch (NumberFormatException e) {
clearInt=1; //default
}
if (clearInt==0) clearIfError=false;
else clearIfError=true;
currentFileItem.delete(); currentFileItem=null;
} else if ( (fieldName.equals("MessageLevel")) && (isFormField) ) {
try {
messageLevel=Integer.parseInt(currentFileItem.getString());
} catch (NumberFormatException e) {
messageLevel=MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES; //default
}
currentFileItem.delete(); currentFileItem=null;
} else { //other field values
if (isFormField) {
otherFieldValues.add(new String(fieldName));
otherFieldValues.add(currentFileItem.getString());
}
currentFileItem.delete(); currentFileItem=null;
}
} //for (int i = 0; i < fileItems.size(); i++ )
if ((serverImportFilename!=null) && (!serverImportFilename.equals(""))) {
//import from server backup repository
String filepath = MVNForumConfig.getBackupDir() + File.separatorChar + serverImportFilename;
importFile=new File(filepath);
deleteAfterImport=false;
} else {
//upload client import file
deleteAfterImport=true; //delete uploaded file
if ((clientImportFilename==null) || (clientImportFilename.equals(""))) {
log.error("Cannot import. Please choose either a file on server, or upload a file.");
throw new ImportException("Cannot import. Please choose either a file on server, or upload a file.");
} else {
log.debug("ImportWebHandler : process upload with temp dir = " + MVNForumConfig.getTempDir());
if (clientImportFileSize == 0) {
throw new ImportException("Cannot process an import file with size = 0. Please check the file size or check if your file is missing.");
}
}
try {
String filepath = MVNForumConfig.getTempDir() + File.separatorChar +
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -