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


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

@Override
	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) {
				itemsTobeUploaded.add(item);
			}
		}

		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
		removeSessionFileItems(request);

		// / 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.

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

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()));
		newDocument.setDescription(description);
		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

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 cloud@inphina.com

Advertisements

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. 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). To know more, send a mail to hello@knoldus.com or visit www.knoldus.com
This entry was posted in Cloud, Java and tagged , , , , . Bookmark the permalink.

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

  1. Pingback: Building Customized Versioning for Google Docs « Inphina Thoughts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s