View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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  package fr.paris.lutece.plugins.geocode.v1.web.rs.service;
35  
36  import fr.paris.lutece.plugins.geocode.v1.web.rs.dto.City;
37  import fr.paris.lutece.plugins.geocode.v1.web.rs.dto.Country;
38  import fr.paris.lutece.plugins.geocode.v1.web.rs.util.Constants;
39  import fr.paris.lutece.plugins.geocode.v1.web.service.IGeoCodeTransportProvider;
40  import fr.paris.lutece.plugins.geocode.v1.web.service.IHttpTransportProvider;
41  import org.apache.log4j.Logger;
42  
43  import java.net.URLEncoder;
44  import java.nio.charset.StandardCharsets;
45  import java.text.SimpleDateFormat;
46  import java.util.Date;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  
51  /**
52   * IdentityQualityRestClientService
53   */
54  public class GeoCodeTransportRest extends AbstractTransportRest implements IGeoCodeTransportProvider
55  {
56  	SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
57  	
58      /**
59       * Logger
60       */
61      private static Logger _logger = Logger.getLogger( GeoCodeTransportRest.class );
62  
63      /** URL for identityStore Quality REST service */
64      private String _strIdentityStoreQualityEndPoint;
65  
66      /**
67       * Simple Constructor
68       */
69      public GeoCodeTransportRest( )
70      {
71          super( new HttpAccessTransport( ) );
72      }
73  
74      /**
75       * Constructor with IHttpTransportProvider parameter
76       *
77       * @param httpTransport
78       *            the provider to use
79       */
80      public GeoCodeTransportRest( final IHttpTransportProvider httpTransport )
81      {
82          super( httpTransport );
83  
84          _strIdentityStoreQualityEndPoint = httpTransport.getApiEndPointUrl( );
85      }
86  
87      /**
88       * {@inheritDoc }
89       */
90      @Override
91      public City getCityByCodeAndDate( final String strCityCode, final Date dateRef ) throws Exception
92      {
93          _logger.debug( "Get City of " + strCityCode );
94          final Map<String, String> mapParams = new HashMap<>( );
95          mapParams.put( Constants.PARAM_DATE_VALIDITY, df.format( dateRef ) );
96  
97          final String url = _strIdentityStoreQualityEndPoint + Constants.VERSION_PATH_V2 + Constants.CITIES_PATH + "/" + strCityCode;
98          return _httpTransport.doGet( url, mapParams,null,  City.class, _mapper );
99      }
100     
101     /**
102      * {@inheritDoc }
103      */
104     @Override
105     public List<City> getListCitesByNameAndDate( final String strCityName, final Date dateRef ) throws Exception
106     {
107         _logger.debug( "Get list City of name " + strCityName );
108         final Map<String, String> mapParams = new HashMap<>( );
109         mapParams.put( Constants.PARAM_SEARCH_NAME_CITY, strCityName );
110         mapParams.put( Constants.PARAM_DATE_VALIDITY, df.format( dateRef ) );
111 
112         final String url = _strIdentityStoreQualityEndPoint + Constants.VERSION_PATH_V2 + Constants.CITIES_PATH;
113         return _httpTransport.doGetList( url, mapParams,null,  City.class, _mapper );
114     }
115     
116     /**
117      * {@inheritDoc }
118      */
119     @Override
120     public List<City> getListCitesByNameAndDateLike( final String strCityName, final Date dateRef ) throws Exception
121     {
122         _logger.debug( "Get list like City of name " + strCityName );
123         final Map<String, String> mapParams = new HashMap<>( );
124         mapParams.put( Constants.PARAM_SEARCH_NAME_CITY, strCityName );
125         mapParams.put( Constants.PARAM_DATE_VALIDITY, df.format( dateRef ) );
126 
127         final String url = _strIdentityStoreQualityEndPoint + Constants.VERSION_PATH_V2 + Constants.CITIES_PATH + Constants.LIST_PATH;
128         return _httpTransport.doGetList( url, mapParams,null,  City.class, _mapper );
129     }
130 
131     /**
132      * {@inheritDoc }
133      */
134     @Override
135     public Country getCountryByCodeAndDate( final String strCountryCode, final Date dateRef ) throws Exception
136     {
137         _logger.debug( "Get Country of " + strCountryCode );
138         final Map<String, String> mapParams = new HashMap<>( );
139         mapParams.put( Constants.PARAM_DATE_VALIDITY, df.format( dateRef ) );
140 
141         final String url = _strIdentityStoreQualityEndPoint + Constants.VERSION_PATH_V2 + Constants.COUNTRIES_PATH + "/" + strCountryCode;
142         return _httpTransport.doGet( url, mapParams,null,  Country.class, _mapper );
143     }
144     
145     /**
146      * {@inheritDoc }
147      */
148     @Override
149     public List<Country> getListCountriesByNameAndDate( final String strCountryName, final Date dateRef ) throws Exception
150     {
151         _logger.debug( "Get Country of " + strCountryName );
152         final Map<String, String> mapParams = new HashMap<>( );
153         mapParams.put( Constants.PARAM_DATE_VALIDITY, df.format( dateRef ) );
154 
155         final String url = _strIdentityStoreQualityEndPoint + Constants.VERSION_PATH_V2 + Constants.COUNTRIES_PATH  + Constants.SEARCH_PATH + "/" + URLEncoder.encode(strCountryName, StandardCharsets.UTF_8.toString());
156         return _httpTransport.doGetList( url, mapParams,null,  Country.class, _mapper );
157     }
158 }