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  /*
35   * To change this license header, choose License Headers in Project Properties.
36   * To change this template file, choose Tools | Templates
37   * and open the template in the editor.
38   */
39  package fr.paris.lutece.plugins.lutecetools.service;
40  
41  import fr.paris.lutece.plugins.lutecetools.business.Component;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.util.httpaccess.HttpAccess;
44  import fr.paris.lutece.util.httpaccess.HttpAccessException;
45  import java.io.ByteArrayInputStream;
46  import java.io.IOException;
47  import java.io.InputStream;
48  import java.util.ArrayList;
49  import java.util.HashMap;
50  import java.util.List;
51  import javax.xml.parsers.DocumentBuilder;
52  import javax.xml.parsers.DocumentBuilderFactory;
53  import javax.xml.parsers.ParserConfigurationException;
54  import org.w3c.dom.DOMException;
55  import org.w3c.dom.Document;
56  import org.w3c.dom.Element;
57  import org.w3c.dom.NodeList;
58  import org.xml.sax.InputSource;
59  import org.xml.sax.SAXException;
60  
61  /**
62   *
63   * @author leridons
64   */
65  public final class PomService
66  {
67  
68      private static final String TAG_DEPENDENCY = "dependency";
69      private static final String TAG_DEPENDENCY_GROUPID = "groupId";
70      private static final String TAG_DEPENDENCY_ARTIFACT_ID = "artifactId";
71      private static final String TAG_DEPENDENCY_VERSION = "version";
72      private static final String TAG_DEPENDENCY_TYPE = "type";
73  
74      private static final String CONSTANT_LUTECE_GROUP = "fr.paris.lutece";
75  
76      private static final String EXCEPTION_MESSAGE = "LuteceTools - PomService : Error retrieving Pom tags : ";
77  
78      private static PomService _singleton;
79  
80      /**
81       * Private constructor
82       */
83      private PomService( )
84      {
85      }
86  
87      /**
88       * Returns the unique instance
89       * 
90       * @return the unique instance
91       */
92      public static synchronized PomService instance( )
93      {
94          if ( _singleton == null )
95          {
96              _singleton = new PomService( );
97          }
98  
99          return _singleton;
100     }
101 
102     /**
103      * Retreive SITE infos for a given component
104      * 
105      * @param component The component
106      * @param strPomUrl
107      * @param sbLogs    Logs
108      */
109     public void getLuteceDependencies( Component component, String strPomUrl, boolean bSnapshot, StringBuilder sbLogs )
110     {
111         try
112         {
113             HttpAccess httpAccess = new HttpAccess( );
114             String strPom = httpAccess.doGet( strPomUrl );
115 
116             DocumentBuilderFactory documentBuilder = DocumentBuilderFactory.newInstance( );
117             InputStream is = new ByteArrayInputStream( strPom.getBytes( ) ); // use inputstream to ignore BOM when
118                                                                              // parsing
119 
120             DocumentBuilder builder = documentBuilder.newDocumentBuilder( );
121             Document document = builder.parse( new InputSource( is ) );
122             Element root = document.getDocumentElement( );
123 
124             List<HashMap<String, String>> dependencyList = new ArrayList<>( );
125 
126             NodeList nodeDependencyList = root.getElementsByTagName( TAG_DEPENDENCY );
127             for ( int i = 0; i < nodeDependencyList.getLength( ); i++ )
128             {
129                 Element elmt = (Element) nodeDependencyList.item( i );
130                 NodeList groupList = elmt.getElementsByTagName( TAG_DEPENDENCY_GROUPID );
131                 String strGroupId = null;
132                 if ( groupList != null && groupList.getLength( ) > 0 )
133                 {
134                     Element groupElement = (Element) groupList.item( 0 );
135                     strGroupId = groupElement.getTextContent( );
136                 }
137                 if ( strGroupId == null || !strGroupId.startsWith( CONSTANT_LUTECE_GROUP ) )
138                 {
139                     continue;
140                 }
141                 NodeList artifactIdList = elmt.getElementsByTagName( TAG_DEPENDENCY_ARTIFACT_ID );
142                 String strArtifactId = null;
143                 if ( artifactIdList != null && artifactIdList.getLength( ) > 0 )
144                 {
145                     Element artifactElement = (Element) artifactIdList.item( 0 );
146                     strArtifactId = artifactElement.getTextContent( );
147                 }
148 
149                 NodeList versionList = elmt.getElementsByTagName( TAG_DEPENDENCY_VERSION );
150                 String strVersion = null;
151                 if ( versionList != null && versionList.getLength( ) > 0 )
152                 {
153                     Element versionElement = (Element) versionList.item( 0 );
154                     strVersion = versionElement.getTextContent( );
155                 }
156 
157                 NodeList typeList = elmt.getElementsByTagName( TAG_DEPENDENCY_TYPE );
158                 String strType = null;
159                 if ( typeList != null && typeList.getLength( ) > 0 )
160                 {
161                     Element typeElement = (Element) typeList.item( 0 );
162                     strType = typeElement.getTextContent( );
163                 }
164 
165                 HashMap<String, String> dependencyMap = new HashMap<>( );
166                 dependencyMap.put( TAG_DEPENDENCY_GROUPID, strGroupId );
167                 dependencyMap.put( TAG_DEPENDENCY_ARTIFACT_ID, strArtifactId );
168                 dependencyMap.put( TAG_DEPENDENCY_VERSION, strVersion );
169                 dependencyMap.put( TAG_DEPENDENCY_TYPE, strType );
170 
171                 dependencyList.add( dependencyMap );
172             }
173 
174             component.set( ( bSnapshot ? "SNAPSHOT_" : "" ) + Component.DEPENDENCY_LIST, dependencyList );
175 
176         }
177         catch ( IOException | ParserConfigurationException | DOMException | SAXException e )
178         {
179             AppLogService.error( EXCEPTION_MESSAGE + component.getArtifactId( ) + " : " + e.getMessage( ), e );
180         }
181         catch ( HttpAccessException e )
182         {
183             sbLogs.append( "\n*** ERROR *** Error reading pom for component :" ).append( component.getArtifactId( ) )
184                     .append( " [url : " ).append( strPomUrl ).append( "]" ).append( EXCEPTION_MESSAGE );
185         }
186     }
187 
188 }