PDA

View Full Version : How to get HTML string out from RenderResponse


osingh
Dec 5th, 2007, 08:49 AM
Hello,

We have a portlet which generates a html fragment. We want to cache this fragment using OSCache. In order to do this we are trying to introduce our oscache caching logic on the html fragment generated by the doView method of the generic protlet.

Example code below

public class HelloWorldPortlet extends GenericPortlet {
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering HelloWorldPortlet.doView");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello World</h1>");
}

}



I am unable to get the html string from the RenderResponse object.
We have now written an interceptor to trap the doView method. Refering to the code below for the method afterRenderCompletion().


public class PortletNameInterceptor extends PortletApplicationObjectSupport
implements HandlerInterceptor, PortletContextAware {

private PortletContext portletContext;

public void setPortletConfig(PortletContext portletContext) {
this.portletContext = portletContext;
}

public void afterRenderCompletion(RenderRequest request, RenderResponse response, Object obj, Exception ex) throws Exception {
System.out.println("The doView method is completed");


}

}


Is there a way to get the html output in form of string from the RenderResponse response object inside afterRenderCompletion method.
Any suggestion, help will be appretiated.

Thanks Om

johnalewis
Dec 7th, 2007, 11:55 AM
Unfortunately, I don't think there is an easy way to do this right now. Getting the markup back out of the response is not possible, both on the servlet and portlet side of things.

One way to attack this might be to extend the ViewRendererServlet, override the renderView method and decorate the HttpServletResponse with something that captures the markup and makes a copy of it into a response attribute. You could also do this with a Servlet Filter instead of extending ViewRendererServlet. Then you could use an interceptor around your portlet requests to catch the markup after a successful render and cache it, and also to use the cached markup for the response when appropriate.

Just some thoughts for the future: JSR 286 has a more sophisticated caching mechanism that allows for caching across multiple users and for validation based caching in addition to expiration based caching. JSR 286 also provides Portlet Filters, which will make capturing the output much easier as well.