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

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

Introduction

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

Prototype

default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception 

Source Link

Document

Intercept the execution of a handler.

Usage

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

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
    if (log.isDebugEnabled())
        log.debug("preHandle " + request + ", " + response + ", " + o);

    for (HandlerInterceptor handler : handlers) {
        if (!handler.preHandle(request, response, o)) {
            return false;
        }/*from  ww  w . ja  v a2s  .  com*/
        // if forward is called, bail out
        if (request.getAttribute(GrailsApplicationAttributes.FORWARD_ISSUED) != null) {
            return false;
        }
    }
    return true;
}

From source file:com.feilong.mock.UriTemplateUtilControllerTest.java

ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
    final HandlerMapping handlerMapping = applicationContext.getBean(HandlerMapping.class);
    final HandlerExecutionChain handler = handlerMapping.getHandler(request);
    // assertNotNull("No handler found for request, check you request mapping", handler);
    final Object controller = handler.getHandler();
    // if you want to override any injected attributes do it here
    final HandlerInterceptor[] interceptors = handlerMapping.getHandler(request).getInterceptors();
    for (HandlerInterceptor interceptor : interceptors) {
        final boolean carryOn = interceptor.preHandle(request, response, controller);
        if (!carryOn) {
            return null;
        }/*from  w ww. j  a v  a2 s.c  om*/
    }
    final ModelAndView mav = handlerAdapter.handle(request, response, controller);
    return mav;
}

From source file:com.expedia.common.controller.WeatherControllerTest.java

/**
 * //from  w w w.ja v  a  2  s  .c  o m
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
    final HandlerMapping handlerMapping = applicationContext.getBean(HandlerMapping.class);
    final HandlerExecutionChain handler = handlerMapping.getHandler(request);
    assertNotNull("No handler found for request, check you request mapping", handler);

    final Object controller = handler.getHandler();
    // if you want to override any injected attributes do it here

    final HandlerInterceptor[] interceptors = handlerMapping.getHandler(request).getInterceptors();
    for (HandlerInterceptor interceptor : interceptors) {
        final boolean carryOn = interceptor.preHandle(request, response, controller);
        if (!carryOn) {
            return null;
        }
    }

    final ModelAndView mav = handlerAdapter.handle(request, response, controller);
    return mav;
}

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

/**
 * Apply preHandle methods of registered interceptors.
 * @return {@code true} if the execution chain should proceed with the
 * next interceptor or the handler itself. Else, DispatcherServlet assumes
 * that this interceptor has already dealt with the response itself.
 *//*from  ww w .  java 2 s  .c o m*/
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HandlerInterceptor[] interceptors = getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        for (int i = 0; i < interceptors.length; i++) {
            HandlerInterceptor interceptor = interceptors[i];
            if (!interceptor.preHandle(request, response, this.handler)) {
                triggerAfterCompletion(request, response, null);
                return false;
            }
            this.interceptorIndex = i;
        }
    }
    return true;
}

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

/**
 * invokes contextInitialized for every registered plugin
 *
 * @param evt/*from w  ww.  j a va 2s.  com*/
 */
public boolean invokePluginPreHandle(HttpServletRequest req, HttpServletResponse res, Object handler) {
    for (WebPlugin plugin : getWebPlugins()) {
        for (HandlerInterceptor interceptor : plugin.getInterceptors()) {
            try {
                boolean result = interceptor.preHandle(req, res, handler);
                if (result == false) {
                    return false;
                }
            } catch (Exception e) {
                log.error("error in preHandle for plugin '" + plugin.getName() + "'", e);
            }
        }
    }
    return true;
}

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  w ww  .  j  a v  a2s.  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:ca.flop.jpublish.i18n.SpringI18nSupport.java

/**
 * Execute the action using the given context.
 *
 * @param context       The current context
 * @param configuration The configuration
 * @throws Exception Any error/*  w  w w. j av a2  s  .c om*/
 */
public void execute(JPublishContext context, Configuration configuration) throws Exception {
    HttpServletRequest request = (HttpServletRequest) context.get("request");
    HttpServletResponse response = (HttpServletResponse) context.get("response");
    Page page = ((Page) context.get("page"));

    if (page != null) {
        Locale locale = page.getLocale();

        ApplicationContext applicationContext = getApplicationContext(context);
        context.put(SPRING, applicationContext);
        HandlerInterceptor localeChangeInterceptor = (HandlerInterceptor) applicationContext
                .getBean("localeChangeInterceptor");

        if (localeChangeInterceptor != null) {
            try {
                LocaleResolver localeResolver = (LocaleResolver) applicationContext.getBean("localeResolver");
                if (localeResolver != null) {
                    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, localeResolver);
                    localeChangeInterceptor.preHandle(request, response, null);
                    locale = localeResolver.resolveLocale(request);
                    MessageSource messageSource = (MessageSource) applicationContext.getBean("messageSource");
                    if (messageSource != null) {
                        context.put(I18N, new MessageSourceAccessor(messageSource, locale));
                    } else {
                        log.error("There is no 'messageSource' defined in your application context."
                                + " Please define one.");
                    }
                } else {
                    log.error("please define a 'localeResolver' bean in your Application context");
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("Cannot handle the locale change event");
            }
        }
        page.setLocale(locale);
    }
}

From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java

private ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
    final HandlerMapping handlerMapping = appContext.getBean(HandlerMapping.class);
    final HandlerAdapter handlerAdapter = appContext.getBean(HandlerAdapter.class);
    final HandlerExecutionChain handler = handlerMapping.getHandler(request);

    Object controller = handler.getHandler();
    // if you want to override any injected attributes do it here

    HandlerInterceptor[] interceptors = handlerMapping.getHandler(request).getInterceptors();
    for (HandlerInterceptor interceptor : interceptors) {
        if (handleInterceptor(interceptor)) {
            final boolean carryOn = interceptor.preHandle(request, response, controller);
            if (!carryOn) {
                return null;
            }//from   w  w  w .  ja  v a 2s  . co  m
        }
    }
    ModelAndView mv = handlerAdapter.handle(request, response, controller);
    return mv;
}

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  .ja  v  a 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.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);//from w  w w . ja va2s. co  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);

}