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.reversegeocoding;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
28  import com.gisgraphy.domain.valueobject.Output;
29  import com.gisgraphy.domain.valueobject.Pagination;
30  import com.gisgraphy.helper.GeolocHelper;
31  import com.gisgraphy.helper.OutputFormatHelper;
32  import com.gisgraphy.serializer.common.OutputFormat;
33  import com.gisgraphy.servlet.GisgraphyServlet;
34  import com.vividsolutions.jts.geom.Point;
35  
36  /**
37   * A GeolocQuery Query builder. it build geolocQuery from HTTP Request
38   * 
39   * @see Pagination
40   * @see Output
41   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
42   */
43  public class ReverseGeocodingQueryHttpBuilder {
44  	
45      
46    
47  	private static ReverseGeocodingQueryHttpBuilder instance = new ReverseGeocodingQueryHttpBuilder();
48  	
49  	public static ReverseGeocodingQueryHttpBuilder getInstance() {
50  		return instance;
51  	}
52  
53      
54      /**
55       * @param req
56       *                an HttpServletRequest to construct a {@link ReverseGeocodingQuery}
57       */
58      public ReverseGeocodingQuery buildFromHttpRequest(HttpServletRequest req) {
59      
60  	// point
61  	Float latitude=null;
62  	Float longitude=null;
63  	// lat
64  	try {
65  			String latParameter = req.getParameter(ReverseGeocodingQuery.LAT_PARAMETER);
66  				if (latParameter!=null){
67  					latitude = GeolocHelper.parseInternationalDouble(latParameter);
68  				} else  {
69  					throw new ReverseGeocodingException("latitude is empty");
70  				} 
71  		} catch (Exception e) {
72  			throw new ReverseGeocodingException("latitude is not correct");
73  		}
74  
75  	// long
76  	try {
77  	    String longParameter = req
78  		    .getParameter(ReverseGeocodingQuery.LONG_PARAMETER);
79  	    if (longParameter!=null){
80  	    	longitude = GeolocHelper.parseInternationalDouble(longParameter);
81  	    } else {
82  	    		throw new ReverseGeocodingException(
83  	    		"longitude is empty");
84  	    	}
85  	} catch (Exception e) {
86  	    throw new ReverseGeocodingException(
87  		    "longitude is not correct ");
88  	}
89  	
90  	// point
91  	Point point = null ;
92  	try {
93  		if (latitude!=null && longitude!=null ){
94  	    point = GeolocHelper.createPoint(longitude, latitude);
95  		} 
96  	} catch (RuntimeException e1) {
97  	    	throw new ReverseGeocodingException("can not determine Point");
98  	}
99  	
100 	ReverseGeocodingQuery geolocQuery = new ReverseGeocodingQuery(point);
101 
102 	// output format
103 	OutputFormat format = OutputFormat.getFromString(req
104 		.getParameter(GisgraphyServlet.FORMAT_PARAMETER));
105 	format = OutputFormatHelper.getDefaultForServiceIfNotSupported(format, GisgraphyServiceType.GEOLOC);
106 	Output output = Output.withFormat(format);
107 
108 	// indent
109 	if ("true".equalsIgnoreCase(req
110 		.getParameter(GisgraphyServlet.INDENT_PARAMETER))
111 		|| "on".equalsIgnoreCase(req
112 			.getParameter(GisgraphyServlet.INDENT_PARAMETER))) {
113 	    output.withIndentation();
114 	}
115 	
116 	// apiKey
117 	String apiKey = req.getParameter(GisgraphyServlet.APIKEY_PARAMETER);
118 	geolocQuery.setApikey(apiKey);
119 	
120 	String CallBackParameter = req.getParameter(ReverseGeocodingQuery.CALLBACK_PARAMETER);
121 	if (CallBackParameter!=null){
122 	    geolocQuery.withCallback(CallBackParameter);
123 	}
124 	
125 	geolocQuery.withOutput(output);
126 	return geolocQuery;
127 
128 }
129 }