Example usage for org.springframework.web.servlet HandlerMapping getHandler

List of usage examples for org.springframework.web.servlet HandlerMapping getHandler

Introduction

In this page you can find the example usage for org.springframework.web.servlet HandlerMapping getHandler.

Prototype

@Nullable
HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;

Source Link

Document

Return a handler and any interceptors for this request.

Usage

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  w w  .  j  av a2 s .  c  o m*/
    }
    final ModelAndView mav = handlerAdapter.handle(request, response, controller);
    return mav;
}

From source file:com.alexshabanov.springrestapi.restapitest.DefaultRestTestSupport.java

@Override
public final MockHttpServletResponse handle(HttpServletRequest request) {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    Object handler = null;//from  w  ww .  ja v  a  2s.  c  o  m

    try {
        // delegates request processing to the spring facilities
        for (HandlerMapping mapping : context.getBeansOfType(HandlerMapping.class).values()) {
            final HandlerExecutionChain chain = mapping.getHandler(request);
            if (chain == null) {
                continue;
            }

            handler = chain.getHandler();
            if (handler != null) {
                handlerAdapter.handle(request, response, chain.getHandler());
                break;
            }
        }

        if (handler == null) {
            throw new AssertionError("Can't handle request in the current context");
        }
    } catch (Exception e) {
        if (handleException(e, request, response, handler)) {
            return response;
        }

        // exception mapping has not been found - propagate error outside the method boundaries
        throw new AssertionError(e);
    }

    return response;
}

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:org.wallride.autoconfigure.WallRideDispatcherServlet.java

@Override
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    if (!CollectionUtils.isEmpty(this.parentHandlerMappings)) {
        for (HandlerMapping hm : this.parentHandlerMappings) {
            if (logger.isTraceEnabled()) {
                logger.trace("Testing handler map [" + hm + "] in DispatcherServlet with name '"
                        + getServletName() + "'");
            }//from  ww w.j  av a2s .co  m
            HandlerExecutionChain handler = hm.getHandler(request);
            if (handler != null) {
                return handler;
            }
        }
    }
    return super.getHandler(request);
}

From source file:org.appcomponents.platform.mvc.PlatformRequestMappingHandlerMapping.java

protected HandlerExecutionChain handle(HttpServletRequest request, Component component) throws Exception {
    ApplicationContext applicationContext = component.getApplicationContext();
    if (this.applicationContext.equals(applicationContext)) {
        return null;
    } else {//  w  ww.j a v a  2 s .co m
        Map<String, HandlerMapping> handlerMappingMap = applicationContext.getBeansOfType(HandlerMapping.class);
        for (HandlerMapping hm : handlerMappingMap.values()) {
            if (hm.getClass().equals(getClass())) {
                continue;
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Testing handler map [" + hm + "] in PlatformRequestMappingHandlerMapping");
            }
            HandlerExecutionChain handler = hm.getHandler(request);
            if (handler != null) {
                return handler;
            }
        }
        return null;
    }
}

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;
            }/*  w ww. ja  va2  s.c o m*/
        }
    }
    ModelAndView mv = handlerAdapter.handle(request, response, controller);
    return mv;
}

From source file:com.mystudy.source.spring.mvc.DispatcherServlet.java

/**
 * Return the HandlerExecutionChain for this request.
 * <p>Tries all handler mappings in order.
 * @param request current HTTP request//from   w w w  .ja v a2s.  c o  m
 * @return the HandlerExecutionChain, or {@code null} if no handler could be found
 */
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    for (HandlerMapping hm : this.handlerMappings) {
        if (logger.isTraceEnabled()) {
            logger.trace("Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName()
                    + "'");
        }
        HandlerExecutionChain handler = hm.getHandler(request);
        if (handler != null) {
            return handler;
        }
    }
    return null;
}

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

/**
 * This method finds the handler for a given request URI. It will also
 * ensure that the URI Parameters i.e. /context/test/{name} are added to the
 * request/*w  ww .j  ava  2 s  .  c  o  m*/
 *
 * @param request
 *            The request object to be used
 * @return The correct handler for the request
 * @throws Exception
 *             Indicates a matching handler could not be found
 */
private Object getHandlerToRequest(final MockHttpServletRequest request) throws Exception {
    HandlerExecutionChain chain = null;

    final Map<String, HandlerMapping> map = applicationContext.getBeansOfType(HandlerMapping.class);

    for (HandlerMapping mapping : map.values()) {
        chain = mapping.getHandler(request);

        if (chain != null) {
            break;
        }
    }

    if (chain == null) {
        throw new InvalidParameterException(
                "Unable to find handler for request URI: " + request.getRequestURI());
    }

    return chain.getHandler();
}

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

/**
 * This method finds the handler for a given request URI. It will also ensure that the URI Parameters i.e.
 * /context/test/{name} are added to the request
 *
 * @param request/*from   w  w w .  j a  v a  2 s  . co m*/
 *            The request object to be used
 * @return The correct handler for the request
 * @throws Exception
 *             Indicates a matching handler could not be found
 */
private HandlerExecutionChain getHandlerToRequest(final MockHttpServletRequest request) throws Exception {
    HandlerExecutionChain chain = null;

    final Map<String, HandlerMapping> map = applicationContext.getBeansOfType(HandlerMapping.class);

    for (final HandlerMapping mapping : map.values()) {
        chain = mapping.getHandler(request);

        if (chain != null) {
            break;
        }
    }

    if (chain == null) {
        throw new InvalidParameterException(
                "Unable to find handler for request URI: " + request.getRequestURI());
    }
    return chain;
}

From source file:org.jahia.services.seo.urlrewrite.UrlRewriteService.java

public boolean prepareInbound(final HttpServletRequest request, HttpServletResponse response) {
    resetState(request);//from   ww  w.  j  av  a 2  s.  c om
    request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_CONTEXT_PATH, request.getContextPath());
    String input = request.getRequestURI();
    if (request.getContextPath().length() > 0) {
        input = StringUtils.substringAfter(input, request.getContextPath());
    }
    if (input.contains(";")) {
        input = StringUtils.substringBefore(input, ";");
    }

    String prefix = StringUtils.EMPTY;
    if (isSeoRemoveCmsPrefix() && input.length() > 1 && input.indexOf('/') == 0) {
        int end = input.indexOf('/', 1);
        prefix = end != -1 ? input.substring(1, end) : input.substring(1);
    }
    if (prefix.length() > 1) {
        boolean contains = reservedUrlPrefixSet.contains(prefix);
        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_ADD_CMS_PREFIX, !contains);
        if (contains && !"cms".equals(prefix) && !"files".equals(prefix)) {
            return false;
        }
    } else {
        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_ADD_CMS_PREFIX, false);
    }

    String path = request.getPathInfo() != null ? request.getPathInfo() : input;
    try {
        List<HandlerMapping> mappings = getRenderMapping();
        if (mappings != null) {
            for (HandlerMapping mapping : mappings) {
                if (mapping instanceof SimpleUrlHandlerMapping) {
                    SimpleUrlHandlerMapping simpleUrlHandlerMapping = (SimpleUrlHandlerMapping) mapping;
                    for (String registeredPattern : simpleUrlHandlerMapping.getUrlMap().keySet()) {
                        if (simpleUrlHandlerMapping.getPathMatcher().match(registeredPattern, path)) {
                            request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_SKIP_INBOUND_SEO_RULES,
                                    Boolean.TRUE);
                            return false;
                        }
                    }
                } else {
                    HandlerExecutionChain handlerExecutionChain = mapping.getHandler(request);
                    if (handlerExecutionChain != null) {
                        // we found an execution chain for this handler, we deactivate SEO rules for this request.
                        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_SKIP_INBOUND_SEO_RULES,
                                Boolean.TRUE);
                        return false;
                    }
                }
            }
        }
    } catch (Exception ex) {
        logger.warn("Unable to load the handler mappings", ex);
    }
    String targetSiteKey = ServerNameToSiteMapper.getSiteKeyByServerName(request);
    request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_LANG, StringUtils.EMPTY);
    request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_PATH, StringUtils.EMPTY);
    if (StringUtils.isNotEmpty(targetSiteKey) && !path.startsWith("/sites")) {
        try {
            List<VanityUrl> vanityUrls = vanityUrlService.findExistingVanityUrls(path, targetSiteKey, "live");
            if (!vanityUrls.isEmpty()) {
                vanityUrls.get(0).getLanguage();
                request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_LANG,
                        vanityUrls.get(0).getLanguage());
                path = StringUtils.substringBefore(vanityUrls.get(0).getPath(),
                        "/" + VanityUrlManager.VANITYURLMAPPINGS_NODE + "/") + ".html";
                request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_VANITY_PATH, path);
            }
        } catch (RepositoryException e) {
            logger.error("Cannot get vanity Url", e);
        }
    } else if (path.startsWith("/sites/")) {
        targetSiteKey = StringUtils.substringAfter(path, "/sites/");
        if (targetSiteKey.contains("/")) {
            targetSiteKey = StringUtils.substringBefore(targetSiteKey, "/");
        } else if (targetSiteKey.contains(".")) {
            // remove templateType from the url
            targetSiteKey = StringUtils.substringBeforeLast(targetSiteKey, ".");
        }
    }

    try {
        String language = siteService.getSiteDefaultLanguage(targetSiteKey);
        if (language == null) {
            // remove template from the url
            language = siteService.getSiteDefaultLanguage(StringUtils.substringBeforeLast(targetSiteKey, "."));
        }
        request.setAttribute(ServerNameToSiteMapper.ATTR_NAME_DEFAULT_LANG, language);
    } catch (JahiaException e) {
        logger.error("Cannot get site for key " + targetSiteKey, e);
    }

    return true;
}