Skip to content

Instantly share code, notes, and snippets.

@armhold
Created August 17, 2011 17:18
Show Gist options
  • Save armhold/1152059 to your computer and use it in GitHub Desktop.
Save armhold/1152059 to your computer and use it in GitHub Desktop.
render a wicket web page to a String
public static String renderPage(Class<? extends Page> pageClass, PageParameters pageParameters)
{
WebApplication application = WebApplication.get();
ServletContext context = application.getServletContext(); //fake a request/response cycle
MockHttpSession servletSession = new MockHttpSession(context);
servletSession.setTemporary(true);
MockHttpServletRequest servletRequest = new MockHttpServletRequest(application, servletSession, context);
MockHttpServletResponse servletResponse = new MockHttpServletResponse(servletRequest); //initialize request and response
servletRequest.initialize();
servletResponse.initialize();
WebRequest webRequest = new ServletWebRequest(servletRequest);
BufferedWebResponse webResponse = new BufferedWebResponse(servletResponse);
webResponse.setAjax(true);
WebRequestCycle requestCycle = new WebRequestCycle(application, webRequest, webResponse);
requestCycle.setRequestTarget(new BookmarkablePageRequestTarget(pageClass, pageParameters));
try
{
requestCycle.getProcessor().respond(requestCycle);
log.info("Response after request: " + webResponse.toString());
if (! requestCycle.wasHandled())
{
requestCycle.setRequestTarget(new WebErrorCodeResponseTarget(HttpServletResponse.SC_NOT_FOUND));
}
requestCycle.detach();
}
finally
{
requestCycle.getResponse().close();
}
return webResponse.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment