1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
73
74
75 public List<CatalogInfos> getCatalogInfos( )
76 {
77
78 _listCatalogs.clear( );
79 _listInfos.clear( );
80
81 try
82 {
83
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
112
113
114 public void addEntry( CatalogInfos ci )
115 {
116 _listInfos.add( ci );
117 }
118
119
120
121
122
123 public void addCatalog( Catalog catalog )
124 {
125 _listCatalogs.add( catalog );
126 }
127
128
129
130
131
132 private void loadCatalogsList( Reader reader )
133 {
134
135 URL rules = getClass( ).getResource( FILE_CATALOGS_LIST_RULES );
136
137 Digester digester = DigesterLoader.createDigester( rules );
138
139
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
162
163
164 private void load( Reader reader )
165 {
166
167 URL rules = getClass( ).getResource( FILE_CATALOG_RULES );
168
169 Digester digester = DigesterLoader.createDigester( rules );
170
171
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 }