Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move samples added in managedvms to managed_vms #84

Merged
merged 4 commits into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions managedvms/async-rest/pom.xml → managed_vms/async-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -36,7 +36,7 @@
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.90.v20151210</version>
<version>2.0.9.96.v20160203</version>
<configuration>
<gcloud_directory>/usr/local/google-cloud-sdk</gcloud_directory>
<verbosity>debug</verbosity>
Expand All @@ -61,10 +61,10 @@
</webResources>
</configuration>
</plugin>
<plugin>
<!--plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.3.2</version>
<version>0.4.0</version>
<executions>
<execution>
<id>build-docker-image</id>
Expand Down Expand Up @@ -102,7 +102,7 @@
</configuration>
</execution>
</executions>
</plugin>
</plugin-->
</plugins>
</build>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.google.appengine.demos;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
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);

PrintWriter out = response.getWriter();

out.println("<h1>DumpServlet</h1>");
out.println("<h2>Context Fields:</h2>");
out.println("<pre>");
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("</pre>");
out.println("<h2>Request Fields:</h2>");
out.println("<pre>");
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("</pre>");
out.println("<h2>Request Headers:</h2>");
out.println("<pre>");
for (String n : Collections.list(request.getHeaderNames())) {
for (String v : Collections.list(request.getHeaders(n))) {
out.printf("%s: %s%n", n, v);
}
}
out.println("</pre>");
out.println("<h2>Response Fields:</h2>");
out.println("<pre>");
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("</pre>");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.google.appengine.demos.asyncrest;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
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 = "<style type='text/css'>"
+ " img.thumb:hover {height:50px}"
+ " img.thumb {vertical-align:text-top}"
+ " span.red {color: #ff0000}"
+ " span.green {color: #00ff00}"
+ " iframe {border: 0px}" + "</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 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);
}
}

public String generateResults(Queue<Map<String, Object>> results) {
StringBuilder thumbs = new StringBuilder();
int resultCount = 0;
Iterator<Map<String, Object>> 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("<img class='thumb' border='1px' height='40px'" + " src='"
+ getPhotoURL((String) (((Map) photos[0]).get("photo_reference"))) + "'" + " title='" + name
+ "'" + "/>");
thumbs.append("</a>&nbsp;");
}
}
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;
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}
Loading