Skip to content

Commit 486352c

Browse files
committed
Merge pull request #84 from GoogleCloudPlatform/ludo-samples
Move samples added in managedvms to managed_vms
2 parents a732ac5 + e5d52ed commit 486352c

File tree

32 files changed

+504
-494
lines changed

32 files changed

+504
-494
lines changed
File renamed without changes.
File renamed without changes.

managedvms/async-rest/README.md renamed to managed_vms/async-rest/README.md

+4-2
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

managedvms/async-rest/pom.xml renamed to managed_vms/async-rest/pom.xml

+6-6
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.google.appengine.demos;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.util.Collections;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
@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+
response.setContentType("text/html");
20+
response.setStatus(HttpServletResponse.SC_OK);
21+
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+
}
51+
}
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+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.google.appengine.demos.asyncrest;
2+
3+
import java.io.IOException;
4+
import java.math.BigDecimal;
5+
import java.math.RoundingMode;
6+
import java.net.URLEncoder;
7+
import java.util.Iterator;
8+
import java.util.Map;
9+
import java.util.Queue;
10+
11+
import javax.servlet.ServletConfig;
12+
import javax.servlet.ServletException;
13+
import javax.servlet.UnavailableException;
14+
import javax.servlet.http.HttpServlet;
15+
import javax.servlet.http.HttpServletRequest;
16+
import javax.servlet.http.HttpServletResponse;
17+
18+
/**
19+
* AbstractRestServlet.
20+
*
21+
*/
22+
public class AbstractRestServlet extends HttpServlet {
23+
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);
50+
}
51+
if (key == null || key.startsWith("${")) {
52+
source = "Environment Variable";
53+
key = System.getenv(APPKEY_ENV);
54+
}
55+
if (key == null) {
56+
throw new UnavailableException("Places App Key not set");
57+
}
58+
if (key.startsWith("${")) {
59+
throw new UnavailableException("Places App Key not expanded from " + source);
60+
}
61+
}
62+
63+
public static String sanitize(String s) {
64+
if (s == null) {
65+
return null;
66+
}
67+
return s.replace("<", "?").replace("&", "?").replace("\n", "?");
68+
}
69+
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");
75+
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;
115+
}
116+
return w;
117+
}
118+
119+
@Override
120+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
121+
throws ServletException, IOException {
122+
doGet(request, response);
123+
}
124+
125+
}

0 commit comments

Comments
 (0)