Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render page to string in Wicket 1.5

I'm working on upgrading our existing Wicket webapp to 1.5 and have hit a snag in our renderPage function that we use to render our HTML emails.

Previously we used the code referenced/listed in this StackOverflow question and this (currently broken but maybe fixed later) link but that code no longer works as a lot of those classes don't exist in 1.5.

I also found this email thread but it is light on the details and I don't know how to create the WebPage from my pageClass and parameters. http://apache-wicket.1842946.n4.nabble.com/Render-WebPage-to-String-in-Wicket-1-5-td3622130.html

Here is my code:

// Renders a page under a temporary request cycle in order to get the rendered markup
public static String renderPage(Class<? extends Page> pageClass, PageParameters pageParameters)
{
    //get the servlet context
    WebApplication application = (WebApplication) 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);

        if (requestCycle.wasHandled() == false)
        {
            requestCycle.setRequestTarget(new WebErrorCodeResponseTarget(HttpServletResponse.SC_NOT_FOUND));
        }
    }
    finally
    {
        requestCycle.detach();
        requestCycle.getResponse().close();
    }

    return webResponse.toString();
}

Specifically, the code breaks because the WebRequestCycle and BookmarkablePageRequestTarget classes no longer exist. I feel like I should be able to use the StringResponse class some how but I'm missing the link that would help me trigger a render on that response.

Any help would be appreciated. Thanks.

My Final Solution

Using the example that I was directed to by the answer below I ended up with the following code. I'm pasting it here as well so that if that link disappears or is changed with a future version of Wicket then people from the future will still be able to get the answer they need.

I ended up passing in a PageProvider because in some cases I needed to pass in an instantiated Page and in others a pageClass + parameters.

public static String renderPage(final PageProvider pageProvider)
{
    final RenderPageRequestHandler handler = new RenderPageRequestHandler(pageProvider, RedirectPolicy.NEVER_REDIRECT);

    final PageRenderer pageRenderer = Application.get().getPageRendererProvider().get(handler);

    RequestCycle requestCycle = RequestCycle.get();

    final Response oldResponse = requestCycle.getResponse();
    BufferedWebResponse tempResponse = new BufferedWebResponse(null);

    try
    {
        requestCycle.setResponse(tempResponse);
        pageRenderer.respond(requestCycle);
    }
    finally
    {
        requestCycle.setResponse(oldResponse);
    }

    return tempResponse.getText().toString();
}
like image 773
jbarz Avatar asked Oct 09 '22 07:10

jbarz


1 Answers

Check the source code of http://www.wicket-library.com/wicket-examples/mailtemplate/ example.

like image 165
martin-g Avatar answered Oct 13 '22 09:10

martin-g