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.util;
35  
36  import java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import org.apache.commons.lang3.StringUtils;
41  import org.codehaus.jackson.annotate.JsonProperty;
42  import org.codehaus.jackson.map.ObjectMapper;
43  
44  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationData;
45  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.FastDeployApplicationDemand;
46  import fr.paris.lutece.plugins.appcenter.modules.fastdeployapplication.business.InvalidRepositoryUrlException;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.service.util.AppPropertiesService;
49  
50  public class RepositoryUtils
51  {
52      /**
53       * Check if the provided Application has a recognized repo url, otherwise, throws an exception
54       * 
55       * @param application
56       *            the Application
57       * @throws InvalidRepositoryUrlException
58       *             if the given application repo url isn't recognized
59       */
60      public static void checkRepository( FastDeployApplicationData application ) throws InvalidRepositoryUrlException
61      {
62          String strUrlRepo = application.getUrlSite( );
63          if ( !strUrlRepo.isEmpty( ) )
64          {
65              URLUtils.checkUrl( strUrlRepo );
66              String strPath = URLUtils.getPath( strUrlRepo );
67  
68              String strSvnUrlSites = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_SVN_SITES_URL );
69              String strGithubUrlSites = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_GITHUB_BASE_URL );
70              String strGitlabUrlSites = AppPropertiesService.getProperty( ConstanteUtils.PROPERTY_GITLAB_BASE_URL );
71  
72              if ( strUrlRepo.startsWith( strSvnUrlSites ) && !strUrlRepo.contains( ConstanteUtils.CONSTANTE_TRUNK ) )
73              {
74                  application.setRepoType( ConstanteUtils.CONSTANTE_REPO_TYPE_SVN );
75              }
76              else
77                  if ( strUrlRepo.startsWith( strGithubUrlSites ) && strUrlRepo.endsWith( ".git" ) )
78                  {
79  
80                      List<String> listAuthorizedGithubRepoKeys = AppPropertiesService.getKeys( ConstanteUtils.PROPERTY_GITHUB_AUTHORIZED_REPO_NAME );
81                      List<String> listAuthorizedGithubRepo = new ArrayList<String>( );
82                      for ( String strKey : listAuthorizedGithubRepoKeys )
83                      {
84                          listAuthorizedGithubRepo.add( AppPropertiesService.getProperty( strKey ) );
85                      }
86  
87                      String strRepoName = strPath.split( ConstanteUtils.CONSTANTE_SEPARATOR_SLASH ) [1];
88                      if ( listAuthorizedGithubRepo.contains( strRepoName ) )
89                      {
90                          application.setRepoType( ConstanteUtils.CONSTANTE_REPO_TYPE_GITHUB );
91                      }
92                      else
93                      {
94                          throw new InvalidRepositoryUrlException( );
95                      }
96                  }
97                  else
98                      if ( strUrlRepo.startsWith( strGitlabUrlSites ) && strUrlRepo.endsWith( ".git" ) )
99                      {
100                         application.setRepoType( ConstanteUtils.CONSTANTE_REPO_TYPE_GITLAB );
101                     }
102                     else
103                     {
104                         throw new InvalidRepositoryUrlException( );
105                     }
106         }
107         else
108         {
109             throw new InvalidRepositoryUrlException( );
110         }
111     }
112 
113     /**
114      * Check if the provided urlRepo is recognized as a correct repository url
115      * 
116      * @param strUrlRepo
117      *            the repo of the url
118      * @return a JSON mapped on a RepoValidation object
119      */
120     public static String checkRepository( String strUrlRepo )
121     {
122         ObjectMapper mapper = new ObjectMapper( );
123         RepoValidation repoValidation = new RepoValidation( );
124 
125         FastDeployApplicationDatas/fastdeployapplication/business/FastDeployApplicationData.html#FastDeployApplicationData">FastDeployApplicationData application = new FastDeployApplicationData( );
126         application.setUrlSite( strUrlRepo );
127         try
128         {
129             checkRepository( application );
130             repoValidation.setIsUrlValid( true );
131             repoValidation.setRepoType( application.getRepoType( ) );
132         }
133         catch( InvalidRepositoryUrlException e )
134         {
135             repoValidation.setIsUrlValid( false );
136         }
137         try
138         {
139             return mapper.writeValueAsString( repoValidation );
140         }
141         catch( IOException e )
142         {
143             AppLogService.error( "Unable to serealize repoValidation as Json obj", e );
144         }
145         return StringUtils.EMPTY;
146 
147     }
148 
149     /**
150      * Check if the provided urlRepo is recognized as a correct repository url
151      * 
152      * @param strUrlRepo
153      *            the repo of the url
154      * @param demand
155      */
156     public static void checkRepository( String strUrlRepo, FastDeployApplicationDemand demand ) throws InvalidRepositoryUrlException
157     {
158         FastDeployApplicationDatas/fastdeployapplication/business/FastDeployApplicationData.html#FastDeployApplicationData">FastDeployApplicationData application = new FastDeployApplicationData( );
159         application.setUrlSite( strUrlRepo );
160         checkRepository( application );
161         demand.setRepoType( application.getRepoType( ) );
162     }
163 
164     /**
165      * This class is only used for the JSON object mapper
166      */
167     private static class RepoValidation
168     {
169         @JsonProperty( "valid_url" )
170         private boolean _isUrlValid;
171         @JsonProperty( "repo_type" )
172         private String _strRepoType;
173 
174         @JsonProperty( "valid_url" )
175         public boolean isIsUrlValid( )
176         {
177             return _isUrlValid;
178         }
179 
180         @JsonProperty( "valid_url" )
181         public void setIsUrlValid( boolean isUrlValid )
182         {
183             _isUrlValid = isUrlValid;
184         }
185 
186         @JsonProperty( "repo_type" )
187         public String getRepoType( )
188         {
189             return _strRepoType;
190         }
191 
192         @JsonProperty( "repo_type" )
193         public void setRepoType( String strRepoType )
194         {
195             _strRepoType = strRepoType;
196         }
197 
198     }
199 }