How to use the resource phase of a portlet ?

Usually when we have to make an AJAX call or when the user should download a file  from a portlet we use the “Resource Phase” of a portlet.

Here are two ways to use this phase :

  • Create a controller that implements the interface “ResourceServingPortlet”. Thus all resource requests would use the method “serveResource”. Using this technique you should manage manually the multiple cases by using request parameters.
  • Create a Spring MVC controller and use the annotation @ResourceMapping

If you are developing your portlets with Spring MVC you should use the annotation @ResourceMapping, and then you have to create resource URL in your JSPs (in the same way as creating action/render URLs).

How to use multiple @ResourceMapping in the same portlet ?

In order to use multiple “Resource phases” in the same portlet you have to give an ID to each method you declared. Then you can associate a resource URL to one of the method by specifying its ID at the URL creation.

Example of use

Controller

This method returns a small JSON object. It should be called via AJAX.

@ResourceMapping("resource_json")
public void getJsonResource(ResourceRequest request, ResourceResponse response) {

    // Get output stream (use Java 7 "try with resources")
    try (final OutputStream os = response.getPortletOutputStream()) {

        // Set response content type to JSON
        response.setContentType("application/json");

        // (Use a proper library for manipulating JSON, do not use String)
        String json = "{'param1':'value1'}";

        // Put bytes in the output stream
        os.write(json.getBytes());

        // Flush the stream and commit response
        response.flushBuffer();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

This method returns a PDF file. It should be called via a HTML link. If the user clicks on it, the downloading of the file will pop up.
Otherwise you can use a JS redirection (window.location) to force the downloading of the file.

public void getFileResource(ResourceRequest request, ResourceResponse response) {
    File pdf = new File("D:/myfile.pdf");
    
    // Set response content type to PDF and set the name of the file to transfer
    response.setProperty("Content-Disposition", "attachment;filename=" + pdf.getName());
    response.setContentType("application/pdf");
    
    // Create a stream from the file and get output stream (use Java 7 "try with resources")
    try (final FileInputStream fis = new FileInputStream(pdf);
        final OutputStream os = response.getPortletOutputStream()) {

        // Apache commons should be your best friend
        os.write(IOUtils.toByteArray(fis));
        
        // Flush the stream and commit response
        response.flushBuffer();
        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

JSP

Then in your JSP you just have to declare your resource URLs using JSTL taglib “portlet” :

<portlet:resourceURL id="resource_json" var="resourceJsonURL" />

<portlet:resourceURL id="resource_file" var="resourceFileURL" />

Et voilà 🙂

One thought on “How to use the resource phase of a portlet ?

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