1 40 package com.mvnforum.admin; 41 42 import java.io.*; 43 import java.text.SimpleDateFormat ; 44 import java.util.Calendar ; 45 import java.util.Locale ; 46 47 import javax.servlet.http.HttpServletRequest ; 48 import javax.servlet.http.HttpServletResponse ; 49 50 import com.mvnforum.MVNForumConfig; 51 import com.mvnforum.MVNForumResourceBundle; 52 import com.mvnforum.auth.*; 53 import net.myvietnam.mvncore.exception.*; 54 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter; 55 import net.myvietnam.mvncore.util.*; 56 import net.myvietnam.mvncore.web.GenericRequest; 57 import org.apache.commons.logging.Log; 58 import org.apache.commons.logging.LogFactory; 59 60 70 public class ExportWebHandler { 71 72 73 private static Log log = LogFactory.getLog(ExportWebHandler.class); 74 75 76 private ExportWebHandler() { 77 } 78 79 92 public static void exportXmlZip(HttpServletRequest request) 93 throws DatabaseException, AuthenticationException, AssertionException, ExportException { 94 95 OnlineUserManager onlineUserManager = OnlineUserManager.getInstance(); 96 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 97 MVNForumPermission permission = onlineUser.getPermission(); 98 permission.ensureCanAdminSystem(); 99 100 int logonMemberID = onlineUser.getMemberID(); 101 String logonMemberName = onlineUser.getMemberName(); 102 Calendar exportTime = Calendar.getInstance(); 103 String exportIP = request.getRemoteAddr(); 104 105 Locale locale = I18nUtil.getLocaleInRequest(request); 106 107 int exportType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; try { 109 exportType = ParamUtil.getParameterInt(request, "ExportType", MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML); 110 } catch (BadInputException e) { 111 exportType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; } 113 114 String timestamp = new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss", Locale.US).format(exportTime.getTime()); 115 String filename = MVNForumConfig.BACKUP_FILE_PREFIX 116 + timestamp 117 + ((exportType == MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP) ? ".zip" 118 : ".xml"); 119 log.debug("Will make export/backup file: " + filename); 120 121 switch (exportType) { 122 case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML: 123 ExportWebHelper.exportXml(filename, request, logonMemberID, logonMemberName, exportTime, exportIP); 124 break; 125 case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP: 126 ExportWebHelper.exportZip(filename, request, logonMemberID, logonMemberName, exportTime, exportIP); 127 break; 128 129 default: 130 log.error("exportXmlZip: invalid exportType = " + exportType); 131 String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ExportException.invalid_export_type_specified"); 132 throw new ExportException(localizedMessage); 133 } 135 } 136 137 public static void getExportXmlZip(HttpServletRequest request, HttpServletResponse response) 138 throws BadInputException, DatabaseException, AuthenticationException, AssertionException, IOException { 139 140 OnlineUserManager onlineUserManager = OnlineUserManager.getInstance(); 141 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 142 MVNForumPermission permission = onlineUser.getPermission(); 143 permission.ensureCanAdminSystem(); 144 145 Locale locale = I18nUtil.getLocaleInRequest(request); 146 147 String filename = ParamUtil.getParameterSafe(request, "filename", true); FileUtil.checkGoodFileName(filename); 149 filename = DisableHtmlTagFilter.filter(filename); 150 151 File f = new File(MVNForumConfig.getBackupDir() + File.separatorChar + filename); 152 if ((!f.exists()) || (!f.isFile())) { 153 log.error("Can't find a file to be downloaded (or maybe it's directory)."); 154 String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.not_exist_or_not_file_to_be_downloaded"); 155 throw new IOException(localizedMessage); 156 } else { 158 try { 159 response.setContentType("application/octet-stream"); 160 response.setHeader("Location", filename); 161 response.setHeader("Content-Disposition", "attachment; filename=" + filename); 162 int len = (int) f.length(); 163 if (len > 0) 164 response.setContentLength(len); 165 166 BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(f), 1024 ); 167 BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream(), 1024 ); 168 response.setBufferSize(1024); 169 sendToUser(inputStream, outputStream); 172 173 if (inputStream != null) { 174 try { 175 inputStream.close(); 176 } catch (IOException e) { 177 } 178 inputStream = null; 179 } 180 if (outputStream != null) { 181 try { 182 outputStream.flush(); 183 outputStream.close(); 184 } catch (IOException e) { 185 } 186 outputStream = null; 187 } 188 } catch (FileNotFoundException e) { 189 log.error("Can't find a backup file on server.", e); 190 String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.not_found_backup_file"); 191 throw new IOException(localizedMessage); 192 } catch (IOException e) { 195 log.error("Error while trying to send backup file from server.", e); 196 String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.error_while_sending_backup_file"); 197 throw new IOException(localizedMessage); 198 } finally { 201 f = null; 202 } 203 } 204 } 205 206 public static void deleteExportXmlZip(GenericRequest request) 207 throws BadInputException, DatabaseException, AuthenticationException, AssertionException, IOException { 208 209 OnlineUserManager onlineUserManager = OnlineUserManager.getInstance(); 210 OnlineUser onlineUser = onlineUserManager.getOnlineUser(request); 211 MVNForumPermission permission = onlineUser.getPermission(); 212 permission.ensureCanAdminSystem(); 213 214 Locale locale = I18nUtil.getLocaleInRequest(request); 215 216 String filename = GenericParamUtil.getParameter(request, "filename", true); 217 FileUtil.checkGoodFileName(filename); 218 filename = DisableHtmlTagFilter.filter(filename); 219 220 File f = new File(MVNForumConfig.getBackupDir() + File.separatorChar + filename); 221 if ((!f.exists()) || (!f.isFile())) { 222 log.error("Can't find a file to be deleted (or maybe it's directory)."); 223 String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.not_exist_or_not_file_to_be_deleted"); 224 throw new IOException(localizedMessage); 225 } else { 227 f.delete(); 228 f = null; 229 } 230 } 231 232 242 private static void sendToUser(BufferedInputStream inputStream, 243 BufferedOutputStream outputStream) { 244 try { 247 int b = 0; 249 while ((b = inputStream.read()) >= 0) { 250 outputStream.write(b); 251 } 252 } catch (IOException e) { 253 try { 254 outputStream.write("FATAL ERROR. Can't continue download.".getBytes()); 255 } catch (IOException ee) { 256 260 } 261 } 262 } 263 } 264
| Popular Tags
|