Handling multipart content types

The HTTP protocol is ready to accept, from a client, a lot of data and/or large chunks of data, at once. A way to achieve this is to use a specific encoding type: multipart/form-data. Such requests will have a body that can hold several data pieces formatted differently and attributed with different names. So, Play! 2 is a web framework that fits into HTTP as much as possible; that's why it deals with such requests goods, and provides an API that hides almost all of the tricky parts.

In this section, we'll see how one could upload an image along with some caption text that will be attached to a specific chat.

Before diving into the workflow, let's first create the holding structure: Image.

Handling multipart content types

This newly introduced type is not hard to understand as well; only two things should be pointed out:

  • The pic() method that relies on the filePath field to recover the file itself. It uses a File instance to memorize subsequent calls.
  • The enum type that prepares the action logic to filter the incoming files based on the given MIME type.

    Note

    This logic could also be defined in the validate method.

These instances are always locked in with the connected user who uploaded it and will be added to a Chat instance. This will allow a chatroom to display all attached images with their caption beside the messages themselves.

Now we're ready to look at the file upload itself by paying some attention to the last action of the Chats controller, that is, receiveImage.

Handling multipart content types

As we are used to simplifying the code (Play! 2 is there to ease our work, after all) and to get straight to the point, we reflected this in our receiveImage action..

In a very few lines, we declared a new action that expects requests to be multipart encoded containing at least two parts, where the first is a map of data (no matter how this map is encoded) to fill in imageForm (essentially a caption). The second will be the image part.

After binding the request with the form and verifying that no errors have occurred, we can move to the body content in order to recover the binary data that was sent along with its metadata: the file content, its content type, its length, and so on.

That was quite an intuitive thing to do – asking the body to be parsed as a multipart/multidata and and get it as an Http.MultipartFormData object, which has a getFile method that returns an Http.MultipartFormData.FilePart value. To understand why we didn't specify a body parser, recall that Play! 2 is able, most of the time, to discover which method fits best by itself. The Http.MultipartFormData.FilePart type is not only allowing us to recover the content as a file, but also its key in the multipart body, its filename header, and (especially) its content type.

Having all of these things in hand, we are now able to check the content-type validity against the image's enum, and to store the image by getting the file path of the provided file.

Note

This file path will target the Temp directory of your machine. In the real world, the file should be relocated in a dedicated folder or maybe on an S3 repository.

Et voilà! We have now learned about some of the features that can provide a very simple forum. The following screenshot shows what it could look like (without any efforts on the design, of course). First, the forms to show and enter archived and active chats:

Handling multipart content types

On entering an active chat, let's say the one named Today, we reach a page similar to the one shown next:

Handling multipart content types

Using the Attach an image form, we can select an image on our filesystem to be sent to the server. The result obtained is shown as follows:

Handling multipart content types

Until now, we have spoken about handling various content types coming from the outside world, but what about our application having to render content other than HTML? That's what we're about to see next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset