Skip to content

Commit

Permalink
Began work on exporting code snippets from docs
Browse files Browse the repository at this point in the history
  • Loading branch information
elibixby committed Mar 30, 2015
1 parent 572674c commit 5f90f59
Show file tree
Hide file tree
Showing 16 changed files with 1,204 additions and 0 deletions.
35 changes: 35 additions & 0 deletions dump/LogFilterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mysite.server;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class LogFilterImpl implements Filter {

private FilterConfig filterConfig;
private static final Logger log = Logger.getLogger(LogFilterImpl.class.getName());

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
log.warning("Log filter processed a " + getFilterConfig().getInitParameter("logType")
+ " request");

filterChain.doFilter(request, response);
}

public FilterConfig getFilterConfig() {
return filterConfig;
}

public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

public void destroy() {}

}
226 changes: 226 additions & 0 deletions dump/appconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// [START pagespeed_example]
<pagespeed>
<domain-to-rewrite>*.cdn.myapp.com</domain-to-rewrite>
<domain-to-rewrite>www.flickr.com</domain-to-rewrite>
<url-blacklist>http://*/*.svg</url-blacklist>
<url-blacklist>http://secure.foo.com/*</url-blacklist>
<enabled-rewriter>CollapseWhitespace</enabled-rewriter>
<disabled-rewriter>CombineJs</disabled-rewriter>
<disabled-rewriter>ProxyImages</disabled-rewriter>
</pagespeed>
</appengine-web-app>
// [END pagespeed_example]

// [START custom_error_example]
<static-error-handlers>
<handler file="default_error.html" />
<handler file="over_quota.html" error-code="over_quota" />
</static-error-handlers>
// [END custom_error_example]

// [START expiration_example]
<static-files>
<include path="/**.png" expiration="4d 5h" />
</static-files>
// [END expiration_example]

// [START expiration_example_yaml]
static_files:
- include: /**.png
expiration: 4d 5h
// [END expiration_example_yaml]

// [START static_codesample_xml]
<static-files>
<include path="/**.png" />
<exclude path="/data/**.png" />
// [END static_codesample_xml]

// [START static_codesample_yaml]
static_files:
- include: /**.png
- exclude: /data/**.png
// [END static_codesample_yaml]

// [START resource_codesample_xml]
<resource-files>
<include path="/**.xml" />
<exclude path="/feeds/**.xml" />
// [END resource_codesample_xml]

// [START resource_codesample_yaml]
resource_files:
- include: /**.xml
- exclude: /feeds/**.xml
// [END resource_codesample_yaml]

//[START header_codesample_xml]
<static-files>
<include path="/my_static-files" >
<http-header name="Access-Control-Allow-Origin" value="http://example.org" />
</include>
</static-files>
//[END header_codesample_xml]

//[START header_codesample_yaml]
static_files:
- include: /static/*
http_headers:
Access-Control-Allow-Origin: http://example.org
//[END header_codesample_yaml]

//[START mapper_codesample_yaml]
handlers:
- url: /red/*
servlet: mysite.server.TeamServlet
init_params:
teamColor: red
bgColor: "#CC0000"
name: redteam
- url: /blue/*
servlet: mysite.server.TeamServlet
init_params:
teamColor: blue
bgColor: "#0000CC"
name: blueteam
- url: /register/*
jsp: /register/start.jsp
- url: /*.special
filter: mysite.server.LogFilterImpl
init_params:
logType: special
//[END mapper_codesample_yaml]

//[START environment_variables]
<system-properties>
<property name="myapp.maximum-message-length" value="140" />
<property name="myapp.notify-every-n-signups" value="1000" />
<property name="myapp.notify-url" value="http://www.example.com/signupnotify" />
</system-properties>

<env-variables>
<env-var name="DEFAULT_ENCODING" value="UTF-8" />
</env-variables>
//[END environment_variables]

//[START environment_variables_yaml]
system_properties:
myapp.maximum-message-length: 140
myapp.notify-every-n-signups: 1000
myapp.notify-url: http://www.example.com/signupnotify
env_variables:
DEFAULT_ENCODING: UTF-8
context_params:
rack.env: production
//[END environment_variables_yaml]

//[START inbound_services]
<inbound-services>
<service>mail</service>
<service>warmup</service>
</inbound-services>
//[END inbound_services]

//[START admin_console_custom_pages]
<admin-console>
<page name="Blog Comment Admin" url="/blog/admin/comments" />
<page name="Create a Blog Post" url="/blog/admin/newentry" />
</admin-console>
//[END admin_console_custom_pages]

//[START about_app_yaml_example]
application: myapp
version: alpha-001
runtime: java
api_version: 1

handlers:
- url: /admin/*
login: admin
//[END about_app_yaml_example]

//[START minimal_appengine_web_xml]
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>_your_app_id_</application>
<version>alpha-001</version>
<threadsafe>true</threadsafe>
</appengine-web-app>
//[END minimal_appengine_web_xml]

//[START secure_handler_yaml]
handlers:

- url: /youraccount/*
login: required
secure: always
//[END secure_handler_yaml]

//[START servlet_listeners]
listeners:
- com.example.MyListener
- com.example.MyOtherListener
//[END servlet_listeners]

//[START login_example_yaml]
handlers:

- url: /profile/*
login: required

- url: /admin/*
servlet: com.example.AdminServlet
login: admin
//[END login_example_yaml]

//[START welcome_files]
welcome_files:
- index.jsp
- index.html
//[END welcome_files]

//[START load_on_startup]
<servlet>
<servlet-name>my-servlet</servlet-name>
<servlet-class>com.company.MyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
//[END load_on_startup]

//[START ServletContextListener]
<listener>
<listener-class>com.company.MyListener</listener-class>
</listener>
//[END ServletContextListener]

//[START listener_filter]
public class MyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// This will be invoked as part of a warmup request, or the first user
// request if no warmup request was invoked.
}
public void contextDestroyed(ServletContextEvent event) {
// App Engine does not currently invoke this method.
}
}
//[END listener_filter]

//[START custom_warmup_servlet]
<servlet>
<servlet-name>_ah_warmup</servlet-name>
<servlet-class>com.company.MyWarmupServlet</servlet-class>
//[END custom_warmup_servlet]

//[START warmup_yaml]
inbound_services:
- xmpp_message
- mail
//[END warmup_yaml]

//[START custom_xml_output]
web_xml: |
<error-page>
<error-code>500</error-code>
<location>/errors/servererror.jsp</location>
</error-page>
//[END custom_xml_output]
56 changes: 56 additions & 0 deletions dump/appidentity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// [START versioned_hostnames]
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
Environment env = ApiProxy.getCurrentEnvironment();
resp.getWriter().println("default_version_hostname: "
+ env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"));
}
// [END versioned_hostnames]

// [START asserting_identity_to_Google_APIs]
import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
// Note that any JSON parser can be used; this one is used for illustrative purposes.
import org.json.JSONObject;
import org.json.JSONTokener;


public String createShortUrl(String longUrl) throws Exception {
try {
ArrayList<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/urlshortener");
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(scopes);
// The token asserts the identity reported by appIdentity.getServiceAccountName()
JSONObject request = new JSONObject();
request.put("longUrl", longUrl);

URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?pp=1");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
request.write(writer);
writer.close();

if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// Note: Should check the content-encoding.
JSONTokener response_tokens = new JSONTokener(connection.getInputStream());
JSONObject response = new JSONObject(response_tokens);
return (String) response.get("id");
} else {
throw new Exception();
}
} catch (Exception e) {
// Error handling elided.
throw e;
}
}
// [END asserting_identity_to_Google_APIs]
48 changes: 48 additions & 0 deletions dump/backends.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// [START shutdown_1]
LifecycleManager.getInstance().setShutdownHook(new ShutdownHook() {
public void shutdown() {
LifecycleManager.getInstance().interruptAllRequests();
}
});
// [END shutdown_1]

// [START shutdown_2]
while (haveMoreWork() &&
!LifecycleManager.getInstance().isShuttingDown()) {
doSomeWork();
saveState();
}
// [END shutdown_2]

// [START addressing_backends]
import com.google.appengine.api.backends.BackendService;
import com.google.appengine.api.backends.BackendServiceFactory;

BackendService backendsApi = BackendServiceFactory.getBackendService();

// Get the backend handling the current request.
String currentBackendName = backendsApi.getCurrentBackend();
// Get the backend instance handling the current request.
int currentInstance = backendsApi.getCurrentInstance();
// [END addressing_backends]

// [START background_threads]
import com.google.appengine.api.ThreadManager;
import java.util.concurrent.AtomicLong;

AtomicLong counter = new AtomicLong();

Thread thread = ThreadManager.createBackgroundThread(new Runnable() {
public void run() {
try {
while (true) {
counter.incrementAndGet();
Thread.sleep(10);
}
} catch (InterruptedException ex) {
throw new RuntimeException("Interrupted in loop:", ex);
}
}
});
thread.start();
// [END background_threads]
29 changes: 29 additions & 0 deletions dump/backends.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!-- [START backends] -->
<backends>
<backend name="memdb">
<class>B8</class>
<instances>5</instances>
</backend>
<backend name="worker">
<options>
<fail-fast>true</fail-fast>
</options>
</backend>
<backend name="cmdline">
<options>
<dynamic>true</dynamic>
</options>
</backend>
</backends>
<!-- [END backends] -->

<!-- Since this page does not have executable code, this section is separate
rather than nested, as nesting would have significantly complicated the
include regions. -->
<!-- [START class-example] -->
<backends>
<backend name="cmdline">
<class>B4</class>
</backend>
</backends>
<!-- [END class-example] -->
Loading

3 comments on commit 5f90f59

@tmatsuo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who reviewed the code above? Please make sure having someone review it before merging :)

@tmatsuo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but I'm going to revert this change in one hour

@tmatsuo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted

Please sign in to comment.