Skip to content

Commit

Permalink
Merge pull request #124 from GoogleCloudPlatform/csasync
Browse files Browse the repository at this point in the history
Checkstyle fixes for async-rest sample.
  • Loading branch information
tswast committed Mar 17, 2016
2 parents 61455e7 + 0540a80 commit abe632e
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 111 deletions.
58 changes: 37 additions & 21 deletions managed_vms/async-rest/pom.xml
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
<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">
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.appengine.demos</groupId>
<artifactId>managed-vms-async-rest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Example Async Rest Webapp</name>

<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>

<properties>
<places.appkey>YOUR_PLACES_APP_KEY</places.appkey>
<jetty.version>9.3.7.v20160115</jetty.version>
<docker.label.prefix>async-</docker.label.prefix>
<jetty.version>9.3.7.v20160115</jetty.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<places.appkey>YOUR_PLACES_APP_KEY</places.appkey>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<systemProperties>
<systemProperty>
<systemProperty>
<name>com.google.appengine.demos.asyncrest.appKey</name>
<value>${places.appkey}</value>
</systemProperty>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>2.0.9.96.v20160203</version>
<version>2.0.9.95.v20160203</version>
<configuration>
<gcloud_directory>/usr/local/google-cloud-sdk</gcloud_directory>
<verbosity>debug</verbosity>
<log_level>debug</log_level>
<non_docker_mode>false</non_docker_mode>
<docker_build>remote</docker_build>
<verbosity>debug</verbosity>
<log_level>debug</log_level>
<non_docker_mode>false</non_docker_mode>
<docker_build>remote</docker_build>
</configuration>
</plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.appengine.demos;

import java.io.IOException;
Expand All @@ -13,9 +29,8 @@
public class DumpServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);

Expand All @@ -28,19 +43,42 @@ protected void doGet(HttpServletRequest request,
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.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(
"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.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>");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.appengine.demos.asyncrest;

import java.io.IOException;
Expand All @@ -16,32 +32,31 @@
import javax.servlet.http.HttpServletResponse;

/**
* AbstractRestServlet.
*
* Abstract base class for REST servlets.
*/
public class AbstractRestServlet extends HttpServlet {

protected final static int MAX_RESULTS = 5;
protected static final int MAX_RESULTS = 5;

protected final static String STYLE = "<style type='text/css'>"
protected static final 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 static final String APPKEY = "com.google.appengine.demos.asyncrest.appKey";
protected static final String APPKEY_ENV = "PLACES_APPKEY";
protected static final String LOC_PARAM = "loc";
protected static final String ITEMS_PARAM = "items";
protected static final String LATITUDE_PARAM = "lat";
protected static final String LONGITUDE_PARAM = "long";
protected static final String RADIUS_PARAM = "radius";
protected String key;

@Override
public void init(ServletConfig servletConfig) throws ServletException {
//first try the servlet context init-param
// First try the servlet context init-param.
String source = "InitParameter";
key = servletConfig.getInitParameter(APPKEY);
if (key == null || key.startsWith("${")) {
Expand All @@ -60,18 +75,19 @@ public void init(ServletConfig servletConfig) throws ServletException {
}
}

public static String sanitize(String s) {
if (s == null) {
public static String sanitize(String str) {
if (str == null) {
return null;
}
return s.replace("<", "?").replace("&", "?").replace("\n", "?");
return str.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");
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);
Expand All @@ -84,21 +100,22 @@ public String generateResults(Queue<Map<String, Object>> results) {
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");
Map map = (Map) itor.next();
String name = (String) map.get("name");
Object[] photos = (Object[]) map.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(
"<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) {
public String getPhotoUrl(String photoref) {
return "https://maps.googleapis.com/maps/api/place/photo?key=" + key + "&photoreference=" + photoref
+ "&maxheight=40";
}
Expand All @@ -109,11 +126,11 @@ protected String ms(long nano) {
}

protected int width(long nano) {
int w = (int) ((nano + 999999L) / 5000000L);
if (w == 0) {
w = 2;
int width = (int) ((nano + 999999L) / 5000000L);
if (width == 0) {
width = 2;
}
return w;
return width;
}

@Override
Expand Down
Loading

0 comments on commit abe632e

Please sign in to comment.