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.service.cloudmgr;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.ServerApplicationInstance;
39  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.util.ConstanteUtils;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  import fr.paris.lutece.util.httpaccess.HttpAccess;
42  import fr.paris.lutece.util.httpaccess.HttpAccessException;
43  
44  public class CloudmgrUtils
45  {
46  
47      /**
48       * This method calls Rest WS archive
49       *
50       * @param strUrl
51       *            the url
52       * @param params
53       *            the params to pass in the post
54       * @param listElements
55       *            the list of elements to include in the signature
56       * @return the response as a string
57       * @throws HttpAccessException
58       *             the exception if there is a problem
59       */
60      public static String callPlateformEnvironmentWs( String strUrl ) throws HttpAccessException
61      {
62          String strResponse = StringUtils.EMPTY;
63  
64          try
65          {
66              HttpAccess httpAccess = new HttpAccess( );
67              strResponse = httpAccess.doGet( strUrl );
68          }
69          catch( HttpAccessException e )
70          {
71              String strError = "ArchiveWebServices - Error connecting to '" + strUrl + "' : ";
72              AppLogService.error( strError + e.getMessage( ), e );
73              throw new HttpAccessException( strError, e );
74          }
75  
76          return strResponse;
77      }
78  
79      public static String getPlateformUrlApplication( String strCodeApplication )
80      {
81          // String strPlateformEnvironmentBaseUrl=
82          // AppPropertiesService.getProperty
83          // (PROPERTY_ENVIRONMENT_PLATEFORM_BASE_URL);
84          return strCodeApplication;
85      }
86  
87      public static String getPlateformUrlEnvironments( String strCodeApplication, String strArea )
88      {
89          return getPlateformUrlApplication( strCodeApplication ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strArea;
90      }
91  
92      public static String getPlateformUrlServerApplicationInstances( String strCodeApplication, String strCodeEnvironment, String strServerApplicationType )
93      {
94          String strPathEnvironment = ( strCodeEnvironment.replace( ConstanteUtils.CONSTANTE_SEPARATOR_POINT, ConstanteUtils.CONSTANTE_SEPARATOR_SLASH ) )
95                  .toUpperCase( );
96  
97          return getPlateformUrlApplication( strCodeApplication ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + strPathEnvironment
98                  + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + getPathServerByType( strServerApplicationType, strCodeEnvironment );
99      }
100 
101     public static String getPlateformUrlServerApplicationActions( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
102     {
103         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
104                 serverApplicationInstance.getType( ) ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + serverApplicationInstance.getCode( ).toUpperCase( );
105     }
106 
107     public static String getPlateformUrlDatabases( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
108     {
109         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
110                 serverApplicationInstance.getType( ) ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + serverApplicationInstance.getCode( ).toUpperCase( )
111                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.CONSTANTE_ACTION_EXECUTE;
112     }
113 
114     public static String getPlateformUrlServerApplicationAction( String strCodeApplication, ServerApplicationInstance serverApplicationInstance,
115             String strCodeAction )
116     {
117         return getPlateformUrlServerApplicationActions( strCodeApplication, serverApplicationInstance ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH
118                 + strCodeAction;
119     }
120 
121     public static String getDirectoryInstall( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
122     {
123         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
124                 serverApplicationInstance.getType( ) ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + serverApplicationInstance.getCode( )
125                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.DIRECTORY_INSTALL_NAME;
126     }
127 
128     public static String getDirectoryScript( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
129     {
130         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
131                 serverApplicationInstance.getType( ) ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + serverApplicationInstance.getCode( )
132                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.DIRECTORY_SCRIPT_NAME;
133     }
134 
135     public static String getContextDirectoryTarget( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
136     {
137         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
138                 serverApplicationInstance.getType( ) ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + serverApplicationInstance.getCode( )
139                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.CONTEXT_DIRECTORY_NAME;
140     }
141 
142     public static String getDirectoryDump( String strCodeApplication, ServerApplicationInstance serverApplicationInstance )
143     {
144         return getPlateformUrlServerApplicationInstances( strCodeApplication, serverApplicationInstance.getCodeEnvironment( ),
145                 serverApplicationInstance.getType( ) ) + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + serverApplicationInstance.getCode( )
146                 + ConstanteUtils.CONSTANTE_SEPARATOR_SLASH + ConstanteUtils.DIRECTORY_DUMP_NAME;
147     }
148 
149     private static String getPathServerByType( String strApplicationType, String strEvironementCode )
150     {
151         String strPathServer = strApplicationType;
152 
153         if ( strEvironementCode.contains( "v1" ) )
154         {
155             if ( ConstanteUtils.CONSTANTE_SERVER_TOMCAT.equals( strApplicationType ) )
156             {
157                 strPathServer = ConstanteUtils.CONSTANTE_SERVER_TOM;
158             }
159             else
160                 if ( ConstanteUtils.CONSTANTE_SERVER_MYSQL.equals( strApplicationType ) )
161                 {
162                     strPathServer = ConstanteUtils.CONSTANTE_SERVER_MYS;
163                 }
164         }
165 
166         return strPathServer;
167     }
168 }