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.File;
37  import java.io.FileInputStream;
38  import java.io.FileNotFoundException;
39  import java.io.IOException;
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.Iterator;
43  import java.util.List;
44  import java.util.Locale;
45  import java.util.Map;
46  import java.util.Set;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpSession;
50  import javax.validation.ConstraintViolation;
51  
52  import org.apache.commons.collections.CollectionUtils;
53  import org.apache.commons.fileupload.FileItem;
54  import org.apache.commons.lang.StringUtils;
55  
56  import fr.paris.lutece.plugins.appcenter.business.Application;
57  import fr.paris.lutece.plugins.appcenter.business.Environment;
58  import fr.paris.lutece.plugins.appcenter.business.User;
59  import fr.paris.lutece.plugins.appcenter.business.userinfos.GitlabUserInfo;
60  import fr.paris.lutece.plugins.appcenter.business.userinfos.SvnUserInfo;
61  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.CommandResult;
62  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
63  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationsData;
64  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployEnvironment;
65  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
66  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.AbstractWorkflowDeployContext;
67  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowDeployAppContext;
68  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowDeployScriptContext;
69  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowInitDatabaseContext;
70  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.context.WorkflowInitTomcatContext;
71  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.ActionHistory;
72  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.fastaction.ActionHistoryHome;
73  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.vcs.AbstractVCSUser;
74  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.vcs.DefaultVCSUser;
75  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.DeploymentFactory;
76  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IDatabaseService;
77  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IFtpService;
78  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.IWorkflowDeploySiteService;
79  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.MavenService;
80  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.service.vcs.IVCSService;
81  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.uploadhandler.DeploymentUploadHandler;
82  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
83  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.DeploymentUtils;
84  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.RepositoryUtils;
85  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.dto.FormDeployAppDTO;
86  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.dto.FormDeployInitContextDTO;
87  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.dto.FormDeployInitDatabaseDTO;
88  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.dto.FormDeployScriptDTO;
89  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.exception.AbstractFastDeployException;
90  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.exception.NoExistingFastDeployConfigException;
91  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.exception.VCSUserNotSignedException;
92  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.web.exception.WrongFastDeployConfigRequiredException;
93  import fr.paris.lutece.plugins.appcenter.service.ApplicationService;
94  import fr.paris.lutece.plugins.appcenter.service.UserService;
95  import fr.paris.lutece.plugins.appcenter.util.CryptoUtil;
96  import fr.paris.lutece.plugins.appcenter.web.AppCenterXPage;
97  import fr.paris.lutece.plugins.appcenter.web.Constants;
98  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
99  import fr.paris.lutece.portal.service.i18n.I18nService;
100 import fr.paris.lutece.portal.service.message.SiteMessageException;
101 import fr.paris.lutece.portal.service.security.UserNotSignedException;
102 import fr.paris.lutece.portal.service.spring.SpringContextService;
103 import fr.paris.lutece.portal.service.util.AppLogService;
104 import fr.paris.lutece.portal.service.util.AppPathService;
105 import fr.paris.lutece.portal.service.util.AppPropertiesService;
106 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
107 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
108 import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
109 import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
110 import fr.paris.lutece.portal.web.xpages.XPage;
111 import fr.paris.lutece.util.ReferenceItem;
112 import fr.paris.lutece.util.ReferenceList;
113 import fr.paris.lutece.util.beanvalidation.BeanValidationUtil;
114 import fr.paris.lutece.util.json.ErrorJsonResponse;
115 import fr.paris.lutece.util.json.JsonResponse;
116 import fr.paris.lutece.util.json.JsonUtil;
117 import freemarker.core._ParserConfigurationWithInheritedFormat;
118 
119 /**
120  * SourcesXPage
121  */
122 @Controller( xpageName = "fastdeploy", pageTitleI18nKey = "appcenter.xpage.fastdeploy.pageTitle", pagePathI18nKey = "appcenter.xpage.fastdeploy.pagePathLabel" )
123 public class FastDeployXPage extends AppCenterXPage
124 {
125     // Templates
126     private static final String TEMPLATE_MANAGE_ENV = "/skin/plugins/appcenter/modules/fastdeployapplication/manage_env.html";
127     private static final String TEMPLATE_DEPLOY_APPLICATION = "/skin/plugins/appcenter/modules/fastdeployapplication/deploy/deploy_application.html";
128     private static final String TEMPLATE_DEPLOY_SCRIPT = "/skin/plugins/appcenter/modules/fastdeployapplication/deploy/deploy_script.html";
129     private static final String TEMPLATE_DEPLOY_INIT_TOMCAT = "/skin/plugins/appcenter/modules/fastdeployapplication/deploy/deploy_init_tomcat.html";
130     private static final String TEMPLATE_DEPLOY_INIT_DATABASE = "/skin/plugins/appcenter/modules/fastdeployapplication/deploy/deploy_init_database.html";
131     private static final String TEMPLATE_HISTORY = "/skin/plugins/appcenter/modules/fastdeployapplication/visualize_history.html";
132     private static final String TEMPLATE_SELECT_FAST_DEPLOY_CONFIG = "/skin/plugins/appcenter/modules/fastdeployapplication/select_fast_deploy_config.html";
133     private static final String TEMPLATE_NO_EXISTING_FAST_DEPLOY_CONFIG = "/skin/plugins/appcenter/modules/fastdeployapplication/no_existing_fast_deploy_config.html";
134 
135     // VIEW
136     private static final String VIEW_MANAGE_ENV = "manageEnv";
137     private static final String VIEW_DEPLOY_APPLICATION = "getDeployApplication";
138     private static final String VIEW_DEPLOY_SCRIPT = "getDeployScript";
139     private static final String VIEW_DEPLOY_INIT_TOMCAT = "getDeployInitTomcat";
140     private static final String VIEW_DEPLOY_INIT_DATABASE = "getDeployInitDatabase";
141     private static final String VIEW_HISTORY = "history";
142     private static final String VIEW_SELECT_FAST_DEPLOY_CONFIG = "selectFastDeployConfig";
143     private static final String VIEW_NO_EXISTING_FAST_DEPLOY_CONFIG = "noExistingFastDeployConfig";
144     private static final String VIEW_ASK_CREDENTIALS = "askCredentials";
145     private static final String VIEW_DATABASE_LIST = "databaseList";
146     private static final String VIEW_REMOTE_SCRIPT_LIST = "remoteScriptList";
147 
148     // JSP MANAGE APPLICATION
149     private static final String JSP_MANAGE_APPLICATION = "jsp/site/Portal.jsp";
150     // PARAMETER
151     protected static final String PARAM_ID_FAST_APPLICATION_DATA = "id_fast_application_data";
152     private static final String PARAM_REDIRECT_VIEW = "redirect_view";
153     private static final String PARAM_AUTHENTICATION_TRY = "authentication_try";
154 
155     // ACTION
156     private static final String ACTION_DEPLOY_APPLICATION = "doDeployApplication";
157     private static final String ACTION_DEPLOY_SCRIPT = "doDeployScript";
158     private static final String ACTION_DEPLOY_INIT_TOMCAT = "doDeployInitTomcat";
159     private static final String ACTION_DEPLOY_INIT_DATABASE = "doDeployInitDatabase";
160     private static final String ACTION_MODIFY_MAVEN_PROFILE = "doModifyMavenProfile";
161     private static final String ACTION_MODIFY_SERVER_NAME = "doModifyServerName";
162     private static final String ACTION_CHECK_REPOSITORY = "doCheckRepository";
163 
164     // MARK URL SITE
165     public static final String DATA_PREFIX_FAST_DEPLOY_SERVICES = "appcenter.fastdeployServices.";
166     public static final String MARK_SERVICES = "services";
167     public static final String MARK_MAP_SERVICES = "map_services";
168     private static final String MARK_DEFAULT_URL_SITE = "default_url_site";
169     protected static final String MARK_ID_FAST_APPLICATION_DATA = "id_fast_application_data";
170     protected static final String MARK_APPLICATION_DATA = "application_data";
171     protected static final String MARK_APPLICATION_DATAS_LIST = "application_datas_list";
172     protected static final String MARK_FAST_DEPLOY_CONFIG_LIST = "application_datas_list";
173     protected static final String MARK_REDIRECT_VIEW = "redirect_view";
174     private static final String MARK_ACTION_HISTORY_LIST = "action_history_list";
175 
176     private static final String ERROR_USER_NOT_AUTHORIZED = "appcenter.error.userNotAuthorized";
177     private static final String ERROR_USER_NOT_AUTHENTICATED = "module.appcenter.fastdeployapplication.message.ask_credentials.error.user_not_authenticated";
178     private static final String MESSAGE_MANDATORY_CHOICE_TAG_TO_DEPLOY = "module.appcenter.fastdeployapplication.message.mandatory_field.choice.tag_to_deploy";
179     private static final String MESSAGE_MANDATORY_CHOICE_SELECT_SCRIPT = "module.appcenter.fastdeployapplication.message.mandatory_field.choice.select_script";
180 
181     // CONSTANTS
182     private static final String REPOSITORY_TYPE_SITE = "site";
183 
184     // SERVICES
185     protected IWorkflowDeploySiteService _workflowDeploySiteService = SpringContextService.getBean( ConstanteUtils.BEAN_WORKFLOW_DEPLOY_SITE_SERVICE );
186     protected DeploymentUploadHandler _handler = SpringContextService.getBean( ConstanteUtils.BEAN_DEPLOYMENT_UPLOAD_HANDLER );
187 
188     // SESSION VARIABLES
189     protected AbstractWorkflowDeployContext _workflowDeployContext;
190     protected FastDeployApplicationData _data;
191     protected AbstractVCSUser _vcsUser;
192     protected IVCSService _vcsService;
193 
194     @View( value = VIEW_MANAGE_ENV, defaultView = true )
195     public XPage getManageEnvs( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException, AccessDeniedException
196     {
197 
198         Application application = getApplication( request );
199         Environment environment = getActiveEnvironment( request );
200 
201         Map<String, Object> model = getModel( );
202         try
203         {
204             _data = getFastDeployData( request );
205         }
206         catch( AbstractFastDeployException e )
207         {
208             redirectFastDeployConfigErrorPage( request, e, VIEW_MANAGE_ENV );
209         }
210 
211         List<FastDeployEnvironment> listFastDeployEnvironments;
212         if ( environment != null )
213         {
214 
215             listFastDeployEnvironments = DeploymentFactory.getInstance( ).getEnvironmentService( _data.getApplicationType( ) )
216                     .getListEnvironmentsByType( application.getCode( ), getLocale( request ), environment.getPrefix( ) );
217             if ( listFastDeployEnvironments != null )
218             {
219 
220                 for ( FastDeployEnvironment fastEnv : listFastDeployEnvironments )
221                 {
222                     checkPermissionByFastDeployEnvironment( request, _data, ConstanteUtils.PERMISSION_VISUALIZE_ENVI, fastEnv.getCode( ) );
223 
224                 }
225             }
226 
227             HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceTomcat = DeploymentFactory.getInstance( )
228                     .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data,
229                             Integer.toString( application.getId( ) ), ConstanteUtils.CONSTANTE_SERVER_TOMCAT, getLocale( request ), true, true,
230                             environment.getPrefix( ) );
231             HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceSql = DeploymentFactory.getInstance( )
232                     .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data,
233                             Integer.toString( application.getId( ) ), ConstanteUtils.CONSTANTE_SERVER_MYSQL, getLocale( request ), true, true,
234                             environment.getPrefix( ) );
235             HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstancePsq = DeploymentFactory.getInstance( )
236                     .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data,
237                             Integer.toString( application.getId( ) ), ConstanteUtils.CONSTANTE_SERVER_PSQ, getLocale( request ), true, true,
238                             environment.getPrefix( ) );
239 
240             for ( Map.Entry<String, List<ServerApplicationInstance>> entry : hashServerApplicationInstancePsq.entrySet( ) )
241             {
242                 String key = entry.getKey( );
243                 List<ServerApplicationInstance> listServerPsq = entry.getValue( );
244 
245                 if ( hashServerApplicationInstanceSql.containsKey( entry.getKey( ) ) && hashServerApplicationInstanceSql.get( entry.getKey( ) ) != null
246                         && listServerPsq != null )
247                 {
248                     hashServerApplicationInstanceSql.get( entry.getKey( ) ).addAll( listServerPsq );
249                 }
250                 else
251                 {
252                     hashServerApplicationInstanceSql.put( key, listServerPsq );
253 
254                 }
255             }
256 
257             model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_TOMCAT, hashServerApplicationInstanceTomcat );
258             model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_SQL, hashServerApplicationInstanceSql );
259             model.put( ConstanteUtils.MARK_ENVIRONMENT_LIST, listFastDeployEnvironments );
260             model.put( MARK_APPLICATION_DATA, _data );
261 
262             fillAppCenterCommons( model, request );
263         }
264         else
265         {
266             redirect( request,
267                     AppPathService.getBaseUrl( request ) + AppPathService.getPortalUrl( ) + "?" + MVCUtils.PARAMETER_PAGE + "=" + Constants.XPAGE_APPLICATION );
268         }
269 
270         return getXPage( TEMPLATE_MANAGE_ENV, request.getLocale( ), model );
271     }
272 
273     /**
274      * Gets the database list.
275      *
276      * @param request
277      *            the request
278      * @return the database list
279      * @throws UserNotSignedException
280      *             the user not signed exception
281      * @throws SiteMessageException
282      *             the site message exception
283      */
284     @View( value = VIEW_DATABASE_LIST )
285     public XPage getDatabaseList( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
286     {
287 
288         String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
289         String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
290         String strServerAppicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
291         boolean bRefreshDatabase = request.getParameter( ConstanteUtils.PARAM_REFRESH_DATABASES ) != null
292                 ? new Boolean( request.getParameter( ConstanteUtils.PARAM_REFRESH_DATABASES ) )
293                 : false;
294 
295         Application application = getApplication( request );
296         FastDeployApplicationData data;
297         try
298         {
299             data = getFastDeployData( request );
300         }
301         catch( AbstractFastDeployException e )
302         {
303 
304             return responseJSON( JsonUtil.buildJsonResponse( new ErrorJsonResponse( "fast deploy data do not exist" ) ) );
305 
306         }
307 
308         IDatabaseService databaseService = DeploymentFactory.getInstance( ).getDatabaseService( data.getApplicationType( ) );
309         ServerApplicationInstance serverApplicationInstance = DeploymentFactory.getInstance( ).getServerApplicationService( data.getApplicationType( ) )
310                 .getServerApplicationInstance( data, Integer.toString( application.getId( ) ), strCodeServerApplicationInstance, strCodeEnvironment,
311                         strServerAppicationType, request.getLocale( ), false, false );
312 
313         List<String> listDatabases = databaseService.getDatabases( data.getApplicationCode( ), serverApplicationInstance, getLocale( request ),
314                 bRefreshDatabase );
315         // Check constraints
316         return responseJSON( JsonUtil.buildJsonResponse( new JsonResponse( listDatabases ) ) );
317 
318     }
319 
320     /**
321      * Gets the database list.
322      *
323      * @param request
324      *            the request
325      * @return the database list
326      * @throws UserNotSignedException
327      *             the user not signed exception
328      * @throws SiteMessageException
329      *             the site message exception
330      */
331     @View( value = VIEW_REMOTE_SCRIPT_LIST )
332     public XPage getRemoteScriptList( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
333     {
334 
335         String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
336         String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
337         String strServerAppicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
338 
339         Application application = getApplication( request );
340         FastDeployApplicationData data;
341         try
342         {
343             data = getFastDeployData( request );
344         }
345         catch( AbstractFastDeployException e )
346         {
347 
348             return responseJSON( JsonUtil.buildJsonResponse( new ErrorJsonResponse( "fast deploy data do not exist" ) ) );
349 
350         }
351 
352         IFtpService ftpService = DeploymentFactory.getInstance( ).getFtpService( data.getApplicationType( ) );
353         ServerApplicationInstance serverApplicationInstance = DeploymentFactory.getInstance( ).getServerApplicationService( data.getApplicationType( ) )
354                 .getServerApplicationInstance( data, Integer.toString( application.getId( ) ), strCodeServerApplicationInstance, strCodeEnvironment,
355                         strServerAppicationType, request.getLocale( ), false, false );
356 
357         List<String> listScript = ftpService.getListDirectoryFiles( serverApplicationInstance.getFtpInfo( ), ".sql$",
358                 serverApplicationInstance.getFtpDirectoryScript( ), new CommandResult( ) );
359         // Check constraints
360         return responseJSON( JsonUtil.buildJsonResponse( new JsonResponse( listScript ) ) );
361 
362     }
363 
364     @View( value = VIEW_DEPLOY_APPLICATION )
365     public XPage getDeployApplication( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException, AccessDeniedException
366     {
367         Map<String, Object> model = getModel( );
368         fillAppCenterCommons( model, request );
369         try
370         {
371             fillCommonsDeploy( model, request );
372             setWorkflowDeployApplicationContext( );
373             checkAuthentication( request );
374             fillDeploySite( model, request );
375             fillWorkflowActions( model );
376         }
377         catch( AbstractFastDeployException e )
378         {
379             return redirectFastDeployConfigErrorPage( request, e, VIEW_DEPLOY_APPLICATION );
380         }
381 
382         return getXPage( TEMPLATE_DEPLOY_APPLICATION, request.getLocale( ), model );
383     }
384 
385     @View( value = VIEW_DEPLOY_SCRIPT )
386     public XPage getDeployScript( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException, AccessDeniedException
387     {
388 
389         Map<String, Object> model = getModel( );
390         fillAppCenterCommons( model, request );
391         try
392         {
393             fillCommonsDeploy( model, request );
394             checkAuthentication( request );
395             setWorkflowDeployScriptContext( );
396             fillWorkflowActions( model );
397             fillDeployScript( model, request );
398         }
399         catch( AbstractFastDeployException e )
400         {
401             return redirectFastDeployConfigErrorPage( request, e, VIEW_DEPLOY_SCRIPT );
402         }
403 
404         return getXPage( TEMPLATE_DEPLOY_SCRIPT, request.getLocale( ), model );
405     }
406 
407     @View( value = VIEW_DEPLOY_INIT_TOMCAT )
408     public XPage getDeployInitTomcat( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException, AccessDeniedException
409     {
410         Map<String, Object> model = getModel( );
411         fillAppCenterCommons( model, request );
412         try
413         {
414             fillCommonsDeploy( model, request );
415             setWorkflowDeployInitTomcatContext( );
416             fillWorkflowActions( model );
417             fillInitTomcatContext( model, request );
418         }
419         catch( AbstractFastDeployException e )
420         {
421             return redirectFastDeployConfigErrorPage( request, e, VIEW_DEPLOY_INIT_TOMCAT );
422         }
423 
424         return getXPage( TEMPLATE_DEPLOY_INIT_TOMCAT, request.getLocale( ), model );
425     }
426 
427     @View( value = VIEW_DEPLOY_INIT_DATABASE )
428     public XPage getDeployInitDatabase( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException, AccessDeniedException
429     {
430         Map<String, Object> model = getModel( );
431         fillAppCenterCommons( model, request );
432         try
433         {
434             fillCommonsDeploy( model, request );
435             setWorkflowInitDatabaseContext( );
436             fillWorkflowActions( model );
437             fillInitDatabase( model, request );
438         }
439         catch( AbstractFastDeployException e )
440         {
441             return redirectFastDeployConfigErrorPage( request, e, VIEW_DEPLOY_INIT_DATABASE );
442         }
443 
444         return getXPage( TEMPLATE_DEPLOY_INIT_DATABASE, request.getLocale( ), model );
445     }
446 
447     @View( value = VIEW_HISTORY )
448     public XPage getHistory( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException, AccessDeniedException
449     {
450         Application application = getApplication( request );
451         Environment environment = getActiveEnvironment( request );
452 
453         List<ActionHistory> listActionHistory = ActionHistoryHome.getActionHistorysListByAppByEnv( application.getId( ), environment.getPrefix( ) );
454 
455         Map<String, Object> model = getModel( );
456         model.put( MARK_ACTION_HISTORY_LIST, listActionHistory );
457 
458         fillAppCenterCommons( model, request );
459 
460         return getXPage( TEMPLATE_HISTORY, request.getLocale( ), model );
461     }
462 
463     @View( value = VIEW_SELECT_FAST_DEPLOY_CONFIG )
464     public XPage getSelectFastDeployConfig( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException, AccessDeniedException
465     {
466         String strReturnUrl = request.getParameter( PARAM_REDIRECT_VIEW );
467         String strDeployType = request.getParameter( ConstanteUtils.PARAM_DEPLOY_TYPE );
468 
469         Map<String, Object> model = getModel( );
470         fillAppCenterCommons( model, request );
471         if ( strReturnUrl != null )
472         {
473             model.put( MARK_REDIRECT_VIEW, strReturnUrl );
474         }
475         FastDeployApplicationsData fastDeployApplicationDatas = ApplicationService.loadApplicationDataSubset( _application, FastDeployApplicationsData.class );
476         model.put( MARK_FAST_DEPLOY_CONFIG_LIST, fastDeployApplicationDatas.getListData( ) );
477         model.put( ConstanteUtils.MARK_DEPLOY_TYPE, strDeployType );
478 
479         return getXPage( TEMPLATE_SELECT_FAST_DEPLOY_CONFIG, request.getLocale( ), model );
480     }
481 
482     /**
483      * Process the deploy application action
484      * 
485      * @param request
486      *            the httpServletRequest
487      * @return a JSON with the command result of the action processing
488      * @throws UserNotSignedException
489      * @throws SiteMessageException
490      */
491     @Action( ACTION_DEPLOY_APPLICATION )
492     public XPage doDeployApplication( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
493     {
494         List<String> listErrors;
495         WorkflowDeployAppContextlutece/plugins/appcenter/modules/fastdeployapplication/business/context/WorkflowDeployAppContext.html#WorkflowDeployAppContext">WorkflowDeployAppContext workflowDeployContext = (WorkflowDeployAppContext) _workflowDeployContext;
496 
497         FormDeployAppDTOr/modules/fastdeployapplication/web/dto/FormDeployAppDTO.html#FormDeployAppDTO">FormDeployAppDTO deployAppDto = new FormDeployAppDTO( );
498         populate( deployAppDto, request );
499         listErrors = getFormErrors( deployAppDto, getLocale( request ) );
500 
501         if ( StringUtils.isEmpty( deployAppDto.getTagToDeploy( ) ) && !deployAppDto.isDeployDevSite( ) )
502         {
503             listErrors.add( I18nService.getLocalizedString( MESSAGE_MANDATORY_CHOICE_TAG_TO_DEPLOY, getLocale( request ) ) );
504 
505         }
506 
507         // Check constraints
508         if ( !CollectionUtils.isEmpty( listErrors ) )
509         {
510             return responseJSON( JsonUtil.buildJsonResponse( new FastDeployResponseError( listErrors ) ) );
511         }
512 
513         updateWorkflowDeployApplicationContextWithDTO( workflowDeployContext, deployAppDto );
514 
515         try
516         {
517 
518             checkPermissionByFastDeployEnvironment( request, _workflowDeployContext.getFastDeployApplicationData( ), _workflowDeployContext.getPermission( ),
519                     _workflowDeployContext.getCodeEnvironement( ) );
520 
521         }
522         catch( Exception e )
523         {
524             AppLogService.error( e );
525             listErrors.add( I18nService.getLocalizedString( ERROR_USER_NOT_AUTHORIZED, getLocale( request ) ) );
526             return responseJSON( JsonUtil.buildJsonResponse( new FastDeployResponseError( listErrors ) ) );
527         }
528 
529         // Add before run
530         _workflowDeploySiteService.addWorkflowDeploySiteContext( _workflowDeployContext );
531         DeploymentUtils.getState( _workflowDeployContext );
532         return responseJSON( JsonUtil.buildJsonResponse( new JsonResponse( _workflowDeployContext ) ) );
533 
534     }
535 
536     /**
537      * Process the deploy script action
538      * 
539      * @param request
540      *            the httpServletRequest
541      * @return a JSON with the command result of the action processing
542      * @throws UserNotSignedException
543      * @throws SiteMessageException
544      */
545     @Action( ACTION_DEPLOY_SCRIPT )
546     public XPage doDeployScript( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
547     {
548         List<String> listErrors;
549         WorkflowDeployScriptContextugins/appcenter/modules/fastdeployapplication/business/context/WorkflowDeployScriptContext.html#WorkflowDeployScriptContext">WorkflowDeployScriptContext workflowDeployScriptContext = (WorkflowDeployScriptContext) _workflowDeployContext;
550         FormDeployScriptDTOles/fastdeployapplication/web/dto/FormDeployScriptDTO.html#FormDeployScriptDTO">FormDeployScriptDTO deployScriptDto = new FormDeployScriptDTO( );
551         populate( deployScriptDto, request );
552         listErrors = getFormErrors( deployScriptDto, getLocale( request ) );
553 
554         // Check constraints
555         if ( CollectionUtils.isEmpty( listErrors ) )
556         {
557             updateWorkflowDeployScriptContextWithDTO( workflowDeployScriptContext, deployScriptDto, request, listErrors );
558 
559             if ( StringUtils.isEmpty( workflowDeployScriptContext.getScriptFileItemName( ) ) )
560             {
561                 listErrors.add( I18nService.getLocalizedString( MESSAGE_MANDATORY_CHOICE_SELECT_SCRIPT, getLocale( request ) ) );
562 
563             }
564         }
565 
566         // Check constraints
567         if ( !CollectionUtils.isEmpty( listErrors ) )
568         {
569             return responseJSON( JsonUtil.buildJsonResponse( new FastDeployResponseError( listErrors ) ) );
570         }
571 
572         checkPermissionByFastDeployEnvironment( request, _workflowDeployContext.getFastDeployApplicationData( ), _workflowDeployContext.getPermission( ),
573                 _workflowDeployContext.getCodeEnvironement( ) );
574 
575         // Add before run
576         _workflowDeploySiteService.addWorkflowDeploySiteContext( _workflowDeployContext );
577         DeploymentUtils.getState( _workflowDeployContext );
578 
579         return responseJSON( JsonUtil.buildJsonResponse( new JsonResponse( _workflowDeployContext ) ) );
580 
581     }
582 
583     /**
584      * Process the deploy init tomcat action
585      * 
586      * @param request
587      *            the httpServletRequest
588      * @return a JSON with the command result of the action processing
589      * @throws UserNotSignedException
590      * @throws SiteMessageException
591      */
592     @Action( ACTION_DEPLOY_INIT_TOMCAT )
593     public XPage doDeployInitTomcat( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
594     {
595         List<String> listErrors;
596         WorkflowInitTomcatContextins/appcenter/modules/fastdeployapplication/business/context/WorkflowInitTomcatContext.html#WorkflowInitTomcatContext">WorkflowInitTomcatContext workflowDeployInitTomcatContext = (WorkflowInitTomcatContext) _workflowDeployContext;
597         FormDeployInitContextDTOeployapplication/web/dto/FormDeployInitContextDTO.html#FormDeployInitContextDTO">FormDeployInitContextDTO deployInitTomcatDto = new FormDeployInitContextDTO( );
598         populate( deployInitTomcatDto, request );
599         listErrors = getFormErrors( deployInitTomcatDto, getLocale( request ) );
600         updateWorkflowDeployInitTomcatContextWithDTO( workflowDeployInitTomcatContext, deployInitTomcatDto );
601 
602         // Check constraints
603         if ( !CollectionUtils.isEmpty( listErrors ) )
604         {
605             return responseJSON( JsonUtil.buildJsonResponse( new FastDeployResponseError( listErrors ) ) );
606         }
607         checkPermissionByFastDeployEnvironment( request, _workflowDeployContext.getFastDeployApplicationData( ), _workflowDeployContext.getPermission( ),
608                 _workflowDeployContext.getCodeEnvironement( ) );
609 
610         // Add before run
611         _workflowDeploySiteService.addWorkflowDeploySiteContext( _workflowDeployContext );
612         DeploymentUtils.getState( _workflowDeployContext );
613 
614         return responseJSON( JsonUtil.buildJsonResponse( new JsonResponse( _workflowDeployContext ) ) );
615 
616     }
617 
618     /**
619      * Process the deploy init database action
620      * 
621      * @param request
622      *            the httpServletRequest
623      * @return a JSON with the command result of the action processing
624      * @throws UserNotSignedException
625      * @throws SiteMessageException
626      */
627     @Action( ACTION_DEPLOY_INIT_DATABASE )
628     public XPage doDeployInitDatabase( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
629     {
630         List<String> listErrors;
631         WorkflowInitDatabaseContextappcenter/modules/fastdeployapplication/business/context/WorkflowInitDatabaseContext.html#WorkflowInitDatabaseContext">WorkflowInitDatabaseContext workflowDeployInitDatabaseContext = (WorkflowInitDatabaseContext) _workflowDeployContext;
632         FormDeployInitDatabaseDTOoyapplication/web/dto/FormDeployInitDatabaseDTO.html#FormDeployInitDatabaseDTO">FormDeployInitDatabaseDTO deployInitDatabaseDto = new FormDeployInitDatabaseDTO( );
633         populate( deployInitDatabaseDto, request );
634         listErrors = getFormErrors( deployInitDatabaseDto, getLocale( request ) );
635         updateWorkflowDeployInitDatabaseContextWithDTO( workflowDeployInitDatabaseContext, deployInitDatabaseDto );
636 
637         // Check constraints
638         if ( !CollectionUtils.isEmpty( listErrors ) )
639         {
640             return responseJSON( JsonUtil.buildJsonResponse( new FastDeployResponseError( listErrors ) ) );
641         }
642 
643         checkPermissionByFastDeployEnvironment( request, _workflowDeployContext.getFastDeployApplicationData( ), _workflowDeployContext.getPermission( ),
644                 _workflowDeployContext.getCodeEnvironement( ) );
645 
646         // Add before run
647         _workflowDeploySiteService.addWorkflowDeploySiteContext( _workflowDeployContext );
648         DeploymentUtils.getState( _workflowDeployContext );
649 
650         return responseJSON( JsonUtil.buildJsonResponse( new JsonResponse( _workflowDeployContext ) ) );
651 
652     }
653 
654     private <T> List<String> getFormErrors( T bean, Locale locale )
655     {
656 
657         List<String> listErrors = new ArrayList<>( );
658         Set<ConstraintViolation<T>> errors = BeanValidationUtil.validate( bean );
659 
660         for ( ConstraintViolation<T> constraint : errors )
661         {
662 
663             listErrors.add( I18nService.getLocalizedString( constraint.getMessage( ), locale ) );
664         }
665 
666         return listErrors;
667     }
668 
669     /**
670      * Get the credentials form
671      * 
672      * @param request
673      *            the HttpServletRequest
674      * @return the credentials form
675      * @throws AccessDeniedException
676      */
677     @View( value = VIEW_ASK_CREDENTIALS )
678     public XPage getFormAskCredentials( HttpServletRequest request ) throws AccessDeniedException, SiteMessageException, UserNotSignedException
679     {
680         FastDeployApplicationData data = null;
681         try
682         {
683             data = getFastDeployData( request );
684         }
685         catch( AbstractFastDeployException e )
686         {
687             return redirectFastDeployConfigErrorPage( request, e, null );
688         }
689 
690         String strActionUrl = request.getParameter( PARAM_REDIRECT_VIEW );
691         String strDeployType = request.getParameter( ConstanteUtils.PARAM_DEPLOY_TYPE );
692         if ( isPrivateRepo( data ) )
693         {
694             HttpSession session = request.getSession( true );
695             AbstractVCSUser./../../../../../fr/paris/lutece/plugins/appcenter/modules/fastdeployapplication/business/vcs/AbstractVCSUser.html#AbstractVCSUser">AbstractVCSUser user = (AbstractVCSUser) session.getAttribute( ConstanteUtils.ATTRIBUTE_VCS_USER );
696             if ( user == null )
697                 user = new DefaultVCSUser( );
698             User userApp = UserService.getCurrentUser( request );
699             if ( data.getRepoType( ).equals( ConstanteUtils.CONSTANTE_REPO_TYPE_GITLAB ) )
700             {
701                 GitlabUserInfo gitlabInfos = UserService.loadUserInfoSubset( userApp, "gitlab", GitlabUserInfo.class );
702                 if ( gitlabInfos == null )
703                 {
704                     gitlabInfos = new GitlabUserInfo( );
705                 }
706                 user.setLogin( gitlabInfos.getGitlabLogin( ) );
707                 user.setPassword( CryptoUtil.decrypt( gitlabInfos.getGitlabPassword( ) ) );
708             }
709 
710             if ( data.getRepoType( ).equals( ConstanteUtils.CONSTANTE_REPO_TYPE_SVN ) )
711             {
712                 SvnUserInfo svnInfos = UserService.loadUserInfoSubset( userApp, "svn", SvnUserInfo.class );
713                 if ( svnInfos == null )
714                 {
715                     svnInfos = new SvnUserInfo( );
716                 }
717                 user.setLogin( svnInfos.getSvnLogin( ) );
718                 user.setPassword( CryptoUtil.decrypt( svnInfos.getSvnPassword( ) ) );
719             }
720 
721             Map<String, Object> model = getModel( );
722             model.put( ConstanteUtils.MARK_VCS_SERVICE, DeploymentUtils.getVCSService( data.getRepoType( ) ) );
723             model.put( MARK_APPLICATION_DATA, data );
724             model.put( ConstanteUtils.MARK_APPLICATION, _application );
725             model.put( MARK_REDIRECT_VIEW, strActionUrl );
726             model.put( ConstanteUtils.MARK_DEPLOY_TYPE, strDeployType );
727             model.put( ConstanteUtils.MARK_USER_VCS, user );
728             fillAppCenterCommons( model, request );
729 
730             // Return ask credentials form
731             return getXPage( ConstanteUtils.TEMPLATE_ASK_CREDENTIALS, request.getLocale( ), model );
732         }
733         else
734         {
735             // No ask for credentials for scripts deployment
736             return redirect( request, strActionUrl );
737         }
738     }
739 
740     protected XPage redirectFastDeployConfigErrorPage( HttpServletRequest request, AbstractFastDeployException e, String strView )
741     {
742         Map<String, String> mapAdditionnalsParameters = new HashMap<String, String>( );
743         mapAdditionnalsParameters.put( PARAM_REDIRECT_VIEW, strView );
744         if ( e instanceof NoExistingFastDeployConfigException )
745         {
746             return redirect( request, VIEW_NO_EXISTING_FAST_DEPLOY_CONFIG, mapAdditionnalsParameters );
747         }
748         if ( e instanceof WrongFastDeployConfigRequiredException )
749         {
750             mapAdditionnalsParameters.put( ConstanteUtils.PARAM_DEPLOY_TYPE, request.getParameter( ConstanteUtils.PARAM_DEPLOY_TYPE ) );
751             return redirect( request, VIEW_SELECT_FAST_DEPLOY_CONFIG, mapAdditionnalsParameters );
752         }
753         if ( e instanceof VCSUserNotSignedException )
754         {
755             if ( request.getParameter( PARAM_AUTHENTICATION_TRY ) != null )
756             {
757                 addError( "#i18n{" + ERROR_USER_NOT_AUTHENTICATED + "} " + e.getMessage( ) );
758             }
759             mapAdditionnalsParameters.put( PARAM_ID_FAST_APPLICATION_DATA, request.getParameter( PARAM_ID_FAST_APPLICATION_DATA ) );
760             mapAdditionnalsParameters.put( ConstanteUtils.PARAM_DEPLOY_TYPE, request.getParameter( ConstanteUtils.PARAM_DEPLOY_TYPE ) );
761             return redirect( request, VIEW_ASK_CREDENTIALS, mapAdditionnalsParameters );
762         }
763         return null;
764     }
765 
766     public static boolean isPrivateRepo( FastDeployApplicationData applicationData )
767     {
768         IVCSService vcsService = DeploymentUtils.getVCSService( applicationData.getRepoType( ) );
769         return vcsService.isPrivate( );
770     }
771 
772     /**
773      * Get the credentials form
774      * 
775      * @param request
776      *            the HttpServletRequest
777      * @return the credentials form
778      * @throws AccessDeniedException
779      */
780     @View( value = VIEW_NO_EXISTING_FAST_DEPLOY_CONFIG )
781     public XPage getNoExistingFastDeloy( HttpServletRequest request ) throws AccessDeniedException, SiteMessageException, UserNotSignedException
782     {
783         Map<String, Object> model = getModel( );
784         fillAppCenterCommons( model, request );
785 
786         return getXPage( TEMPLATE_NO_EXISTING_FAST_DEPLOY_CONFIG, request.getLocale( ), model );
787     }
788 
789     private void fillDeploySite( Map<String, Object> model, HttpServletRequest request )
790     {
791         Environment environment = getActiveEnvironment( request );
792 
793         HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceTomcat = DeploymentFactory.getInstance( )
794                 .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data, Integer.toString( _application.getId( ) ),
795                         ConstanteUtils.CONSTANTE_SERVER_TOMCAT, getLocale( request ), false, false, environment.getPrefix( ) );
796 
797         ReferenceList refListTagSite = DeploymentUtils.getVCSService( _data.getRepoType( ) ).getTagsSite( _data.getUrlSite( ), _vcsUser,
798                 _data.getArtifactId( ) );
799         ReferenceItem emptyItem = new ReferenceItem( );
800         emptyItem.setCode( ConstanteUtils.CONSTANTE_EMPTY_STRING );
801         emptyItem.setName( ConstanteUtils.CONSTANTE_EMPTY_STRING );
802         refListTagSite.add( 0, emptyItem );
803 
804         model.put( ConstanteUtils.MARK_SITE_LIST, refListTagSite );
805         model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_TOMCAT, hashServerApplicationInstanceTomcat );
806         if ( !_data.isLuteceSite( ) )
807         {
808             model.put( ConstanteUtils.MARK_DEPLOY_NON_LUTECE, ConstanteUtils.MARK_DEPLOY_NON_LUTECE );
809         }
810         model.put( ConstanteUtils.MARK_DISK_REQUIRED_FREESPACE,
811                 AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOY_SITE_DISK_REQUIRED_FREESPACE ) );
812     }
813 
814     /**
815      * Fill the model for init tomcat context deployment
816      * 
817      * @param model
818      * @param request
819      */
820     private void fillInitTomcatContext( Map<String, Object> model, HttpServletRequest request )
821     {
822         Environment environment = getActiveEnvironment( request );
823 
824         HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceTomcat = DeploymentFactory.getInstance( )
825                 .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data, Integer.toString( _application.getId( ) ),
826                         ConstanteUtils.CONSTANTE_SERVER_TOMCAT, getLocale( request ), false, false, environment.getPrefix( ) );
827 
828         model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_TOMCAT, hashServerApplicationInstanceTomcat );
829     }
830 
831     /**
832      * Fill the model for init database deployment process
833      * 
834      * @param model
835      * @param request
836      */
837     private void fillInitDatabase( Map<String, Object> model, HttpServletRequest request )
838     {
839         Environment environment = getActiveEnvironment( request );
840 
841         HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceSql = DeploymentFactory.getInstance( )
842                 .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data, Integer.toString( _application.getId( ) ),
843                         ConstanteUtils.CONSTANTE_SERVER_MYSQL, getLocale( request ), false, false, environment.getPrefix( ) );
844         model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_SQL, hashServerApplicationInstanceSql );
845         model.put( ConstanteUtils.MARK_DISK_REQUIRED_FREESPACE,
846                 AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOY_SCRIT_DISK_REQUIRED_FREESPACE ) );
847     }
848 
849     /**
850      * Fill the model for script deployment process
851      * 
852      * @param model
853      * @param request
854      */
855     private void fillDeployScript( Map<String, Object> model, HttpServletRequest request )
856     {
857 
858         Environment environment = getActiveEnvironment( request );
859 
860         ReferenceList refListUpgradeFilesList = DeploymentUtils.getVCSService( _data.getRepoType( ) ).getUpgradesFiles( _data.getArtifactId( ),
861                 _data.getUrlSite( ), _vcsUser );
862         if ( refListUpgradeFilesList != null )
863         {
864             refListUpgradeFilesList = DeploymentUtils.addEmptyRefenceItem( refListUpgradeFilesList );
865         }
866 
867         HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceSql = DeploymentFactory.getInstance( )
868                 .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data, Integer.toString( _application.getId( ) ),
869                         ConstanteUtils.CONSTANTE_SERVER_MYSQL, getLocale( request ), false, false, environment.getPrefix( ) );
870 
871         HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstancePSQ = DeploymentFactory.getInstance( )
872                 .getServerApplicationService( _data.getApplicationType( ) ).getHashServerApplicationInstance( _data, Integer.toString( _application.getId( ) ),
873                         ConstanteUtils.CONSTANTE_SERVER_PSQ, getLocale( request ), false, false, environment.getPrefix( ) );
874 
875         for ( Map.Entry<String, List<ServerApplicationInstance>> entry : hashServerApplicationInstancePSQ.entrySet( ) )
876         {
877             String key = entry.getKey( );
878             List<ServerApplicationInstance> listServerPsq = entry.getValue( );
879 
880             if ( hashServerApplicationInstanceSql.containsKey( entry.getKey( ) ) && hashServerApplicationInstanceSql.get( entry.getKey( ) ) != null
881                     && listServerPsq != null )
882             {
883                 hashServerApplicationInstanceSql.get( entry.getKey( ) ).addAll( listServerPsq );
884             }
885             else
886             {
887                 hashServerApplicationInstanceSql.put( key, listServerPsq );
888 
889             }
890         }
891 
892         /**
893          * HashMap<String, List<String>> hashDatabase = DeploymentFactory.getInstance( ).getDatabaseService( _data.getApplicationType( ) ).getHashDatabases(
894          * _application.getCode( ), hashServerApplicationInstanceSql, getLocale(request ) );
895          **/
896         model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_SQL, hashServerApplicationInstanceSql );
897         // model.put( ConstanteUtils.MARK_DATABASE_MAP, hashDatabase );
898         model.put( ConstanteUtils.MARK_UPGRADE_FILE_REF_LIST, refListUpgradeFilesList );
899         model.put( ConstanteUtils.MARK_HANDLER, SpringContextService.getBean( DeploymentUploadHandler.BEAN_NAME ) );
900         model.put( ConstanteUtils.MARK_LIST_UPLOADED_FILE, _handler.getListUploadedFiles( ConstanteUtils.PARAM_SCRIPT_UPLOAD, request.getSession( ) ) );
901         model.put( ConstanteUtils.MARK_DISK_REQUIRED_FREESPACE,
902                 AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOY_SCRIT_DISK_REQUIRED_FREESPACE ) );
903         model.put( ConstanteUtils.MARK_DISK_REQUIRED_FREESPACE_WITH_DUMP,
904                 AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_DEPLOY_SCRIT_WITH_DUMP_DISK_REQUIRED_FREESPACE ) );
905 
906     }
907 
908     /**
909      * Fill the commons deploy in session and model
910      * 
911      * @param model
912      * @param request
913      * @throws AbstractFastDeployException
914      */
915     private void fillCommonsDeploy( Map<String, Object> model, HttpServletRequest request ) throws AbstractFastDeployException
916     {
917         _data = getFastDeployData( request );
918         _vcsUser = getVCSUser( request, _data );
919         _vcsService = getVCSService( _data );
920 
921         List<FastDeployEnvironment> listFastDeployEnvironments;
922         Environment environment = getActiveEnvironment( request );
923         if ( environment != null )
924         {
925             listFastDeployEnvironments = DeploymentFactory.getInstance( ).getEnvironmentService( _data.getApplicationType( ) )
926                     .getListEnvironmentsByType( _application.getCode( ), getLocale( request ), environment.getPrefix( ) );
927         }
928         else
929         {
930             listFastDeployEnvironments = DeploymentFactory.getInstance( ).getEnvironmentService( _data.getApplicationType( ) )
931                     .getListEnvironments( _application.getCode( ), getLocale( request ) );
932         }
933         model.put( ConstanteUtils.MARK_ENVIRONMENT_LIST, listFastDeployEnvironments );
934         model.put( MARK_APPLICATION_DATA, _data );
935 
936     }
937 
938     @Action( value = ACTION_MODIFY_MAVEN_PROFILE )
939     public XPage doModifyMavenProfil( HttpServletRequest request ) throws AccessDeniedException
940     {
941         String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
942         String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
943         String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
944         String strMavenProfil = request.getParameter( ConstanteUtils.PARAM_MAVEN_PROFIL );
945 
946         FastDeployEnvironment environment = DeploymentFactory.getInstance( ).getEnvironmentService( _data.getApplicationType( ) )
947                 .getEnvironment( strCodeEnvironment, request.getLocale( ) );
948 
949         MavenService.getService( ).saveMvnProfilName( strMavenProfil, strIdApplication, strCodeEnvironment, strCodeServerApplicationInstance );
950 
951         return redirectView( request, VIEW_MANAGE_ENV );
952     }
953 
954     @Action( value = ACTION_MODIFY_SERVER_NAME )
955     public XPage doModifyServerName( HttpServletRequest request ) throws AccessDeniedException
956     {
957         String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
958         String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
959         String strServerType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
960         String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
961         String strServerName = request.getParameter( ConstanteUtils.PARAM_SERVER_NAME );
962 
963         DeploymentFactory.getInstance( ).getServerApplicationService( _data.getApplicationType( ) ).saveServerName( strServerName, strIdApplication,
964                 strCodeEnvironment, strServerType, strCodeServerApplicationInstance );
965 
966         return redirectView( request, VIEW_MANAGE_ENV );
967     }
968 
969     /**
970      * Check if the provided repository is valid
971      * 
972      * @param request
973      *            the HttpServletRequest request
974      * @return a JSON with the validity and type of the repo
975      */
976     @Action( value = ACTION_CHECK_REPOSITORY )
977     public XPage checkRepository( HttpServletRequest request )
978     {
979         String strRepoUrl = request.getParameter( ConstanteUtils.PARAM_URL_REPO );
980         return responseJSON( RepositoryUtils.checkRepository( strRepoUrl ) );
981     }
982 
983     /**
984      * Set the workflow deploy application context
985      */
986     private void setWorkflowDeployApplicationContext( )
987     {
988         WorkflowDeployAppContextapplication/business/context/WorkflowDeployAppContext.html#WorkflowDeployAppContext">WorkflowDeployAppContext workflowDeployAppContext = new WorkflowDeployAppContext( );
989         workflowDeployAppContext.setLuteceSite( _data.isLuteceSite( ) );
990         workflowDeployAppContext.setDeployDevSite( false );
991         workflowDeployAppContext.setFastDeployApplicationData( _data );
992         workflowDeployAppContext.setVcsUser( _vcsUser );
993         workflowDeployAppContext.setCustomMavenCommand( _data.getMavenDeployCustomGoal( ) );
994         workflowDeployAppContext.setIdApplication( _application.getId( ) );
995         _workflowDeployContext = workflowDeployAppContext;
996     }
997 
998     /**
999      * Set the workflow deploy script context
1000      */
1001     private void setWorkflowDeployScriptContext( )
1002     {
1003         WorkflowDeployScriptContextation/business/context/WorkflowDeployScriptContext.html#WorkflowDeployScriptContext">WorkflowDeployScriptContext workflowDeployScriptContext = new WorkflowDeployScriptContext( );
1004         workflowDeployScriptContext.setFastDeployApplicationData( _data );
1005         workflowDeployScriptContext.setVcsUser( _vcsUser );
1006         workflowDeployScriptContext.setIdApplication( _application.getId( ) );
1007         _workflowDeployContext = workflowDeployScriptContext;
1008     }
1009 
1010     /**
1011      * Set the workflow init tomcat context
1012      */
1013     private void setWorkflowDeployInitTomcatContext( )
1014     {
1015         WorkflowInitTomcatContextplication/business/context/WorkflowInitTomcatContext.html#WorkflowInitTomcatContext">WorkflowInitTomcatContext workflowInitTomcatContext = new WorkflowInitTomcatContext( );
1016         workflowInitTomcatContext.setFastDeployApplicationData( _data );
1017         workflowInitTomcatContext.setVcsUser( _vcsUser );
1018         workflowInitTomcatContext.setIdApplication( _application.getId( ) );
1019         _workflowDeployContext = workflowInitTomcatContext;
1020     }
1021 
1022     /**
1023      * Get the workflow init database context
1024      */
1025     private void setWorkflowInitDatabaseContext( )
1026     {
1027         WorkflowInitDatabaseContextation/business/context/WorkflowInitDatabaseContext.html#WorkflowInitDatabaseContext">WorkflowInitDatabaseContext workflowInitDatabaseContext = new WorkflowInitDatabaseContext( );
1028         workflowInitDatabaseContext.setFastDeployApplicationData( _data );
1029         workflowInitDatabaseContext.setVcsUser( _vcsUser );
1030         workflowInitDatabaseContext.setIdApplication( _application.getId( ) );
1031         _workflowDeployContext = workflowInitDatabaseContext;
1032     }
1033 
1034     /**
1035      * Update the workflow deploy application context when processing the action of deployment
1036      * 
1037      * @param context
1038      *            the workflow context
1039      * @param dto
1040      *            the data transfert object of the action form
1041      */
1042     private void updateWorkflowDeployApplicationContextWithDTO( WorkflowDeployAppContext context, FormDeployAppDTO dto )
1043     {
1044         context.setCodeEnvironement( dto.getCodeEnvironment( ) );
1045         context.setCodeServerInstance( dto.getCodeServerApplicationInstance( ), ConstanteUtils.CONSTANTE_SERVER_TOMCAT );
1046         context.setDeployDevSite( dto.isDeployDevSite( ) );
1047         if ( !context.isDeployDevSite( ) )
1048         {
1049             context.setTagToDeploy( dto.getTagToDeploy( ) );
1050         }
1051         context.setCommandResult( new CommandResult( ) );
1052         context.setCustomMavenCommand( dto.getMavenDeployCustomGoal( ) );
1053         context.setDiskRequiredFreespace( dto.getDiskRequiredFreespace( ) );
1054         _workflowDeployContext = context;
1055     }
1056 
1057     /**
1058      * Update the workflow deploy script context when processing the action of deployment
1059      * 
1060      * @param context
1061      *            the workflow context
1062      * @param dto
1063      *            the data transfert object of the action form
1064      */
1065     private void updateWorkflowDeployScriptContextWithDTO( WorkflowDeployScriptContext context, FormDeployScriptDTO dto, HttpServletRequest request,
1066             List<String> listErrors )
1067     {
1068         List<FileItem> scriptListItem = _handler.getListUploadedFiles( ConstanteUtils.PARAM_SCRIPT_UPLOAD, request.getSession( ) );
1069         if ( !dto.isExecuteRemoteScript( ) && !CollectionUtils.isEmpty( scriptListItem ) )
1070         {
1071             try
1072             {
1073                 dto.setScriptFileItem( scriptListItem.get( 0 ).getInputStream( ) );
1074             }
1075             catch( IOException e )
1076             {
1077                 AppLogService.error( e );
1078             }
1079             dto.setScriptFileItemName( scriptListItem.get( 0 ).getName( ) );
1080 
1081         }
1082 
1083         // delete file item for a given session
1084         _handler.removeAllFileItem( request.getSession( ) );
1085         if ( !dto.isExecuteRemoteScript( ) && !StringUtils.isEmpty( dto.getScriptFileSelected( ) ) )
1086         {
1087             try
1088             {
1089                 File upgradeFile = DeploymentUtils.getVCSService( _data.getRepoType( ) ).getUpgradeFile( _data.getArtifactId( ), _data.getUrlSite( ), _vcsUser,
1090                         dto.getScriptFileSelected( ) );
1091                 dto.setScriptFileItem( new FileInputStream( upgradeFile ) );
1092                 dto.setScriptFileItemName( upgradeFile.getName( ) );
1093             }
1094             catch( FileNotFoundException e )
1095             {
1096                 AppLogService.error( e );
1097             }
1098         }
1099 
1100         // Check Upload File
1101 
1102         if ( !dto.isExecuteRemoteScript( ) && StringUtils.isEmpty( dto.getScriptFileSelected( ) )
1103                 && ( CollectionUtils.isEmpty( scriptListItem ) || scriptListItem.get( 0 ) == null || scriptListItem.get( 0 ).getSize( ) == 0 ) )
1104         {
1105             listErrors.add( I18nService.getLocalizedString( ConstanteUtils.PROPERTY_LABEL_SCRIPT_UPLOAD, getLocale( request ) ) );
1106         }
1107         context.setCodeEnvironement( dto.getCodeEnvironment( ) );
1108         context.setCodeServerInstance( dto.getCodeServerApplicationInstanceSql( ).split( "_" ) [0],
1109                 dto.getCodeServerApplicationInstanceSql( ).split( "_" ) [1] );
1110         context.setDoDump( dto.getDoDump( ) );
1111         context.setDatabaseName( dto.getCodeDatabase( ) );
1112         if ( dto.isExecuteRemoteScript( ) )
1113         {
1114             context.setScriptFileItemName( dto.getScriptRemoteFileSelected( ) );
1115 
1116         }
1117         else
1118         {
1119             context.setScriptFileItemName( dto.getScriptFileItemName( ) );
1120             context.setScriptFileItem( dto.getScriptFileItem( ) );
1121             context.setScriptFileSelected( dto.getScriptFileSelected( ) );
1122         }
1123         context.setDiskRequiredFreespace( dto.getDiskRequiredFreespace( ) );
1124         _workflowDeployContext = context;
1125     }
1126 
1127     /**
1128      * Update the workflow deploy init tomcat context when processing the action of deployment
1129      * 
1130      * @param context
1131      *            the workflow context
1132      * @param dto
1133      *            the data transfert object of the action form
1134      */
1135     private void updateWorkflowDeployInitTomcatContextWithDTO( WorkflowInitTomcatContext context, FormDeployInitContextDTO dto )
1136     {
1137         context.setCodeEnvironement( dto.getCodeEnvironment( ) );
1138         context.setCodeServerInstance( dto.getCodeServerApplicationInstance( ), ConstanteUtils.CONSTANTE_SERVER_TOMCAT );
1139         context.setCommandResult( new CommandResult( ) );
1140         context.setDiskRequiredFreespace( dto.getDiskRequiredFreespace( ) );
1141         _workflowDeployContext = context;
1142     }
1143 
1144     /**
1145      * Update the workflow deploy init database context when processing the action of deployment
1146      * 
1147      * @param context
1148      *            the workflow context
1149      * @param dto
1150      *            the data transfert object of the action form
1151      */
1152     private void updateWorkflowDeployInitDatabaseContextWithDTO( WorkflowInitDatabaseContext context, FormDeployInitDatabaseDTO dto )
1153     {
1154         context.setCodeEnvironement( dto.getCodeEnvironment( ) );
1155         context.setCodeServerInstance( dto.getCodeServerApplicationInstanceSql( ), ConstanteUtils.CONSTANTE_SERVER_MYSQL );
1156         context.setDatabaseName( dto.getCodeDatabase( ) );
1157         context.setDiskRequiredFreespace( dto.getDiskRequiredFreespace( ) );
1158         _workflowDeployContext = context;
1159     }
1160 
1161     /**
1162      * Get the fast deploy data
1163      * 
1164      * @param request
1165      * @return the fastdeploy data
1166      * @throws AbstractFastDeployException
1167      */
1168     protected FastDeployApplicationData getFastDeployData( HttpServletRequest request ) throws AbstractFastDeployException
1169     {
1170         FastDeployApplicationsData fastDeployApplicationDatas = ApplicationService.loadApplicationDataSubset( _application, FastDeployApplicationsData.class );
1171 
1172         String strIdFastDeployApplicationData = request.getParameter( PARAM_ID_FAST_APPLICATION_DATA );
1173 
1174         if ( fastDeployApplicationDatas == null )
1175         {
1176             throw new NoExistingFastDeployConfigException( );
1177         }
1178         else
1179             if ( !CollectionUtils.isEmpty( fastDeployApplicationDatas.getListData( ) ) )
1180             {
1181                 // set id application
1182                 fastDeployApplicationDatas.getListData( ).forEach( x -> x.setIdApplication( _application.getId( ) ) );
1183 
1184             }
1185 
1186         if ( fastDeployApplicationDatas.getListData( ).size( ) == 1 )
1187         {
1188             return fastDeployApplicationDatas.getListData( ).get( 0 );
1189         }
1190 
1191         try
1192         {
1193             FastDeployApplicationData data = fastDeployApplicationDatas.getListData( ).stream( )
1194                     .filter( x -> x.getIdApplicationData( ).equals( Integer.parseInt( strIdFastDeployApplicationData ) ) ).findFirst( ).get( );
1195             // If the strIdData doesnt correspond to existing FastDeployApplicationData, then null is returned
1196             if ( data == null )
1197             {
1198                 throw new WrongFastDeployConfigRequiredException( );
1199             }
1200             return data;
1201         }
1202         catch( NumberFormatException e )
1203         {
1204             throw new WrongFastDeployConfigRequiredException( );
1205         }
1206     }
1207 
1208     /**
1209      * Get the VCS User
1210      * 
1211      * @param request
1212      * @param data
1213      * @return
1214      * @throws AbstractFastDeployException
1215      */
1216     private AbstractVCSUser getVCSUser( HttpServletRequest request, FastDeployApplicationData data ) throws AbstractFastDeployException
1217     {
1218         return DeploymentUtils.getVCSUser( request, data );
1219     }
1220 
1221     /**
1222      * Get the VCS Service
1223      * 
1224      * @param data
1225      * @return
1226      */
1227     private IVCSService getVCSService( FastDeployApplicationData data )
1228     {
1229         return DeploymentUtils.getVCSService( data.getRepoType( ) );
1230     }
1231 
1232     /**
1233      * Check if user is well authenticated
1234      * 
1235      * @throws AbstractFastDeployException
1236      */
1237     private void checkAuthentication( HttpServletRequest request ) throws AbstractFastDeployException, UserNotSignedException
1238     {
1239         _vcsService.checkAuthentication( _data.getUrlSite( ), _vcsUser );
1240 
1241         if ( request.getParameter( ConstanteUtils.PARAM_SAVE ) != null )
1242         {
1243             if ( _vcsService.getVCSType( ).equals( ConstanteUtils.CONSTANTE_REPO_TYPE_GITLAB ) )
1244             {
1245                 GitlabUserInfo gitlabInfo = new GitlabUserInfo( );
1246                 gitlabInfo.setGitlabLogin( _vcsUser.getLogin( ) );
1247                 gitlabInfo.setGitlabPassword( CryptoUtil.encrypt( _vcsUser.getPassword( ) ) );
1248 
1249                 User user = UserService.getCurrentUser( request );
1250                 UserService.saveUserInfos( user, gitlabInfo );
1251             }
1252             if ( _vcsService.getVCSType( ).equals( ConstanteUtils.CONSTANTE_REPO_TYPE_SVN ) )
1253             {
1254                 SvnUserInfo svnInfo = new SvnUserInfo( );
1255                 svnInfo.setSvnLogin( _vcsUser.getLogin( ) );
1256                 svnInfo.setSvnPassword( CryptoUtil.encrypt( _vcsUser.getPassword( ) ) );
1257 
1258                 User user = UserService.getCurrentUser( request );
1259                 UserService.saveUserInfos( user, svnInfo );
1260             }
1261         }
1262     }
1263 
1264     /**
1265      * Fill the workflow actions in the model
1266      * 
1267      * @param model
1268      *            the model
1269      */
1270     private void fillWorkflowActions( Map<String, Object> model )
1271     {
1272         model.put( ConstanteUtils.MARK_ACTION_LIST, DeploymentUtils.getActionList( _workflowDeployContext ) );
1273     }
1274 
1275     protected void checkPermissionByFastDeployEnvironment( HttpServletRequest request, FastDeployApplicationData data, String strPermission,
1276             String strCodeEnvironment ) throws SiteMessageException, UserNotSignedException
1277     {
1278 
1279         FastDeployEnvironment fastEnvironement = DeploymentFactory.getInstance( ).getEnvironmentService( data.getApplicationType( ) )
1280                 .getEnvironment( strCodeEnvironment, request.getLocale( ) );
1281 
1282         try
1283         {
1284             if ( fastEnvironement != null )
1285             {
1286                 checkPermission( request, strPermission, fastEnvironement.getEnvType( ) );
1287             }
1288             else
1289             {
1290                 getUnauthorizedAccessMessage( request );
1291 
1292             }
1293         }
1294         catch( AccessDeniedException e )
1295         {
1296             getUnauthorizedAccessMessage( request );
1297         }
1298     }
1299 }