Example usage for org.springframework.web.servlet HandlerExecutionChain getInterceptors

List of usage examples for org.springframework.web.servlet HandlerExecutionChain getInterceptors

Introduction

In this page you can find the example usage for org.springframework.web.servlet HandlerExecutionChain getInterceptors.

Prototype

@Nullable
public HandlerInterceptor[] getInterceptors() 

Source Link

Document

Return the array of interceptors to apply (in the given order).

Usage

From source file:org.jboss.arquillian.warp.extension.spring.servlet.WarpDispatcherServlet.java

/**
 * {@inheritDoc}//from w ww  .j a va2 s  .c  o m
 */
@Override
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    HandlerExecutionChain handlerExecutionChain = super.getHandler(request);

    if (handlerExecutionChain != null) {

        buildMvc(request).setHandler(handlerExecutionChain.getHandler())
                .setInterceptors(handlerExecutionChain.getInterceptors());
    }

    return handlerExecutionChain;
}

From source file:org.withinsea.izayoi.adapter.springmvc.SpringIzayoiDispatcherFilter.java

@Override
protected HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache) throws Exception {
    HandlerExecutionChain mappedHandler = super.getHandler(request, cache);
    if (mappedHandler == null || mappedHandler.getHandler() == null) {
        return mappedHandler;
    } else {/*w w  w.ja va  2  s .  c o  m*/
        HandlerInterceptor[] originalInterceptors = mappedHandler.getInterceptors();
        HandlerInterceptor[] interceptors = new HandlerInterceptor[originalInterceptors.length + 1];
        interceptors[0] = izayoiInterceptor;
        System.arraycopy(originalInterceptors, 0, interceptors, 1, originalInterceptors.length);
        return new HandlerExecutionChain(mappedHandler.getHandler(), interceptors);
    }
}

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);
    }/*from  ww  w  . j  a  va  2  s  .c om*/
    // 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.springframework.web.portlet.DispatcherPortlet.java

/**
 * Processes the actual dispatching to the handler for action requests.
 * <p>The handler will be obtained by applying the portlet's HandlerMappings in order.
 * The HandlerAdapter will be obtained by querying the portlet's installed
 * HandlerAdapters to find the first that supports the handler class.
 * @param request current portlet action request
 * @param response current portlet Action response
 * @throws Exception in case of any kind of processing failure
 *///w w  w  .ja v a  2s  .  c o m
@Override
protected void doActionService(ActionRequest request, ActionResponse response) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("DispatcherPortlet with name '" + getPortletName() + "' received action request");
    }

    ActionRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    int interceptorIndex = -1;

    try {
        processedRequest = checkMultipart(request);

        // Determine handler for the current request.
        mappedHandler = getHandler(processedRequest);
        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.preHandleAction(processedRequest, response, mappedHandler.getHandler())) {
                    triggerAfterActionCompletion(mappedHandler, interceptorIndex, processedRequest, response,
                            null);
                    return;
                }
                interceptorIndex = i;
            }
        }

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

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

    catch (Exception ex) {
        // Trigger after-completion for thrown exception.
        triggerAfterActionCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
        // Forward the exception to the render phase to be displayed.
        if (this.forwardActionException) {
            try {
                exposeActionException(request, response, ex);
                logger.debug("Caught exception during action phase - forwarding to render phase", ex);
            } catch (IllegalStateException ex2) {
                // Probably sendRedirect called... need to rethrow exception immediately.
                throw ex;
            }
        } else {
            throw ex;
        }
    } catch (Error err) {
        PortletException ex = new PortletException(
                "Error occured during request processing: " + err.getMessage(), err);
        // Trigger after-completion for thrown exception.
        triggerAfterActionCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);
        throw ex;
    }

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

From source file:org.springframework.web.portlet.DispatcherPortlet.java

/**
 * Processes the actual dispatching to the handler for render requests.
 * <p>The handler will be obtained by applying the portlet's HandlerMappings in order.
 * The HandlerAdapter will be obtained by querying the portlet's installed
 * HandlerAdapters to find the first that supports the handler class.
 * @param request current portlet render request
 * @param response current portlet render response
 * @throws Exception in case of any kind of processing failure
 *//*w w w . j  a v  a  2s  . com*/
@Override
protected void doRenderService(RenderRequest request, RenderResponse response) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("DispatcherPortlet with name '" + getPortletName() + "' received render request");
    }

    HandlerExecutionChain mappedHandler = null;
    int interceptorIndex = -1;

    try {
        ModelAndView mv;
        try {
            // Determine handler for the current request.
            mappedHandler = getHandler(request);
            if (mappedHandler == null || mappedHandler.getHandler() == null) {
                noHandlerFound(request, 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.preHandleRender(request, response, mappedHandler.getHandler())) {
                        triggerAfterRenderCompletion(mappedHandler, interceptorIndex, request, response, null);
                        return;
                    }
                    interceptorIndex = i;
                }
            }

            // Check for forwarded exception from the action phase
            PortletSession session = request.getPortletSession(false);
            if (session != null) {
                if (request.getParameter(ACTION_EXCEPTION_RENDER_PARAMETER) != null) {
                    Exception ex = (Exception) session.getAttribute(ACTION_EXCEPTION_SESSION_ATTRIBUTE);
                    if (ex != null) {
                        logger.debug("Render phase found exception caught during action phase - rethrowing it");
                        throw ex;
                    }
                } else {
                    session.removeAttribute(ACTION_EXCEPTION_SESSION_ATTRIBUTE);
                }
            }

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

            // Apply postHandle methods of registered interceptors.
            if (interceptors != null) {
                for (int i = interceptors.length - 1; i >= 0; i--) {
                    HandlerInterceptor interceptor = interceptors[i];
                    interceptor.postHandleRender(request, 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(request, response, handler, ex);
        }

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

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

    catch (Exception ex) {
        // Trigger after-completion for thrown exception.
        triggerAfterRenderCompletion(mappedHandler, interceptorIndex, request, response, ex);
        throw ex;
    } catch (Error err) {
        PortletException ex = new PortletException(
                "Error occured during request processing: " + err.getMessage(), err);
        // Trigger after-completion for thrown exception.
        triggerAfterRenderCompletion(mappedHandler, interceptorIndex, request, response, ex);
        throw ex;
    }
}

From source file:org.springframework.web.portlet.DispatcherPortlet.java

/**
 * Processes the actual dispatching to the handler for resource requests.
 * <p>The handler will be obtained by applying the portlet's HandlerMappings in order.
 * The HandlerAdapter will be obtained by querying the portlet's installed
 * HandlerAdapters to find the first that supports the handler class.
 * @param request current portlet render request
 * @param response current portlet render response
 * @throws Exception in case of any kind of processing failure
 */// w  w  w .j  a v  a2 s.c o m
@Override
protected void doResourceService(ResourceRequest request, ResourceResponse response) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("DispatcherPortlet with name '" + getPortletName() + "' received resource request");
    }

    HandlerExecutionChain mappedHandler = null;
    int interceptorIndex = -1;

    try {
        ModelAndView mv;
        try {
            // Determine handler for the current request.
            mappedHandler = getHandler(request);
            if (mappedHandler == null || mappedHandler.getHandler() == null) {
                noHandlerFound(request, 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.preHandleResource(request, response, mappedHandler.getHandler())) {
                        triggerAfterResourceCompletion(mappedHandler, interceptorIndex, request, response,
                                null);
                        return;
                    }
                    interceptorIndex = i;
                }
            }

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

            // Apply postHandle methods of registered interceptors.
            if (interceptors != null) {
                for (int i = interceptors.length - 1; i >= 0; i--) {
                    HandlerInterceptor interceptor = interceptors[i];
                    interceptor.postHandleResource(request, 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(request, response, handler, ex);
        }

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

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

    catch (Exception ex) {
        // Trigger after-completion for thrown exception.
        triggerAfterResourceCompletion(mappedHandler, interceptorIndex, request, response, ex);
        throw ex;
    } catch (Error err) {
        PortletException ex = new PortletException(
                "Error occured during request processing: " + err.getMessage(), err);
        // Trigger after-completion for thrown exception.
        triggerAfterResourceCompletion(mappedHandler, interceptorIndex, request, response, ex);
        throw ex;
    }
}

From source file:org.springframework.web.portlet.DispatcherPortlet.java

/**
 * Processes the actual dispatching to the handler for event requests.
 * <p>The handler will be obtained by applying the portlet's HandlerMappings in order.
 * The HandlerAdapter will be obtained by querying the portlet's installed
 * HandlerAdapters to find the first that supports the handler class.
 * @param request current portlet action request
 * @param response current portlet Action response
 * @throws Exception in case of any kind of processing failure
 *//*from   w  w w . j a v  a 2 s . c o  m*/
@Override
protected void doEventService(EventRequest request, EventResponse response) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("DispatcherPortlet with name '" + getPortletName() + "' received action request");
    }

    HandlerExecutionChain mappedHandler = null;
    int interceptorIndex = -1;

    try {
        // Determine handler for the current request.
        mappedHandler = getHandler(request);
        if (mappedHandler == null || mappedHandler.getHandler() == null) {
            noHandlerFound(request, 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.preHandleEvent(request, response, mappedHandler.getHandler())) {
                    triggerAfterEventCompletion(mappedHandler, interceptorIndex, request, response, null);
                    return;
                }
                interceptorIndex = i;
            }
        }

        // Actually invoke the handler.
        HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
        ha.handleEvent(request, response, mappedHandler.getHandler());

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

    catch (Exception ex) {
        // Trigger after-completion for thrown exception.
        triggerAfterEventCompletion(mappedHandler, interceptorIndex, request, response, ex);
        // Forward the exception to the render phase to be displayed.
        if (this.forwardEventException) {
            try {
                exposeActionException(request, response, ex);
                logger.debug("Caught exception during event phase - forwarding to render phase", ex);
            } catch (IllegalStateException ex2) {
                // Probably sendRedirect called... need to rethrow exception immediately.
                throw ex;
            }
        } else {
            throw ex;
        }
    } catch (Error err) {
        PortletException ex = new PortletException(
                "Error occured during request processing: " + err.getMessage(), err);
        // Trigger after-completion for thrown exception.
        triggerAfterEventCompletion(mappedHandler, interceptorIndex, request, response, ex);
        throw ex;
    }
}

From source file:org.springframework.web.portlet.DispatcherPortlet.java

/**
 * Trigger afterCompletion callbacks on the mapped HandlerInterceptors.
 * Will just invoke afterCompletion for all interceptors whose preHandle
 * invocation has successfully completed and returned true.
 * @param mappedHandler the mapped HandlerExecutionChain
 * @param interceptorIndex index of last interceptor that successfully completed
 * @param ex Exception thrown on handler execution, or null if none
 * @see HandlerInterceptor#afterRenderCompletion
 *///from   www.ja v a  2s  .co  m
private void triggerAfterActionCompletion(HandlerExecutionChain mappedHandler, int interceptorIndex,
        ActionRequest request, ActionResponse response, Exception ex) throws Exception {

    // Apply afterCompletion methods of registered interceptors.
    if (mappedHandler != null) {
        HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
        if (interceptors != null) {
            for (int i = interceptorIndex; i >= 0; i--) {
                HandlerInterceptor interceptor = interceptors[i];
                try {
                    interceptor.afterActionCompletion(request, response, mappedHandler.getHandler(), ex);
                } catch (Throwable ex2) {
                    logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
                }
            }
        }
    }
}

From source file:org.springframework.web.portlet.DispatcherPortlet.java

/**
 * Trigger afterCompletion callbacks on the mapped HandlerInterceptors.
 * Will just invoke afterCompletion for all interceptors whose preHandle
 * invocation has successfully completed and returned true.
 * @param mappedHandler the mapped HandlerExecutionChain
 * @param interceptorIndex index of last interceptor that successfully completed
 * @param ex Exception thrown on handler execution, or null if none
 * @see HandlerInterceptor#afterRenderCompletion
 *//*from   w  w w.j a v a 2  s . c om*/
private void triggerAfterRenderCompletion(HandlerExecutionChain mappedHandler, int interceptorIndex,
        RenderRequest request, RenderResponse response, Exception ex) throws Exception {

    // Apply afterCompletion methods of registered interceptors.
    if (mappedHandler != null) {
        HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
        if (interceptors != null) {
            for (int i = interceptorIndex; i >= 0; i--) {
                HandlerInterceptor interceptor = interceptors[i];
                try {
                    interceptor.afterRenderCompletion(request, response, mappedHandler.getHandler(), ex);
                } catch (Throwable ex2) {
                    logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
                }
            }
        }
    }
}

From source file:org.springframework.web.portlet.DispatcherPortlet.java

/**
 * Trigger afterCompletion callbacks on the mapped HandlerInterceptors.
 * Will just invoke afterCompletion for all interceptors whose preHandle
 * invocation has successfully completed and returned true.
 * @param mappedHandler the mapped HandlerExecutionChain
 * @param interceptorIndex index of last interceptor that successfully completed
 * @param ex Exception thrown on handler execution, or null if none
 * @see HandlerInterceptor#afterRenderCompletion
 *//*w  ww . ja v  a  2  s .  c  om*/
private void triggerAfterResourceCompletion(HandlerExecutionChain mappedHandler, int interceptorIndex,
        ResourceRequest request, ResourceResponse response, Exception ex) throws Exception {

    // Apply afterCompletion methods of registered interceptors.
    if (mappedHandler != null) {
        HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
        if (interceptors != null) {
            for (int i = interceptorIndex; i >= 0; i--) {
                HandlerInterceptor interceptor = interceptors[i];
                try {
                    interceptor.afterResourceCompletion(request, response, mappedHandler.getHandler(), ex);
                } catch (Throwable ex2) {
                    logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
                }
            }
        }
    }
}