|
14 | 14 | import javax.servlet.http.HttpServletRequest;
|
15 | 15 | import javax.servlet.http.HttpServletResponse;
|
16 | 16 |
|
17 |
| -/** |
18 |
| - * Abstract Servlet implementation class AsyncRESTServlet. |
19 |
| - * Enquires ebay REST service for auctions by key word. |
20 |
| - * May be configured with init parameters: <dl> |
21 |
| - * <dt>appid</dt><dd>The eBay application ID to use</dd> |
22 |
| - * </dl> |
23 |
| - * Each request examines the following request parameters:<dl> |
24 |
| - * <dt>items</dt><dd>The keyword to search for</dd> |
25 |
| - * </dl> |
| 17 | +/** |
| 18 | + * AbstractRestServlet. |
| 19 | + * |
26 | 20 | */
|
27 |
| -public class AbstractRestServlet extends HttpServlet |
28 |
| -{ |
29 |
| - protected final static int MAX_RESULTS = 5; |
30 |
| - protected final static String GOOGLE_API_KEY_NAME = "async_example_place_key"; |
31 |
| - protected final static String GOOGLE_API_KEY = "AIzaSyABCgUB4wc290F9LcIigNXurT6yNV92yfY"; |
32 |
| - |
33 |
| - protected final static String STYLE = |
34 |
| - "<style type='text/css'>"+ |
35 |
| - " img.thumb:hover {height:50px}"+ |
36 |
| - " img.thumb {vertical-align:text-top}"+ |
37 |
| - " span.red {color: #ff0000}"+ |
38 |
| - " span.green {color: #00ff00}"+ |
39 |
| - " iframe {border: 0px}"+ |
40 |
| - "</style>"; |
| 21 | +public class AbstractRestServlet extends HttpServlet { |
| 22 | + protected final static int MAX_RESULTS = 5; |
41 | 23 |
|
| 24 | + protected final static String GOOGLE_API_KEY = "AIzaSyABCgUB4wc290F9LcIigNXurT6yNV92yfY"; |
| 25 | + |
| 26 | + protected final static String STYLE = "<style type='text/css'>" |
| 27 | + + " img.thumb:hover {height:50px}" |
| 28 | + + " img.thumb {vertical-align:text-top}" |
| 29 | + + " span.red {color: #ff0000}" |
| 30 | + + " span.green {color: #00ff00}" |
| 31 | + + " iframe {border: 0px}" + "</style>"; |
42 | 32 |
|
43 | 33 | protected final static String APPID_PARAM = "appid";
|
44 | 34 | protected final static String LOC_PARAM = "loc";
|
45 | 35 | protected final static String ITEMS_PARAM = "items";
|
46 | 36 | protected final static String LATITUDE_PARAM = "lat";
|
47 | 37 | protected final static String LONGITUDE_PARAM = "long";
|
48 |
| - protected final static String RADIUS_PARAM ="radius"; |
49 |
| - protected String _key; |
| 38 | + protected final static String RADIUS_PARAM = "radius"; |
| 39 | + protected String key; |
50 | 40 |
|
51 | 41 | @Override
|
52 |
| - public void init(ServletConfig servletConfig) throws ServletException |
53 |
| - { |
54 |
| - if (servletConfig.getInitParameter(APPID_PARAM) == null) |
55 |
| - _key = GOOGLE_API_KEY; |
56 |
| - else |
57 |
| - _key = servletConfig.getInitParameter(APPID_PARAM); |
| 42 | + public void init(ServletConfig servletConfig) throws ServletException { |
| 43 | + if (servletConfig.getInitParameter(APPID_PARAM) == null) |
| 44 | + key = GOOGLE_API_KEY; |
| 45 | + else |
| 46 | + key = servletConfig.getInitParameter(APPID_PARAM); |
58 | 47 | }
|
59 | 48 |
|
60 |
| - |
61 |
| - public static String sanitize(String s) |
62 |
| - { |
63 |
| - if (s==null) |
64 |
| - return null; |
65 |
| - return s.replace("<","?").replace("&","?").replace("\n","?"); |
| 49 | + public static String sanitize(String s) { |
| 50 | + if (s == null) |
| 51 | + return null; |
| 52 | + return s.replace("<", "?").replace("&", "?").replace("\n", "?"); |
66 | 53 | }
|
67 |
| - |
68 |
| - protected String restQuery (String coordinates, String radius, String item) |
69 |
| - { |
70 |
| - try |
71 |
| - { |
72 |
| - return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key="+_key |
73 |
| - +"&location="+URLEncoder.encode(coordinates,"UTF-8") |
74 |
| - +"&types="+URLEncoder.encode(item,"UTF-8") |
75 |
| - +"&radius="+URLEncoder.encode(radius, "UTF-8"); |
76 |
| - |
77 |
| - } |
78 |
| - catch (Exception e) |
79 |
| - { |
80 |
| - throw new RuntimeException(e); |
81 |
| - } |
| 54 | + |
| 55 | + protected String restQuery(String coordinates, String radius, String item) { |
| 56 | + try { |
| 57 | + return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=" + key + "&location=" |
| 58 | + + URLEncoder.encode(coordinates, "UTF-8") + "&types=" + URLEncoder.encode(item, "UTF-8") |
| 59 | + + "&radius=" + URLEncoder.encode(radius, "UTF-8"); |
| 60 | + |
| 61 | + } catch (Exception e) { |
| 62 | + throw new RuntimeException(e); |
| 63 | + } |
82 | 64 | }
|
83 |
| - |
84 |
| - |
85 |
| - |
86 |
| - public String generateResults (Queue<Map<String,Object>> results) |
87 |
| - { |
88 |
| - StringBuilder thumbs = new StringBuilder(); |
89 |
| - int resultCount = 0; |
90 |
| - Iterator<Map<String,Object>> itor = results.iterator(); |
91 |
| - |
92 |
| - while (resultCount < MAX_RESULTS && itor.hasNext()) |
93 |
| - { |
94 |
| - Map m = (Map)itor.next(); |
95 |
| - String name = (String)m.get("name"); |
96 |
| - Object[] photos = (Object[])m.get("photos"); |
97 |
| - if (photos != null && photos.length > 0) |
98 |
| - { |
99 |
| - resultCount++; |
100 |
| - thumbs.append("<img class='thumb' border='1px' height='40px'"+ |
101 |
| - " src='"+getPhotoURL((String)(((Map)photos[0]).get("photo_reference")))+"'"+ |
102 |
| - " title='"+name+"'"+ |
103 |
| - "/>"); |
104 |
| - thumbs.append("</a> "); |
105 |
| - } |
106 |
| - } |
107 |
| - return thumbs.toString(); |
| 65 | + |
| 66 | + public String generateResults(Queue<Map<String, Object>> results) { |
| 67 | + StringBuilder thumbs = new StringBuilder(); |
| 68 | + int resultCount = 0; |
| 69 | + Iterator<Map<String, Object>> itor = results.iterator(); |
| 70 | + |
| 71 | + while (resultCount < MAX_RESULTS && itor.hasNext()) { |
| 72 | + Map m = (Map) itor.next(); |
| 73 | + String name = (String) m.get("name"); |
| 74 | + Object[] photos = (Object[]) m.get("photos"); |
| 75 | + if (photos != null && photos.length > 0) { |
| 76 | + resultCount++; |
| 77 | + thumbs.append("<img class='thumb' border='1px' height='40px'" + " src='" |
| 78 | + + getPhotoURL((String) (((Map) photos[0]).get("photo_reference"))) + "'" + " title='" + name |
| 79 | + + "'" + "/>"); |
| 80 | + thumbs.append("</a> "); |
| 81 | + } |
| 82 | + } |
| 83 | + return thumbs.toString(); |
108 | 84 | }
|
109 |
| - |
110 |
| - public String getPhotoURL (String photoref) |
111 |
| - { |
112 |
| - return "https://maps.googleapis.com/maps/api/place/photo?key="+_key |
113 |
| - +"&photoreference="+photoref |
114 |
| - +"&maxheight=40"; |
| 85 | + |
| 86 | + public String getPhotoURL(String photoref) { |
| 87 | + return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref |
| 88 | + + "&maxheight=40"; |
115 | 89 | }
|
116 |
| - |
117 | 90 |
|
118 |
| - protected String ms(long nano) |
119 |
| - { |
120 |
| - BigDecimal dec = new BigDecimal(nano); |
121 |
| - return dec.divide(new BigDecimal(1000000L)).setScale(1,RoundingMode.UP).toString(); |
| 91 | + protected String ms(long nano) { |
| 92 | + BigDecimal dec = new BigDecimal(nano); |
| 93 | + return dec.divide(new BigDecimal(1000000L)).setScale(1, RoundingMode.UP).toString(); |
122 | 94 | }
|
123 |
| - |
124 |
| - protected int width(long nano) |
125 |
| - { |
126 |
| - int w=(int)((nano+999999L)/5000000L); |
127 |
| - if (w==0) |
128 |
| - w=2; |
129 |
| - return w; |
| 95 | + |
| 96 | + protected int width(long nano) { |
| 97 | + int w = (int) ((nano + 999999L) / 5000000L); |
| 98 | + if (w == 0) |
| 99 | + w = 2; |
| 100 | + return w; |
130 | 101 | }
|
131 |
| - |
132 |
| - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
133 |
| - { |
134 |
| - doGet(request, response); |
| 102 | + |
| 103 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 104 | + throws ServletException, IOException { |
| 105 | + doGet(request, response); |
135 | 106 | }
|
136 | 107 |
|
137 | 108 | }
|
0 commit comments