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.appstore.service;
35  
36  import fr.paris.lutece.plugins.appstore.business.Component;
37  import fr.paris.lutece.portal.service.datastore.DatastoreService;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.portal.service.util.AppPropertiesService;
40  import fr.paris.lutece.util.httpaccess.HttpAccess;
41  import fr.paris.lutece.util.httpaccess.HttpAccessException;
42  
43  import org.jsoup.Jsoup;
44  
45  import org.jsoup.nodes.Document;
46  import org.jsoup.nodes.Element;
47  
48  import org.jsoup.select.Elements;
49  
50  /**
51   * Component Info Service
52   */
53  public class ComponentInfoService
54  {
55      private static final String PROPERTY_MAVEN_REPO_URL = "appstore.maven.repository.url";
56      private static final String URL_MAVEN_REPO = AppPropertiesService.getProperty( PROPERTY_MAVEN_REPO_URL );
57      private static final String PROPERTY_MAVEN_PATH_PLUGINS = "appstore.maven.repository.path.plugins";
58      private static final String URL_PLUGINS = URL_MAVEN_REPO + AppPropertiesService.getProperty( PROPERTY_MAVEN_PATH_PLUGINS );
59      private static final String PROPERTY_MAVEN_PATH_SITE_POM = "appstore.maven.repository.path.site-pom";
60      private static final String URL_SITE_POM = URL_MAVEN_REPO + AppPropertiesService.getProperty( PROPERTY_MAVEN_PATH_SITE_POM );
61      private static final String KEY_SITE_POM_VERSION = "appstore.pom.site.version";
62      private static final String RELEASE_NOT_FOUND = "Release not found";
63  
64      /**
65       * Set the component's version
66       * 
67       * @param component
68       *            The component
69       */
70      public static void setReleaseVersion( Component component )
71      {
72          component.setVersion( getVersion( URL_PLUGINS + component.getArtifactId( ) ) );
73      }
74  
75      /**
76       * Set the POM site version
77       */
78      public static void setPomSiteVersion( )
79      {
80          String strVersion = getVersion( URL_SITE_POM );
81          DatastoreService.setDataValue( KEY_SITE_POM_VERSION, strVersion );
82      }
83  
84      /**
85       * Retrieve a version from the maven repository
86       * 
87       * @param strUrl
88       *            The maven repository URL
89       * @return The version
90       */
91      private static String getVersion( String strUrl )
92      {
93          String strVersion = RELEASE_NOT_FOUND;
94  
95          try
96          {
97              HttpAccess httpAccess = new HttpAccess( );
98              String strHtml = httpAccess.doGet( strUrl );
99              Document jarList = Jsoup.parse( strHtml );
100             Elements trList = jarList.select( "td a" );
101 
102             for ( int i = 0; i < trList.size( ); i++ )
103             {
104                 Element tr = trList.get( i );
105                 String strAnchor = tr.text( );
106                 if ( strAnchor.matches( "^[\\d].*" ) )
107                 {
108                     strVersion = strAnchor.replaceAll( "\\/", "" );
109                 }
110             }
111         }
112         catch( HttpAccessException e )
113         {
114             AppLogService.error( "AppStore - ComponentInfoService : Error retrieving release version : " + e.getMessage( ), e );
115         }
116 
117         return strVersion;
118     }
119 }