View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.deployment.web;
35  
36  import java.io.IOException;
37  
38  import javax.servlet.ServletException;
39  import javax.servlet.http.HttpServlet;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  import fr.paris.lutece.plugins.deployment.business.Application;
44  import fr.paris.lutece.plugins.deployment.business.CommandResult;
45  import fr.paris.lutece.plugins.deployment.business.ServerApplicationInstance;
46  import fr.paris.lutece.plugins.deployment.service.DeploymentPlugin;
47  import fr.paris.lutece.plugins.deployment.service.IApplicationService;
48  import fr.paris.lutece.plugins.deployment.service.IFtpService;
49  import fr.paris.lutece.plugins.deployment.service.IServerApplicationService;
50  import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
51  import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
52  import fr.paris.lutece.portal.service.plugin.Plugin;
53  import fr.paris.lutece.portal.service.plugin.PluginService;
54  import fr.paris.lutece.portal.service.spring.SpringContextService;
55  import fr.paris.lutece.portal.service.util.AppLogService;
56  
57  /**
58   * Servlet serving document file resources
59   */
60  public class DownloadServlet extends HttpServlet
61  {
62      /**
63  	 *
64  	 */
65      private static final long serialVersionUID = 8625639667629973645L;
66      private IFtpService _ftpService = SpringContextService.getBean( "deployment.FtpService" );
67      private IServerApplicationService _serverApplicationService = SpringContextService.getBean( "deployment.ServerApplicationService" );
68      private IApplicationService _applicationService = SpringContextService.getBean( "deployment.ApplicationService" );
69  
70      /**
71       * Handles the HTTP <code>GET</code> method.
72       * 
73       * @param request
74       *            servlet request
75       * @param response
76       *            servlet response
77       * @throws ServletException
78       *             the servlet Exception
79       * @throws IOException
80       *             the io exception
81       */
82      @Override
83      protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
84      {
85  
86          Plugin plugin = PluginService.getPlugin( DeploymentPlugin.PLUGIN_NAME );
87          String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
88          String strCodeServerApplicationInstanceSql = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE_SQL );
89          String strCodeDatabase = request.getParameter( ConstanteUtils.PARAM_CODE_DATABASE );
90          String strIdApplication = request.getParameter( ConstanteUtils.PARAM_CODE_APPLICATION );
91          String strServerApplicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
92  
93          Application application = _applicationService.getApplication( DeploymentUtils.getIntegerParameter( strIdApplication ), plugin );
94  
95          ServerApplicationInstance serverApplicationInstance = _serverApplicationService.getServerApplicationInstance( application,
96                  strCodeServerApplicationInstanceSql, strCodeEnvironment, strServerApplicationType, request.getLocale( ), false, false );
97  
98          response.setHeader( "Content-Disposition", "attachment ;filename=\"dump_" + strCodeDatabase + ".sql\";" );
99          response.setHeader( "Pragma", "public" );
100         response.setHeader( "Expires", "0" );
101         response.setHeader( "Cache-Control", "must-revalidate,post-check=0,pre-check=0" );
102         response.setHeader( "Content-Type", "application/octet-stream" );
103 
104         CommandResult commandResult = new CommandResult( );
105 
106         try
107         {
108             _ftpService.getFile( response.getOutputStream( ), serverApplicationInstance.getFtpInfo( ),
109                     DeploymentUtils.getDumpFileDirectory( application.getCode( ), serverApplicationInstance ) + "FROM_Z00-" + strCodeDatabase
110                             + "-ALL_TABLES.sql", commandResult );
111         }
112         catch( IOException e )
113         {
114             AppLogService.error( e );
115         }
116 
117     }
118 
119     /**
120      * Returns a short description of the servlet.
121      * 
122      * @return message
123      */
124     @Override
125     public String getServletInfo( )
126     {
127         return "Servlet serving files content from core_file and core_physical_file tables";
128     }
129 }