View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.servlet;
24  
25  import java.io.IOException;
26  import java.util.ResourceBundle;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.web.context.WebApplicationContext;
35  import org.springframework.web.context.support.WebApplicationContextUtils;
36  
37  import com.gisgraphy.domain.valueobject.Constants;
38  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
39  import com.gisgraphy.geoloc.GeolocErrorVisitor;
40  import com.gisgraphy.geoloc.GeolocQuery;
41  import com.gisgraphy.helper.HTMLHelper;
42  import com.gisgraphy.reversegeocoding.IReverseGeocodingService;
43  import com.gisgraphy.reversegeocoding.ReverseGeocodingQuery;
44  import com.gisgraphy.reversegeocoding.ReverseGeocodingQueryHttpBuilder;
45  import com.gisgraphy.serializer.common.IoutputFormatVisitor;
46  import com.gisgraphy.serializer.common.OutputFormat;
47  
48  /**
49   * Provides a servlet Wrapper around The Gisgraphy Reverse geocoding Service, it Maps web
50   * parameters to create a {@linkplain ReverseGeocodingQuery}
51   * 
52   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
53   */
54  public class ReverseGeocodingServlet extends GisgraphyServlet {
55  
56     
57      /*
58       * (non-Javadoc)
59       * 
60       * @see javax.servlet.GenericServlet#init()
61       */
62      @Override
63      public void init() throws ServletException {
64  	try {
65  	    super.init();
66  	    WebApplicationContext springContext = WebApplicationContextUtils
67  		    .getWebApplicationContext(getServletContext());
68  	    reverseGeocodingService = (IReverseGeocodingService) springContext
69  		    .getBean("reverseGeocodingService");
70  	    logger
71  		    .info("geolocSearchEngine is injected :"
72  			    + reverseGeocodingService);
73  	} catch (Exception e) {
74  	    logger.error("Can not start ReverseGeocodingServlet : " + e.getMessage(),e);
75  	}
76      }
77  
78      /**
79       * Default serialVersionUID
80       */
81      private static final long serialVersionUID = -9054548241743095743L;
82  
83      /**
84       * The logger
85       */
86      protected static final Logger logger = LoggerFactory
87  	    .getLogger(ReverseGeocodingServlet.class);
88    
89  
90      private IReverseGeocodingService reverseGeocodingService;
91  
92      /*
93       * (non-Javadoc)
94       * 
95       * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
96       *      javax.servlet.http.HttpServletResponse)
97       */
98      @Override
99      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
100 	    throws ServletException, IOException {
101 	OutputFormat format = OutputFormat.getDefault();
102 	try {
103 	    format = setResponseContentType(req, resp);
104 	    // check empty query
105 	    if (HTMLHelper
106 		    .isParametersEmpty(req, GeolocQuery.LAT_PARAMETER, GeolocQuery.LONG_PARAMETER)) {
107 		sendCustomError(ResourceBundle.getBundle(
108 			Constants.BUNDLE_ERROR_KEY).getString(
109 			"error.emptyLatLong"), format, resp,req);
110 		return;
111 	    }
112 	    ReverseGeocodingQuery query = ReverseGeocodingQueryHttpBuilder.getInstance().buildFromHttpRequest(req);
113 	    if (logger.isDebugEnabled()){
114 	    logger.debug("query=" + query);
115 	    logger.debug("reverse geocoding engine=" + reverseGeocodingService);
116 	    }
117 	    String UA = req.getHeader("User-Agent");
118 	    String referer = req.getHeader("Referer");
119 	    if (logger.isInfoEnabled()){
120 		logger.info("A reverse  geocoding request from "+req.getRemoteHost()+" / "+req.getRemoteAddr()+" was received , Referer : "+referer+" , UA : "+UA);
121 	    }
122 
123 	    reverseGeocodingService.executeAndSerialize(query, resp
124 		    .getOutputStream());
125 	} catch (RuntimeException e) {
126 	    logger.error("error while execute a reverse geocoding query from http request : " + e,e);
127 	    String errorMessage = isDebugMode() ? " : " + e.getMessage() : "";
128 	    sendCustomError(ResourceBundle
129 		    .getBundle(Constants.BUNDLE_ERROR_KEY).getString(
130 			    "error.error")
131 		    + errorMessage, format, resp,req);
132 	    return;
133 	}
134 
135     }
136 
137 
138     /* (non-Javadoc)
139      * @see com.gisgraphy.servlet.GisgraphyServlet#getGisgraphyServiceType()
140      */
141     @Override
142     public GisgraphyServiceType getGisgraphyServiceType() {
143 	return GisgraphyServiceType.REVERSEGEOCODING;
144     }
145 
146 
147     /* (non-Javadoc)
148      * @see com.gisgraphy.servlet.GisgraphyServlet#getErrorVisitor(java.lang.String)
149      */
150     @Override
151     public IoutputFormatVisitor getErrorVisitor(String errorMessage) {
152 	return new GeolocErrorVisitor(errorMessage);
153     }
154 
155 
156 
157 	public void setReverseGeocodingService(
158 			IReverseGeocodingService reverseGeocodingService) {
159 		this.reverseGeocodingService = reverseGeocodingService;
160 	}
161 
162 }