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.util;
35  
36  import java.io.File;
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  import java.util.Map.Entry;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  import net.sf.json.JSONArray;
48  import net.sf.json.JSONException;
49  import net.sf.json.JSONObject;
50  import net.sf.json.JSONSerializer;
51  
52  import org.apache.commons.collections.CollectionUtils;
53  import org.apache.commons.lang.StringUtils;
54  
55  import fr.paris.lutece.plugins.deployment.business.ActionParameter;
56  import fr.paris.lutece.plugins.deployment.business.Application;
57  import fr.paris.lutece.plugins.deployment.business.CommandResult;
58  import fr.paris.lutece.plugins.deployment.business.IAction;
59  import fr.paris.lutece.plugins.deployment.business.vcs.SvnUser;
60  import fr.paris.lutece.plugins.deployment.business.ServerApplicationInstance;
61  import fr.paris.lutece.plugins.deployment.business.WorkflowDeploySiteContext;
62  import fr.paris.lutece.plugins.deployment.business.vcs.AbstractVCSUser;
63  import fr.paris.lutece.plugins.deployment.business.vcs.GitUser;
64  import fr.paris.lutece.plugins.deployment.service.DeploymentPlugin;
65  import fr.paris.lutece.plugins.deployment.service.vcs.IVCSService;
66  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
67  import fr.paris.lutece.plugins.workflowcore.business.state.State;
68  import fr.paris.lutece.portal.business.user.attribute.AdminUserField;
69  import fr.paris.lutece.portal.service.i18n.I18nService;
70  import fr.paris.lutece.portal.service.spring.SpringContextService;
71  import fr.paris.lutece.portal.service.user.attribute.AdminUserFieldService;
72  import fr.paris.lutece.portal.service.util.AppException;
73  import fr.paris.lutece.portal.service.util.AppLogService;
74  import fr.paris.lutece.portal.service.util.AppPropertiesService;
75  import fr.paris.lutece.util.ReferenceItem;
76  import fr.paris.lutece.util.ReferenceList;
77  import fr.paris.lutece.util.httpaccess.HttpAccess;
78  import fr.paris.lutece.util.httpaccess.HttpAccessException;
79  import java.io.IOException;
80  import java.nio.file.Files;
81  import java.nio.file.Path;
82  import java.nio.file.Paths;
83  import org.apache.commons.io.FileUtils;
84  
85  public class DeploymentUtils
86  {
87      /**
88       * Builds a query with filters placed in parameters. Consider using {@link #buildQueryWithFilter(StringBuilder, List, String)} instead.
89       *
90       * @param strSelect
91       *            the select of the query
92       * @param listStrFilter
93       *            the list of filter to add in the query
94       * @param strOrder
95       *            the order by of the query
96       * @return a query
97       */
98      public static String buildRequetteWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
99      {
100         return buildQueryWithFilter( new StringBuilder( strSelect ), listStrFilter, strOrder );
101     }
102 
103     /**
104      * Builds a query with filters placed in parameters
105      *
106      * @param sbSQL
107      *            the beginning of the query
108      * @param listFilter
109      *            the list of filter to add in the query
110      * @param strOrder
111      *            the order by of the query
112      * @return a query
113      */
114     public static String buildQueryWithFilter( StringBuilder sbSQL, List<String> listFilter, String strOrder )
115     {
116         int nCount = 0;
117 
118         for ( String strFilter : listFilter )
119         {
120             if ( ++nCount == 1 )
121             {
122                 sbSQL.append( ConstanteUtils.CONSTANTE_SQL_WHERE );
123             }
124 
125             sbSQL.append( strFilter );
126 
127             if ( nCount != listFilter.size( ) )
128             {
129                 sbSQL.append( ConstanteUtils.CONSTANTE_SQL_AND );
130             }
131         }
132 
133         if ( strOrder != null )
134         {
135             sbSQL.append( strOrder );
136         }
137 
138         return sbSQL.toString( );
139     }
140 
141     /**
142      * This method calls Rest WS archive
143      *
144      * @param strUrl
145      *            the url
146      * @param params
147      *            the params to pass in the post
148      * @param listElements
149      *            the list of elements to include in the signature
150      * @return the response as a string
151      * @throws HttpAccessException
152      *             the exception if there is a problem
153      */
154     public static String callPlateformEnvironmentWs( String strUrl ) throws HttpAccessException
155     {
156         String strResponse = StringUtils.EMPTY;
157 
158         try
159         {
160             HttpAccess httpAccess = new HttpAccess( );
161             strResponse = httpAccess.doGet( strUrl );
162         }
163         catch( HttpAccessException e )
164         {
165             String strError = "ArchiveWebServices - Error connecting to '" + strUrl + "' : ";
166             AppLogService.error( strError + e.getMessage( ), e );
167             throw new HttpAccessException( strError, e );
168         }
169 
170         return strResponse;
171     }
172 
173     public static List<String> getJSONDictionary( String dictionaryName, String strJSONFlux )
174     {
175         List<String> jsonCollection = new ArrayList<String>( );
176         JSONObject jo = (JSONObject) JSONSerializer.toJSON( strJSONFlux );
177         JSONArray jsonArray = jo.getJSONArray( dictionaryName );
178         Iterator iterator = jsonArray.iterator( );
179 
180         while ( iterator.hasNext( ) )
181         {
182             jsonCollection.add( (String) iterator.next( ) );
183         }
184 
185         return jsonCollection;
186     }
187 
188     public static List<String> getJSONDictionary( String objectName, String dictionaryName, String strJSONFlux )
189     {
190         List<String> jsonCollection = new ArrayList<String>( );
191         JSONObject jo = (JSONObject) JSONSerializer.toJSON( strJSONFlux );
192 
193         JSONArray jsonArray = jo.getJSONObject( objectName ).getJSONArray( dictionaryName );
194         Iterator iterator = jsonArray.iterator( );
195 
196         while ( iterator.hasNext( ) )
197         {
198             jsonCollection.add( (String) iterator.next( ) );
199         }
200 
201         return jsonCollection;
202     }
203 
204     public static JSONObject getJSONOBject( String strJSONFlux )
205     {
206         JSONObject jo = (JSONObject) JSONSerializer.toJSON( strJSONFlux );
207 
208         return jo;
209     }
210 
211     /**
212      * Retourne l'emplacement du pom
213      *
214      * @param strBasePath
215      * @param strPluginName
216      * @return
217      */
218     public static String getPathPomFile( String strPathSite )
219     {
220         return strPathSite + File.separator + ConstanteUtils.CONSTANTE_POM_XML;
221     }
222 
223     /**
224      * Retourne l'emplacement du pom
225      *
226      * @param strBasePath
227      * @param strPluginName
228      * @return
229      */
230     public static String getPathUpgradeFiles( String strPathSite )
231     {
232 
233         String strPath = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_UPGRADE_DIRECTORY_PATH );
234         StringBuffer strBuffer = new StringBuffer( strPathSite );
235 
236         for ( String strP : strPath.split( ConstanteUtils.CONSTANTE_SEPARATOR_SLASH ) )
237         {
238             strBuffer.append( File.separator );
239             strBuffer.append( strP );
240 
241         }
242 
243         return strBuffer.toString( );
244     }
245 
246     /**
247      * Retourne l'emplacement du pom
248      *
249      * @param strBasePath
250      * @param strPluginName
251      * @return
252      */
253     public static String getPathUpgradeFile( String strPathSite, String strFileName )
254     {
255 
256         StringBuffer strBuffer = new StringBuffer( getPathUpgradeFiles( strPathSite ) );
257         strBuffer.append( File.separator );
258         strBuffer.append( strFileName );
259         return strBuffer.toString( );
260     }
261 
262     /**
263      * Retourne l'emplacement du pom
264      *
265      * @param strBasePath
266      * @param strPluginName
267      * @return
268      */
269     public static String getPathArchiveGenerated( String strPathSite, String strWarName, String strExtension )
270     {
271 
272         List<String> listFileInTarget = FileUtil.list( strPathSite + File.separator + ConstanteUtils.CONSTANTE_TARGET, strExtension, false );
273         if ( !CollectionUtils.isEmpty( listFileInTarget ) && listFileInTarget.size( ) == 1 )
274         {
275             return strPathSite + File.separator + ConstanteUtils.CONSTANTE_TARGET + File.separator + listFileInTarget.get( 0 );
276         }
277 
278         return strPathSite + File.separator + ConstanteUtils.CONSTANTE_TARGET + File.separator + strWarName + strExtension;
279     }
280 
281     /**
282      * Retourne l'emplacement du pom
283      *
284      * @param strBasePath
285      * @param strPluginName
286      * @return
287      */
288     public static String getPathCheckoutSite( String strSiteName )
289     {
290         String strCheckoutBasePath = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_CHECKOUT_BASE_PAH );
291 
292         return strCheckoutBasePath + File.separator + strSiteName;
293     }
294 
295     public static String getPlateformUrlApplication( String strCodeApplication )
296     {
297         // String strPlateformEnvironmentBaseUrl=
298         // AppPropertiesService.getProperty
299         // (PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL);
300         return strCodeApplication;
301     }
302 
303     public static String getPlateformUrlEnvironments( String strCodeApplication, String strArea )
304     {
305         return getPlateformUrlApplication( strCodeApplication ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strArea;
306     }
307 
308     public static String getPlateformUrlServerApplicationInstances( String strCodeApplication, String strCodeEnvironment, String strServerApplicationType )
309     {
310         String strPathEnvironment = ( strCodeEnvironment.replace( ConstanteUtils.CONSTANTE_SEPARATOR_POINT, ConstanteUtils.CONSTANTE_SEPARATOR_SLASH ) )
311                 .toUpperCase( );
312 
313         return getPlateformUrlApplication( strCodeApplication ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strPathEnvironment
314                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + getPathServerByType( strServerApplicationType, strCodeEnvironment );
315     }
316 
317     public static String getPlateformUrlServerApplicationActions( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
318     {
319         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
320                 serverApplicationInstance.getType( ) )
321                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + serverApplicationInstance.getCode( ).toUpperCase( );
322     }
323 
324     public static String getPlateformUrlDatabases( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
325     {
326         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
327                 serverApplicationInstance.getType( ) )
328                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
329                 + serverApplicationInstance.getCode( ).toUpperCase( )
330                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
331                 + ConstanteUtils.CONSTANTE_ACTION_EXECUTE;
332     }
333 
334     public static String getPlateformUrlServerApplicationAction( String strCodeApplication, ServerApplicationInstance serverApplicationInstance,
335             String strCodeAction )
336     {
337         return getPlateformUrlServerApplicationActions( strCodeApplication, serverApplicationInstance ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
338                 + strCodeAction;
339     }
340 
341     public static String getDeployDirectoryTarget( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
342     {
343         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
344                 serverApplicationInstance.getType( ) )
345                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
346                 + serverApplicationInstance.getCode( )
347                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
348                 + serverApplicationInstance.getFtpDirectoryTarget( );
349     }
350 
351     public static String getContextDirectoryTarget( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
352     {
353         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
354                 serverApplicationInstance.getType( ) )
355                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
356                 + serverApplicationInstance.getCode( )
357                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
358                 + ConstanteUtils.CONTEXT_DIRECTORY_NAME;
359     }
360 
361     public static String getDumpFileDirectory( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
362     {
363         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
364                 serverApplicationInstance.getType( ) )
365                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
366                 + serverApplicationInstance.getCode( )
367                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
368                 + serverApplicationInstance.getFtpDirectoryDump( ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH;
369     }
370 
371     /**
372      * convert a string to int
373      *
374      * @param strParameter
375      *            the string parameter to convert
376      * @return the conversion
377      */
378     public static int getIntegerParameter( String strParameter )
379     {
380         int nIdParameter = -1;
381 
382         try
383         {
384             if ( ( strParameter != null ) && strParameter.matches( ConstanteUtils.REGEX_ID ) )
385             {
386                 nIdParameter = Integer.parseInt( strParameter );
387             }
388         }
389         catch( NumberFormatException ne )
390         {
391             AppLogService.error( ne );
392         }
393 
394         return nIdParameter;
395     }
396 
397     public static SvnUser getSvnUser( int nIdAdminUser, Locale locale )
398     {
399 
400         SvnUser mavenUser = null;
401         boolean bUsedApplicationAccount = AppPropertiesService.getPropertyBoolean( ConstanteUtils.PROPERTY_SVN_USED_DEPLOYMENT_ACCOUNT, false );
402         if ( !bUsedApplicationAccount )
403 
404         {
405 
406             String strIdAttributeLogin = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ADMINUSER_ID_ATTRIBUTE_SVN_LOGIN );
407             String strIdAttributePasssword = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_ADMINUSER_ID_ATTRIBUTE_SVN_PASSWORD );
408             String strLoginValue = null;
409             String strPasswordValue = null;
410 
411             Map<String, Object> mapAttributeUser = AdminUserFieldService.getAdminUserFields( nIdAdminUser, locale );
412 
413             if ( mapAttributeUser.containsKey( strIdAttributeLogin ) && mapAttributeUser.containsKey( strIdAttributePasssword ) )
414             {
415                 strLoginValue = ( (ArrayList<AdminUserField>) mapAttributeUser.get( strIdAttributeLogin ) ).get( 0 ).getValue( );
416                 strPasswordValue = ( (ArrayList<AdminUserField>) mapAttributeUser.get( strIdAttributePasssword ) ).get( 0 ).getValue( );
417 
418                 if ( !StringUtils.isEmpty( strLoginValue ) && !( StringUtils.isEmpty( strPasswordValue ) ) )
419                 {
420                     mavenUser = new SvnUser( );
421                     mavenUser.setLogin( strLoginValue );
422                     mavenUser.setPassword( strPasswordValue );
423                 }
424             }
425         }
426         else
427         {
428             String strApplicationLogin = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SVN_LOGIN_APPLICATION_DEPLOYMENT );
429             String strApplicationPasssword = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SVN_PASSWORD_APPLICATION_DEPLOYMENT );
430 
431             mavenUser = new SvnUser( );
432             mavenUser.setLogin( strApplicationLogin );
433             mavenUser.setPassword( strApplicationPasssword );
434         }
435 
436         return mavenUser;
437     }
438 
439     public static JSONObject getJSONForCommandResult( CommandResult result )
440     {
441         JSONObject jo = new JSONObject( );
442         JSONObject joResult = new JSONObject( );
443 
444         try
445         {
446             jo.put( ConstanteUtils.JSON_STATUS, result.getStatus( ) );
447 
448             // pour les logs tr�s longs, on ne prend que la fin
449             StringBuffer sbLog = result.getLog( );
450             int nMaxLogSize = AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_MAX_LOG_SIZE, ConstanteUtils.CONSTANTE_ID_NULL );
451             String strLog;
452 
453             if ( nMaxLogSize == -ConstanteUtils.CONSTANTE_ID_NULL )
454             {
455                 nMaxLogSize = ConstanteUtils.CONSTANTE_DEFAULT_LOG_SIZE;
456             }
457 
458             // sbLog null entre le lancement du thread et la premiere requete
459             if ( sbLog != null )
460             {
461                 if ( sbLog.length( ) > nMaxLogSize )
462                 {
463                     strLog = sbLog.substring( sbLog.length( ) - nMaxLogSize );
464                 }
465                 else
466                 {
467                     strLog = sbLog.toString( );
468                 }
469             }
470             else
471             {
472                 strLog = ConstanteUtils.CONSTANTE_EMPTY_STRING;
473             }
474 
475             jo.put( ConstanteUtils.JSON_LOG, strLog );
476             jo.put( ConstanteUtils.JSON_RUNNING, result.isRunning( ) );
477             jo.put( ConstanteUtils.JSON_ERROR, result.getError( ) );
478             jo.put( ConstanteUtils.JSON_ERROR_TYPE, result.getErrorType( ) );
479 
480             for ( Entry<String, String> resultInformations : result.getResultInformations( ).entrySet( ) )
481             {
482 
483                 joResult.put( resultInformations.getKey( ), resultInformations.getValue( ) );
484             }
485             jo.put( ConstanteUtils.JSON_RESULT, joResult );
486 
487         }
488         catch( JSONException e )
489         {
490             AppLogService.error( "JSON error : " + e.getMessage( ), e );
491         }
492 
493         return jo;
494     }
495 
496     public static JSONObject getJSONForWorkflowAction( String strJspForceRedirect, String strFormError, CommandResult result, State state,
497             Collection<Action> listAction )
498     {
499         JSONObject jo = new JSONObject( );
500 
501         try
502         {
503             jo.put( ConstanteUtils.JSON_JSP_FOM_DISPLAY, ( strJspForceRedirect != null ) ? strJspForceRedirect : ConstanteUtils.CONSTANTE_EMPTY_STRING );
504 
505             jo.put( ConstanteUtils.JSON_STATE, ( state != null ) ? state.getName( ) : ConstanteUtils.CONSTANTE_EMPTY_STRING );
506             jo.put( ConstanteUtils.JSON_FORM_ERROR, ( strFormError != null ) ? strFormError : ConstanteUtils.CONSTANTE_EMPTY_STRING );
507 
508             JSONObject joAction;
509             JSONArray joListAction = new JSONArray( );
510 
511             if ( listAction != null )
512             {
513                 for ( Action action : listAction )
514                 {
515                     joAction = new JSONObject( );
516                     joAction.put( ConstanteUtils.JSON_ACTION_ID, action.getId( ) );
517                     joAction.put( ConstanteUtils.JSON_ACTION_NAME, action.getName( ) );
518                     joAction.put( ConstanteUtils.JSON_ACTION_DESCRIPTION, action.getDescription( ) );
519 
520                     joListAction.add( joAction );
521                 }
522             }
523 
524             jo.put( ConstanteUtils.JSON_ACTION_LIST, joListAction );
525 
526             jo.put( ConstanteUtils.JSON_RUNNING, ( result != null ) ? result.isRunning( ) : ConstanteUtils.CONSTANTE_EMPTY_STRING );
527             jo.put( ConstanteUtils.JSON_ERROR, ( result != null ) ? result.getError( ) : ConstanteUtils.CONSTANTE_EMPTY_STRING );
528         }
529         catch( JSONException e )
530         {
531             AppLogService.error( "JSON error : " + e.getMessage( ), e );
532         }
533 
534         return jo;
535     }
536 
537     public static JSONObject getJSONForServerAction( int nIdApplication, String strCodeEnvironment, String strServerCodeInstance,
538             String strServerApplicationType, String strJspForceRedirect, CommandResult result, List<IAction> listServersActions, Integer newServerStatus )
539     {
540         JSONObject jo = getJSONForCommandResult( result );
541 
542         try
543         {
544             jo.put( ConstanteUtils.JSON_JSP_FOM_DISPLAY, ( strJspForceRedirect != null ) ? strJspForceRedirect : ConstanteUtils.CONSTANTE_EMPTY_STRING );
545 
546         }
547         catch( JSONException e )
548         {
549             AppLogService.error( "JSON error : " + e.getMessage( ), e );
550         }
551         JSONObject joAction;
552         JSONArray joListAction = new JSONArray( );
553 
554         if ( listServersActions != null )
555         {
556             for ( IAction action : listServersActions )
557             {
558                 if ( action.isDisplay( ) && ( action.getStatus( ) == null || action.getStatus( ).equals( newServerStatus ) ) )
559                 {
560 
561                     joAction = new JSONObject( );
562                     joAction.put( ConstanteUtils.JSON_ACTION_CODE, action.getCode( ) );
563                     joAction.put( ConstanteUtils.JSON_ACTION_NAME, action.getName( ) );
564                     joAction.put( ConstanteUtils.JSON_ACTION_ICON_CSS_CLASS, action.getIconCssClass( ) );
565                     joListAction.add( joAction );
566                 }
567             }
568         }
569         jo.put( ConstanteUtils.JSON_ACTION_LIST, joListAction );
570         jo.put( ConstanteUtils.JSON_SERVER_STATUS, ( newServerStatus != null ) ? newServerStatus : ConstanteUtils.CONSTANTE_EMPTY_STRING );
571         jo.put( ConstanteUtils.JSON_ID_APPLIACTION, nIdApplication );
572         jo.put( ConstanteUtils.JSON_CODE_ENVIRONMENT, strCodeEnvironment );
573         jo.put( ConstanteUtils.JSON_CODE_SERVER_APPLICATION_INSTANCE, strServerCodeInstance );
574         jo.put( ConstanteUtils.JSON_SERVER_APPLICATION_TYPE, strServerApplicationType );
575         return jo;
576     }
577 
578     public static int getIdWorkflowSiteDeploy( WorkflowDeploySiteContext workflowDeploySiteContext )
579     {
580         int nIdWorkflow = ConstanteUtils.CONSTANTE_ID_NULL;
581 
582         if ( workflowDeploySiteContext.isDeploySql( ) )
583         {
584             nIdWorkflow = AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_ID_WORKFLOW_DEPLOY_SCRIPT, ConstanteUtils.CONSTANTE_ID_NULL );
585         }
586         else
587             if ( workflowDeploySiteContext.isInitAppContext( ) )
588             {
589                 nIdWorkflow = AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_ID_WORKFLOW_INIT_APP_CONTEXT, ConstanteUtils.CONSTANTE_ID_NULL );
590             }
591             else
592                 if ( workflowDeploySiteContext.isInitBdd( ) )
593                 {
594                     nIdWorkflow = AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_ID_WORKFLOW_INIT_DATABASE, ConstanteUtils.CONSTANTE_ID_NULL );
595                 }
596                 else
597                 {
598                     nIdWorkflow = AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_ID_WORKFLOW_DEPLOY_SITE, ConstanteUtils.CONSTANTE_ID_NULL );
599                 }
600 
601         return nIdWorkflow;
602     }
603 
604     public static void startCommandResult( WorkflowDeploySiteContext context )
605     {
606         CommandResult commandResult = new CommandResult( );
607         startCommandResult( commandResult );
608         context.setCommandResult( commandResult );
609 
610     }
611 
612     public static void startCommandResult( CommandResult commandResult )
613     {
614 
615         commandResult.setLog( new StringBuffer( ) );
616         commandResult.setRunning( true );
617         commandResult.setStatus( CommandResult.STATUS_OK );
618     }
619 
620     public static void stopCommandResult( WorkflowDeploySiteContext context )
621     {
622         stopCommandResult( context.getCommandResult( ) );
623     }
624 
625     public static void stopCommandResult( CommandResult commandResult )
626     {
627         commandResult.setRunning( false );
628     }
629 
630     public static void addTechnicalError( CommandResult commandResult, String strError, Exception e )
631     {
632 
633         if ( e != null )
634         {
635             AppLogService.error( strError, e );
636         }
637         else
638         {
639             AppLogService.error( strError );
640         }
641 
642         if ( commandResult != null )
643         {
644             commandResult.setError( strError );
645             commandResult.setStatus( CommandResult.STATUS_ERROR );
646             commandResult.setRunning( false );
647             commandResult.setErrorType( CommandResult.ERROR_TYPE_STOP );
648         }
649     }
650 
651     public static void addTechnicalError( CommandResult commandResult, String strError )
652     {
653         addTechnicalError( commandResult, strError, null );
654     }
655 
656     public static ReferenceList addEmptyRefenceItem( ReferenceList referenceList )
657     {
658         ReferenceList referenceList2 = new ReferenceList( );
659 
660         ReferenceItem referenceItem = new ReferenceItem( );
661 
662         referenceItem.setCode( ConstanteUtils.CONSTANTE_EMPTY_STRING );
663         referenceItem.setName( ConstanteUtils.CONSTANTE_EMPTY_STRING );
664 
665         referenceList2.add( 0, referenceItem );
666         referenceList2.addAll( referenceList );
667 
668         return referenceList2;
669     }
670 
671     public static ActionParameter [ ] getActionParameters( HttpServletRequest request, List<String> listParameterNames )
672     {
673         if ( listParameterNames != null )
674         {
675             List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
676             ActionParameter actionParameter;
677 
678             for ( String param : listParameterNames )
679             {
680                 actionParameter = new ActionParameter( );
681                 actionParameter.setName( param );
682                 actionParameter.setValue( request.getParameter( param ) );
683                 listActionParameters.add( actionParameter );
684             }
685 
686             return listActionParameters.toArray( new ActionParameter [ listParameterNames.size( )] );
687         }
688 
689         return null;
690     }
691 
692     public static ActionParameter [ ] getActionParameters( WorkflowDeploySiteContext workkflowContext )
693     {
694 
695         List<ActionParameter> listActionParameters = new ArrayList<ActionParameter>( );
696         ActionParameter actionParameter;
697         if ( workkflowContext.isDeployWar( ) || workkflowContext.isInitAppContext( ) )
698         {
699             actionParameter = new ActionParameter( );
700             actionParameter.setName( ConstanteUtils.PARAM_TAG_TO_DEPLOY );
701             actionParameter.setValue( workkflowContext.getTagToDeploy( ) );
702 
703             listActionParameters.add( actionParameter );
704 
705             ActionParameter initAppContext = new ActionParameter( );
706             initAppContext.setName( ConstanteUtils.PARAM_INIT_APP_CONTEXT );
707             initAppContext.setValue( Boolean.toString( workkflowContext.isInitAppContext( ) ) );
708 
709             listActionParameters.add( initAppContext );
710 
711         }
712 
713         if ( workkflowContext.isDeploySql( ) || workkflowContext.isInitBdd( ) )
714         {
715             if ( !workkflowContext.isInitBdd( ) )
716             {
717                 actionParameter = new ActionParameter( );
718                 actionParameter.setName( ConstanteUtils.PARAM_CODE_DATABASE );
719                 actionParameter.setValue( workkflowContext.getDatabaseName( ) );
720                 listActionParameters.add( actionParameter );
721             }
722 
723             if ( workkflowContext.getScriptFileItemName( ) != null )
724             {
725                 actionParameter = new ActionParameter( );
726                 actionParameter.setName( ConstanteUtils.PARAM_SCRIPT_NAME );
727                 actionParameter.setValue( workkflowContext.getScriptFileItemName( ) );
728                 listActionParameters.add( actionParameter );
729 
730             }
731 
732             if ( workkflowContext.isInitBdd( ) )
733             {
734                 actionParameter = new ActionParameter( );
735                 actionParameter.setName( ConstanteUtils.PARAM_INIT_DATABASE );
736                 actionParameter.setValue( Boolean.toString( workkflowContext.isInitBdd( ) ) );
737                 listActionParameters.add( actionParameter );
738             }
739 
740         }
741 
742         return listActionParameters.toArray( new ActionParameter [ listActionParameters.size( )] );
743 
744     }
745 
746     public static ReferenceList getReferenceListServerType( Locale locale )
747     {
748         ReferenceList referenceList = new ReferenceList( );
749         referenceList.addItem( ConstanteUtils.CONSTANTE_EMPTY_STRING, ConstanteUtils.CONSTANTE_EMPTY_STRING );
750         referenceList.addItem( ConstanteUtils.CONSTANTE_SERVER_TOMCAT,
751                 I18nService.getLocalizedString( ConstanteUtils.PROPERTY_SERVER_TYPE_TOMCAT_LABEL, locale ) );
752         referenceList
753                 .addItem( ConstanteUtils.CONSTANTE_SERVER_MYSQL, I18nService.getLocalizedString( ConstanteUtils.PROPERTY_SERVER_TYPE_MYSQL_LABEL, locale ) );
754         referenceList
755                 .addItem( ConstanteUtils.CONSTANTE_SERVER_HTTPD, I18nService.getLocalizedString( ConstanteUtils.PROPERTY_SERVER_TYPE_HTTPD_LABEL, locale ) );
756 
757         return referenceList;
758     }
759 
760     public static ReferenceList getReferenceListAction( List<IAction> listAction )
761     {
762         ReferenceList referenceList = new ReferenceList( );
763         referenceList.addItem( ConstanteUtils.CONSTANTE_EMPTY_STRING, ConstanteUtils.CONSTANTE_EMPTY_STRING );
764 
765         for ( IAction action : listAction )
766         {
767             referenceList.addItem( getActionKey( action.getCode( ), action.getServerType( ) ), action.getName( ) );
768         }
769 
770         return referenceList;
771     }
772 
773     public static String getActionKey( String strCode, String strServerType )
774     {
775         return strCode + "_" + strServerType;
776     }
777 
778     private static String getPathServerByType( String strApplicationType, String strEvironementCode )
779     {
780         String strPathServer = strApplicationType;
781 
782         if ( strEvironementCode.contains( "v1" ) )
783         {
784             if ( ConstanteUtils.CONSTANTE_SERVER_TOMCAT.equals( strApplicationType ) )
785             {
786                 strPathServer = ConstanteUtils.CONSTANTE_SERVER_TOM;
787             }
788             else
789                 if ( ConstanteUtils.CONSTANTE_SERVER_MYSQL.equals( strApplicationType ) )
790                 {
791                     strPathServer = ConstanteUtils.CONSTANTE_SERVER_MYS;
792                 }
793         }
794 
795         return strPathServer;
796     }
797 
798     public static ReferenceList getSimpleReferenceList( List<String> list )
799     {
800         ReferenceList reflist = new ReferenceList( );
801         for ( String strCode : list )
802         {
803             reflist.addItem( strCode, strCode );
804 
805         }
806         return reflist;
807     }
808 
809     /**
810      * Get the VCS service from the repo type
811      * 
812      * @param strKey
813      *            the repo type key
814      * @return the VCS service
815      */
816     public static IVCSService getVCSService( String strKey )
817     {
818         switch( strKey )
819         {
820             case ConstanteUtils.CONSTANTE_REPO_TYPE_GITHUB:
821                 return SpringContextService.getBean( ConstanteUtils.BEAN_GITHUB_SERVICE );
822             case ConstanteUtils.CONSTANTE_REPO_TYPE_SVN:
823                 return SpringContextService.getBean( ConstanteUtils.BEAN_SVN_SERVICE );
824             case ConstanteUtils.CONSTANTE_REPO_TYPE_GITLAB:
825                 return SpringContextService.getBean( ConstanteUtils.BEAN_GITLAB_SERVICE );
826         }
827         return null;
828     }
829 
830     /**
831      * Get an empty VCS user based on the repo type of the provided application
832      * 
833      * @param application
834      *            the Application
835      * @return an empty VCS user based on the repo type of the provided application
836      */
837     public static AbstractVCSUser getVCSUser( Application application )
838     {
839         switch( application.getRepoType( ) )
840         {
841             case ConstanteUtils.CONSTANTE_REPO_TYPE_GITHUB:
842                 return new GitUser( );
843             case ConstanteUtils.CONSTANTE_REPO_TYPE_SVN:
844                 return new SvnUser( );
845             case ConstanteUtils.CONSTANTE_REPO_TYPE_GITLAB:
846                 return new GitUser( );
847         }
848         return null;
849     }
850 
851     /**
852      * Get the VCS user from the request and the given application
853      * 
854      * @param request
855      *            the HttpServletRequest
856      * @param application
857      *            the Application
858      * @return the VCS user
859      */
860     public static AbstractVCSUser getVCSUser( HttpServletRequest request, Application application )
861     {
862         String strLogin = request.getParameter( ConstanteUtils.PARAM_LOGIN );
863         String strPassword = request.getParameter( ConstanteUtils.PARAM_PASSWORD );
864         AbstractVCSUser user = getVCSUser( application );
865         if ( strLogin != null && strPassword != null )
866         {
867 
868             user.setLogin( strLogin );
869             user.setPassword( strPassword );
870         }
871         return user;
872     }
873     
874     /**
875      * Delete the local repository of the application sources
876      * @param context
877      * @param application
878      * @return 
879      */
880     public static String deleteLocalRepository( WorkflowDeploySiteContext context, Application application )
881     {
882         String strLocalPath = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_CHECKOUT_BASE_PAH ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + application.getArtifactId();
883         Path path = Paths.get( strLocalPath );
884         try
885         {
886             if ( Files.exists(path ) )
887             {
888                 FileUtils.forceDelete( path.toFile( ) );
889             }
890         }
891         catch( IOException e )
892         {
893             context.getCommandResult( ).getLog( ).append( "Unable to delete the local directory " + strLocalPath  + " \n" );
894             throw new AppException( "Unable to delete the local directory " + strLocalPath , e);
895         }
896         return null;
897     }
898 }