Favicon.ico In Errors Screen

Some applications may see the path /favicon.ico listed in the errors screen within the admin console. The error will look similar to the following image:

Browsers request favicon.ico to display in the address bar and as the icon in a bookmark. If no favicon is available, browsers will substitute a default icon – for example, Firefox displays an icon that looks like a blank page.

Setting a favicon isn’t required, but is useful for branding your web site. Once you create an icon, save it as favicon.ico in the root folder of your application.

Servlet doOptions() For CORS Preflight Check

The following code sample adds a doOptions method within a servlet and sends the appropriate headers in response to a CORS preflight request.

A CORS preflight request is a HTTP OPTIONS request that most browsers send prior to making an AJAX-JSONP call. The browser expects a series of access control headers stating if the server allows cross-domain information sharing. The settings in the code below will fit the needs of the vast majority of AJAX-JSONP applications.

public void doOptions(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    //The following are CORS headers. Max age informs the 
    //browser to keep the results of this call for 1 day.
    resp.setHeader("Access-Control-Allow-Origin", "*");
    resp.setHeader("Access-Control-Allow-Methods", "GET, POST");
    resp.setHeader("Access-Control-Allow-Headers", "Content-Type");
    resp.setHeader("Access-Control-Max-Age", "86400");
    //Tell the browser what requests we allow.
    resp.setHeader("Allow", "GET, HEAD, POST, TRACE, OPTIONS");
}

DeadlineExceededException at java.util.zip.ZipFile.open

During instance startup, large applications may see a DeadlineExceededException and the following stack trace:

Failed startup of context 
    com.google.apphosting.utils.jetty.RuntimeAppEngineWebAppContext
com.google.apphosting.api.DeadlineExceededException:
    This request started at (time) and was still executing at (time + 60 seconds).
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:227)
at java.util.zip.ZipFile.<init>(ZipFile.java:156)
at java.util.jar.JarFile.<init>(JarFile.java:153)
at java.util.jar.JarFile.<init>(JarFile.java:90)

This exception (including the reference to ZipFile.open ) means that App Engine was unable to extract the application’s JAR file within the 60 second request limit.

Generally, this means that the application is too large. Try to reduce the app’s size by removing unused code. Also, cut down on the size of included libraries by removing unused portions. Running the application through a code optimizer can also help.

This error can also appear during times when App Engine is experiencing an outage or is slow in general. In this case, the error will most likely resolve itself when App Engine returns to normal serving status.

Releasing A Static IP In Compute Engine

Compute Engine allows static IPs to be reserved, even if no VM is attached. However, IPs that are unattached to VMs or load balancers are charged at $0.01 per hour. To avoid this charge, it’s a good idea to release IPs that are no longer in use.

To release a static IP, first go to the Compute Engine section of the Google Cloud Console. Select the Networks option:

A list of the IPs allocated to the project will be displayed:

Select the IP to remove and click the link labeled Release IP address:

You’ll see a confirmation box next. Press Yes.

The console will need a few moments to release the IP:

Stacking Delays On Tasks

One of my favorite task queue tricks is to add a stacking delay whenever multiple tasks need to be added. Here’s a sample piece of code::

TaskOptions task = TaskOptions.Builder.withUrl("/example");
task.param(parameter_name, parameter_value);
//Stacking delay here. Every subsequent task is delayed 
//an extra 5 minutes.
task.countdownMillis((1000 * 5) + (i * 5 * 60 * 1000));
Queue queue = QueueFactory.getDefaultQueue();
queue.add(task);

Notice the i variable in the countdownMillis function. The countdown function tells the task to delay a certain amount of milliseconds.

Placing this code within a for loop (and with i as the loop counter) causes each added task to be delayed for 5 seconds, plus an additional 5 minutes over the previous task. To see how it works, consider how the i variable is processed: the first task (when i = 0) will execute in 5 seconds (due to the 1000 times 5 part – 5,000 milliseconds translates into 5 seconds). The second task (when i = 1) will execute in 5 minutes & 5 seconds (1000 * 5 + 1 * 5 * 60 * 1000 turns into 5,000 + 1 * 300,000 which is 305,000 milliseconds, which is also 5 minutes, 5 seconds). The third task will wait for 605,000 milliseconds, which is 10 minutes plus 5 seconds, and so forth.

Staggering tasks like this can make an application run smoother and with fewer spikes in resource usage.

CORS Preflight Request Testing In cURL

When browsers send AJAX-JSONP requests, they often send a “preflight request” before the JSONP call. This request is a HTTP OPTIONS call asking the server whether it supports the cross origin resource sharing specification (in other words, JSONP requests).

To test a server’s support for cross origin resource sharing (CORS), you can use the cURL utility to emulate a HTTP OPTIONS request. A server that supports CORS will return a number of Access-Control headers specifying the requests it supports. Here’s an example cURL command:

curl -H "Origin: http://www.example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: X-Requested-With" \
  -X OPTIONS --verbose \
  http://ip.jsontest.com/

Here’s an example of a proper CORS preflight response:

Access-Control-Allow-Origin is set to a wildcard, which means that all domains are permitted to make requests to it. Access-Control-Max-Age means that the results of this preflight request can be saved for 86,400 seconds (1 day). Access-Control-Allow-Methods means that GET and POST requests are supported.

Cloudflare Error

Many App Engine applications use Cloudflare for caching, SSL, and other services. However if Cloudflare can’t reach your website (for example, if the request is taking too long to finish) the above screen will be shown to the user.

Extract All HTTP Headers In Java

Here’s a code snippet that extracts all HTTP request headers and loops through them.

The variable req represents a javax.servlet.http.HttpServletRequest reference, while header_name and header_value represent the name and value of the header. A single header name may have multiple values; if so, header_value will record the first value listed.

Enumeration<String> headers = req.getHeaderNames();
//Loop through all headers
while (headers.hasMoreElements()) {
    String header_name = headers.nextElement();
    String header_value = req.getHeader(header_name);
    //Do something with the header name and value
}//end while loop going through headers.

Remember that HTTP header names are case-insensitive. If you’re doing any comparisons, you may want to turn the name into a lowercase form:

String header_name_lowercase = header_name.toLowerCase();

Don’t forget to import the Enumeration class:

import java.util.Enumeration;

Clearing Memcache

A quick note today: If you upload new data to the datastore via the bulk uploader or change your application’s data model, you should flush your application’s memcache to prevent stale data from being served to browsers. To do this, go to the Memcache Viewer screen (under the Data heading in the navigation bar) and press the button marked Flush Cache:

Looking Up App Engine Issues

Found a bug within App Engine? Want to request a feature? The best way to notify Google is to file an issue within App Engine’s tracker, located at: https://code.google.com/p/googleappengine/issues/list .

You can file an issue by clicking the New Issue button in the top left corner of the page:

But before you file a new issue, make sure that someone else hasn’t already filed the same issue. You can search issues by using the search box on the upper portion of the page:

If you don’t want to search, you can browse through the issues by using the dropdown boxes in each header:

Here’s an example of what an issue looks like: