01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: import javax.servlet.ServletException;
31: import javax.servlet.http.HttpServlet;
32: import javax.servlet.http.HttpServletRequest;
33: import javax.servlet.http.HttpServletResponse;
34:
35: /**
36: * YahooGeocodeProxyServlet
37: *
38: *
39: * @author jonathan david phillips
40: * @version $Id: YahooGeocodeProxyServlet.java 000001 2006-04-25 00:57:00Z jdp $
41: */
42:
43: public class YahooGeocodeProxyServlet extends HttpServlet {
44: /**
45: * Configuration
46: */
47: private static final String YAHOO_REQUEST = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&location=";
48:
49: /**
50: * doGet() override doGet
51: */
52: protected void doGet(HttpServletRequest req,
53: HttpServletResponse resp) throws ServletException,
54: java.io.IOException {
55: String location = req.getParameter("location");
56: location = URLEncoder.encode(location, "UTF-8");
57: String url = YAHOO_REQUEST + location;
58: String content = "<error/>";
59:
60: // get content from yahoo, code from http://jakarta.apache.org/commons/httpclient/tutorial.html
61: HttpClient client = new HttpClient();
62: GetMethod method = new GetMethod(url);
63: method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
64: new DefaultHttpMethodRetryHandler(3, false));
65: try {
66: int statusCode = client.executeMethod(method);
67: if (statusCode != HttpStatus.SC_OK) {
68: System.err.println("Method failed: "
69: + method.getStatusLine());
70: }
71: // set content
72: content = method.getResponseBodyAsString();
73:
74: } catch (HttpException e) {
75: System.err.println("Fatal protocol violation: "
76: + e.getMessage());
77: e.printStackTrace();
78: } catch (IOException e) {
79: System.err.println("Fatal transport error: "
80: + e.getMessage());
81: e.printStackTrace();
82: } finally {
83: method.releaseConnection();
84: }
85:
86: // return content
87: resp.setContentType("text/xml");
88: PrintWriter out = resp.getWriter();
89: out.print(content);
90: out.close();
91: }
92: }
|