View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.gems.googlemaps;
18  
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.net.URLEncoder;
22  
23  import org.apache.commons.httpclient.HttpClient;
24  import org.apache.commons.httpclient.params.HttpMethodParams;
25  import org.apache.commons.httpclient.methods.GetMethod;
26  import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
27  import org.apache.commons.httpclient.HttpStatus;
28  import org.apache.commons.httpclient.HttpException;
29  
30  
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /***
37   * YahooGeocodeProxyServlet
38   * 
39   * 
40   * @author jonathan david phillips
41   * @version $Id: YahooGeocodeProxyServlet.java 000001 2006-04-25 00:57:00Z jdp $
42   */
43  
44  public class YahooGeocodeProxyServlet extends HttpServlet
45  {
46      /***
47       * Configuration 
48       */
49       private static final String YAHOO_REQUEST = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&location=";
50  
51      /***
52       * doGet() override doGet
53       */
54       protected void doGet(HttpServletRequest req, HttpServletResponse resp)
55  		throws ServletException, java.io.IOException {
56  		String location = req.getParameter("location");
57  		location = URLEncoder.encode(location,"UTF-8");
58  		String url = YAHOO_REQUEST + location;
59  		String content = "<error/>";
60  		
61  		// get content from yahoo, code from http://jakarta.apache.org/commons/httpclient/tutorial.html
62  		HttpClient client = new HttpClient();
63  		GetMethod method = new GetMethod(url);
64  		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
65      			new DefaultHttpMethodRetryHandler(3, false));
66  		try {
67  			int statusCode = client.executeMethod(method);
68  			if (statusCode != HttpStatus.SC_OK) {
69  				System.err.println("Method failed: " + method.getStatusLine());
70  			}
71  			// set content
72  			content = method.getResponseBodyAsString();
73  
74  		} catch (HttpException e) {
75  			System.err.println("Fatal protocol violation: " + e.getMessage());
76  			e.printStackTrace();
77  		} catch (IOException e) {
78  			System.err.println("Fatal transport error: " + e.getMessage());
79  			e.printStackTrace();
80  		} finally {
81  			method.releaseConnection();
82  		}
83  					
84  		//  return content
85  		resp.setContentType("text/xml");
86  		PrintWriter out = resp.getWriter();
87  		out.print(content);
88  		out.close();
89  	}
90  }
91