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+
117package com .google .appengine .demos .asyncrest ;
218
319import java .io .IOException ;
1632import javax .servlet .http .HttpServletResponse ;
1733
1834/**
19- * AbstractRestServlet.
20- *
35+ * Abstract base class for REST servlets.
2136 */
2237public class AbstractRestServlet extends HttpServlet {
2338
24- protected final static int MAX_RESULTS = 5 ;
39+ protected static final int MAX_RESULTS = 5 ;
2540
26- protected final static String STYLE = "<style type='text/css'>"
41+ protected static final String STYLE = "<style type='text/css'>"
2742 + " img.thumb:hover {height:50px}"
2843 + " img.thumb {vertical-align:text-top}"
2944 + " span.red {color: #ff0000}"
3045 + " span.green {color: #00ff00}"
3146 + " iframe {border: 0px}" + "</style>" ;
3247
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" ;
4055 protected String key ;
4156
4257 @ Override
4358 public void init (ServletConfig servletConfig ) throws ServletException {
44- //first try the servlet context init-param
59+ // First try the servlet context init-param.
4560 String source = "InitParameter" ;
4661 key = servletConfig .getInitParameter (APPKEY );
4762 if (key == null || key .startsWith ("${" )) {
@@ -60,18 +75,19 @@ public void init(ServletConfig servletConfig) throws ServletException {
6075 }
6176 }
6277
63- public static String sanitize (String s ) {
64- if (s == null ) {
78+ public static String sanitize (String str ) {
79+ if (str == null ) {
6580 return null ;
6681 }
67- return s .replace ("<" , "?" ).replace ("&" , "?" ).replace ("\n " , "?" );
82+ return str .replace ("<" , "?" ).replace ("&" , "?" ).replace ("\n " , "?" );
6883 }
6984
7085 protected String restQuery (String coordinates , String radius , String item ) {
7186 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" );
7591
7692 } catch (Exception e ) {
7793 throw new RuntimeException (e );
@@ -84,21 +100,22 @@ public String generateResults(Queue<Map<String, Object>> results) {
84100 Iterator <Map <String , Object >> itor = results .iterator ();
85101
86102 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" );
90106 if (photos != null && photos .length > 0 ) {
91107 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 + "' />" );
95112 thumbs .append ("</a> " );
96113 }
97114 }
98115 return thumbs .toString ();
99116 }
100117
101- public String getPhotoURL (String photoref ) {
118+ public String getPhotoUrl (String photoref ) {
102119 return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref
103120 + "&maxheight=40" ;
104121 }
@@ -109,11 +126,11 @@ protected String ms(long nano) {
109126 }
110127
111128 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 ;
115132 }
116- return w ;
133+ return width ;
117134 }
118135
119136 @ Override
0 commit comments