From 7bfa1d7db66f47538d3c08ba3eb8e6cd9c1f3d1c Mon Sep 17 00:00:00 2001 From: ludovic Champenois Date: Fri, 5 Feb 2016 14:15:28 -0800 Subject: [PATCH 1/4] Move samples added in managedvms to managed_vms --- {managedvms => managed_vms}/.gitignore | 0 {managedvms => managed_vms}/async-rest/LICENSE | 0 {managedvms => managed_vms}/async-rest/README.md | 0 {managedvms => managed_vms}/async-rest/pom.xml | 0 .../com/google/appengine/demos/DumpServlet.java | 0 .../demos/asyncrest/AbstractRestServlet.java | 0 .../appengine/demos/asyncrest/AsyncRestServlet.java | 0 .../demos/asyncrest/SerialRestServlet.java | 0 .../async-rest/src/main/webapp/Dockerfile | 0 .../src/main/webapp/WEB-INF/appengine-web.xml | 0 .../src/main/webapp/WEB-INF/jetty-web.xml | 0 .../async-rest/src/main/webapp/WEB-INF/web.xml | 0 .../async-rest/src/main/webapp/asyncrest/green.png | Bin .../async-rest/src/main/webapp/asyncrest/red.png | Bin .../async-rest/src/main/webapp/index.html | 0 {managedvms => managed_vms}/sparkjava/README.md | 0 {managedvms => managed_vms}/sparkjava/pom.xml | 0 .../sparkjava/src/main/appengine/Dockerfile | 0 .../sparkjava/src/main/appengine/app.yaml | 0 .../java/com/google/appengine/sparkdemo/Main.java | 0 .../google/appengine/sparkdemo/ResponseError.java | 0 .../java/com/google/appengine/sparkdemo/User.java | 0 .../google/appengine/sparkdemo/UserController.java | 0 .../com/google/appengine/sparkdemo/UserService.java | 0 .../sparkjava/src/main/resources/public/index.html | 0 .../appengine/sparkdemo/UserControllerTest.java | 0 .../google/appengine/sparkdemo/UserServiceTest.java | 0 27 files changed, 0 insertions(+), 0 deletions(-) rename {managedvms => managed_vms}/.gitignore (100%) rename {managedvms => managed_vms}/async-rest/LICENSE (100%) rename {managedvms => managed_vms}/async-rest/README.md (100%) rename {managedvms => managed_vms}/async-rest/pom.xml (100%) rename {managedvms => managed_vms}/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java (100%) rename {managedvms => managed_vms}/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java (100%) rename {managedvms => managed_vms}/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java (100%) rename {managedvms => managed_vms}/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java (100%) rename {managedvms => managed_vms}/async-rest/src/main/webapp/Dockerfile (100%) rename {managedvms => managed_vms}/async-rest/src/main/webapp/WEB-INF/appengine-web.xml (100%) rename {managedvms => managed_vms}/async-rest/src/main/webapp/WEB-INF/jetty-web.xml (100%) rename {managedvms => managed_vms}/async-rest/src/main/webapp/WEB-INF/web.xml (100%) rename {managedvms => managed_vms}/async-rest/src/main/webapp/asyncrest/green.png (100%) rename {managedvms => managed_vms}/async-rest/src/main/webapp/asyncrest/red.png (100%) rename {managedvms => managed_vms}/async-rest/src/main/webapp/index.html (100%) rename {managedvms => managed_vms}/sparkjava/README.md (100%) rename {managedvms => managed_vms}/sparkjava/pom.xml (100%) rename {managedvms => managed_vms}/sparkjava/src/main/appengine/Dockerfile (100%) rename {managedvms => managed_vms}/sparkjava/src/main/appengine/app.yaml (100%) rename {managedvms => managed_vms}/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java (100%) rename {managedvms => managed_vms}/sparkjava/src/main/java/com/google/appengine/sparkdemo/ResponseError.java (100%) rename {managedvms => managed_vms}/sparkjava/src/main/java/com/google/appengine/sparkdemo/User.java (100%) rename {managedvms => managed_vms}/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java (100%) rename {managedvms => managed_vms}/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java (100%) rename {managedvms => managed_vms}/sparkjava/src/main/resources/public/index.html (100%) rename {managedvms => managed_vms}/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java (100%) rename {managedvms => managed_vms}/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java (100%) diff --git a/managedvms/.gitignore b/managed_vms/.gitignore similarity index 100% rename from managedvms/.gitignore rename to managed_vms/.gitignore diff --git a/managedvms/async-rest/LICENSE b/managed_vms/async-rest/LICENSE similarity index 100% rename from managedvms/async-rest/LICENSE rename to managed_vms/async-rest/LICENSE diff --git a/managedvms/async-rest/README.md b/managed_vms/async-rest/README.md similarity index 100% rename from managedvms/async-rest/README.md rename to managed_vms/async-rest/README.md diff --git a/managedvms/async-rest/pom.xml b/managed_vms/async-rest/pom.xml similarity index 100% rename from managedvms/async-rest/pom.xml rename to managed_vms/async-rest/pom.xml diff --git a/managedvms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java similarity index 100% rename from managedvms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java rename to managed_vms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java diff --git a/managedvms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java similarity index 100% rename from managedvms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java rename to managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java diff --git a/managedvms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java similarity index 100% rename from managedvms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java rename to managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java diff --git a/managedvms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java similarity index 100% rename from managedvms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java rename to managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java diff --git a/managedvms/async-rest/src/main/webapp/Dockerfile b/managed_vms/async-rest/src/main/webapp/Dockerfile similarity index 100% rename from managedvms/async-rest/src/main/webapp/Dockerfile rename to managed_vms/async-rest/src/main/webapp/Dockerfile diff --git a/managedvms/async-rest/src/main/webapp/WEB-INF/appengine-web.xml b/managed_vms/async-rest/src/main/webapp/WEB-INF/appengine-web.xml similarity index 100% rename from managedvms/async-rest/src/main/webapp/WEB-INF/appengine-web.xml rename to managed_vms/async-rest/src/main/webapp/WEB-INF/appengine-web.xml diff --git a/managedvms/async-rest/src/main/webapp/WEB-INF/jetty-web.xml b/managed_vms/async-rest/src/main/webapp/WEB-INF/jetty-web.xml similarity index 100% rename from managedvms/async-rest/src/main/webapp/WEB-INF/jetty-web.xml rename to managed_vms/async-rest/src/main/webapp/WEB-INF/jetty-web.xml diff --git a/managedvms/async-rest/src/main/webapp/WEB-INF/web.xml b/managed_vms/async-rest/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from managedvms/async-rest/src/main/webapp/WEB-INF/web.xml rename to managed_vms/async-rest/src/main/webapp/WEB-INF/web.xml diff --git a/managedvms/async-rest/src/main/webapp/asyncrest/green.png b/managed_vms/async-rest/src/main/webapp/asyncrest/green.png similarity index 100% rename from managedvms/async-rest/src/main/webapp/asyncrest/green.png rename to managed_vms/async-rest/src/main/webapp/asyncrest/green.png diff --git a/managedvms/async-rest/src/main/webapp/asyncrest/red.png b/managed_vms/async-rest/src/main/webapp/asyncrest/red.png similarity index 100% rename from managedvms/async-rest/src/main/webapp/asyncrest/red.png rename to managed_vms/async-rest/src/main/webapp/asyncrest/red.png diff --git a/managedvms/async-rest/src/main/webapp/index.html b/managed_vms/async-rest/src/main/webapp/index.html similarity index 100% rename from managedvms/async-rest/src/main/webapp/index.html rename to managed_vms/async-rest/src/main/webapp/index.html diff --git a/managedvms/sparkjava/README.md b/managed_vms/sparkjava/README.md similarity index 100% rename from managedvms/sparkjava/README.md rename to managed_vms/sparkjava/README.md diff --git a/managedvms/sparkjava/pom.xml b/managed_vms/sparkjava/pom.xml similarity index 100% rename from managedvms/sparkjava/pom.xml rename to managed_vms/sparkjava/pom.xml diff --git a/managedvms/sparkjava/src/main/appengine/Dockerfile b/managed_vms/sparkjava/src/main/appengine/Dockerfile similarity index 100% rename from managedvms/sparkjava/src/main/appengine/Dockerfile rename to managed_vms/sparkjava/src/main/appengine/Dockerfile diff --git a/managedvms/sparkjava/src/main/appengine/app.yaml b/managed_vms/sparkjava/src/main/appengine/app.yaml similarity index 100% rename from managedvms/sparkjava/src/main/appengine/app.yaml rename to managed_vms/sparkjava/src/main/appengine/app.yaml diff --git a/managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java similarity index 100% rename from managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java rename to managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java diff --git a/managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/ResponseError.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/ResponseError.java similarity index 100% rename from managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/ResponseError.java rename to managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/ResponseError.java diff --git a/managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/User.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/User.java similarity index 100% rename from managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/User.java rename to managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/User.java diff --git a/managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java similarity index 100% rename from managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java rename to managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java diff --git a/managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java similarity index 100% rename from managedvms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java rename to managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java diff --git a/managedvms/sparkjava/src/main/resources/public/index.html b/managed_vms/sparkjava/src/main/resources/public/index.html similarity index 100% rename from managedvms/sparkjava/src/main/resources/public/index.html rename to managed_vms/sparkjava/src/main/resources/public/index.html diff --git a/managedvms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java similarity index 100% rename from managedvms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java rename to managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java diff --git a/managedvms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java similarity index 100% rename from managedvms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java rename to managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java From c10a05adba700a3d4e476d6ebac6b06b431ed1de Mon Sep 17 00:00:00 2001 From: ludo Date: Fri, 5 Feb 2016 14:30:33 -0800 Subject: [PATCH 2/4] 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. --- managed_vms/async-rest/README.md | 6 +- managed_vms/async-rest/pom.xml | 12 +- .../google/appengine/demos/DumpServlet.java | 87 ++--- .../demos/asyncrest/AbstractRestServlet.java | 177 ++++++----- .../demos/asyncrest/AsyncRestServlet.java | 297 +++++++++--------- .../demos/asyncrest/SerialRestServlet.java | 147 +++++---- 6 files changed, 366 insertions(+), 360 deletions(-) diff --git a/managed_vms/async-rest/README.md b/managed_vms/async-rest/README.md index 6f949bd70bc..c4162700843 100644 --- a/managed_vms/async-rest/README.md +++ b/managed_vms/async-rest/README.md @@ -3,7 +3,7 @@ This web app demonstrates using asynchronous servlet techniques to reduce server resources. -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). +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). ## Initial Setup ## @@ -38,7 +38,9 @@ Go to http://localhost:8080 to see the webapp. ## Running locally using Docker ## -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: +The project also can build a docker image based on the jetty9 image for [Google Container Engine](https://cloud.google.com/container-engine/). +First uncomment the maven plugin section for docker-maven-plugin in the pom.xml. +The WAR file is installed in the webapps directory and the resulting image can be run locally with: docker run --rm -it -p 8080:8080 jetty9-async-rest --exec -Dcom.google.appengine.demos.asyncrest.appKey=YOUR_PLACES_APP_KEY diff --git a/managed_vms/async-rest/pom.xml b/managed_vms/async-rest/pom.xml index 4cb08253692..7e5f50081cc 100644 --- a/managed_vms/async-rest/pom.xml +++ b/managed_vms/async-rest/pom.xml @@ -16,8 +16,8 @@ maven-compiler-plugin 3.3 - 1.7 - 1.7 + 1.8 + 1.8 @@ -36,7 +36,7 @@ com.google.appengine gcloud-maven-plugin - 2.0.9.90.v20151210 + 2.0.9.96.v20160203 /usr/local/google-cloud-sdk debug @@ -61,10 +61,10 @@ - + diff --git a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java index 9320c03c2b4..87012af49cd 100644 --- a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java +++ b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/DumpServlet.java @@ -10,50 +10,51 @@ import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") -public class DumpServlet extends HttpServlet -{ - @Override - protected void doGet( HttpServletRequest request, - HttpServletResponse response ) throws ServletException, - IOException - { - response.setContentType("text/html"); - response.setStatus(HttpServletResponse.SC_OK); +public class DumpServlet extends HttpServlet { - PrintWriter out = response.getWriter(); + @Override + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, + IOException { + response.setContentType("text/html"); + response.setStatus(HttpServletResponse.SC_OK); - out.println("

DumpServlet

"); - out.println("

Context Fields:

"); - out.println("
");
-        out.printf("serverInfo=%s%n",getServletContext().getServerInfo());
-        out.printf("getServletContextName=%s%n",getServletContext().getServletContextName());
-        out.printf("virtualServerName=%s%n",getServletContext().getVirtualServerName());
-        out.printf("contextPath=%s%n",getServletContext().getContextPath());
-        out.printf("version=%d.%d%n",getServletContext().getMajorVersion(),getServletContext().getMinorVersion());
-        out.printf("effectiveVersion=%d.%d%n",getServletContext().getEffectiveMajorVersion(),getServletContext().getEffectiveMinorVersion());
-        out.println("
"); - out.println("

Request Fields:

"); - out.println("
");
-        out.printf("remoteHost/Addr:port=%s/%s:%d%n",request.getRemoteHost(),request.getRemoteAddr(),request.getRemotePort());
-        out.printf("localName/Addr:port=%s/%s:%d%n",request.getLocalName(),request.getLocalAddr(),request.getLocalPort());
-        out.printf("scheme=%s method=%s protocol=%s%n",request.getScheme(), request.getMethod(), request.getProtocol());
-        out.printf("serverName:serverPort=%s:%d%n",request.getServerName(),request.getServerPort());
-        out.printf("requestURI=%s%n",request.getRequestURI());
-        out.printf("requestURL=%s%n",request.getRequestURL().toString());
-        out.printf("contextPath|servletPath|pathInfo=%s|%s|%s%n",request.getContextPath(),request.getServletPath(),request.getPathInfo());
-        out.printf("session/new=%s/%b%n",request.getSession(true).getId(),request.getSession().isNew());
-        out.println("
"); - out.println("

Request Headers:

"); - out.println("
");
-        for (String n : Collections.list(request.getHeaderNames()))
-            for (String v : Collections.list(request.getHeaders(n)))
-                out.printf("%s: %s%n",n,v);
-        out.println("
"); - out.println("

Response Fields:

"); - out.println("
");
-        out.printf("bufferSize=%d%n",response.getBufferSize());
-        out.printf("encodedURL(\"/foo/bar\")=%s%n",response.encodeURL("/foo/bar"));
-        out.printf("encodedRedirectURL(\"/foo/bar\")=%s%n",response.encodeRedirectURL("/foo/bar"));
-        out.println("
"); + PrintWriter out = response.getWriter(); + + out.println("

DumpServlet

"); + out.println("

Context Fields:

"); + out.println("
");
+    out.printf("serverInfo=%s%n", getServletContext().getServerInfo());
+    out.printf("getServletContextName=%s%n", getServletContext().getServletContextName());
+    out.printf("virtualServerName=%s%n", getServletContext().getVirtualServerName());
+    out.printf("contextPath=%s%n", getServletContext().getContextPath());
+    out.printf("version=%d.%d%n", getServletContext().getMajorVersion(), getServletContext().getMinorVersion());
+    out.printf("effectiveVersion=%d.%d%n", getServletContext().getEffectiveMajorVersion(), getServletContext().getEffectiveMinorVersion());
+    out.println("
"); + out.println("

Request Fields:

"); + out.println("
");
+    out.printf("remoteHost/Addr:port=%s/%s:%d%n", request.getRemoteHost(), request.getRemoteAddr(), request.getRemotePort());
+    out.printf("localName/Addr:port=%s/%s:%d%n", request.getLocalName(), request.getLocalAddr(), request.getLocalPort());
+    out.printf("scheme=%s method=%s protocol=%s%n", request.getScheme(), request.getMethod(), request.getProtocol());
+    out.printf("serverName:serverPort=%s:%d%n", request.getServerName(), request.getServerPort());
+    out.printf("requestURI=%s%n", request.getRequestURI());
+    out.printf("requestURL=%s%n", request.getRequestURL().toString());
+    out.printf("contextPath|servletPath|pathInfo=%s|%s|%s%n", request.getContextPath(), request.getServletPath(), request.getPathInfo());
+    out.printf("session/new=%s/%b%n", request.getSession(true).getId(), request.getSession().isNew());
+    out.println("
"); + out.println("

Request Headers:

"); + out.println("
");
+    for (String n : Collections.list(request.getHeaderNames())) {
+      for (String v : Collections.list(request.getHeaders(n))) {
+        out.printf("%s: %s%n", n, v);
+      }
     }
+    out.println("
"); + out.println("

Response Fields:

"); + out.println("
");
+    out.printf("bufferSize=%d%n", response.getBufferSize());
+    out.printf("encodedURL(\"/foo/bar\")=%s%n", response.encodeURL("/foo/bar"));
+    out.printf("encodedRedirectURL(\"/foo/bar\")=%s%n", response.encodeRedirectURL("/foo/bar"));
+    out.println("
"); + } } diff --git a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java index b54d19f72d6..be30758adf6 100644 --- a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java +++ b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java @@ -15,105 +15,110 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -/** +/** * AbstractRestServlet. * */ public class AbstractRestServlet extends HttpServlet { - protected final static int MAX_RESULTS = 5; - - protected final static String STYLE = ""; - - protected final static String APPKEY = "com.google.appengine.demos.asyncrest.appKey"; - protected final static String APPKEY_ENV = "PLACES_APPKEY"; - protected final static String LOC_PARAM = "loc"; - protected final static String ITEMS_PARAM = "items"; - protected final static String LATITUDE_PARAM = "lat"; - protected final static String LONGITUDE_PARAM = "long"; - protected final static String RADIUS_PARAM = "radius"; - protected String key; - - @Override - public void init(ServletConfig servletConfig) throws ServletException { - //first try the servlet context init-param - String source="InitParameter"; - key = servletConfig.getInitParameter(APPKEY); - if (key==null || key.startsWith("${")) { - source="System Property"; - key = System.getProperty(APPKEY); - } - if (key == null || key.startsWith("${")) { - source="Environment Variable"; - key = System.getenv(APPKEY_ENV); - } - if (key == null) - throw new UnavailableException("Places App Key not set"); - if (key.startsWith("${")) - throw new UnavailableException("Places App Key not expanded from "+source); - } - public static String sanitize(String s) { - if (s == null) - return null; - return s.replace("<", "?").replace("&", "?").replace("\n", "?"); + protected final static int MAX_RESULTS = 5; + + protected final static String STYLE = ""; + + protected final static String APPKEY = "com.google.appengine.demos.asyncrest.appKey"; + protected final static String APPKEY_ENV = "PLACES_APPKEY"; + protected final static String LOC_PARAM = "loc"; + protected final static String ITEMS_PARAM = "items"; + protected final static String LATITUDE_PARAM = "lat"; + protected final static String LONGITUDE_PARAM = "long"; + protected final static String RADIUS_PARAM = "radius"; + protected String key; + + @Override + public void init(ServletConfig servletConfig) throws ServletException { + //first try the servlet context init-param + String source = "InitParameter"; + key = servletConfig.getInitParameter(APPKEY); + if (key == null || key.startsWith("${")) { + source = "System Property"; + key = System.getProperty(APPKEY); } - - protected String restQuery(String coordinates, String radius, String item) { - try { - return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=" + key + "&location=" - + URLEncoder.encode(coordinates, "UTF-8") + "&types=" + URLEncoder.encode(item, "UTF-8") - + "&radius=" + URLEncoder.encode(radius, "UTF-8"); - - } catch (Exception e) { - throw new RuntimeException(e); - } + if (key == null || key.startsWith("${")) { + source = "Environment Variable"; + key = System.getenv(APPKEY_ENV); } - - public String generateResults(Queue> results) { - StringBuilder thumbs = new StringBuilder(); - int resultCount = 0; - Iterator> itor = results.iterator(); - - while (resultCount < MAX_RESULTS && itor.hasNext()) { - Map m = (Map) itor.next(); - String name = (String) m.get("name"); - Object[] photos = (Object[]) m.get("photos"); - if (photos != null && photos.length > 0) { - resultCount++; - thumbs.append(""); - thumbs.append(" "); - } - } - return thumbs.toString(); + if (key == null) { + throw new UnavailableException("Places App Key not set"); } - - public String getPhotoURL(String photoref) { - return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref - + "&maxheight=40"; + if (key.startsWith("${")) { + throw new UnavailableException("Places App Key not expanded from " + source); } + } - protected String ms(long nano) { - BigDecimal dec = new BigDecimal(nano); - return dec.divide(new BigDecimal(1000000L)).setScale(1, RoundingMode.UP).toString(); + public static String sanitize(String s) { + if (s == null) { + return null; } + return s.replace("<", "?").replace("&", "?").replace("\n", "?"); + } - protected int width(long nano) { - int w = (int) ((nano + 999999L) / 5000000L); - if (w == 0) - w = 2; - return w; - } + protected String restQuery(String coordinates, String radius, String item) { + try { + return "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=" + key + "&location=" + + URLEncoder.encode(coordinates, "UTF-8") + "&types=" + URLEncoder.encode(item, "UTF-8") + + "&radius=" + URLEncoder.encode(radius, "UTF-8"); - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - doGet(request, response); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public String generateResults(Queue> results) { + StringBuilder thumbs = new StringBuilder(); + int resultCount = 0; + Iterator> itor = results.iterator(); + + while (resultCount < MAX_RESULTS && itor.hasNext()) { + Map m = (Map) itor.next(); + String name = (String) m.get("name"); + Object[] photos = (Object[]) m.get("photos"); + if (photos != null && photos.length > 0) { + resultCount++; + thumbs.append(""); + thumbs.append(" "); + } + } + return thumbs.toString(); + } + + public String getPhotoURL(String photoref) { + return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref + + "&maxheight=40"; + } + + protected String ms(long nano) { + BigDecimal dec = new BigDecimal(nano); + return dec.divide(new BigDecimal(1000000L)).setScale(1, RoundingMode.UP).toString(); + } + + protected int width(long nano) { + int w = (int) ((nano + 999999L) / 5000000L); + if (w == 0) { + w = 2; } + return w; + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request, response); + } } diff --git a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java index 195226a39fa..0bee71ec433 100644 --- a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java +++ b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AsyncRestServlet.java @@ -32,166 +32,171 @@ * */ public class AsyncRestServlet extends AbstractRestServlet { - final static String RESULTS_ATTR = "com.google.appengine.demos.asyncrest.client"; - final static String DURATION_ATTR = "com.google.appengine.demos.asyncrest.duration"; - final static String START_ATTR = "com.google.appengine.demos.asyncrest.start"; - HttpClient client; + final static String RESULTS_ATTR = "com.google.appengine.demos.asyncrest.client"; + final static String DURATION_ATTR = "com.google.appengine.demos.asyncrest.duration"; + final static String START_ATTR = "com.google.appengine.demos.asyncrest.start"; - @Override - public void init(ServletConfig servletConfig) throws ServletException { - super.init(servletConfig); - SslContextFactory sslContextFactory = new SslContextFactory(); - client = new HttpClient(sslContextFactory); - - try { - client.start(); - } catch (Exception e) { - throw new ServletException(e); - } - } + HttpClient client; - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - if (key == null) { - response.sendError(500, APPKEY + " not set"); - return; - } - - Long start = System.nanoTime(); - - // Do we have results yet? - Queue> results = (Queue>) request.getAttribute(RESULTS_ATTR); - - // If no results, this must be the first dispatch, so send the REST - // request(s) - if (results == null) { - // define results data structures - final Queue> resultsQueue = new ConcurrentLinkedQueue<>(); - request.setAttribute(RESULTS_ATTR, results = resultsQueue); - - // suspend the request - // This is done before scheduling async handling to avoid race of - // dispatch before startAsync! - final AsyncContext async = request.startAsync(); - async.setTimeout(30000); - - // extract keywords to search for - String lat = sanitize(request.getParameter(LATITUDE_PARAM)); - String longitude = sanitize(request.getParameter(LONGITUDE_PARAM)); - String radius = sanitize(request.getParameter(RADIUS_PARAM)); - String[] keywords = sanitize(request.getParameter(ITEMS_PARAM)).split(","); - - final AtomicInteger outstanding = new AtomicInteger(keywords.length); - - // Send request each keyword - for (final String item : keywords) { - client.newRequest(restQuery(lat + "," + longitude, radius, item)).method(HttpMethod.GET) - .send(new AsyncRestRequest() { - @Override - void onLocationFound(Map result) { - resultsQueue.add(result); - } - - @Override - void onComplete() { - if (outstanding.decrementAndGet() <= 0) - async.dispatch(); - } - }); - } - - // save timing info and return - request.setAttribute(START_ATTR, start); - request.setAttribute(DURATION_ATTR, System.nanoTime() - start); - - return; - } - - // We have results! - // Generate the response - String thumbs = generateResults(results); - - response.setContentType("text/html"); - PrintWriter out = response.getWriter(); - out.println(""); - out.println(STYLE); - out.println(""); - - long initial = (Long) request.getAttribute(DURATION_ATTR); - long start0 = (Long) request.getAttribute(START_ATTR); - - long now = System.nanoTime(); - long total = now - start0; - long generate = now - start; - long thread = initial + generate; - - String loc = sanitize(request.getParameter(LOC_PARAM)); - out.print("Asynchronous: Requesting " + sanitize(request.getParameter(ITEMS_PARAM)) + " near " - + (loc != null ? loc - : "lat=" + sanitize(request.getParameter(LATITUDE_PARAM)) + " long=" - + sanitize(request.getParameter(LONGITUDE_PARAM))) - + "
"); - out.print("Total Time: " + ms(total) + "ms
"); - - out.print("Thread held (red): " + ms(thread) + "ms (" + ms(initial) + " initial + " - + ms(generate) + " generate )
"); - out.print("Async wait (green): " + ms(total - thread) + "ms
"); - - out.println("" - + "" - + ""); - - out.println("
"); - out.print("First 5 results of "+results.size()+":
"); - if ("".equals(thumbs)) - out.print("No results. Ensure "+APPKEY+" property is set correctly."); - else - out.println(thumbs); - - out.println("
"); - out.println(""); - out.close(); + @Override + public void init(ServletConfig servletConfig) throws ServletException { + super.init(servletConfig); + SslContextFactory sslContextFactory = new SslContextFactory(); + client = new HttpClient(sslContextFactory); + + try { + client.start(); + } catch (Exception e) { + throw new ServletException(e); } + } - private abstract class AsyncRestRequest extends Response.Listener.Adapter { - final Utf8StringBuilder _content = new Utf8StringBuilder(); + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { - AsyncRestRequest() { - } + if (key == null) { + response.sendError(500, APPKEY + " not set"); + return; + } - @Override - public void onContent(Response response, ByteBuffer content) { - byte[] bytes = BufferUtil.toArray(content); - _content.append(bytes, 0, bytes.length); - } + Long start = System.nanoTime(); + + // Do we have results yet? + Queue> results = (Queue>) request.getAttribute(RESULTS_ATTR); + + // If no results, this must be the first dispatch, so send the REST + // request(s) + if (results == null) { + // define results data structures + final Queue> resultsQueue = new ConcurrentLinkedQueue<>(); + request.setAttribute(RESULTS_ATTR, results = resultsQueue); + + // suspend the request + // This is done before scheduling async handling to avoid race of + // dispatch before startAsync! + final AsyncContext async = request.startAsync(); + async.setTimeout(30000); + + // extract keywords to search for + String lat = sanitize(request.getParameter(LATITUDE_PARAM)); + String longitude = sanitize(request.getParameter(LONGITUDE_PARAM)); + String radius = sanitize(request.getParameter(RADIUS_PARAM)); + String[] keywords = sanitize(request.getParameter(ITEMS_PARAM)).split(","); + + final AtomicInteger outstanding = new AtomicInteger(keywords.length); + + // Send request each keyword + for (final String item : keywords) { + client.newRequest(restQuery(lat + "," + longitude, radius, item)).method(HttpMethod.GET) + .send(new AsyncRestRequest() { + @Override + void onLocationFound(Map result) { + resultsQueue.add(result); + } + + @Override + void onComplete() { + if (outstanding.decrementAndGet() <= 0) { + async.dispatch(); + } + } + }); + } + + // save timing info and return + request.setAttribute(START_ATTR, start); + request.setAttribute(DURATION_ATTR, System.nanoTime() - start); + + return; + } - @Override - public void onComplete(Result result) { - // extract results - Map data = (Map) JSON.parse(_content.toString()); - if (data != null) { - Object[] results = (Object[]) data.get("results"); - if (results != null) { - for (Object o : results) - onLocationFound((Map) o); - } - } - onComplete(); + // We have results! + // Generate the response + String thumbs = generateResults(results); + + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println(""); + out.println(STYLE); + out.println(""); + + long initial = (Long) request.getAttribute(DURATION_ATTR); + long start0 = (Long) request.getAttribute(START_ATTR); + + long now = System.nanoTime(); + long total = now - start0; + long generate = now - start; + long thread = initial + generate; + + String loc = sanitize(request.getParameter(LOC_PARAM)); + out.print("Asynchronous: Requesting " + sanitize(request.getParameter(ITEMS_PARAM)) + " near " + + (loc != null ? loc + : "lat=" + sanitize(request.getParameter(LATITUDE_PARAM)) + " long=" + + sanitize(request.getParameter(LONGITUDE_PARAM))) + + "
"); + out.print("Total Time: " + ms(total) + "ms
"); + + out.print("Thread held (red): " + ms(thread) + "ms (" + ms(initial) + " initial + " + + ms(generate) + " generate )
"); + out.print("Async wait (green): " + ms(total - thread) + "ms
"); + + out.println("" + + "" + + ""); + + out.println("
"); + out.print("First 5 results of " + results.size() + ":
"); + if ("".equals(thumbs)) { + out.print("No results. Ensure " + APPKEY + " property is set correctly."); + } else { + out.println(thumbs); + } - } + out.println("
"); + out.println(""); + out.close(); + } - abstract void onLocationFound(Map details); + private abstract class AsyncRestRequest extends Response.Listener.Adapter { - abstract void onComplete(); + final Utf8StringBuilder _content = new Utf8StringBuilder(); + AsyncRestRequest() { } @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - doGet(request, response); + public void onContent(Response response, ByteBuffer content) { + byte[] bytes = BufferUtil.toArray(content); + _content.append(bytes, 0, bytes.length); } + + @Override + public void onComplete(Result result) { + // extract results + Map data = (Map) JSON.parse(_content.toString()); + if (data != null) { + Object[] results = (Object[]) data.get("results"); + if (results != null) { + for (Object o : results) { + onLocationFound((Map) o); + } + } + } + onComplete(); + + } + + abstract void onLocationFound(Map details); + + abstract void onComplete(); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request, response); + } } diff --git a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java index e670a1282d1..0fb81d11f53 100644 --- a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java +++ b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java @@ -19,92 +19,85 @@ /** * SerialRestServlet - * - * May be configured with init parameters: + * + * May be configured with init parameters: *
*
appid
*
The Google app key to use
*
*/ -public class SerialRestServlet extends AbstractRestServlet -{ - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException - { - if (key == null) { - response.sendError(500, APPKEY + " not set"); - return; - } - long start = System.nanoTime(); - - String loc = sanitize(request.getParameter(LOC_PARAM)); - String lat = sanitize(request.getParameter(LATITUDE_PARAM)); - String longitude = sanitize(request.getParameter(LONGITUDE_PARAM)); - String radius = sanitize(request.getParameter(RADIUS_PARAM)); - - String[] keywords=sanitize(request.getParameter(ITEMS_PARAM)).split(","); - Queue> results = new LinkedList>(); - - // make all requests serially - for (String itemName : keywords) - { - URL url = new URL(restQuery(lat+","+longitude, radius, itemName)); - - HttpURLConnection connection = (HttpURLConnection)url.openConnection(); - connection.setRequestMethod("GET"); - - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - - Map query = (Map)JSON.parse(new BufferedReader(new InputStreamReader(connection.getInputStream()))); - Object[] tmp = (Object[]) query.get("results"); - if (tmp != null) - { - for (Object o : tmp) - { - Map m = (Map)o; - results.add(m); - } - } +public class SerialRestServlet extends AbstractRestServlet { + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + if (key == null) { + response.sendError(500, APPKEY + " not set"); + return; + } + long start = System.nanoTime(); + + String loc = sanitize(request.getParameter(LOC_PARAM)); + String lat = sanitize(request.getParameter(LATITUDE_PARAM)); + String longitude = sanitize(request.getParameter(LONGITUDE_PARAM)); + String radius = sanitize(request.getParameter(RADIUS_PARAM)); + + String[] keywords = sanitize(request.getParameter(ITEMS_PARAM)).split(","); + Queue> results = new LinkedList>(); + + // make all requests serially + for (String itemName : keywords) { + URL url = new URL(restQuery(lat + "," + longitude, radius, itemName)); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + + Map query = (Map) JSON.parse(new BufferedReader(new InputStreamReader(connection.getInputStream()))); + Object[] tmp = (Object[]) query.get("results"); + if (tmp != null) { + for (Object o : tmp) { + Map m = (Map) o; + results.add(m); } - - - // Generate the response - String thumbs = generateResults(results); - - response.setContentType("text/html"); - PrintWriter out = response.getWriter(); - out.println(""); - out.println(STYLE); - out.println(""); - - long now = System.nanoTime(); - long total=now-start; - - out.print("Blocking: Requesting "+sanitize(request.getParameter(ITEMS_PARAM))+" near "+(loc != null?loc:"lat="+lat+" long="+longitude)+"
"); - out.print("Total Time: "+ms(total)+"ms
"); - out.print("Thread held (red): "+ms(total)+"ms
"); - - out.println(""); - - out.println("
"); - out.print("First 5 results of "+results.size()+":
"); - if ("".equals(thumbs)) - out.print("No results. Ensure "+APPKEY+" property is set correctly."); - else - out.println(thumbs); - out.println("
"); - out.println(""); - out.close(); + } } - + // Generate the response + String thumbs = generateResults(results); + + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println(""); + out.println(STYLE); + out.println(""); + + long now = System.nanoTime(); + long total = now - start; - /** - * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse - * response) - */ - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException - { - doGet(request, response); + out.print("Blocking: Requesting " + sanitize(request.getParameter(ITEMS_PARAM)) + " near " + (loc != null ? loc : "lat=" + lat + " long=" + longitude) + "
"); + out.print("Total Time: " + ms(total) + "ms
"); + out.print("Thread held (red): " + ms(total) + "ms
"); + + out.println(""); + + out.println("
"); + out.print("First 5 results of " + results.size() + ":
"); + if ("".equals(thumbs)) { + out.print("No results. Ensure " + APPKEY + " property is set correctly."); + } else { + out.println(thumbs); } + out.println("
"); + out.println(""); + out.close(); + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse + * response) + */ + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request, response); + } } From e66fc0060022778a0393d20f87b5815d3c66b81a Mon Sep 17 00:00:00 2001 From: ludo Date: Fri, 5 Feb 2016 14:31:39 -0800 Subject: [PATCH 3/4] Remove IDE warnings. --- .../google/appengine/demos/asyncrest/AbstractRestServlet.java | 1 + .../com/google/appengine/demos/asyncrest/SerialRestServlet.java | 2 ++ 2 files changed, 3 insertions(+) diff --git a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java index be30758adf6..07956c2dc3d 100644 --- a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java +++ b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/AbstractRestServlet.java @@ -116,6 +116,7 @@ protected int width(long nano) { return w; } + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); diff --git a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java index 0fb81d11f53..359fd1f3ec1 100644 --- a/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java +++ b/managed_vms/async-rest/src/main/java/com/google/appengine/demos/asyncrest/SerialRestServlet.java @@ -28,6 +28,7 @@ */ public class SerialRestServlet extends AbstractRestServlet { + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (key == null) { response.sendError(500, APPKEY + " not set"); @@ -96,6 +97,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ + @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } From e5d52ed982969d32ef970dbef01dd374652f7436 Mon Sep 17 00:00:00 2001 From: ludovic Champenois Date: Fri, 5 Feb 2016 14:39:00 -0800 Subject: [PATCH 4/4] Fix reference to moved projects. --- pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d951bccee4b..c8f6370685d 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,8 @@ managed_vms/sendgrid managed_vms/static-files managed_vms/twilio - managedvms/sparkjava + managed_vms/async-rest + managed_vms/sparkjava bigquery logging monitoring