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.GeolocQuery;
40  import com.gisgraphy.geoloc.GisgraphyCommunicationException;
41  import com.gisgraphy.helper.HTMLHelper;
42  import com.gisgraphy.serializer.common.IoutputFormatVisitor;
43  import com.gisgraphy.serializer.common.OutputFormat;
44  import com.gisgraphy.street.IStreetSearchEngine;
45  import com.gisgraphy.street.StreetSearchErrorVisitor;
46  import com.gisgraphy.street.StreetSearchQuery;
47  import com.gisgraphy.street.StreetSearchQueryHttpBuilder;
48  
49  /**
50   * Provides a servlet Wrapper around The Gisgraphy street Service, it Maps web
51   * parameters to create a {@linkplain StreetSearchQuery}
52   * 
53   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
54   */
55  public class StreetServlet extends GisgraphyServlet {
56  
57      /**
58       * Default serialVersionUID
59       */
60      private static final long serialVersionUID = 8544156407519263142L;
61      
62     
63      public static final String STREET_SEARCH_MODE_PARAMETER = "mode";
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see javax.servlet.GenericServlet#init()
69       */
70      @Override
71      public void init() throws ServletException {
72  	try {
73  	    super.init();
74  	    WebApplicationContext springContext = WebApplicationContextUtils
75  		    .getWebApplicationContext(getServletContext());
76  	    streetSearchEngine = (IStreetSearchEngine) springContext
77  		    .getBean("streetSearchEngine");
78  	    logger
79  		    .info("streetSearchEngine is injected :"
80  			    + streetSearchEngine);
81  	} catch (Exception e) {
82  	    logger.error("Can not start StreetServlet : " + e.getMessage(),e);
83  	}
84      }
85  
86  
87      /**
88       * The logger
89       */
90      protected static final Logger logger = LoggerFactory
91  	    .getLogger(StreetServlet.class);
92  
93      private IStreetSearchEngine streetSearchEngine;
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) && HTMLHelper
110 		    .isParametersEmpty(req, StreetSearchQuery.NAME_PARAMETER)) {
111 		sendCustomError(ResourceBundle.getBundle(
112 			Constants.BUNDLE_ERROR_KEY).getString(
113 			"error.emptyLatLong"), format, resp,req);
114 		return;
115 	    }
116 	    StreetSearchQuery query = StreetSearchQueryHttpBuilder.getInstance().buildFromHttpRequest(req);
117 	    if (logger.isDebugEnabled()){
118 	    logger.debug("query=" + query);
119 	    }
120 	    String UA = req.getHeader("User-Agent");
121 	    String referer = req.getHeader("Referer");
122 	    if (logger.isInfoEnabled()){
123 		logger.info("A street request from "+req.getRemoteHost()+" / "+req.getRemoteAddr()+" was received , Referer : "+referer+" , UA : "+UA);
124 	    }
125 
126 	    streetSearchEngine.executeAndSerialize(query, resp
127 		    .getOutputStream());
128 	} catch (RuntimeException e) {
129 	    if (e instanceof GisgraphyCommunicationException){
130 		logger.warn("A communication error has occured, maybe the socket has been closed probably because the client has cancel the request, it is probably not important");
131 		return;
132 	    }
133 	    logger.error("error while execute a streetsearch query from http request : " + e,e);
134 	    String errorMessage = isDebugMode() ? " : " + e.getMessage() : "";
135 	    sendCustomError(ResourceBundle
136 		    .getBundle(Constants.BUNDLE_ERROR_KEY).getString(
137 			    "error.error")
138 		    + errorMessage, format, resp,req);
139 	    return;
140 	}
141 
142     }
143 
144 
145  
146 
147    
148     /**
149      * @param streetSearchEngine
150      *                the streetSearchEngine to set
151      */
152     public void setStreetSearchEngine(IStreetSearchEngine streetSearchEngine) {
153 	this.streetSearchEngine = streetSearchEngine;
154     }
155 
156    
157 
158     /* (non-Javadoc)
159      * @see com.gisgraphy.servlet.GisgraphyServlet#getGisgraphyServiceType()
160      */
161     @Override
162     public GisgraphyServiceType getGisgraphyServiceType() {
163 	return GisgraphyServiceType.STREET;
164     }
165 
166 
167     /* (non-Javadoc)
168      * @see com.gisgraphy.servlet.GisgraphyServlet#getErrorVisitor(java.lang.String)
169      */
170     @Override
171     public IoutputFormatVisitor getErrorVisitor(String errorMessage) {
172 	return new StreetSearchErrorVisitor(errorMessage);
173     }
174 
175 }