Resource Serving Example

The JSR 286 portlet API introduced resource serving with method serveResource(…), which enables portlets to serve resources, for example, markup or binary data (PNG, PDF, etc), from within the portlet context using resource requests. A separate servlet is no longer required. This feature also encourages a better programming model.

This example uses resource serving to generate binary content (PNG image) from a portlet.

Resource Serving Portlet:

package gdf.qa.portlets;
 
import java.io.IOException;
 
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;
 
import com.liferay.util.bridges.mvc.MVCPortlet;
import gdf.qa.util.PortletChartUtilities;
 
/**
 * Portlet implementation class QAMetrics
 */
public class QAMetrics extends MVCPortlet {
    public void serveResource(ResourceRequest request, ResourceResponse response)
            throws PortletException, IOException {
        String resourceID = request.getResourceID();
 
        if (resourceID.equals("chart-image")) {
            PortletChartUtilities.writeChartImageData(request, response);
        }
    }
 
    @Override
    public void doView(RenderRequest renderRequest,
        RenderResponse renderResponse) throws IOException, PortletException {
 
        // Pass the resource URL as a bean called resourceUrl to the view JSP
        ResourceURL chartUrl = renderResponse.createResourceURL();
        chartUrl.setResourceID("chart-image");
        String resourceUrl = chartUrl.toString();
        renderRequest.setAttribute("resourceUrl", resourceUrl);
 
        include(viewJSP, renderRequest, renderResponse);
 
    }
 
}

Data Transfer Object:

package gdf.qa.util;
 
import java.io.*;
import javax.portlet.*;
 
import org.jfree.chart.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
 
public class PortletChartUtilities {
 
    public static void writeChartImageData(ResourceRequest request, ResourceResponse response) throws IOException
    {
        response.setContentType("image/png");
        OutputStream out = response.getPortletOutputStream();
 
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(1.0, "Row 1", "Column 1");
        dataset.addValue(2.0, "Row 1", "Column 2");
        dataset.addValue(3.0, "Row 1", "Column 3");
 
        dataset.addValue(4.0, "Row 2", "Column 1");
        dataset.addValue(5.0, "Row 2", "Column 2");
        dataset.addValue(6.0, "Row 2", "Column 3");
 
        try {
            JFreeChart chart = ChartFactory.createBarChart("Bar Chart", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false);
 
            if (chart != null) {
                CategoryPlot plot = (CategoryPlot) chart.getPlot();
                BarRenderer renderer = (BarRenderer) plot.getRenderer();
                renderer.setItemMargin(0.0);
                ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
            }
        } catch (Exception e) {
            System.err.println(e.toString());
        } finally {
            out.close();
        }
 
    }
 
}

View JSP file view.jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page import="javax.portlet.*"%>
<portlet:defineObjects />
 
<img src="${resourceUrl}"/>

Please remember to import the following libs that can be downloaded from http://www.jfree.org/jfreechart/download.html

jfreechart-libs.png

Screenshot of the portlet:

QAMetrics-portlet.png
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License