001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/ExportWebHandler.java,v 1.17 2007/10/09 11:09:12 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.17 $
005: * $Date: 2007/10/09 11:09:12 $
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.text.SimpleDateFormat;
044: import java.util.Calendar;
045: import java.util.Locale;
046:
047: import javax.servlet.http.HttpServletRequest;
048: import javax.servlet.http.HttpServletResponse;
049:
050: import com.mvnforum.MVNForumConfig;
051: import com.mvnforum.MVNForumResourceBundle;
052: import com.mvnforum.auth.*;
053: import net.myvietnam.mvncore.exception.*;
054: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
055: import net.myvietnam.mvncore.util.*;
056: import net.myvietnam.mvncore.web.GenericRequest;
057: import org.apache.commons.logging.Log;
058: import org.apache.commons.logging.LogFactory;
059:
060: /**
061: * @author <a href="mailto:">Igor Manic </a>
062: * @version $Revision: 1.17 $, $Date: 2007/10/09 11:09:12 $<br/>
063: * <code>ExportWebHandler</code> class implements methods that
064: * process HTTP requests for export. Data could be exported to MVN
065: * Forum XML file conforming <a
066: * href="http://www.mvnforum.com/mvn.dtd">http://www.mvnforum.com/mvn.dtd
067: * </a>, or to MVN Forum backup ZIP file.
068: *
069: */
070: public class ExportWebHandler {
071:
072: /** Message log. */
073: private static Log log = LogFactory.getLog(ExportWebHandler.class);
074:
075: /** Cannot instantiate. */
076: private ExportWebHandler() {
077: }
078:
079: /**
080: * Processes export requests made from corresponding HTML page. <br/>
081: * Request should contain the parameter <code>ExportType</code> that tells
082: * us what type of destination backup file user wants.
083: *
084: * @param request
085: * <code>HttpServletRequest</code> of the request.
086: *
087: * @throws DatabaseException
088: * @throws AuthenticationException
089: * @throws ExportException
090: */
091: public static void exportXmlZip(HttpServletRequest request)
092: throws DatabaseException, AuthenticationException,
093: ExportException {
094:
095: OnlineUserManager onlineUserManager = OnlineUserManager
096: .getInstance();
097: OnlineUser onlineUser = onlineUserManager
098: .getOnlineUser(request);
099: MVNForumPermission permission = onlineUser.getPermission();
100: permission.ensureCanAdminSystem();
101:
102: int logonMemberID = onlineUser.getMemberID();
103: String logonMemberName = onlineUser.getMemberName();
104: Calendar exportTime = Calendar.getInstance();
105: String exportIP = request.getRemoteAddr();
106:
107: Locale locale = I18nUtil.getLocaleInRequest(request);
108:
109: int exportType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default
110: try {
111: exportType = ParamUtil.getParameterInt(request,
112: "ExportType",
113: MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML);
114: } catch (BadInputException e) {
115: exportType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default;
116: }
117:
118: String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss",
119: Locale.US).format(exportTime.getTime());
120: String filename = MVNForumConfig.BACKUP_FILE_PREFIX
121: + timestamp
122: + ((exportType == MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP) ? ".zip"
123: : ".xml");
124: log.debug("Will make export/backup file: " + filename);
125:
126: switch (exportType) {
127: case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML:
128: ExportWebHelper.exportXml(filename, request, logonMemberID,
129: logonMemberName, exportTime, exportIP);
130: break;
131: case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP:
132: ExportWebHelper.exportZip(filename, request, logonMemberID,
133: logonMemberName, exportTime, exportIP);
134: break;
135:
136: default:
137: log.error("exportXmlZip: invalid exportType = "
138: + exportType);
139: String localizedMessage = MVNForumResourceBundle
140: .getString(locale,
141: "mvncore.exception.ExportException.invalid_export_type_specified");
142: throw new ExportException(localizedMessage);
143: //throw new ExportException("Invalid export type specified.");
144: }
145: }
146:
147: public static void getExportXmlZip(HttpServletRequest request,
148: HttpServletResponse response) throws BadInputException,
149: DatabaseException, AuthenticationException, IOException {
150:
151: OnlineUserManager onlineUserManager = OnlineUserManager
152: .getInstance();
153: OnlineUser onlineUser = onlineUserManager
154: .getOnlineUser(request);
155: MVNForumPermission permission = onlineUser.getPermission();
156: permission.ensureCanAdminSystem();
157:
158: Locale locale = I18nUtil.getLocaleInRequest(request);
159:
160: String filename = ParamUtil.getParameterSafe(request,
161: "filename", true);// must check empty
162: FileUtil.checkGoodFileName(filename);
163: filename = DisableHtmlTagFilter.filter(filename);
164:
165: File f = new File(MVNForumConfig.getBackupDir()
166: + File.separatorChar + filename);
167: if ((!f.exists()) || (!f.isFile())) {
168: log
169: .error("Can't find a file to be downloaded (or maybe it's directory).");
170: String localizedMessage = MVNForumResourceBundle
171: .getString(locale,
172: "java.io.IOException.not_exist_or_not_file_to_be_downloaded");
173: throw new IOException(localizedMessage);
174: //throw new IOException("Can't find a file to be downloaded (or maybe it's directory).");
175: } else {
176: try {
177: response.setContentType("application/octet-stream");
178: response.setHeader("Location", filename);
179: response.setHeader("Content-Disposition",
180: "attachment; filename=" + filename);
181: int len = (int) f.length();
182: if (len > 0)
183: response.setContentLength(len);
184:
185: BufferedInputStream inputStream = new BufferedInputStream(
186: new FileInputStream(f), 1024 /* buffer size */);
187: BufferedOutputStream outputStream = new BufferedOutputStream(
188: response.getOutputStream(), 1024 /* buffer size */);
189: response.setBufferSize(1024);
190: //when we start download, we cannot redirect or raise
191: // exceptions
192: sendToUser(inputStream, outputStream);
193:
194: if (inputStream != null) {
195: try {
196: inputStream.close();
197: } catch (IOException e) {
198: }
199: inputStream = null;
200: }
201: if (outputStream != null) {
202: try {
203: outputStream.flush();
204: outputStream.close();
205: } catch (IOException e) {
206: }
207: outputStream = null;
208: }
209: } catch (FileNotFoundException e) {
210: log.error("Can't find a backup file on server.", e);
211: String localizedMessage = MVNForumResourceBundle
212: .getString(locale,
213: "java.io.IOException.not_found_backup_file");
214: throw new IOException(localizedMessage);
215: //throw new IOException("Can't find a backup file on server.");
216: //rethrow - the user will be redirected to errorpage
217: } catch (IOException e) {
218: log
219: .error(
220: "Error while trying to send backup file from server.",
221: e);
222: String localizedMessage = MVNForumResourceBundle
223: .getString(locale,
224: "java.io.IOException.error_while_sending_backup_file");
225: throw new IOException(localizedMessage);
226: //throw new IOException("Error while trying to send backup file from server.");
227: //rethrow - the user will be redirected to errorpage
228: } finally {
229: f = null;
230: }
231: }
232: }
233:
234: public static void deleteExportXmlZip(GenericRequest request)
235: throws BadInputException, DatabaseException,
236: AuthenticationException, IOException {
237:
238: OnlineUserManager onlineUserManager = OnlineUserManager
239: .getInstance();
240: OnlineUser onlineUser = onlineUserManager
241: .getOnlineUser(request);
242: MVNForumPermission permission = onlineUser.getPermission();
243: permission.ensureCanAdminSystem();
244:
245: Locale locale = I18nUtil.getLocaleInRequest(request);
246:
247: String filename = GenericParamUtil.getParameter(request,
248: "filename", true);
249: FileUtil.checkGoodFileName(filename);
250: filename = DisableHtmlTagFilter.filter(filename);
251:
252: File f = new File(MVNForumConfig.getBackupDir()
253: + File.separatorChar + filename);
254: if ((!f.exists()) || (!f.isFile())) {
255: log
256: .error("Can't find a file to be deleted (or maybe it's directory).");
257: String localizedMessage = MVNForumResourceBundle
258: .getString(locale,
259: "java.io.IOException.not_exist_or_not_file_to_be_deleted");
260: throw new IOException(localizedMessage);
261: //throw new IOException("Can't find a file to be deleted (or maybe it's directory).");
262: } else {
263: f.delete();
264: f = null;
265: }
266: }
267:
268: /**
269: * Sends (downloads) a file from server to the user.
270: *
271: * @param inputStream
272: * <code>BufferedInputStream</code> connected to the backup
273: * file that was made on server.
274: * @param outputStream
275: * <code>BufferedOutputStream</code> connected to the
276: * <code>HTTP response</code>.
277: */
278: private static void sendToUser(BufferedInputStream inputStream,
279: BufferedOutputStream outputStream) {
280: //this method should not throw any exception, since we are now starting
281: // commiting output
282: try {
283: //used stream objects are already buffered, so I won't do buffering
284: int b = 0;
285: while ((b = inputStream.read()) >= 0) {
286: outputStream.write(b);
287: }
288: } catch (IOException e) {
289: try {
290: outputStream
291: .write("FATAL ERROR. Can't continue download."
292: .getBytes());
293: } catch (IOException ee) {
294: /*
295: * Nothing we can do now. Since output was already commited, we
296: * can't raise exception here.
297: */
298: }
299: }
300: }
301: }
|