View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.googleapi.service;
35  
36  import fr.paris.lutece.plugins.googleapi.business.FeedProvider;
37  import fr.paris.lutece.plugins.googleapi.business.Item;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  import fr.paris.lutece.portal.service.util.AppLogService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.util.ReferenceList;
42  import fr.paris.lutece.util.url.UrlItem;
43  
44  import org.xml.sax.SAXException;
45  
46  import java.io.IOException;
47  import java.io.InputStream;
48  
49  import java.net.HttpURLConnection;
50  import java.net.URL;
51  import java.net.URLEncoder;
52  
53  import java.util.HashMap;
54  import java.util.List;
55  import java.util.Map;
56  
57  import javax.xml.parsers.ParserConfigurationException;
58  import javax.xml.parsers.SAXParser;
59  import javax.xml.parsers.SAXParserFactory;
60  
61  
62  /**
63   * This service provides an access to all feeds defined in the Spring context file : googleapi_context.xml
64   */
65  public class FeedsService
66  {
67      private static final String PLUGIN_NAME = "googleapi";
68      private static final String BEAN_FEEDS = "googleapi.feeds";
69      private static final String PROPERTY_API_KEY = "googleapi.api.key";
70      private static Map<String, FeedProvider> _mapProviders = new HashMap<String, FeedProvider>(  );
71      private static FeedsService _singleton;
72  
73      /** Creates a new instance of FeedsService */
74      private FeedsService(  )
75      {
76      }
77  
78      /**
79       * Returns the unique instance of the service
80       * @return The instance
81       */
82      public static FeedsService getInstance(  )
83      {
84          if ( _singleton == null )
85          {
86              _singleton = (FeedsService) SpringContextService.getPluginBean( PLUGIN_NAME, BEAN_FEEDS );
87          }
88  
89          return _singleton;
90      }
91  
92      /**
93       * Sets the feeds map (used for spring injection)
94       * @param map The feeds map
95       */
96      public void setFeedsMap( Map<String, FeedProvider> map )
97      {
98          _mapProviders = map;
99      }
100 
101     /**
102      * Returns a provider from its key
103      * @param strKey The provider's key
104      * @return The provider
105      */
106     public FeedProvider getProvider( String strKey )
107     {
108         return _mapProviders.get( strKey );
109     }
110 
111     /**
112      * Returns the providers list
113      * @return the providers list
114      */
115     public ReferenceList getProviders(  )
116     {
117         ReferenceList listProviders = new ReferenceList(  );
118 
119         for ( FeedProvider fp : _mapProviders.values(  ) )
120         {
121             listProviders.addItem( fp.getKey(  ), fp.getName(  ) );
122         }
123 
124         return listProviders;
125     }
126 
127     /**
128      * Returns the providers list
129      * @return the providers list
130      */
131     public ReferenceList getProviders( Class classProvider )
132     {
133         ReferenceList listProviders = new ReferenceList(  );
134 
135         for ( FeedProvider fp : _mapProviders.values(  ) )
136         {
137             if ( classProvider.isInstance( fp ) )
138             {
139                 listProviders.addItem( fp.getKey(  ), fp.getName(  ) );
140             }
141         }
142 
143         return listProviders;
144     }
145 
146     /**
147     * Connect to the Google Base data API server, retrieve the items
148     *
149     * @param strQuery The query
150     * @param listItems A list of item to feed
151     * @param provider The feed provider
152     * @throws java.io.IOException if an error occured during the process
153     * @throws org.xml.sax.SAXException if an error occured during the process
154     * @throws javax.xml.parsers.ParserConfigurationException if an error occured during the process
155     */
156     public void getItems( String strQuery, List<Item> listItems, FeedProvider provider )
157         throws IOException, SAXException, ParserConfigurationException
158     {
159         /*
160          * Create a URL object, open an Http connection on it and get the input
161          * stream that reads the Http response.
162          */
163         String strApiKey = AppPropertiesService.getProperty( PROPERTY_API_KEY );
164 
165         UrlItem url = new UrlItem( provider.getFeedsUrl(  ) );
166         url.addParameter( provider.getQueryParameter(  ), URLEncoder.encode( strQuery, "UTF-8" ) );
167 
168         if ( ( strApiKey != null ) && ( !strApiKey.equals( "" ) ) )
169         {
170             url.addParameter( "key", strApiKey );
171         }
172 
173         URL urlFeed = new URL( url.getUrl(  ) );
174         HttpURLConnection httpConnection = (HttpURLConnection) urlFeed.openConnection(  );
175         InputStream inputStream = httpConnection.getInputStream(  );
176 
177         AppLogService.info( "Retrieving items URL : " + url.getUrl(  ) );
178 
179         //        AppLogService.info( "Feed : " + getFeedAsString( inputStream ) );
180 
181         /*
182          * Create a SAX XML parser and pass in the input stream to the parser to extract
183          * fields from the stream.
184          */
185         SAXParserFactory factory = SAXParserFactory.newInstance(  );
186         SAXParser parser = factory.newSAXParser(  );
187         FeedHandler handler = provider.getFeedHandler(  );
188         handler.setItemList( listItems );
189         parser.parse( inputStream, handler );
190     }
191 }