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