001/*
002 * Shredzone Commons
003 *
004 * Copyright (C) 2012 Richard "Shred" Körber
005 *   http://commons.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as
009 * published by the Free Software Foundation, either version 3 of the
010 * License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020
021package org.shredzone.commons.view;
022
023import java.lang.reflect.Method;
024
025import javax.annotation.ParametersAreNonnullByDefault;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import org.shredzone.commons.view.exception.ErrorResponseException;
030
031/**
032 * Spring beans implementing this interface are allowed to inspect or change the output
033 * of view handlers.
034 *
035 * @author Richard "Shred" Körber
036 */
037@ParametersAreNonnullByDefault
038public interface ViewInterceptor {
039
040    /**
041     * Called when a HTTP request was sent to the view service. The request has not yet
042     * been processed.
043     *
044     * @param req
045     *            {@link HttpServletRequest} to be handled
046     * @param resp
047     *            {@link HttpServletResponse} with the response
048     */
049    void onRequest(HttpServletRequest req, HttpServletResponse resp);
050
051    /**
052     * Called when a ViewHandler is about to be invoked.
053     *
054     * @param context
055     *            {@link ViewContext} passed to the view handler
056     * @param bean
057     *            Spring bean containing the view handler
058     * @param method
059     *            View handler method that will be invoked
060     */
061    void onViewHandlerInvocation(ViewContext context, Object bean, Method method);
062
063    /**
064     * Called when a ViewHandler successfully processed the request and returned a
065     * template string that is now to be rendered. The interceptor can change the template
066     * name.
067     *
068     * @param template
069     *            Template name returned by the view handler
070     * @param req
071     *            {@link HttpServletRequest} that was handled
072     * @param resp
073     *            {@link HttpServletResponse} with the response
074     * @return a different template name, or {@code null} for keeping the original
075     *         template name
076     */
077    String onRendering(String template, HttpServletRequest req, HttpServletResponse resp);
078
079    /**
080     * Called when an {@link ErrorResponseException} occured. The interceptor is able to
081     * catch the error.
082     *
083     * @param ex
084     *            {@link ErrorResponseException} that occured
085     * @param req
086     *            {@link HttpServletRequest} that led to this error
087     * @param resp
088     *            {@link HttpServletResponse} with the response
089     * @return {@code true} if the interceptor responded to the error. Other interceptors
090     *         will not be invoked, and the {@link HttpServletResponse} is sent back.
091     *         {@code false} if the error handling shall be continued.
092     */
093    boolean onErrorResponse(ErrorResponseException ex, HttpServletRequest req, HttpServletResponse resp);
094
095}