Skip to content

Commit c10a05a

Browse files
author
ludo
committed
Upgrade to 2.0.9.95.v20160203 and Java 8.
Reformat Java code. Made the Docker image step optional (this complicates the sample if you do not have Docker installed and started.
1 parent 7bfa1d7 commit c10a05a

File tree

6 files changed

+366
-360
lines changed

6 files changed

+366
-360
lines changed

managed_vms/async-rest/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
This web app demonstrates using asynchronous servlet techniques to reduce server resources.
55

6-
The code for this tutorial is here: [https://github.com/GoogleCloudPlatform/java-docs-samples/managedvms/async-rest](https://github.com/GoogleCloudPlatform/java-docs-samples/managedvms/async-rest).
6+
The code for this tutorial is here: [https://github.com/GoogleCloudPlatform/java-docs-samples/managed_vms/async-rest](https://github.com/GoogleCloudPlatform/java-docs-samples/managed_vms/async-rest).
77

88

99
## Initial Setup ##
@@ -38,7 +38,9 @@ Go to http://localhost:8080 to see the webapp.
3838

3939
## Running locally using Docker ##
4040

41-
The project also builds a docker image based on the jetty9 image for [Google Container Engine](https://cloud.google.com/container-engine/). The WAR file is installed in the webapps directory and the resulting image can be run locally with:
41+
The project also can build a docker image based on the jetty9 image for [Google Container Engine](https://cloud.google.com/container-engine/).
42+
First uncomment the maven plugin section for docker-maven-plugin in the pom.xml.
43+
The WAR file is installed in the webapps directory and the resulting image can be run locally with:
4244

4345
docker run --rm -it -p 8080:8080 jetty9-async-rest --exec -Dcom.google.appengine.demos.asyncrest.appKey=YOUR_PLACES_APP_KEY
4446

managed_vms/async-rest/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
<artifactId>maven-compiler-plugin</artifactId>
1717
<version>3.3</version>
1818
<configuration>
19-
<source>1.7</source>
20-
<target>1.7</target>
19+
<source>1.8</source>
20+
<target>1.8</target>
2121
</configuration>
2222
</plugin>
2323
<plugin>
@@ -36,7 +36,7 @@
3636
<plugin>
3737
<groupId>com.google.appengine</groupId>
3838
<artifactId>gcloud-maven-plugin</artifactId>
39-
<version>2.0.9.90.v20151210</version>
39+
<version>2.0.9.96.v20160203</version>
4040
<configuration>
4141
<gcloud_directory>/usr/local/google-cloud-sdk</gcloud_directory>
4242
<verbosity>debug</verbosity>
@@ -61,10 +61,10 @@
6161
</webResources>
6262
</configuration>
6363
</plugin>
64-
<plugin>
64+
<!--plugin>
6565
<groupId>com.spotify</groupId>
6666
<artifactId>docker-maven-plugin</artifactId>
67-
<version>0.3.2</version>
67+
<version>0.4.0</version>
6868
<executions>
6969
<execution>
7070
<id>build-docker-image</id>
@@ -102,7 +102,7 @@
102102
</configuration>
103103
</execution>
104104
</executions>
105-
</plugin>
105+
</plugin-->
106106
</plugins>
107107
</build>
108108
<dependencies>

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

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,51 @@
1010
import javax.servlet.http.HttpServletResponse;
1111

1212
@SuppressWarnings("serial")
13-
public class DumpServlet extends HttpServlet
14-
{
15-
@Override
16-
protected void doGet( HttpServletRequest request,
17-
HttpServletResponse response ) throws ServletException,
18-
IOException
19-
{
20-
response.setContentType("text/html");
21-
response.setStatus(HttpServletResponse.SC_OK);
13+
public class DumpServlet extends HttpServlet {
2214

23-
PrintWriter out = response.getWriter();
15+
@Override
16+
protected void doGet(HttpServletRequest request,
17+
HttpServletResponse response) throws ServletException,
18+
IOException {
19+
response.setContentType("text/html");
20+
response.setStatus(HttpServletResponse.SC_OK);
2421

25-
out.println("<h1>DumpServlet</h1>");
26-
out.println("<h2>Context Fields:</h2>");
27-
out.println("<pre>");
28-
out.printf("serverInfo=%s%n",getServletContext().getServerInfo());
29-
out.printf("getServletContextName=%s%n",getServletContext().getServletContextName());
30-
out.printf("virtualServerName=%s%n",getServletContext().getVirtualServerName());
31-
out.printf("contextPath=%s%n",getServletContext().getContextPath());
32-
out.printf("version=%d.%d%n",getServletContext().getMajorVersion(),getServletContext().getMinorVersion());
33-
out.printf("effectiveVersion=%d.%d%n",getServletContext().getEffectiveMajorVersion(),getServletContext().getEffectiveMinorVersion());
34-
out.println("</pre>");
35-
out.println("<h2>Request Fields:</h2>");
36-
out.println("<pre>");
37-
out.printf("remoteHost/Addr:port=%s/%s:%d%n",request.getRemoteHost(),request.getRemoteAddr(),request.getRemotePort());
38-
out.printf("localName/Addr:port=%s/%s:%d%n",request.getLocalName(),request.getLocalAddr(),request.getLocalPort());
39-
out.printf("scheme=%s method=%s protocol=%s%n",request.getScheme(), request.getMethod(), request.getProtocol());
40-
out.printf("serverName:serverPort=%s:%d%n",request.getServerName(),request.getServerPort());
41-
out.printf("requestURI=%s%n",request.getRequestURI());
42-
out.printf("requestURL=%s%n",request.getRequestURL().toString());
43-
out.printf("contextPath|servletPath|pathInfo=%s|%s|%s%n",request.getContextPath(),request.getServletPath(),request.getPathInfo());
44-
out.printf("session/new=%s/%b%n",request.getSession(true).getId(),request.getSession().isNew());
45-
out.println("</pre>");
46-
out.println("<h2>Request Headers:</h2>");
47-
out.println("<pre>");
48-
for (String n : Collections.list(request.getHeaderNames()))
49-
for (String v : Collections.list(request.getHeaders(n)))
50-
out.printf("%s: %s%n",n,v);
51-
out.println("</pre>");
52-
out.println("<h2>Response Fields:</h2>");
53-
out.println("<pre>");
54-
out.printf("bufferSize=%d%n",response.getBufferSize());
55-
out.printf("encodedURL(\"/foo/bar\")=%s%n",response.encodeURL("/foo/bar"));
56-
out.printf("encodedRedirectURL(\"/foo/bar\")=%s%n",response.encodeRedirectURL("/foo/bar"));
57-
out.println("</pre>");
22+
PrintWriter out = response.getWriter();
23+
24+
out.println("<h1>DumpServlet</h1>");
25+
out.println("<h2>Context Fields:</h2>");
26+
out.println("<pre>");
27+
out.printf("serverInfo=%s%n", getServletContext().getServerInfo());
28+
out.printf("getServletContextName=%s%n", getServletContext().getServletContextName());
29+
out.printf("virtualServerName=%s%n", getServletContext().getVirtualServerName());
30+
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());
33+
out.println("</pre>");
34+
out.println("<h2>Request Fields:</h2>");
35+
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());
39+
out.printf("serverName:serverPort=%s:%d%n", request.getServerName(), request.getServerPort());
40+
out.printf("requestURI=%s%n", request.getRequestURI());
41+
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());
44+
out.println("</pre>");
45+
out.println("<h2>Request Headers:</h2>");
46+
out.println("<pre>");
47+
for (String n : Collections.list(request.getHeaderNames())) {
48+
for (String v : Collections.list(request.getHeaders(n))) {
49+
out.printf("%s: %s%n", n, v);
50+
}
5851
}
52+
out.println("</pre>");
53+
out.println("<h2>Response Fields:</h2>");
54+
out.println("<pre>");
55+
out.printf("bufferSize=%d%n", response.getBufferSize());
56+
out.printf("encodedURL(\"/foo/bar\")=%s%n", response.encodeURL("/foo/bar"));
57+
out.printf("encodedRedirectURL(\"/foo/bar\")=%s%n", response.encodeRedirectURL("/foo/bar"));
58+
out.println("</pre>");
59+
}
5960
}

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

Lines changed: 91 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,105 +15,110 @@
1515
import javax.servlet.http.HttpServletRequest;
1616
import javax.servlet.http.HttpServletResponse;
1717

18-
/**
18+
/**
1919
* AbstractRestServlet.
2020
*
2121
*/
2222
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-
}
5923

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);
6450
}
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);
7554
}
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>&nbsp;");
92-
}
93-
}
94-
return thumbs.toString();
55+
if (key == null) {
56+
throw new UnavailableException("Places App Key not set");
9557
}
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);
10060
}
61+
}
10162

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;
10566
}
67+
return s.replace("<", "?").replace("&", "?").replace("\n", "?");
68+
}
10669

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");
11375

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>&nbsp;");
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;
117115
}
116+
return w;
117+
}
118+
119+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
120+
throws ServletException, IOException {
121+
doGet(request, response);
122+
}
118123

119124
}

0 commit comments

Comments
 (0)