Skip to content

Commit abe632e

Browse files
committed
Merge pull request #124 from GoogleCloudPlatform/csasync
Checkstyle fixes for async-rest sample.
2 parents 61455e7 + 0540a80 commit abe632e

File tree

5 files changed

+238
-111
lines changed

5 files changed

+238
-111
lines changed

managed_vms/async-rest/pom.xml

+37-21
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,66 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2016 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project>
218
<modelVersion>4.0.0</modelVersion>
319
<groupId>com.google.appengine.demos</groupId>
420
<artifactId>managed-vms-async-rest</artifactId>
521
<version>1.0.0-SNAPSHOT</version>
622
<packaging>war</packaging>
7-
<name>Example Async Rest Webapp</name>
23+
24+
<parent>
25+
<artifactId>doc-samples</artifactId>
26+
<groupId>com.google.cloud</groupId>
27+
<version>1.0.0</version>
28+
<relativePath>../..</relativePath>
29+
</parent>
30+
831
<properties>
9-
<places.appkey>YOUR_PLACES_APP_KEY</places.appkey>
10-
<jetty.version>9.3.7.v20160115</jetty.version>
1132
<docker.label.prefix>async-</docker.label.prefix>
33+
<jetty.version>9.3.7.v20160115</jetty.version>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
<places.appkey>YOUR_PLACES_APP_KEY</places.appkey>
1237
</properties>
1338
<build>
1439
<plugins>
15-
<plugin>
16-
<artifactId>maven-compiler-plugin</artifactId>
17-
<version>3.3</version>
18-
<configuration>
19-
<source>1.8</source>
20-
<target>1.8</target>
21-
</configuration>
22-
</plugin>
2340
<plugin>
2441
<groupId>org.eclipse.jetty</groupId>
2542
<artifactId>jetty-maven-plugin</artifactId>
2643
<version>${jetty.version}</version>
2744
<configuration>
2845
<systemProperties>
29-
<systemProperty>
46+
<systemProperty>
3047
<name>com.google.appengine.demos.asyncrest.appKey</name>
3148
<value>${places.appkey}</value>
32-
</systemProperty>
49+
</systemProperty>
3350
</systemProperties>
3451
</configuration>
3552
</plugin>
3653
<plugin>
3754
<groupId>com.google.appengine</groupId>
3855
<artifactId>gcloud-maven-plugin</artifactId>
39-
<version>2.0.9.96.v20160203</version>
56+
<version>2.0.9.95.v20160203</version>
4057
<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>
58+
<verbosity>debug</verbosity>
59+
<log_level>debug</log_level>
60+
<non_docker_mode>false</non_docker_mode>
61+
<docker_build>remote</docker_build>
4662
</configuration>
47-
</plugin>
63+
</plugin>
4864
<plugin>
4965
<groupId>org.apache.maven.plugins</groupId>
5066
<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)