View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.dansmarue.modules.rest.service;
35  
36  import java.util.HashMap;
37  import java.util.Map;
38  
39  import org.apache.commons.lang.StringUtils;
40  
41  import fr.paris.lutece.plugins.dansmarue.modules.rest.util.constants.SignalementRestConstants;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.util.httpaccess.HttpAccess;
45  import fr.paris.lutece.util.httpaccess.HttpAccessException;
46  import net.sf.json.JSONObject;
47  
48  /**
49   * The Class RamenClientService.
50   */
51  public class RamenClientService
52  {
53  
54      /**
55       * Gets the dossiers courrants by geom.
56       *
57       * @param lng
58       *            the lng
59       * @param lat
60       *            the lat
61       * @return the dossiers courrants by geom
62       */
63      public String getDossiersCourrantsByGeom( Double lng, Double lat )
64      {
65          StringBuilder strUrl = new StringBuilder( AppPropertiesService.getProperty( SignalementRestConstants.PROPERTY_URL_RAMEN_REST ) );
66          strUrl.append( SignalementRestConstants.QUESTION_MARK );
67  
68          strUrl.append( SignalementRestConstants.PARAMETER_LATITUDE );
69          strUrl.append( SignalementRestConstants.EQUAL );
70          strUrl.append( lat );
71  
72          strUrl.append( SignalementRestConstants.AMPERSAND );
73  
74          strUrl.append( SignalementRestConstants.PARAMETER_LONGITUDE );
75          strUrl.append( SignalementRestConstants.EQUAL );
76          strUrl.append( lng );
77  
78          strUrl.append( SignalementRestConstants.AMPERSAND );
79  
80          strUrl.append( SignalementRestConstants.PARAMETER_RADIUS );
81          strUrl.append( SignalementRestConstants.EQUAL );
82          strUrl.append( AppPropertiesService.getProperty( SignalementRestConstants.PROPERTY_RADIUS ) );
83  
84          String strReponseXml = StringUtils.EMPTY;
85          HttpAccess httpAccess = new HttpAccess( );
86  
87          try
88          {
89              strReponseXml = httpAccess.doGet( strUrl.toString( ) );
90          }
91          catch( HttpAccessException e )
92          {
93              AppLogService.error( e.getMessage( ), e );
94          }
95  
96          return strReponseXml;
97      }
98  
99      /**
100      * Gets the dossiers courrants by geomm with limit.
101      *
102      * @param lng
103      *            the lng
104      * @param lat
105      *            the lat
106      * @return the dossiers courrants by geomm with limit
107      */
108     public String getDossiersCourrantsByGeommWithLimit( Double lng, Double lat )
109     {
110 
111         String strReponse = null;
112 
113         StringBuilder strUrl = new StringBuilder( AppPropertiesService.getProperty( SignalementRestConstants.PROPERTY_URL_RAMEN_REST_GEO_WITH_LIMIT ) );
114 
115         HttpAccess httpAccess = new HttpAccess( );
116 
117         JSONObject jsonObj = new JSONObject( );
118         jsonObj.accumulate( SignalementRestConstants.PARAMETER_RADIUS,
119                 Integer.valueOf( ( AppPropertiesService.getProperty( SignalementRestConstants.PROPERTY_RADIUS ) ) ) );
120         jsonObj.accumulate( SignalementRestConstants.PARAMETER_LONGITUDE, lng );
121         jsonObj.accumulate( SignalementRestConstants.PARAMETER_LATITUDE, lat );
122 
123         Map<String, String> headersRequest = new HashMap<>( );
124         Map<String, String> headersResponse = new HashMap<>( );
125 
126         try
127         {
128 
129             strReponse = httpAccess.doPostJSON( strUrl.toString( ), jsonObj.toString( ), headersRequest, headersResponse );
130 
131         }
132         catch( HttpAccessException e )
133         {
134             AppLogService.error( e.getMessage( ), e );
135         }
136 
137         return strReponse;
138     }
139 }