You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jack Nuzbit <fn...@googlemail.com> on 2009/08/17 12:29:54 UTC

T5.1: no post-redirect on invalid forms

Hi folks,
I thought I'd try out a RequestExceptionHandler that could catch custom form
validation exceptions and render the response directly rather than use the
standard redirect response.
This means I can avoid using sessions entirely and in my limited testing it
works perfectly but was wondering if anyone has any thoughts about this or
knows any reasons why it could cause problems.

Cheers,

Jack


public class LoginPage
{

    @Property
    private String email;
    @Property
    private String password;

    void onSubmitFromLogin() throws PageValidationException {
       throw new PageValidationException(this);
    }
}

public class PageValidationException extends Exception {

    private Object page;

    public PageValidationException(Object page) {
        super();
        this.page = page;
    }

    public Object getPage() {
        return page;
    }

}


public class PageValidationExceptionHandler implements
RequestExceptionHandler
{
    private final RequestPageCache pageCache;

    private final PageResponseRenderer pageRenderer;

    private final ResponseRenderer responseRenderer;

    private final Logger logger;

    private final Response response;

    public PageValidationExceptionHandler(

            RequestPageCache pageCache,
            ResponseRenderer responseRenderer,
            PageResponseRenderer pageRenderer,
            Logger logger,
            Response response)

    {
        this.pageCache = pageCache;
        this.pageRenderer = pageRenderer;
        this.responseRenderer = responseRenderer;
        this.logger = logger;
        this.response = response;
    }

    public void handleRequestException(Throwable throwable) throws
IOException
    {
        Throwable exception = throwable;
        while (exception.getCause() !=  null) {
            exception = exception.getCause();
            if (exception instanceof PageValidationException) {
                PageValidationException pageValidationException =
(PageValidationException) exception;
                String pageName = ((Component)
pageValidationException.getPage())
                        .getComponentResources().getPageName();

                responseRenderer.renderPageMarkupResponse(pageName);
                return;
            }
        }

        logger.error(ServicesMessages.requestException(throwable),
throwable);

        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        response.setHeader("X-Tapestry-ErrorMessage",
InternalUtils.toMessage(throwable));

        Page page = pageCache.get("ExceptionReport");

        ExceptionReporter report = (ExceptionReporter)
page.getRootComponent();
        report.reportException(throwable);

        pageRenderer.renderPageResponse(page);
    }
}


And the decorateRequestExceptionHandler method in AppModule:

    public RequestExceptionHandler decorateRequestExceptionHandler(
            final Logger logger,
            final ResponseRenderer responseRenderer,
            final PageResponseRenderer pageRenderer,
            final Response response,
            final RequestPageCache pageCache)
    {
        return new PageValidationExceptionHandler(pageCache,
responseRenderer, pageRenderer, logger, response);
    }