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.geocoloc.IGeolocSearchEngine;
40  import com.gisgraphy.geoloc.GeolocErrorVisitor;
41  import com.gisgraphy.geoloc.GeolocQuery;
42  import com.gisgraphy.geoloc.GeolocQueryHttpBuilder;
43  import com.gisgraphy.helper.HTMLHelper;
44  import com.gisgraphy.serializer.common.IoutputFormatVisitor;
45  import com.gisgraphy.serializer.common.OutputFormat;
46  
47  /**
48   * Provides a servlet Wrapper around The Gisgraphy geoloc Service, it Maps web
49   * parameters to create a {@linkplain GeolocQuery}
50   * 
51   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
52   */
53  public class GeolocServlet extends GisgraphyServlet {
54  
55     
56      
57      
58  
59  
60      /*
61       * (non-Javadoc)
62       * 
63       * @see javax.servlet.GenericServlet#init()
64       */
65      @Override
66      public void init() throws ServletException {
67  	try {
68  	    super.init();
69  	    WebApplicationContext springContext = WebApplicationContextUtils
70  		    .getWebApplicationContext(getServletContext());
71  	    geolocSearchEngine = (IGeolocSearchEngine) springContext
72  		    .getBean("geolocSearchEngine");
73  	    logger
74  		    .info("geolocSearchEngine is injected :"
75  			    + geolocSearchEngine);
76  	} catch (Exception e) {
77  	    logger.error("Can not start GeolocServlet : " + e.getMessage(),e);
78  	}
79      }
80  
81      /**
82       * Default serialVersionUID
83       */
84      private static final long serialVersionUID = -9054548241743095743L;
85  
86      /**
87       * The logger
88       */
89      protected static final Logger logger = LoggerFactory
90  	    .getLogger(GeolocServlet.class);
91    
92  
93      private IGeolocSearchEngine geolocSearchEngine;
94  
95      /*
96       * (non-Javadoc)
97       * 
98       * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
99       *      javax.servlet.http.HttpServletResponse)
100      */
101     @Override
102     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
103 	    throws ServletException, IOException {
104 	OutputFormat format = OutputFormat.getDefault();
105 	try {
106 	    format = setResponseContentType(req, resp);
107 	    // check empty query
108 	    if (HTMLHelper
109 		    .isParametersEmpty(req, GeolocQuery.LAT_PARAMETER, GeolocQuery.LONG_PARAMETER)) {
110 		sendCustomError(ResourceBundle.getBundle(
111 			Constants.BUNDLE_ERROR_KEY).getString(
112 			"error.emptyLatLong"), format, resp,req);
113 		return;
114 	    }
115 	    GeolocQuery query = GeolocQueryHttpBuilder.getInstance().buildFromHttpRequest(req);
116 	    if (logger.isDebugEnabled()){
117 	    logger.debug("query=" + query);
118 	    logger.debug("geolocSearchEngine=" + geolocSearchEngine);
119 	    }
120 	    String UA = req.getHeader("User-Agent");
121 	    String referer = req.getHeader("Referer");
122 	    if (logger.isInfoEnabled()){
123 		logger.info("A geoloc request from "+req.getRemoteHost()+" / "+req.getRemoteAddr()+" was received , Referer : "+referer+" , UA : "+UA);
124 	    }
125 
126 	    geolocSearchEngine.executeAndSerialize(query, resp
127 		    .getOutputStream());
128 	} catch (RuntimeException e) {
129 	    logger.error("error while execute a geoloc query from http request : " + e,e);
130 	    String errorMessage = isDebugMode() ? " : " + e.getMessage() : "";
131 	    sendCustomError(ResourceBundle
132 		    .getBundle(Constants.BUNDLE_ERROR_KEY).getString(
133 			    "error.error")
134 		    + errorMessage, format, resp,req);
135 	    return;
136 	}
137 
138     }
139 
140 
141  
142 
143    
144     /**
145      * @param geolocSearchEngine
146      *                the geolocSearchEngine to set
147      */
148     public void setGeolocSearchEngine(IGeolocSearchEngine geolocSearchEngine) {
149 	this.geolocSearchEngine = geolocSearchEngine;
150     }
151 
152    
153 
154     /* (non-Javadoc)
155      * @see com.gisgraphy.servlet.GisgraphyServlet#getGisgraphyServiceType()
156      */
157     @Override
158     public GisgraphyServiceType getGisgraphyServiceType() {
159 	return GisgraphyServiceType.GEOLOC;
160     }
161 
162 
163     /* (non-Javadoc)
164      * @see com.gisgraphy.servlet.GisgraphyServlet#getErrorVisitor(java.lang.String)
165      */
166     @Override
167     public IoutputFormatVisitor getErrorVisitor(String errorMessage) {
168 	return new GeolocErrorVisitor(errorMessage);
169     }
170 
171 }