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.addressparser.IAddressParserService;
37  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
38  import com.gisgraphy.service.IStatsUsageService;
39  import com.gisgraphy.stats.StatsUsageType;
40  
41  /**
42   * Provides a servlet Wrapper around The Gisgraphy address parser Service, it Maps web
43   * parameters to create a {@linkplain AddressQuery}
44   * 
45   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
46   */
47  public class AddressParserServlet extends AbstractAddressServlet {
48  
49     
50      private IAddressParserService addressParser;
51      
52      private IStatsUsageService statsUsageService;
53  
54      /**
55       * Default serialVersionUID
56       */
57      private static final long serialVersionUID = -9054548241743095743L;
58  
59      /**
60       * The logger
61       */
62      protected static final Logger logger = LoggerFactory
63  	    .getLogger(AddressParserServlet.class);
64    
65  
66      /* (non-Javadoc)
67       * @see com.gisgraphy.servlet.GisgraphyServlet#getGisgraphyServiceType()
68       */
69      @Override
70      public GisgraphyServiceType getGisgraphyServiceType() {
71  	return GisgraphyServiceType.ADDRESS_PARSER;
72      }
73  
74  
75  
76      /*
77       * (non-Javadoc)
78       * 
79       * @see javax.servlet.GenericServlet#init()
80       */
81      @Override
82      public void init() throws ServletException {
83  	try {
84  	    super.init();
85  	    WebApplicationContext springContext = WebApplicationContextUtils
86  		    .getWebApplicationContext(getServletContext());
87  	    addressParser = (IAddressParserService) springContext
88  		    .getBean("addressParser");
89  	    statsUsageService = (IStatsUsageService) springContext
90  			    .getBean("statsUsageService");
91  	    logger
92  		    .info("Address parser is injected :"
93  			    + addressParser);
94  	    logger
95  	    .info("statsusage service is injected :"
96  		    + statsUsageService);
97  	} catch (Exception e) {
98  	    logger.error("Can not start AdressParserServlet : " + e.getMessage(),e);
99  	}
100     }
101 
102 
103 
104     @Override
105     public void processRequest(AddressQuery query, HttpServletResponse resp) throws IOException {
106    	statsUsageService.increaseUsage(StatsUsageType.ADDRESSPARSER);
107 	addressParser.executeAndSerialize(query, resp.getOutputStream());
108     }
109 
110 
111 
112     public void setAddressParserService(IAddressParserService addressParser) {
113         this.addressParser = addressParser;
114     }
115     
116 	public void setStatsUsageService(IStatsUsageService statsUsageService) {
117 		this.statsUsageService = statsUsageService;
118 	}
119 
120 
121 
122 	@Override
123 	public boolean checkparameter() {
124 		return true;
125 	}
126 
127 }