View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.appcenter.modules.fastdeployapplication.web;
35  
36  import java.io.IOException;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.commons.lang.StringUtils;
42  
43  import fr.paris.lutece.plugins.appcenter.business.Application;
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployEnvironment;
47  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
48  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.DeploymentFactory;
49  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IFtpService;
50  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
51  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
52  import fr.paris.lutece.portal.service.message.SiteMessageException;
53  import fr.paris.lutece.portal.service.security.UserNotSignedException;
54  import fr.paris.lutece.portal.service.util.AppLogService;
55  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
56  import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
57  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
58  import fr.paris.lutece.portal.web.LocalVariables;
59  import fr.paris.lutece.portal.web.xpages.XPage;
60  
61  @Controller( xpageName = "fastDeployDownloadXPage", pageTitleI18nKey = "appcenter.xpage.fastdeploy.pageTitle", pagePathI18nKey = "appcenter.xpage.fastdeploy.pagePathLabel" )
62  public class FastDeployDownloadXPage extends FastDeployXPage
63  {
64      private static final String ACTION_DOWNLOAD_DUMP_FILE = "doDownloadDumpFile";
65  
66      /**
67       * return the tasks form
68       *
69       * @param request
70       *            the request
71       * @return the tasks form
72       * @throws SiteMessageException
73       * @throws UserNotSignedException
74       */
75      @Action( value = ACTION_DOWNLOAD_DUMP_FILE )
76      public XPage doDownloadDumpFile( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
77      {
78  
79          String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
80          String strCodeServerApplicationInstanceSql = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE_SQL );
81          String strCodeDatabase = request.getParameter( ConstanteUtils.PARAM_CODE_DATABASE );
82          String strServerApplicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
83          String strDumpFileName = request.getParameter( ConstanteUtils.PARAM_DUMP_FILE_NAME );
84          strDumpFileName = !StringUtils.isEmpty( strDumpFileName ) ? strDumpFileName : "FROM_Z00-" + strCodeDatabase + "-ALL_TABLES.sql";
85  
86          Application application = getApplication( request );
87  
88          FastDeployApplicationData data;
89          try
90          {
91  
92              data = getFastDeployData( request );
93  
94              try
95              {
96                  // first check permission View env
97                  checkPermissionByFastDeployEnvironment( request, data, ConstanteUtils.PERMISSION_VISUALIZE_ENVI, strCodeEnvironment );
98  
99              }
100             catch( Exception e )
101             {
102                 // check Permisison Deploy script
103                 checkPermissionByFastDeployEnvironment( request, data, ConstanteUtils.PERMISSION_DEPLOY_SCRIPT, strCodeEnvironment );
104 
105             }
106 
107             IFtpService ftpService = DeploymentFactory.getInstance( ).getFtpService( data.getApplicationType( ) );
108 
109             ServerApplicationInstance serverApplicationInstance = DeploymentFactory.getInstance( ).getServerApplicationService( data.getApplicationType( ) )
110                     .getServerApplicationInstance( data, Integer.toString( application.getId( ) ), strCodeServerApplicationInstanceSql, strCodeEnvironment,
111                             strServerApplicationType, request.getLocale( ), false, false );
112             String strFtpRemoteDumpDirectoryPath = serverApplicationInstance.getFtpDirectoryDump( );
113             CommandResultter/modules/fastdeployapplication/business/CommandResult.html#CommandResult">CommandResult commandResult = new CommandResult( );
114             HttpServletResponse response = LocalVariables.getResponse( );
115             MVCUtils.addDownloadHeaderToResponse( response, "dump_" + strCodeDatabase + ".sql", "application/octet-stream" );
116             ftpService.getFile( strDumpFileName, response.getOutputStream( ), serverApplicationInstance.getFtpInfo( ), strFtpRemoteDumpDirectoryPath,
117                     commandResult );
118             return new XPage( );
119         }
120         catch( IOException e )
121         {
122             AppLogService.error( e );
123         }
124         catch( Exception e )
125         {
126         }
127         finally
128         {
129             return new XPage( );
130         }
131     }
132 }