Skip to content

Commit 0f33dac

Browse files
committed
Checkstyle fixes for async-rest sample.
Tested manually by deploying to http://async-rest-test-dot-java-docs-samples-swast-1.appspot.com/. When doing so, I noticed that the `mvn gcloud:deploy` command would not run, so I updated the POM to the latest version of the gcloud maven plugin, too.
1 parent a53f0cb commit 0f33dac

File tree

5 files changed

+217
-100
lines changed

5 files changed

+217
-100
lines changed

managed_vms/async-rest/pom.xml

+16-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
<artifactId>managed-vms-async-rest</artifactId>
55
<version>1.0.0-SNAPSHOT</version>
66
<packaging>war</packaging>
7-
<name>Example Async Rest Webapp</name>
7+
8+
<parent>
9+
<artifactId>doc-samples</artifactId>
10+
<groupId>com.google.cloud</groupId>
11+
<version>1.0.0</version>
12+
<relativePath>../..</relativePath>
13+
</parent>
14+
815
<properties>
916
<places.appkey>YOUR_PLACES_APP_KEY</places.appkey>
1017
<jetty.version>9.3.7.v20160115</jetty.version>
@@ -26,25 +33,24 @@
2633
<version>${jetty.version}</version>
2734
<configuration>
2835
<systemProperties>
29-
<systemProperty>
36+
<systemProperty>
3037
<name>com.google.appengine.demos.asyncrest.appKey</name>
3138
<value>${places.appkey}</value>
32-
</systemProperty>
39+
</systemProperty>
3340
</systemProperties>
3441
</configuration>
3542
</plugin>
3643
<plugin>
3744
<groupId>com.google.appengine</groupId>
3845
<artifactId>gcloud-maven-plugin</artifactId>
39-
<version>2.0.9.96.v20160203</version>
46+
<version>2.0.9.95.v20160203</version>
4047
<configuration>
41-
<gcloud_directory>/usr/local/google-cloud-sdk</gcloud_directory>
42-
<verbosity>debug</verbosity>
43-
<log_level>debug</log_level>
44-
<non_docker_mode>false</non_docker_mode>
45-
<docker_build>remote</docker_build>
48+
<verbosity>debug</verbosity>
49+
<log_level>debug</log_level>
50+
<non_docker_mode>false</non_docker_mode>
51+
<docker_build>remote</docker_build>
4652
</configuration>
47-
</plugin>
53+
</plugin>
4854
<plugin>
4955
<groupId>org.apache.maven.plugins</groupId>
5056
<artifactId>maven-war-plugin</artifactId>

managed_vms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java

+48-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package com.google.appengine.demos;
218

319
import java.io.IOException;
@@ -13,9 +29,8 @@
1329
public class DumpServlet extends HttpServlet {
1430

1531
@Override
16-
protected void doGet(HttpServletRequest request,
17-
HttpServletResponse response) throws ServletException,
18-
IOException {
32+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
33+
throws ServletException, IOException {
1934
response.setContentType("text/html");
2035
response.setStatus(HttpServletResponse.SC_OK);
2136

@@ -28,19 +43,42 @@ protected void doGet(HttpServletRequest request,
2843
out.printf("getServletContextName=%s%n", getServletContext().getServletContextName());
2944
out.printf("virtualServerName=%s%n", getServletContext().getVirtualServerName());
3045
out.printf("contextPath=%s%n", getServletContext().getContextPath());
31-
out.printf("version=%d.%d%n", getServletContext().getMajorVersion(), getServletContext().getMinorVersion());
32-
out.printf("effectiveVersion=%d.%d%n", getServletContext().getEffectiveMajorVersion(), getServletContext().getEffectiveMinorVersion());
46+
out.printf(
47+
"version=%d.%d%n",
48+
getServletContext().getMajorVersion(),
49+
getServletContext().getMinorVersion());
50+
out.printf(
51+
"effectiveVersion=%d.%d%n",
52+
getServletContext().getEffectiveMajorVersion(),
53+
getServletContext().getEffectiveMinorVersion());
3354
out.println("</pre>");
3455
out.println("<h2>Request Fields:</h2>");
3556
out.println("<pre>");
36-
out.printf("remoteHost/Addr:port=%s/%s:%d%n", request.getRemoteHost(), request.getRemoteAddr(), request.getRemotePort());
37-
out.printf("localName/Addr:port=%s/%s:%d%n", request.getLocalName(), request.getLocalAddr(), request.getLocalPort());
38-
out.printf("scheme=%s method=%s protocol=%s%n", request.getScheme(), request.getMethod(), request.getProtocol());
57+
out.printf(
58+
"remoteHost/Addr:port=%s/%s:%d%n",
59+
request.getRemoteHost(),
60+
request.getRemoteAddr(),
61+
request.getRemotePort());
62+
out.printf(
63+
"localName/Addr:port=%s/%s:%d%n",
64+
request.getLocalName(),
65+
request.getLocalAddr(),
66+
request.getLocalPort());
67+
out.printf(
68+
"scheme=%s method=%s protocol=%s%n",
69+
request.getScheme(),
70+
request.getMethod(),
71+
request.getProtocol());
3972
out.printf("serverName:serverPort=%s:%d%n", request.getServerName(), request.getServerPort());
4073
out.printf("requestURI=%s%n", request.getRequestURI());
4174
out.printf("requestURL=%s%n", request.getRequestURL().toString());
42-
out.printf("contextPath|servletPath|pathInfo=%s|%s|%s%n", request.getContextPath(), request.getServletPath(), request.getPathInfo());
43-
out.printf("session/new=%s/%b%n", request.getSession(true).getId(), request.getSession().isNew());
75+
out.printf(
76+
"contextPath|servletPath|pathInfo=%s|%s|%s%n",
77+
request.getContextPath(),
78+
request.getServletPath(),
79+
request.getPathInfo());
80+
out.printf(
81+
"session/new=%s/%b%n", request.getSession(true).getId(), request.getSession().isNew());
4482
out.println("</pre>");
4583
out.println("<h2>Request Headers:</h2>");
4684
out.println("<pre>");

managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java

+46-29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package com.google.appengine.demos.asyncrest;
218

319
import java.io.IOException;
@@ -16,32 +32,31 @@
1632
import javax.servlet.http.HttpServletResponse;
1733

1834
/**
19-
* AbstractRestServlet.
20-
*
35+
* Abstract base class for REST servlets.
2136
*/
2237
public 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>&nbsp;");
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

Comments
 (0)