View Javadoc

1   /*
2    * Copyright (c) 2002-2014, 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.updater.service.catalog;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPathService;
38  import fr.paris.lutece.util.httpaccess.HttpAccess;
39  import fr.paris.lutece.util.httpaccess.HttpAccessException;
40  
41  import org.apache.commons.digester.Digester;
42  import org.apache.commons.digester.xmlrules.DigesterLoader;
43  
44  import org.xml.sax.SAXException;
45  
46  import java.io.File;
47  import java.io.FileNotFoundException;
48  import java.io.FileReader;
49  import java.io.IOException;
50  import java.io.Reader;
51  import java.io.StringReader;
52  
53  import java.net.URL;
54  
55  import java.util.ArrayList;
56  import java.util.List;
57  
58  
59  /**
60   * CatalogService
61   */
62  public class CatalogService implements ICatalogService
63  {
64      private static final String FILE_CATALOGS_LIST_RULES = "/fr/paris/lutece/plugins/updater/service/catalog/catalogs-list-digester-rules.xml";
65      private static final String FILE_CATALOG_RULES = "/fr/paris/lutece/plugins/updater/service/catalog/catalog-digester-rules.xml";
66      private static final String FILE_CATALOGS_LIST = "/plugins/updater/catalogs.xml";
67      private static final String EXCEPTION_MESSAGE = "Error loading catalog : ";
68      private List<CatalogInfos> _listInfos = new ArrayList<CatalogInfos>(  );
69      private List<Catalog> _listCatalogs = new ArrayList<Catalog>(  );
70  
71      /**
72       * Retrieve all plugin releases from a repository
73       * @return A CatalogInfos list
74       */
75      public List<CatalogInfos> getCatalogInfos(  )
76      {
77          // clear catalogs data
78          _listCatalogs.clear(  );
79          _listInfos.clear(  );
80  
81          try
82          {
83              // load catalogs list
84              File fileCatalogs = new File( AppPathService.getWebAppPath(  ) + FILE_CATALOGS_LIST );
85              FileReader readerFile = new FileReader( fileCatalogs );
86              loadCatalogsList( readerFile );
87  
88              HttpAccess httpAccess = new HttpAccess(  );
89  
90              for ( Catalog catalog : _listCatalogs )
91              {
92                  String strXmlCatalog = httpAccess.doGet( catalog.getUrl(  ) );
93  
94                  Reader reader = new StringReader( strXmlCatalog );
95                  load( reader );
96              }
97          }
98          catch ( FileNotFoundException e )
99          {
100             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e );
101         }
102         catch ( HttpAccessException e )
103         {
104             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e.getCause(  ) );
105         }
106 
107         return _listInfos;
108     }
109 
110     /**
111      * Add an entry to the catalog
112      * @param ci A CatalogInfos entry
113      */
114     public void addEntry( CatalogInfos ci )
115     {
116         _listInfos.add( ci );
117     }
118 
119     /**
120      * Add a catalog to the list
121      * @param catalog A Catalog
122      */
123     public void addCatalog( Catalog catalog )
124     {
125         _listCatalogs.add( catalog );
126     }
127 
128     /**
129      * Load a catalog list from a reader object
130      * @param reader The reader
131      */
132     private void loadCatalogsList( Reader reader )
133     {
134         // Configure Digester from XML ruleset
135         URL rules = getClass(  ).getResource( FILE_CATALOGS_LIST_RULES );
136 
137         Digester digester = DigesterLoader.createDigester( rules );
138 
139         // Push empty List onto Digester's Stack
140         digester.push( this );
141 
142         try
143         {
144             digester.parse( reader );
145         }
146         catch ( FileNotFoundException e )
147         {
148             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e );
149         }
150         catch ( SAXException e )
151         {
152             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e );
153         }
154         catch ( IOException e )
155         {
156             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e );
157         }
158     }
159 
160     /**
161      * Load a catalog from a reader object
162      * @param reader The reader
163      */
164     private void load( Reader reader )
165     {
166         // Configure Digester from XML ruleset
167         URL rules = getClass(  ).getResource( FILE_CATALOG_RULES );
168 
169         Digester digester = DigesterLoader.createDigester( rules );
170 
171         // Push empty List onto Digester's Stack
172         digester.push( this );
173 
174         try
175         {
176             digester.parse( reader );
177         }
178         catch ( FileNotFoundException e )
179         {
180             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e );
181         }
182         catch ( SAXException e )
183         {
184             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e );
185         }
186         catch ( IOException e )
187         {
188             AppLogService.error( EXCEPTION_MESSAGE + e.getMessage(  ), e );
189         }
190     }
191 }