Generating Callbacks For JSONP Code

Here’s a code snippet demonstrating how to wrap JSON text within a JSONP response.

The text of the JSON object is in the string json. The variable resp represents a HttpServletResponseobject. Callback represents contents of the HTTP parameter callback , which should be set with the name of the Javascript function to call. Before calling this code, it’s a good idea to validate that parameter.

String callback = req.getParameter("callback");
String json_text = callback + "(" + json + ");";
resp.getWriter().println(json_text);

Here’s an example of a simple JSONP response:

findIP({"ip": "8.8.8.8"});

This response will call the JS function findIP (the callback parameter contents), with an object containing the property ip (the JSON data).

Extracting Files From A Zip File

Here’s a code snippet demonstrating how to download a zip file and extract the files contained within.

The variable zip_url_string represents the URL to download the zip file from. To read in each file, put your code after the BufferedReader statement.

/**
 * Our first task is to grab the file, and open up a ZipInputStream to decompress the file.
 * A ZipEntry processes each entry within the ZipInputStream.
 */
URL url_addr = new URL(zip_url_string);
HttpURLConnection con = (HttpURLConnection)url_addr.openConnection();
InputStream fis = con.getInputStream();
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry = zis.getNextEntry();
/**
 * We are going to loop through each ZipEntry until we get to a null entry (which 
 * represents the end of the ZipEntry list within the ZipInputStream).
 **/
while (entry != null) {
    //Collect the entry name. This is the filename, including any directory information.
    //Do any validation or processing needed.
    String entry_name = entry.getName().trim();
    //Create a BufferedReader to read in the contents of this file contained within the zip.
    InputStreamReader isr = new InputStreamReader(zis);
    BufferedReader reader = new BufferedReader(isr);
    /**
     * Do something here with the file. Use BufferedReader to read it in, then  
     * process it as necessary.
     */
    //Grab the next entry for processing
    entry = zis.getNextEntry();
}//end while loop going through ZipEntry entries

If the zip file being downloaded is especially large, you may want to add a longer timeout:

con.setConnectTimeout(milliseconds_to_wait);

Remember to add the appropriate import statements:

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.util.zip.*;

Java GET Using java.net

Here’s a simple code snippet showing how to fetch the contents of a URL. The URL to fetch is in the string url_string , and html stores the contents of the fetched file.

URL url = new URL(url_string);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String html = "";
String line = "";
while ((line = reader.readLine()) != null) {
    html += line;
}
reader.close();

This code may throw a java.io.IOException if an error is encountered while fetching the URL.

This code requires the below imports:

import java.net.URL;
import java.io.InputStreamReader;
import java.io.BufferedReader;

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.

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;

CRLF In HTTP Response Headers

A quick note: App Engine will convert CRLF characters in headers (new line characters, such as /r and /n) into underscore characters.

For example, setting the following header should cause Line One and Line Two to appear on two different lines:

resp.setHeader("Example-Header-Name", "Line One \r\n Line Two");

App Engine will run this code, but it will convert the newline characters into two underscore characters before sending the header to the client browser.

While the HTTP 1.1 specification allows header fields to be split across multiple lines (RFC 2616 Section 4.2), App Engine apparently doesn’t allow this. It isn’t too much of an inconvenience, since response headers can be grouped up into one line, but it’s a good idea to keep this limitation in mind especially when communicating with APIs.

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");
}