Setting arbitrary HTTP headers during serveResource

As per portlet specification "Given that the portal/portlet container does not render any additional markup for a serveResource response it is important for the portlet to be able to access the incoming request headers and to be able to set new headers for the response.
A portlet can access the headers of the HTTP client request through the getProperty or getProperties call, like all portlet requests. A portlet can set HTTP headers for the response via the setProperty or addProperty call in the PortletResponse. To be successfully transmitted back to the client, headers must be set before the response is committed. Headers set after the response is committed will be ignored by the portlet container"

So i tried building a sample portlet to see if i can set

public class ServeResourceHttpRequestPortlet extends GenericPortlet{
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering ServeResourceHttpRequestPortlet.doView()");
getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Exiting ServeResourceHttpRequestPortlet.doView()");
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
System.out.println("Entering ServeResourceHttpRequestPortlet.serveResource()");

response.setProperty(ResourceResponse.EXPIRATION_CACHE, "600");
response.setProperty("Expires", "1000")

System.out.println("Request method is " + request.getMethod());
response.getWriter().println("HTTP Method used is " + request.getMethod());
System.out.println("Exiting ServeResourceHttpRequestPortlet.serveResource()");

The doView() method is passing control to the index.jsp which is making HTTP GET call to the resource URL.

When i tried this portlet in the WebSphere Portal i could see that the additional headers that i am setting are getting passed back in the response like this

As you can see the response has Expires and cache-control header that i set in the serveResource method

No comments: