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