Custom Versioning for Google Docs: Handling File Operations on Google App Engine

Table of contents
Reading Time: 2 minutes

In the last post you saw how easy it was to use the functionality of GWTUpload and upload the file to the server. In this post we would look at the file handling for Google App Engine.

As you would know that File operations are not allowed on the app engine. You can however, read from a file if it is uploaded as a part of your application war or your application can use the IO classes which are useful for reading from the file system. The whitelisted list of classes is available here, but for us, the following are supported File, FileInputStream, FileReader, or RandomAccessFile.

By default the uploaded files are stored in the session for further processing by the GWTUpload servlet. We get all the files which are present in the session with the following code

[sourcecode language=”java”]
public String executeAction(HttpServletRequest request, List sessionFiles) throws UploadActionException {
String response = "";
List<FileItem> itemsTobeUploaded = new ArrayList();
String description = getDescription(request);

for (FileItem item : sessionFiles) {
if (item.isFormField() == false) {

for (FileItem fileItem : itemsTobeUploaded) {
try {
documentService.uploadFileToGoogleDocs(fileItem, description);
} catch (Exception e) {
// catch all exceptions here for the purpose of this blogpost
// / Remove files from session because we have a copy of them

// / Send your customized message to the client.
return response;

As you would notice, we are overriding the executeAction method of the AppEngineUploadAction servlet. Here we are doing processing which is very specific to our application.

Here, we are passing the FileItem to the documentService which would upload the document on Google Docs. FileItem is an apache commons fileupload interface. It represents a file or a form item which was received within a multipart/form-data post request.

The uploadFileToGoogleDocs does some logic with Google Docs which we are not I nterested in right now. That would be the details of the post in which we talk about calling Google Docs from App Engine.

[sourcecode language=”java”]
public boolean uploadFileToGoogleDocs(FileItem file, String description) throws MalformedURLException, IOException, ServiceException {
boolean uploadStatus = false;

//… some logic

uploadNewFile(docsService, file, description);

//… some more logic


For now let us look at the details of the uploadNewFile(docsService, file, description); method

[sourcecode language=”java”]
private void uploadNewFile(DocsService docsService, FileItem file, String description) throws AuthenticationException, IOException,
ServiceException, MalformedURLException {
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();

DocumentListEntry newDocument = new DocumentListEntry();
newDocument.setMediaSource(new MediaByteArraySource(file.get(), mimeType));
newDocument.setTitle(new PlainTextConstruct(file.getName()));
System.out.println("Uploaded document with description " + description);
docsService.insert(new URL(DOCUMENT_URL), newDocument);

As you would notice that we get the contents of the file not as a file but as a byte[]. We have a method in DocumentListEntry called setMediaSource

public void setMediaSource(MediaSource mediaSource)
Description copied from interface: IMediaEntry
Sets the MediaSource that contains the media data for the entry.

Through this method, we can set the MediaSource to be a ByteArray. The file.get() method results in a byte[] and the mimeType is easily obtained with file.getName().getMimeType()
Hence we are able to extract the contents of the file from the app engine and pass it onto the Google document service.
In the next post, we would get into the details of Google Document Service and see how we can use it to work with Google Docs.

Inphina, as an expert on Google App Engine and Google Apps has helped more than 10 medium to large organizations help take advantage of the cloud by building, migrating or re-engineering their complex line of business applications to the cloud thereby making significant reductions in their capex expenditure. If you are interested in an assessment send us a mail at

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to or visit

1 thought on “Custom Versioning for Google Docs: Handling File Operations on Google App Engine3 min read

Comments are closed.