1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
85 resp.setContentType("text/xml");
86 PrintWriter out = resp.getWriter();
87 out.print(content);
88 out.close();
89 }
90 }
91