1
+ /*
2
+ * Copyright 2016 Google Inc. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
1
17
package com .google .appengine .demos .asyncrest ;
2
18
3
19
import java .io .IOException ;
16
32
import javax .servlet .http .HttpServletResponse ;
17
33
18
34
/**
19
- * AbstractRestServlet.
20
- *
35
+ * Abstract base class for REST servlets.
21
36
*/
22
37
public class AbstractRestServlet extends HttpServlet {
23
38
24
- protected final static int MAX_RESULTS = 5 ;
39
+ protected static final int MAX_RESULTS = 5 ;
25
40
26
- protected final static String STYLE = "<style type='text/css'>"
41
+ protected static final String STYLE = "<style type='text/css'>"
27
42
+ " img.thumb:hover {height:50px}"
28
43
+ " img.thumb {vertical-align:text-top}"
29
44
+ " span.red {color: #ff0000}"
30
45
+ " span.green {color: #00ff00}"
31
46
+ " iframe {border: 0px}" + "</style>" ;
32
47
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" ;
48
+ protected static final String APPKEY = "com.google.appengine.demos.asyncrest.appKey" ;
49
+ protected static final String APPKEY_ENV = "PLACES_APPKEY" ;
50
+ protected static final String LOC_PARAM = "loc" ;
51
+ protected static final String ITEMS_PARAM = "items" ;
52
+ protected static final String LATITUDE_PARAM = "lat" ;
53
+ protected static final String LONGITUDE_PARAM = "long" ;
54
+ protected static final String RADIUS_PARAM = "radius" ;
40
55
protected String key ;
41
56
42
57
@ Override
43
58
public void init (ServletConfig servletConfig ) throws ServletException {
44
- //first try the servlet context init-param
59
+ // First try the servlet context init-param.
45
60
String source = "InitParameter" ;
46
61
key = servletConfig .getInitParameter (APPKEY );
47
62
if (key == null || key .startsWith ("${" )) {
@@ -60,18 +75,19 @@ public void init(ServletConfig servletConfig) throws ServletException {
60
75
}
61
76
}
62
77
63
- public static String sanitize (String s ) {
64
- if (s == null ) {
78
+ public static String sanitize (String str ) {
79
+ if (str == null ) {
65
80
return null ;
66
81
}
67
- return s .replace ("<" , "?" ).replace ("&" , "?" ).replace ("\n " , "?" );
82
+ return str .replace ("<" , "?" ).replace ("&" , "?" ).replace ("\n " , "?" );
68
83
}
69
84
70
85
protected String restQuery (String coordinates , String radius , String item ) {
71
86
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" );
87
+ return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=" + key
88
+ + "&location=" + URLEncoder .encode (coordinates , "UTF-8" )
89
+ + "&types=" + URLEncoder .encode (item , "UTF-8" )
90
+ + "&radius=" + URLEncoder .encode (radius , "UTF-8" );
75
91
76
92
} catch (Exception e ) {
77
93
throw new RuntimeException (e );
@@ -84,21 +100,22 @@ public String generateResults(Queue<Map<String, Object>> results) {
84
100
Iterator <Map <String , Object >> itor = results .iterator ();
85
101
86
102
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" );
103
+ Map map = (Map ) itor .next ();
104
+ String name = (String ) map .get ("name" );
105
+ Object [] photos = (Object []) map .get ("photos" );
90
106
if (photos != null && photos .length > 0 ) {
91
107
resultCount ++;
92
- thumbs .append ("<img class='thumb' border='1px' height='40px'" + " src='"
93
- + getPhotoURL ((String ) (((Map ) photos [0 ]).get ("photo_reference" ))) + "'" + " title='" + name
94
- + "'" + "/>" );
108
+ thumbs .append (
109
+ "<img class='thumb' border='1px' height='40px' "
110
+ + "src='" + getPhotoUrl ((String ) (((Map ) photos [0 ]).get ("photo_reference" ))) + "' "
111
+ + "title='" + name + "' />" );
95
112
thumbs .append ("</a> " );
96
113
}
97
114
}
98
115
return thumbs .toString ();
99
116
}
100
117
101
- public String getPhotoURL (String photoref ) {
118
+ public String getPhotoUrl (String photoref ) {
102
119
return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref
103
120
+ "&maxheight=40" ;
104
121
}
@@ -109,11 +126,11 @@ protected String ms(long nano) {
109
126
}
110
127
111
128
protected int width (long nano ) {
112
- int w = (int ) ((nano + 999999L ) / 5000000L );
113
- if (w == 0 ) {
114
- w = 2 ;
129
+ int width = (int ) ((nano + 999999L ) / 5000000L );
130
+ if (width == 0 ) {
131
+ width = 2 ;
115
132
}
116
- return w ;
133
+ return width ;
117
134
}
118
135
119
136
@ Override
0 commit comments