|
15 | 15 | import javax.servlet.http.HttpServletRequest;
|
16 | 16 | import javax.servlet.http.HttpServletResponse;
|
17 | 17 |
|
18 |
| -/** |
| 18 | +/** |
19 | 19 | * AbstractRestServlet.
|
20 | 20 | *
|
21 | 21 | */
|
22 | 22 | public class AbstractRestServlet extends HttpServlet {
|
23 |
| - protected final static int MAX_RESULTS = 5; |
24 |
| - |
25 |
| - protected final static String STYLE = "<style type='text/css'>" |
26 |
| - + " img.thumb:hover {height:50px}" |
27 |
| - + " img.thumb {vertical-align:text-top}" |
28 |
| - + " span.red {color: #ff0000}" |
29 |
| - + " span.green {color: #00ff00}" |
30 |
| - + " iframe {border: 0px}" + "</style>"; |
31 |
| - |
32 |
| - protected final static String APPKEY = "com.google.appengine.demos.asyncrest.appKey"; |
33 |
| - protected final static String APPKEY_ENV = "PLACES_APPKEY"; |
34 |
| - protected final static String LOC_PARAM = "loc"; |
35 |
| - protected final static String ITEMS_PARAM = "items"; |
36 |
| - protected final static String LATITUDE_PARAM = "lat"; |
37 |
| - protected final static String LONGITUDE_PARAM = "long"; |
38 |
| - protected final static String RADIUS_PARAM = "radius"; |
39 |
| - protected String key; |
40 |
| - |
41 |
| - @Override |
42 |
| - public void init(ServletConfig servletConfig) throws ServletException { |
43 |
| - //first try the servlet context init-param |
44 |
| - String source="InitParameter"; |
45 |
| - key = servletConfig.getInitParameter(APPKEY); |
46 |
| - if (key==null || key.startsWith("${")) { |
47 |
| - source="System Property"; |
48 |
| - key = System.getProperty(APPKEY); |
49 |
| - } |
50 |
| - if (key == null || key.startsWith("${")) { |
51 |
| - source="Environment Variable"; |
52 |
| - key = System.getenv(APPKEY_ENV); |
53 |
| - } |
54 |
| - if (key == null) |
55 |
| - throw new UnavailableException("Places App Key not set"); |
56 |
| - if (key.startsWith("${")) |
57 |
| - throw new UnavailableException("Places App Key not expanded from "+source); |
58 |
| - } |
59 | 23 |
|
60 |
| - public static String sanitize(String s) { |
61 |
| - if (s == null) |
62 |
| - return null; |
63 |
| - return s.replace("<", "?").replace("&", "?").replace("\n", "?"); |
| 24 | + protected final static int MAX_RESULTS = 5; |
| 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>"; |
| 32 | + |
| 33 | + protected final static String APPKEY = "com.google.appengine.demos.asyncrest.appKey"; |
| 34 | + protected final static String APPKEY_ENV = "PLACES_APPKEY"; |
| 35 | + protected final static String LOC_PARAM = "loc"; |
| 36 | + protected final static String ITEMS_PARAM = "items"; |
| 37 | + protected final static String LATITUDE_PARAM = "lat"; |
| 38 | + protected final static String LONGITUDE_PARAM = "long"; |
| 39 | + protected final static String RADIUS_PARAM = "radius"; |
| 40 | + protected String key; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void init(ServletConfig servletConfig) throws ServletException { |
| 44 | + //first try the servlet context init-param |
| 45 | + String source = "InitParameter"; |
| 46 | + key = servletConfig.getInitParameter(APPKEY); |
| 47 | + if (key == null || key.startsWith("${")) { |
| 48 | + source = "System Property"; |
| 49 | + key = System.getProperty(APPKEY); |
64 | 50 | }
|
65 |
| - |
66 |
| - protected String restQuery(String coordinates, String radius, String item) { |
67 |
| - try { |
68 |
| - return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=" + key + "&location=" |
69 |
| - + URLEncoder.encode(coordinates, "UTF-8") + "&types=" + URLEncoder.encode(item, "UTF-8") |
70 |
| - + "&radius=" + URLEncoder.encode(radius, "UTF-8"); |
71 |
| - |
72 |
| - } catch (Exception e) { |
73 |
| - throw new RuntimeException(e); |
74 |
| - } |
| 51 | + if (key == null || key.startsWith("${")) { |
| 52 | + source = "Environment Variable"; |
| 53 | + key = System.getenv(APPKEY_ENV); |
75 | 54 | }
|
76 |
| - |
77 |
| - public String generateResults(Queue<Map<String, Object>> results) { |
78 |
| - StringBuilder thumbs = new StringBuilder(); |
79 |
| - int resultCount = 0; |
80 |
| - Iterator<Map<String, Object>> itor = results.iterator(); |
81 |
| - |
82 |
| - while (resultCount < MAX_RESULTS && itor.hasNext()) { |
83 |
| - Map m = (Map) itor.next(); |
84 |
| - String name = (String) m.get("name"); |
85 |
| - Object[] photos = (Object[]) m.get("photos"); |
86 |
| - if (photos != null && photos.length > 0) { |
87 |
| - resultCount++; |
88 |
| - thumbs.append("<img class='thumb' border='1px' height='40px'" + " src='" |
89 |
| - + getPhotoURL((String) (((Map) photos[0]).get("photo_reference"))) + "'" + " title='" + name |
90 |
| - + "'" + "/>"); |
91 |
| - thumbs.append("</a> "); |
92 |
| - } |
93 |
| - } |
94 |
| - return thumbs.toString(); |
| 55 | + if (key == null) { |
| 56 | + throw new UnavailableException("Places App Key not set"); |
95 | 57 | }
|
96 |
| - |
97 |
| - public String getPhotoURL(String photoref) { |
98 |
| - return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref |
99 |
| - + "&maxheight=40"; |
| 58 | + if (key.startsWith("${")) { |
| 59 | + throw new UnavailableException("Places App Key not expanded from " + source); |
100 | 60 | }
|
| 61 | + } |
101 | 62 |
|
102 |
| - protected String ms(long nano) { |
103 |
| - BigDecimal dec = new BigDecimal(nano); |
104 |
| - return dec.divide(new BigDecimal(1000000L)).setScale(1, RoundingMode.UP).toString(); |
| 63 | + public static String sanitize(String s) { |
| 64 | + if (s == null) { |
| 65 | + return null; |
105 | 66 | }
|
| 67 | + return s.replace("<", "?").replace("&", "?").replace("\n", "?"); |
| 68 | + } |
106 | 69 |
|
107 |
| - protected int width(long nano) { |
108 |
| - int w = (int) ((nano + 999999L) / 5000000L); |
109 |
| - if (w == 0) |
110 |
| - w = 2; |
111 |
| - return w; |
112 |
| - } |
| 70 | + protected String restQuery(String coordinates, String radius, String item) { |
| 71 | + try { |
| 72 | + return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=" + key + "&location=" |
| 73 | + + URLEncoder.encode(coordinates, "UTF-8") + "&types=" + URLEncoder.encode(item, "UTF-8") |
| 74 | + + "&radius=" + URLEncoder.encode(radius, "UTF-8"); |
113 | 75 |
|
114 |
| - protected void doPost(HttpServletRequest request, HttpServletResponse response) |
115 |
| - throws ServletException, IOException { |
116 |
| - doGet(request, response); |
| 76 | + } catch (Exception e) { |
| 77 | + throw new RuntimeException(e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public String generateResults(Queue<Map<String, Object>> results) { |
| 82 | + StringBuilder thumbs = new StringBuilder(); |
| 83 | + int resultCount = 0; |
| 84 | + Iterator<Map<String, Object>> itor = results.iterator(); |
| 85 | + |
| 86 | + while (resultCount < MAX_RESULTS && itor.hasNext()) { |
| 87 | + Map m = (Map) itor.next(); |
| 88 | + String name = (String) m.get("name"); |
| 89 | + Object[] photos = (Object[]) m.get("photos"); |
| 90 | + if (photos != null && photos.length > 0) { |
| 91 | + resultCount++; |
| 92 | + thumbs.append("<img class='thumb' border='1px' height='40px'" + " src='" |
| 93 | + + getPhotoURL((String) (((Map) photos[0]).get("photo_reference"))) + "'" + " title='" + name |
| 94 | + + "'" + "/>"); |
| 95 | + thumbs.append("</a> "); |
| 96 | + } |
| 97 | + } |
| 98 | + return thumbs.toString(); |
| 99 | + } |
| 100 | + |
| 101 | + public String getPhotoURL(String photoref) { |
| 102 | + return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref |
| 103 | + + "&maxheight=40"; |
| 104 | + } |
| 105 | + |
| 106 | + protected String ms(long nano) { |
| 107 | + BigDecimal dec = new BigDecimal(nano); |
| 108 | + return dec.divide(new BigDecimal(1000000L)).setScale(1, RoundingMode.UP).toString(); |
| 109 | + } |
| 110 | + |
| 111 | + protected int width(long nano) { |
| 112 | + int w = (int) ((nano + 999999L) / 5000000L); |
| 113 | + if (w == 0) { |
| 114 | + w = 2; |
117 | 115 | }
|
| 116 | + return w; |
| 117 | + } |
| 118 | + |
| 119 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 120 | + throws ServletException, IOException { |
| 121 | + doGet(request, response); |
| 122 | + } |
118 | 123 |
|
119 | 124 | }
|
0 commit comments