Example usage for org.springframework.web.servlet HandlerInterceptor postHandle

List of usage examples for org.springframework.web.servlet HandlerInterceptor postHandle

Introduction

In this page you can find the example usage for org.springframework.web.servlet HandlerInterceptor postHandle.

Prototype

default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        @Nullable ModelAndView modelAndView) throws Exception 

Source Link

Document

Intercept the execution of a handler.

Usage

From source file:org.grails.plugins.web.filters.CompositeInterceptor.java

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o,
        ModelAndView modelAndView) throws Exception {
    if (log.isDebugEnabled())
        log.debug("postHandle " + request + ", " + response + ", " + o + ", " + modelAndView);

    for (HandlerInterceptor handler : handlersReversed) {
        handler.postHandle(request, response, o, modelAndView);
    }//from  w w  w. j a  va2s  .c o m
}

From source file:jun.learn.scene.handlerMapping.HandlerExecutionChain.java

/**
 * Apply postHandle methods of registered interceptors.
 *//* ww w .  ja va2 s.c om*/
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv)
        throws Exception {
    HandlerInterceptor[] interceptors = getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        for (int i = interceptors.length - 1; i >= 0; i--) {
            HandlerInterceptor interceptor = interceptors[i];
            interceptor.postHandle(request, response, this.handler, mv);
        }
    }
}

From source file:org.parancoe.web.plugin.PluginHelper.java

public void invokePluginPostHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) {/*from  w w  w  .j  av a2 s  .co m*/
    for (WebPlugin plugin : getWebPlugins()) {
        for (HandlerInterceptor interceptor : plugin.getInterceptors()) {
            try {
                interceptor.postHandle(request, response, handler, modelAndView);
            } catch (Exception e) {
                log.error("error in postHandle for plugin '" + plugin.getName() + "'", e);
            }
        }
    }
}

From source file:org.zkoss.zk.grails.web.ZULUrlMappingsFilter.java

private boolean renderViewForUrlMappingInfo(HttpServletRequest request, HttpServletResponse response,
        UrlMappingInfo info, String viewName) {
    if (viewResolver != null) {
        View v;/*from ww  w  .jav a  2 s  .c  o m*/
        try {
            // execute pre handler interceptors
            for (HandlerInterceptor handlerInterceptor : handlerInterceptors) {
                if (!handlerInterceptor.preHandle(request, response, this))
                    return false;
            }

            // execute post handlers directly after, since there is no controller. The filter has a chance to modify the view at this point;
            final ModelAndView modelAndView = new ModelAndView(viewName);
            for (HandlerInterceptor handlerInterceptor : handlerInterceptors) {
                handlerInterceptor.postHandle(request, response, this, modelAndView);
            }

            v = WebUtils.resolveView(request, info, modelAndView.getViewName(), viewResolver);
            v.render(modelAndView.getModel(), request, response);

            // after completion
            for (HandlerInterceptor handlerInterceptor : handlerInterceptors) {
                handlerInterceptor.afterCompletion(request, response, this, null);
            }
        } catch (Throwable e) {
            // let the sitemesh filter re-run for the error
            reapplySitemesh(request);
            for (HandlerInterceptor handlerInterceptor : handlerInterceptors) {
                try {
                    handlerInterceptor.afterCompletion(request, response, this,
                            e instanceof Exception ? (Exception) e
                                    : new GroovyPagesException(e.getMessage(), e));
                } catch (Exception e1) {
                    UrlMappingException ume = new UrlMappingException(
                            "Error executing filter after view error: " + e1.getMessage() + ". Original error: "
                                    + e.getMessage(),
                            e1);
                    filterAndThrow(ume);
                }
            }
            UrlMappingException ume = new UrlMappingException(
                    "Error mapping onto view [" + viewName + "]: " + e.getMessage(), e);
            filterAndThrow(ume);
        }
    }
    return true;
}

From source file:org.cloudifysource.rest.ControllerTest.java

private MockHttpServletResponse testRequest(final MockHttpServletRequest request,
        final HandlerMethod expectedHandlerMethod) throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();

    final HandlerExecutionChain handlerExecutionChain = getHandlerToRequest(request);
    Object handler = handlerExecutionChain.getHandler();
    Assert.assertEquals("Wrong handler selected for request uri: " + request.getRequestURI(),
            expectedHandlerMethod.toString(), handler.toString());

    HandlerInterceptor[] interceptors = handlerExecutionChain.getInterceptors();
    // pre handle
    for (HandlerInterceptor handlerInterceptor : interceptors) {
        handlerInterceptor.preHandle(request, response, handler);
    }//  w w w.  jav  a  2 s  . co  m
    // handle the request
    ModelAndView modelAndView = handlerAdapter.handle(request, response, handler);
    // post handle
    for (HandlerInterceptor handlerInterceptor : interceptors) {
        handlerInterceptor.postHandle(request, response, handler, modelAndView);
    }

    // validate the response
    Assert.assertTrue("Wrong response status: " + response.getStatus(),
            response.getStatus() == HttpStatus.OK.value());
    Assert.assertTrue(response.getContentType().contains(MediaType.APPLICATION_JSON));

    return response;
}

From source file:org.geoserver.test.GeoServerAbstractTestSupport.java

private void dispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    final DispatcherServlet dispatcher = getDispatcher();

    // build a filter chain so that we can test with filters as well
    MockFilterChain chain = new MockFilterChain();
    List<Filter> filters = getFilters();
    if (filters != null) {
        for (Filter filter : filters) {
            chain.addFilter(filter);// w  ww. j ava  2 s. c  o  m
        }
    }
    chain.setServlet(new HttpServlet() {
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            try {
                //excute the pre handler step
                Collection interceptors = GeoServerExtensions.extensions(HandlerInterceptor.class,
                        applicationContext);
                for (Iterator i = interceptors.iterator(); i.hasNext();) {
                    HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
                    interceptor.preHandle(request, response, dispatcher);
                }

                //execute 
                //dispatcher.handleRequest( request, response );
                dispatcher.service(request, response);

                //execute the post handler step
                for (Iterator i = interceptors.iterator(); i.hasNext();) {
                    HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
                    interceptor.postHandle(request, response, dispatcher, null);
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (IOException e) {
                throw e;
            } catch (ServletException e) {
                throw e;
            } catch (Exception e) {
                throw (IOException) new IOException("Failed to handle the request").initCause(e);
            }
        }
    });

    chain.doFilter(request, response);

}

From source file:org.springframework.web.servlet.HandlerExecutionChain.java

/**
 * Apply postHandle methods of registered interceptors.
 *///from   w ww  . j  a  va2 s .com
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)
        throws Exception {

    HandlerInterceptor[] interceptors = getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        for (int i = interceptors.length - 1; i >= 0; i--) {
            HandlerInterceptor interceptor = interceptors[i];
            interceptor.postHandle(request, response, this.handler, mv);
        }
    }
}

From source file:org.springframework.web.servlet.MyDispatcherServlet.java

/**
 * Process the actual dispatching to the handler.
 * <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
 * The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
 * to find the first that supports the handler class.
 * <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
 * themselves to decide which methods are acceptable.
 * @param request current HTTP request/* w w w.j  a v  a 2  s.  c o  m*/
 * @param response current HTTP response
 * @throws Exception in case of any kind of processing failure
 */
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    int interceptorIndex = -1;

    try {
        ModelAndView mv;
        boolean errorView = false;

        try {
            processedRequest = checkMultipart(request);

            // Determine handler for the current request.
            mappedHandler = getHandler(processedRequest, false);
            if (mappedHandler == null || mappedHandler.getHandler() == null) {
                noHandlerFound(processedRequest, response);
                return;
            }

            // Determine handler adapter for the current request.
            HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

            // Process last-modified header, if supported by the handler.
            String method = request.getMethod();
            boolean isGet = "GET".equals(method);
            if (isGet || "HEAD".equals(method)) {
                long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
                if (logger.isDebugEnabled()) {
                    String requestUri = urlPathHelper.getRequestUri(request);
                    logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified);
                }
                if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
                    return;
                }
            }

            // Apply preHandle methods of registered interceptors.
            HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
            if (interceptors != null) {
                for (int i = 0; i < interceptors.length; i++) {
                    HandlerInterceptor interceptor = interceptors[i];
                    if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {
                        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response,
                                null);
                        return;
                    }
                    interceptorIndex = i;
                }
            }

            // Actually invoke the handler.
            mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

            // Do we need view name translation?
            if (mv != null && !mv.hasView()) {
                mv.setViewName(getDefaultViewName(request));
            }

            // Apply postHandle methods of registered interceptors.
            if (interceptors != null) {
                for (int i = interceptors.length - 1; i >= 0; i--) {
                    HandlerInterceptor interceptor = interceptors[i];
                    interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
                }
            }
        } catch (ModelAndViewDefiningException ex) {
            logger.debug("ModelAndViewDefiningException encountered", ex);
            mv = ex.getModelAndView();
        } catch (Exception ex) {
            Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
            mv = processHandlerException(processedRequest, response, handler, ex);
            errorView = (mv != null);
        }

        // Did the handler return a view to render?
        if (mv != null && !mv.wasCleared()) {
            render(mv, processedRequest, response);
            if (errorView) {
                WebUtils.clearErrorRequestAttributes(request);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName()
                        + "': assuming HandlerAdapter completed request handling");
            }
        }

        // Trigger after-completion for successful outcome.
        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
    }

    catch (Exception ex) {
        // Trigger after-completion for thrown exception.
        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
        throw ex;
    } catch (Error err) {
        ServletException ex = new NestedServletException("Handler processing failed", err);
        // Trigger after-completion for thrown exception.
        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
        throw ex;
    }

    finally {
        // Clean up any resources used by a multipart request.
        if (processedRequest != request) {
            cleanupMultipart(processedRequest);
        }
    }
}

From source file:org.springframework.web.servlet.SimpleDispatcherServlet.java

/**
 * Process the actual dispatching to the handler.
 * <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
 * The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
 * to find the first that supports the handler class.
 * <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
 * themselves to decide which methods are acceptable.
 * @param request current HTTP request//from w  w w.  j  a v a 2s  .  c  o  m
 * @param response current HTTP response
 * @throws Exception in case of any kind of processing failure
 */
public void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    int interceptorIndex = -1;

    try {
        ModelAndView mv;
        boolean errorView = false;

        try {
            processedRequest = checkMultipart(request);

            // Determine handler for the current request.
            mappedHandler = getHandler(processedRequest, false);
            if (mappedHandler == null || mappedHandler.getHandler() == null) {
                noHandlerFound(processedRequest, response);
                return;
            }

            // Apply preHandle methods of registered interceptors.
            HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
            if (interceptors != null) {
                for (int i = 0; i < interceptors.length; i++) {
                    HandlerInterceptor interceptor = interceptors[i];
                    if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {
                        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response,
                                null);
                        return;
                    }
                    interceptorIndex = i;
                }
            }

            // Actually invoke the handler.
            HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
            mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

            // Do we need view name translation?
            if (mv != null && !mv.hasView()) {
                mv.setViewName(getDefaultViewName(request));
            }

            // Apply postHandle methods of registered interceptors.
            if (interceptors != null) {
                for (int i = interceptors.length - 1; i >= 0; i--) {
                    HandlerInterceptor interceptor = interceptors[i];
                    interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);
                }
            }
        } catch (ModelAndViewDefiningException ex) {
            logger.debug("ModelAndViewDefiningException encountered", ex);
            mv = ex.getModelAndView();
        } catch (Exception ex) {
            Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
            mv = processHandlerException(processedRequest, response, handler, ex);
            errorView = (mv != null);
        }

        // Did the handler return a view to render?
        if (mv != null && !mv.wasCleared()) {
            render(mv, processedRequest, response);
            if (errorView) {
                WebUtils.clearErrorRequestAttributes(request);
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName()
                        + "': assuming HandlerAdapter completed request handling");
            }
        }

        // Trigger after-completion for successful outcome.
        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);
    }

    catch (Exception ex) {
        // Trigger after-completion for thrown exception.
        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
        throw ex;
    } catch (Error err) {
        ServletException ex = new NestedServletException("Handler processing failed", err);
        // Trigger after-completion for thrown exception.
        triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
        throw ex;
    }

    finally {
        // Clean up any resources used by a multipart request.
        if (processedRequest != request) {
            cleanupMultipart(processedRequest);
        }
    }
}