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  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.web.context.WebApplicationContext;
33  import org.springframework.web.context.support.WebApplicationContextUtils;
34  
35  import com.gisgraphy.addressparser.AddressQuery;
36  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
37  import com.gisgraphy.geocoding.IGeocodingService;
38  
39  /**
40   * Provides a servlet Wrapper around The Gisgraphy geocoding Service, it Maps web
41   * parameters to create a {@linkplain AddressQuery}
42   * 
43   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
44   */
45  public class GeocodingServlet extends AbstractAddressServlet {
46  
47  
48     
49  
50      public void setGeocodingService(IGeocodingService geocodingService) {
51          this.geocodingService = geocodingService;
52      }
53  
54  
55  
56      /**
57       * Default serialVersionUID
58       */
59      private static final long serialVersionUID = -9054548241743095743L;
60  
61      /**
62       * The logger
63       */
64      protected static final Logger logger = LoggerFactory
65  	    .getLogger(GeocodingServlet.class);
66    
67  
68      private IGeocodingService geocodingService;
69  
70    
71  
72      /* (non-Javadoc)
73       * @see com.gisgraphy.servlet.GisgraphyServlet#getGisgraphyServiceType()
74       */
75      @Override
76      public GisgraphyServiceType getGisgraphyServiceType() {
77  	return GisgraphyServiceType.GEOCODING;
78      }
79  
80  
81  
82      /*
83       * (non-Javadoc)
84       * 
85       * @see javax.servlet.GenericServlet#init()
86       */
87      @Override
88      public void init() throws ServletException {
89  	try {
90  	    super.init();
91  	    WebApplicationContext springContext = WebApplicationContextUtils
92  		    .getWebApplicationContext(getServletContext());
93  	    geocodingService = (IGeocodingService) springContext
94  		    .getBean("geocodingService");
95  	    logger
96  		    .info("geocodingService is injected :"
97  			    + geocodingService);
98  	} catch (Exception e) {
99  	    logger.error("Can not start GeocodingServlet : " + e.getMessage(),e);
100 	}
101     }
102 
103 
104 
105     @Override
106     public void processRequest(AddressQuery query, HttpServletResponse resp) throws IOException {
107     	geocodingService.geocodeAndSerialize(query, resp.getOutputStream());
108     }
109 
110 
111 
112 	/* (non-Javadoc)
113 	 * @see com.gisgraphy.servlet.AbstractAddressServlet#checkparameter()
114 	 */
115 	@Override
116 	public boolean checkparameter() {
117 		//we do not check parameter because address parameter is not required since we can provide structured address
118 		//and country parameter is not required since v 4.0
119 		return false;
120 	}
121 
122 }