001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.web; 017 018import java.io.BufferedInputStream; 019import java.io.BufferedOutputStream; 020import java.io.File; 021import java.io.FileInputStream; 022import java.io.FileNotFoundException; 023import java.io.IOException; 024import javax.servlet.ServletContext; 025import javax.servlet.http.HttpServlet; 026import javax.servlet.http.HttpServletRequest; 027import javax.servlet.http.HttpServletResponse; 028import org.modeshape.common.logging.Logger; 029import org.modeshape.common.util.FileUtil; 030import org.modeshape.common.util.IoUtil; 031 032/** 033 * 034 * @author kulikov 035 */ 036public class BackupExportServlet extends HttpServlet { 037 038 private static final long serialVersionUID = 1L; 039 private File tempDir; 040 041 private Logger logger = Logger.getLogger("Servlet"); 042 043 @Override 044 protected void doGet(HttpServletRequest request, HttpServletResponse response) 045 throws IOException { 046 String fname = request.getParameter("file"); 047 String qs = request.getQueryString(); 048 049 logger.debug("--------- Query string=" + qs); 050 051 File file = new File(tempDir.getAbsoluteFile() + File.separator + fname); 052 if (!file.exists()) { 053 throw new FileNotFoundException(file.getAbsolutePath()); 054 } 055 056 File zipFile = new File(tempDir.getAbsolutePath() + File.separator + "bak.zip"); 057 058 FileUtil.zipDir(file.getAbsolutePath(), zipFile.getAbsolutePath()); 059 060 response.setHeader("Content-Type", "application/zip"); 061 response.setHeader("Content-Length", String.valueOf(zipFile.length())); 062 response.setHeader("Content-disposition", "attachment;filename=\"bak.zip\""); 063 064 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFile)); 065 BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); 066 067 IoUtil.write(bis, bos); 068 069 bos.flush(); 070 bos.close(); 071 bis.close(); 072 } 073 074 @Override 075 protected void doPost(HttpServletRequest request, HttpServletResponse response) 076 throws IOException { 077 doGet(request, response); 078 } 079 080 @Override 081 public void init() { 082 // Configure a repository (to ensure a secure temp location is used) 083 ServletContext servletContext = this.getServletConfig().getServletContext(); 084 tempDir = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); 085 } 086 087} 088 089