Skip to content

Commit 5f90f59

Browse files
committed
Began work on exporting code snippets from docs
1 parent 572674c commit 5f90f59

16 files changed

+1204
-0
lines changed

dump/LogFilterImpl.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package mysite.server;
2+
3+
import java.io.IOException;
4+
import java.util.logging.Logger;
5+
import javax.servlet.Filter;
6+
import javax.servlet.FilterChain;
7+
import javax.servlet.FilterConfig;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.ServletRequest;
10+
import javax.servlet.ServletResponse;
11+
12+
public class LogFilterImpl implements Filter {
13+
14+
private FilterConfig filterConfig;
15+
private static final Logger log = Logger.getLogger(LogFilterImpl.class.getName());
16+
17+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
18+
throws IOException, ServletException {
19+
log.warning("Log filter processed a " + getFilterConfig().getInitParameter("logType")
20+
+ " request");
21+
22+
filterChain.doFilter(request, response);
23+
}
24+
25+
public FilterConfig getFilterConfig() {
26+
return filterConfig;
27+
}
28+
29+
public void init(FilterConfig filterConfig) {
30+
this.filterConfig = filterConfig;
31+
}
32+
33+
public void destroy() {}
34+
35+
}

dump/appconfig.xml

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
// [START pagespeed_example]
2+
<pagespeed>
3+
<domain-to-rewrite>*.cdn.myapp.com</domain-to-rewrite>
4+
<domain-to-rewrite>www.flickr.com</domain-to-rewrite>
5+
<url-blacklist>http://*/*.svg</url-blacklist>
6+
<url-blacklist>http://secure.foo.com/*</url-blacklist>
7+
<enabled-rewriter>CollapseWhitespace</enabled-rewriter>
8+
<disabled-rewriter>CombineJs</disabled-rewriter>
9+
<disabled-rewriter>ProxyImages</disabled-rewriter>
10+
</pagespeed>
11+
</appengine-web-app>
12+
// [END pagespeed_example]
13+
14+
// [START custom_error_example]
15+
<static-error-handlers>
16+
<handler file="default_error.html" />
17+
<handler file="over_quota.html" error-code="over_quota" />
18+
</static-error-handlers>
19+
// [END custom_error_example]
20+
21+
// [START expiration_example]
22+
<static-files>
23+
<include path="/**.png" expiration="4d 5h" />
24+
</static-files>
25+
// [END expiration_example]
26+
27+
// [START expiration_example_yaml]
28+
static_files:
29+
- include: /**.png
30+
expiration: 4d 5h
31+
// [END expiration_example_yaml]
32+
33+
// [START static_codesample_xml]
34+
<static-files>
35+
<include path="/**.png" />
36+
<exclude path="/data/**.png" />
37+
// [END static_codesample_xml]
38+
39+
// [START static_codesample_yaml]
40+
static_files:
41+
- include: /**.png
42+
- exclude: /data/**.png
43+
// [END static_codesample_yaml]
44+
45+
// [START resource_codesample_xml]
46+
<resource-files>
47+
<include path="/**.xml" />
48+
<exclude path="/feeds/**.xml" />
49+
// [END resource_codesample_xml]
50+
51+
// [START resource_codesample_yaml]
52+
resource_files:
53+
- include: /**.xml
54+
- exclude: /feeds/**.xml
55+
// [END resource_codesample_yaml]
56+
57+
//[START header_codesample_xml]
58+
<static-files>
59+
<include path="/my_static-files" >
60+
<http-header name="Access-Control-Allow-Origin" value="http://example.org" />
61+
</include>
62+
</static-files>
63+
//[END header_codesample_xml]
64+
65+
//[START header_codesample_yaml]
66+
static_files:
67+
- include: /static/*
68+
http_headers:
69+
Access-Control-Allow-Origin: http://example.org
70+
//[END header_codesample_yaml]
71+
72+
//[START mapper_codesample_yaml]
73+
handlers:
74+
- url: /red/*
75+
servlet: mysite.server.TeamServlet
76+
init_params:
77+
teamColor: red
78+
bgColor: "#CC0000"
79+
name: redteam
80+
- url: /blue/*
81+
servlet: mysite.server.TeamServlet
82+
init_params:
83+
teamColor: blue
84+
bgColor: "#0000CC"
85+
name: blueteam
86+
- url: /register/*
87+
jsp: /register/start.jsp
88+
- url: /*.special
89+
filter: mysite.server.LogFilterImpl
90+
init_params:
91+
logType: special
92+
//[END mapper_codesample_yaml]
93+
94+
//[START environment_variables]
95+
<system-properties>
96+
<property name="myapp.maximum-message-length" value="140" />
97+
<property name="myapp.notify-every-n-signups" value="1000" />
98+
<property name="myapp.notify-url" value="http://www.example.com/signupnotify" />
99+
</system-properties>
100+
101+
<env-variables>
102+
<env-var name="DEFAULT_ENCODING" value="UTF-8" />
103+
</env-variables>
104+
//[END environment_variables]
105+
106+
//[START environment_variables_yaml]
107+
system_properties:
108+
myapp.maximum-message-length: 140
109+
myapp.notify-every-n-signups: 1000
110+
myapp.notify-url: http://www.example.com/signupnotify
111+
env_variables:
112+
DEFAULT_ENCODING: UTF-8
113+
context_params:
114+
rack.env: production
115+
//[END environment_variables_yaml]
116+
117+
//[START inbound_services]
118+
<inbound-services>
119+
<service>mail</service>
120+
<service>warmup</service>
121+
</inbound-services>
122+
//[END inbound_services]
123+
124+
//[START admin_console_custom_pages]
125+
<admin-console>
126+
<page name="Blog Comment Admin" url="/blog/admin/comments" />
127+
<page name="Create a Blog Post" url="/blog/admin/newentry" />
128+
</admin-console>
129+
//[END admin_console_custom_pages]
130+
131+
//[START about_app_yaml_example]
132+
application: myapp
133+
version: alpha-001
134+
runtime: java
135+
api_version: 1
136+
137+
handlers:
138+
- url: /admin/*
139+
login: admin
140+
//[END about_app_yaml_example]
141+
142+
//[START minimal_appengine_web_xml]
143+
<?xml version="1.0" encoding="utf-8"?>
144+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
145+
<application>_your_app_id_</application>
146+
<version>alpha-001</version>
147+
<threadsafe>true</threadsafe>
148+
</appengine-web-app>
149+
//[END minimal_appengine_web_xml]
150+
151+
//[START secure_handler_yaml]
152+
handlers:
153+
154+
- url: /youraccount/*
155+
login: required
156+
secure: always
157+
//[END secure_handler_yaml]
158+
159+
//[START servlet_listeners]
160+
listeners:
161+
- com.example.MyListener
162+
- com.example.MyOtherListener
163+
//[END servlet_listeners]
164+
165+
//[START login_example_yaml]
166+
handlers:
167+
168+
- url: /profile/*
169+
login: required
170+
171+
- url: /admin/*
172+
servlet: com.example.AdminServlet
173+
login: admin
174+
//[END login_example_yaml]
175+
176+
//[START welcome_files]
177+
welcome_files:
178+
- index.jsp
179+
- index.html
180+
//[END welcome_files]
181+
182+
//[START load_on_startup]
183+
<servlet>
184+
<servlet-name>my-servlet</servlet-name>
185+
<servlet-class>com.company.MyServlet</servlet-class>
186+
<load-on-startup>1</load-on-startup>
187+
</servlet>
188+
//[END load_on_startup]
189+
190+
//[START ServletContextListener]
191+
<listener>
192+
<listener-class>com.company.MyListener</listener-class>
193+
</listener>
194+
//[END ServletContextListener]
195+
196+
//[START listener_filter]
197+
public class MyListener implements ServletContextListener {
198+
public void contextInitialized(ServletContextEvent event) {
199+
// This will be invoked as part of a warmup request, or the first user
200+
// request if no warmup request was invoked.
201+
}
202+
public void contextDestroyed(ServletContextEvent event) {
203+
// App Engine does not currently invoke this method.
204+
}
205+
}
206+
//[END listener_filter]
207+
208+
//[START custom_warmup_servlet]
209+
<servlet>
210+
<servlet-name>_ah_warmup</servlet-name>
211+
<servlet-class>com.company.MyWarmupServlet</servlet-class>
212+
//[END custom_warmup_servlet]
213+
214+
//[START warmup_yaml]
215+
inbound_services:
216+
- xmpp_message
217+
- mail
218+
//[END warmup_yaml]
219+
220+
//[START custom_xml_output]
221+
web_xml: |
222+
<error-page>
223+
<error-code>500</error-code>
224+
<location>/errors/servererror.jsp</location>
225+
</error-page>
226+
//[END custom_xml_output]

dump/appidentity.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// [START versioned_hostnames]
2+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
3+
resp.setContentType("text/plain");
4+
Environment env = ApiProxy.getCurrentEnvironment();
5+
resp.getWriter().println("default_version_hostname: "
6+
+ env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"));
7+
}
8+
// [END versioned_hostnames]
9+
10+
// [START asserting_identity_to_Google_APIs]
11+
import com.google.appengine.api.appidentity.AppIdentityService;
12+
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
13+
import java.io.OutputStreamWriter;
14+
import java.net.HttpURLConnection;
15+
import java.net.URL;
16+
import java.util.ArrayList;
17+
// Note that any JSON parser can be used; this one is used for illustrative purposes.
18+
import org.json.JSONObject;
19+
import org.json.JSONTokener;
20+
21+
22+
public String createShortUrl(String longUrl) throws Exception {
23+
try {
24+
ArrayList<String> scopes = new ArrayList<String>();
25+
scopes.add("https://www.googleapis.com/auth/urlshortener");
26+
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
27+
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
28+
// The token asserts the identity reported by appIdentity.getServiceAccountName()
29+
JSONObject request = new JSONObject();
30+
request.put("longUrl", longUrl);
31+
32+
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1");
33+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
34+
connection.setDoOutput(true);
35+
connection.setRequestMethod("POST");
36+
connection.addRequestProperty("Content-Type", "application/json");
37+
connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());
38+
39+
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
40+
request.write(writer);
41+
writer.close();
42+
43+
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
44+
// Note: Should check the content-encoding.
45+
JSONTokener response_tokens = new JSONTokener(connection.getInputStream());
46+
JSONObject response = new JSONObject(response_tokens);
47+
return (String) response.get("id");
48+
} else {
49+
throw new Exception();
50+
}
51+
} catch (Exception e) {
52+
// Error handling elided.
53+
throw e;
54+
}
55+
}
56+
// [END asserting_identity_to_Google_APIs]

dump/backends.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// [START shutdown_1]
2+
LifecycleManager.getInstance().setShutdownHook(new ShutdownHook() {
3+
public void shutdown() {
4+
LifecycleManager.getInstance().interruptAllRequests();
5+
}
6+
});
7+
// [END shutdown_1]
8+
9+
// [START shutdown_2]
10+
while (haveMoreWork() &&
11+
!LifecycleManager.getInstance().isShuttingDown()) {
12+
doSomeWork();
13+
saveState();
14+
}
15+
// [END shutdown_2]
16+
17+
// [START addressing_backends]
18+
import com.google.appengine.api.backends.BackendService;
19+
import com.google.appengine.api.backends.BackendServiceFactory;
20+
21+
BackendService backendsApi = BackendServiceFactory.getBackendService();
22+
23+
// Get the backend handling the current request.
24+
String currentBackendName = backendsApi.getCurrentBackend();
25+
// Get the backend instance handling the current request.
26+
int currentInstance = backendsApi.getCurrentInstance();
27+
// [END addressing_backends]
28+
29+
// [START background_threads]
30+
import com.google.appengine.api.ThreadManager;
31+
import java.util.concurrent.AtomicLong;
32+
33+
AtomicLong counter = new AtomicLong();
34+
35+
Thread thread = ThreadManager.createBackgroundThread(new Runnable() {
36+
public void run() {
37+
try {
38+
while (true) {
39+
counter.incrementAndGet();
40+
Thread.sleep(10);
41+
}
42+
} catch (InterruptedException ex) {
43+
throw new RuntimeException("Interrupted in loop:", ex);
44+
}
45+
}
46+
});
47+
thread.start();
48+
// [END background_threads]

dump/backends.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!-- [START backends] -->
2+
<backends>
3+
<backend name="memdb">
4+
<class>B8</class>
5+
<instances>5</instances>
6+
</backend>
7+
<backend name="worker">
8+
<options>
9+
<fail-fast>true</fail-fast>
10+
</options>
11+
</backend>
12+
<backend name="cmdline">
13+
<options>
14+
<dynamic>true</dynamic>
15+
</options>
16+
</backend>
17+
</backends>
18+
<!-- [END backends] -->
19+
20+
<!-- Since this page does not have executable code, this section is separate
21+
rather than nested, as nesting would have significantly complicated the
22+
include regions. -->
23+
<!-- [START class-example] -->
24+
<backends>
25+
<backend name="cmdline">
26+
<class>B4</class>
27+
</backend>
28+
</backends>
29+
<!-- [END class-example] -->

0 commit comments

Comments
 (0)