Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
Use env vars for places.appkey
Added DumpServlet for debugging
  • Loading branch information
gregw committed Feb 2, 2016
1 parent 2bbcd7e commit 4c89bf7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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
Expand Up @@ -10,6 +10,7 @@

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;
Expand All @@ -29,6 +30,7 @@ public class AbstractRestServlet extends HttpServlet {
+ " 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";
Expand All @@ -39,9 +41,20 @@ public class AbstractRestServlet extends HttpServlet {
@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)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<precompilation-enabled>false</precompilation-enabled>
<threadsafe>true</threadsafe>
<sessions-enabled>false</sessions-enabled>
<system-properties>
<property name="com.google.appengine.demos.asyncrest.appKey" value="${places.appkey}" />
</system-properties>
<env-variables>
<env-var name="PLACES_APPKEY" value="${places.appkey}" />
</env-variables>
</appengine-web-app>
9 changes: 9 additions & 0 deletions managedvms/async-rest/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@
<url-pattern>/testAsync</url-pattern>
</servlet-mapping>

<servlet>
<display-name>DumpServlet</display-name>
<servlet-name>DumpServlet</servlet-name>
<servlet-class>com.google.appengine.demos.DumpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DumpServlet</servlet-name>
<url-pattern>/dump/*</url-pattern>
</servlet-mapping>

</web-app>

0 comments on commit 4c89bf7

Please sign in to comment.