App Engine SDK 1.8.4 release.
[gae.git] / java / src / main / com / google / appengine / tools / development / LocalApiProxyServletFilter.java
blobe69b6355491acb4e762ec2489f738879b588f04d
1 // Copyright 2008 Google Inc. All Rights Reserved.
3 package com.google.appengine.tools.development;
5 import com.google.apphosting.api.ApiProxy;
6 import com.google.apphosting.utils.config.AppEngineWebXml;
7 import com.google.apphosting.utils.config.AppEngineWebXmlReader;
8 import com.google.apphosting.utils.config.WebModule;
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.logging.Logger;
15 import javax.servlet.Filter;
16 import javax.servlet.FilterChain;
17 import javax.servlet.FilterConfig;
18 import javax.servlet.ServletContext;
19 import javax.servlet.ServletException;
20 import javax.servlet.ServletRequest;
21 import javax.servlet.ServletResponse;
22 import javax.servlet.http.HttpServletRequest;
24 /**
25 * This filter is not currently used. It was originally written for
26 * IBM so they could see how to use our API's from their own webserver.
27 * We kept it around because we'd ultimately like to use this ourselves but
28 * we don't currently have a way to ensure that this filter runs before any
29 * other filters get initialized.
31 * {@code LocalApiProxyServletFilter} is a servlet {@link Filter} that
32 * sets up {@link ApiProxy} for use with the stub API implementations.
34 * <p>There are two parts to this:<ul>
36 * <li>At initialization, this filter installs a {@link
37 * ApiProxy.Delegate} instance that locates any API stub
38 * implementations on the classpath and registers them for use by
39 * future requests. It also looks for an App Engine-specific
40 * deployment descriptor ({@code WEB-INF/appengine-web.xml}) and
41 * parses it to obtain some metadata about the application
42 * (e.g. application identifier).</li>
44 * <li>Around each request, a {@link ApiProxy.Environment} instance is
45 * installed into a {@link ThreadLocal} managed by {@link ApiProxy}.
46 * This environment instance contains the application metadata that
47 * was extracted earlier, and also provides access to the
48 * authentication data maintained by the stub implementation of the
49 * Users API.</li> </ul>
51 * N.B. Does not support Modules.
54 @Deprecated
55 public class LocalApiProxyServletFilter implements Filter {
56 private static final Logger logger = Logger.getLogger(LocalApiProxyServletFilter.class.getName());
57 private static final String AE_WEB_XML = "/WEB-INF/appengine-web.xml";
59 private AppEngineWebXml appEngineWebXml;
61 /**
62 * Register a custom {@link ApiProxy.Delegate instance}.
64 @Override
65 public void init(FilterConfig config) {
66 logger.info("Filter initialization invoked -- registering ApiProxy delegate.");
67 ApiProxyLocalFactory factory = new ApiProxyLocalFactory();
68 ApiProxy.setDelegate(factory.create(getLocalServerEnvironment(config)));
70 logger.info("Parsing custom deployment descriptor (" + AE_WEB_XML + ").");
71 ServletAppEngineWebXmlReader reader =
72 new ServletAppEngineWebXmlReader(config.getServletContext());
73 appEngineWebXml = reader.readAppEngineWebXml();
74 logger.info("Application identifier is: " + appEngineWebXml.getAppId());
77 private LocalServerEnvironment getLocalServerEnvironment(final FilterConfig config) {
78 return new LocalServerEnvironment() {
80 @Override
81 public File getAppDir() {
82 return new File(".");
85 @Override
86 public String getAddress() {
87 throw new UnsupportedOperationException();
90 @Override
91 public int getPort() {
92 throw new UnsupportedOperationException();
95 @Override
96 public String getHostName() {
97 throw new UnsupportedOperationException();
100 @Override
101 public void waitForServerToStart() { }
103 @Override
104 public boolean simulateProductionLatencies() {
105 return true;
108 @Override
109 public boolean enforceApiDeadlines() {
110 return false;
116 * Remove the custom {@link ApiProxy.Delegate} instance.
118 @Override
119 public void destroy() {
120 logger.info("Filter destruction invoked -- removing delegate.");
121 ApiProxy.setDelegate(null);
125 * Wrap the request with calls to the environment-management method
126 * on {@link ApiProxy}.
128 @Override
129 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
130 throws IOException, ServletException {
131 HttpServletRequest httpRequest = (HttpServletRequest) request;
133 logger.fine("Filter received a request, setting environment ThreadLocal.");
134 ApiProxy.setEnvironmentForCurrentThread(new LocalHttpRequestEnvironment(
135 appEngineWebXml.getAppId(), WebModule.getModuleName(appEngineWebXml),
136 appEngineWebXml.getMajorVersionId(), LocalEnvironment.MAIN_INSTANCE,
137 request.getLocalPort(), httpRequest, null, null));
138 try {
139 chain.doFilter(request, response);
140 } finally {
141 logger.fine("Request has completed. Removing environment ThreadLocal.");
142 ApiProxy.clearEnvironmentForCurrentThread();
147 * {@code ServletAppEngineWebXmlReader} is a specialization of
148 * {@link AppEngineWebXmlReader} that reads the custom deployment
149 * descriptor ({@code WEB-INF/appengine-web.xml}) from a {@link
150 * ServletContext} rather than looking for an actual file on the
151 * filesystem.
153 private static class ServletAppEngineWebXmlReader extends AppEngineWebXmlReader {
154 private final ServletContext context;
156 public ServletAppEngineWebXmlReader(ServletContext context) {
157 super("");
158 this.context = context;
161 @Override
162 protected InputStream getInputStream() {
163 return context.getResourceAsStream(AE_WEB_XML);