Access-Control-Allow-Origin In JSONP Responses

A quick note for today: JSONP responses need to include an Access-Control-Allow-Origin header set to a wildcard. If this header isn’t included, browsers won’t allow JSONP responses to be read in by Javascript.

The header key and value should look like this:

Access-Control-Allow-Origin: *

Here’s an example of this header in Java:

resp.setHeader("Access-Control-Allow-Origin", "*");

The variable resp represents a HttpServletResponse reference. Make sure to add this line before writing the body of the response to the client.

Communicating With Sockets On Compute Engine

Writing custom applications on Compute Engine requires the use of sockets to communicate with clients. Here’s a code example demonstrating how to read and write to sockets in Java.

Assume that client_socket is the socket communicating with the client:

/**
 * Contains the socket we're communicating with.
 */
Socket client_socket;

Create a socket by using a ServerSocket (represented by server_socket ) to listen to and accept incoming connections:

Socket client_socket = server_socket.accept();

Then extract a PrintWriter and a BufferedReader from the socket. The PrintWriter out object sends data to the client, while the BufferedReader in object lets the application read in data sent by the client:

/**
 * Handles sending communications to the client.
 */
PrintWriter out;
/**
 * Handles receiving communications from the client.
 */
BufferedReader in;
try {
    //We can send information to the client by writing to out.
    out = new PrintWriter(client_socket.getOutputStream(), true);
    //We receive information from the client by reading in.
    in = new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
    /**
     * Start talking to the other server.
     */
    //Read in data sent to us.
    String in_line = in.readLine();
    //Send back information to the client.
    out.println(send_info);
}//end try
catch (IOException e) {
    //A general problem was encountered while handling client communications.
}

A line of text sent by the client can be read in by calling in.readLine() demonstrated by the in_line string. To send a line of text to the client, call out.println(send_info) where send_info represents a string. If an error occurs during communication, an IOException will be thrown and caught by the above catchstatement.

Remember to add the following imports:

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.BufferedReader;

App Engine Default Time & Date

The default time zone for the Java runtime on App Engine is UTC (frequently referred to as GMT). The following code will record the current time and date in UTC:

Date current_date = new Date();
String date = new java.text.SimpleDateFormat("MM-dd-yyyy").format(current_date);
String time = new java.text.SimpleDateFormat("hh:mm:ss aa").format(current_date);

For instance, date would be set to 09-21-2013 (September 21, 2013) and time would be set to 01:40:42 AM . If your application needs to record time in a different time zone, use the setTimeZone method of SimpleDateFormat .

Don’t forget to import the Date class:

import java.util.Date;

HTTP POST URLFetch Using The Low Level Java API

Here’s a code snippet demonstrating how to generate a POST request using the low level urlfetch API.

The variables post_url and post_data represent the request URL and the content of the POST as strings. The response from the urlfetch is stored in response_content . If the request was unsuccessful (the target server returned a non-200 HTTP status code) a RuntimeException will be thrown.

HTTPRequest request = new HTTPRequest(new URL(post_url), HTTPMethod.POST);
request.setHeader(new HTTPHeader("User-Agent", "Example Application"));
request.setPayload(post_data.getBytes("UTF-8"));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
//If the response wasn't successful, throw an exception.
if (response.getResponseCode() != 200) {
    throw new RuntimeException("Response code was not 200: " + response.getResponseCode());
}
String response_content = new String(response.getContent());

Remember to import the URLFetch library:

import com.google.appengine.api.urlfetch.*;

Sleeping An Application In Java

Some applications need to pause execution for a short amount of time. In App Engine, there are two ways to do that.

For short pauses, most programming languages have the concept of sleep operations. Here’s how to sleep for 2,000 milliseconds (2 seconds) in Java:

try {
    Thread.sleep(2000);
}
catch (InterruptedException e) {
}

If you want to pause the application for a few seconds, using the sleep function is a good idea. But if you need a longer pause – more than a minute or so – it’s a better idea to use the Task Queue’s countdownMillis function.

Here’s how to create a task that will wait for 5 minutes, then launch off another request:

TaskOptions task = TaskOptions.Builder.withCountdownMillis(5 * 60 * 1000);

In short, whenever you need a long pause in execution, queue up a task with a countdownMillis() delay. Then the task can call a request handler which can continue any application logic needed.

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.

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;

Retrieving All Entities Older Than An Arbitrary Date

Here’s a Java code example to search the datastore for all entities within a kind older than a given date.

The variable kind is the entity kind being searched, add_date is a property on each entity that is set to the date the entity was created, and entities is a java.util.List object containing the returned entities. The variable time_point represents a point in time; we query the datastore for all entities with a date less than that.

/**
 * Retrieve all entities older than a set amount of time.
 */
Query q = new Query(kind);
//Represents a point in time 48 hours ago.
Date time_point = new Date((new Date().getTime()) - (1000 * 60 * 60 * 48));
Query.Filter time_point_filter = new Query.FilterPredicate("add_date", Query.FilterOperator.LESS_THAN_OR_EQUAL, time_point);
q.setFilter(time_point_filter);
PreparedQuery pq = DatastoreServiceFactory.getDatastoreService().prepare(q);
List<Entity> entities = pq.asList(FetchOptions.Builder.withLimit(30));
System.out.println(entities.size() + " entities returned.");

Suppose you wanted to loop through all of the returned entities. Here’s an example:

//Loop through all entities
for (int i = 0; i < entities.size(); i++) {
    Entity entity = entities.get(i);
    System.out.println("Entity: " + entity.toString());
    //Do something with the entity variable.
}//end loop going through all entities

Basic Java Task Queue Code

Here’s a simple example of how to use the task queue in Java. The code below retrieves the default queue and queues up a task. The task will request the /example_url path and pass in the parameter parameter1 with the value parameter1_value.

Queue queue = QueueFactory.getDefaultQueue();
TaskOptions task = TaskOptions.Builder.withUrl("/example_url");
task.param("parameter1", parameter1_value);
queue.add(task);

Remember to import the task queue classes:

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;