private void registerMvcSingletons(StubWebApplicationContext cxt) {

    StandaloneConfiguration configuration = new StandaloneConfiguration();

    RequestMappingHandlerMapping handlerMapping = configuration.requestMappingHandlerMapping();
    handlerMapping.setServletContext(cxt.getServletContext());
    handlerMapping.setApplicationContext(cxt);
    cxt.addBean("requestMappingHandlerMapping", handlerMapping);

    RequestMappingHandlerAdapter handlerAdapter = configuration.requestMappingHandlerAdapter();
    handlerAdapter.setServletContext(cxt.getServletContext());
    handlerAdapter.setApplicationContext(cxt);
    handlerAdapter.afterPropertiesSet();
    cxt.addBean("requestMappingHandlerAdapter", handlerAdapter);

    cxt.addBean("handlerExceptionResolver", configuration.handlerExceptionResolver());

    cxt.addBeans(initViewResolvers(cxt));
    cxt.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver);
    cxt.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver());
    cxt.addBean(
        DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME,
        new DefaultRequestToViewNameTranslator());

    this.flashMapManager = new SessionFlashMapManager();
    cxt.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager);
  }
Esempio n. 2
0
 @Override
 public RequestMappingHandlerMapping requestMappingHandlerMapping() {
   RequestMappingHandlerMapping requestMappingHandlerMapping =
       super.requestMappingHandlerMapping();
   requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
   requestMappingHandlerMapping.setUseTrailingSlashMatch(false);
   return requestMappingHandlerMapping;
 }
  public void init() {
    for (final RequestMappingHandlerMapping requestMappingHandlerMapping :
        requestMappingHandlerMappings) {
      requestMappingHandlerMapping.setUrlPathHelper(getUrlPathHelper());
    }

    requestMappingHandlerAdapter.setSynchronizeOnSession(true);
  }
Esempio n. 4
0
 @Bean
 public RequestMappingHandlerMapping requestMappingHandlerMapping() {
   RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
   handlerMapping.setOrder(1);
   handlerMapping.setInterceptors(Arrays.asList(new SecurityInterceptor()).toArray());
   // handlerMapping.setContentNegotiationManager(cnManager().getObject());
   return handlerMapping;
 }
Esempio n. 5
0
  @Test
  public void requestMappingHandlerMapping() throws Exception {
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    cxt.registerSingleton("controller", TestController.class);

    RequestMappingHandlerMapping handlerMapping = mvcConfiguration.requestMappingHandlerMapping();
    assertEquals(0, handlerMapping.getOrder());

    handlerMapping.setApplicationContext(cxt);
    HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
    assertNotNull(chain.getInterceptors());
    assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[0].getClass());
  }
 @Override
 public void afterPropertiesSet() {
   super.afterPropertiesSet();
   if (!this.disabled) {
     for (MvcEndpoint endpoint : this.endpoints) {
       detectHandlerMethods(endpoint);
     }
   }
 }
 @Override
 @Deprecated
 protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
   if (mapping == null) {
     return;
   }
   String[] patterns = getPatterns(handler, mapping);
   if (!ObjectUtils.isEmpty(patterns)) {
     super.registerHandlerMethod(handler, method, withNewPatterns(mapping, patterns));
   }
 }
 @Override
 protected void handleMatch(
     RequestMappingInfo info, String lookupPath, HttpServletRequest request) {
   super.handleMatch(info, lookupPath, request);
   AccessExpressionRequestCondition c =
       (AccessExpressionRequestCondition) info.getCustomCondition();
   if (!c.isAuthorized(request)) {
     throw new AccessDeniedException(
         "Authentication does not "
             + "match access expression '"
             + c.getExpression().getExpressionString()
             + "'");
   }
 }
 @Test
 public void testPathMatchingConfiguration() {
   loadBeanDefinitions("mvc-config-path-matching.xml");
   RequestMappingHandlerMapping hm = appContext.getBean(RequestMappingHandlerMapping.class);
   assertNotNull(hm);
   assertTrue(hm.useSuffixPatternMatch());
   assertFalse(hm.useTrailingSlashMatch());
   assertTrue(hm.useRegisteredSuffixPatternMatch());
   assertThat(hm.getUrlPathHelper(), Matchers.instanceOf(TestPathHelper.class));
   assertThat(hm.getPathMatcher(), Matchers.instanceOf(TestPathMatcher.class));
   List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions();
   assertThat(fileExtensions, Matchers.contains("xml"));
   assertThat(fileExtensions, Matchers.hasSize(1));
 }
  @Override
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void afterPropertiesSet() {
    // code borrowed from AbstractAuthorizeTag
    ApplicationContext appContext =
        WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    Map<String, SecurityExpressionHandler> handlers =
        appContext.getBeansOfType(SecurityExpressionHandler.class);

    for (SecurityExpressionHandler h : handlers.values()) {
      if (FilterInvocation.class.equals(
          GenericTypeResolver.resolveTypeArgument(h.getClass(), SecurityExpressionHandler.class))) {
        handler = h;
        break;
      }
    }

    if (handler == null) {
      throw new IllegalStateException(
          "No visible WebSecurityExpressionHandler instance "
              + "could be found in the application context");
    }
    super.afterPropertiesSet();
  }
 public void registerHandlers(Object... handlers) {
   for (Object handler : handlers) {
     super.detectHandlerMethods(handler);
   }
 }
 /**
  * {@link HttpRequestHandlingEndpointSupport}s may depend on auto-created {@code requestChannel}s,
  * so MVC Handlers detection should be postponed as late as possible.
  *
  * @see RequestMappingHandlerMapping#afterPropertiesSet()
  */
 @Override
 public void onApplicationEvent(ContextRefreshedEvent event) {
   if (!this.initialized.getAndSet(true)) {
     super.afterPropertiesSet();
   }
 }
Esempio n. 13
0
  @Test
  public void webMvcConfigurerExtensionHooks() throws Exception {

    StaticWebApplicationContext appCxt = new StaticWebApplicationContext();
    appCxt.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
    appCxt.registerSingleton("controller", TestController.class);

    WebConfig webConfig = new WebConfig();
    webConfig.setApplicationContext(appCxt);
    webConfig.setServletContext(appCxt.getServletContext());

    String actual = webConfig.mvcConversionService().convert(new TestBean(), String.class);
    assertEquals("converted", actual);

    RequestMappingHandlerAdapter adapter = webConfig.requestMappingHandlerAdapter();
    assertEquals(1, adapter.getMessageConverters().size());

    ConfigurableWebBindingInitializer initializer =
        (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    assertNotNull(initializer);

    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(null, "");
    initializer.getValidator().validate(null, bindingResult);
    assertEquals("invalid", bindingResult.getAllErrors().get(0).getCode());

    @SuppressWarnings("unchecked")
    List<HandlerMethodArgumentResolver> argResolvers =
        (List<HandlerMethodArgumentResolver>)
            new DirectFieldAccessor(adapter).getPropertyValue("customArgumentResolvers");
    assertEquals(1, argResolvers.size());

    @SuppressWarnings("unchecked")
    List<HandlerMethodReturnValueHandler> handlers =
        (List<HandlerMethodReturnValueHandler>)
            new DirectFieldAccessor(adapter).getPropertyValue("customReturnValueHandlers");
    assertEquals(1, handlers.size());

    HandlerExceptionResolverComposite composite =
        (HandlerExceptionResolverComposite) webConfig.handlerExceptionResolver();
    assertEquals(1, composite.getExceptionResolvers().size());

    RequestMappingHandlerMapping rmHandlerMapping = webConfig.requestMappingHandlerMapping();
    rmHandlerMapping.setApplicationContext(appCxt);
    HandlerExecutionChain chain =
        rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
    assertNotNull(chain.getInterceptors());
    assertEquals(2, chain.getInterceptors().length);
    assertEquals(LocaleChangeInterceptor.class, chain.getInterceptors()[0].getClass());
    assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());

    AbstractHandlerMapping handlerMapping =
        (AbstractHandlerMapping) webConfig.viewControllerHandlerMapping();
    handlerMapping.setApplicationContext(appCxt);
    assertNotNull(handlerMapping);
    assertEquals(1, handlerMapping.getOrder());
    HandlerExecutionChain handler =
        handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
    assertNotNull(handler.getHandler());

    handlerMapping = (AbstractHandlerMapping) webConfig.resourceHandlerMapping();
    handlerMapping.setApplicationContext(appCxt);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE - 1, handlerMapping.getOrder());
    handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
    assertNotNull(handler.getHandler());

    handlerMapping = (AbstractHandlerMapping) webConfig.defaultServletHandlerMapping();
    handlerMapping.setApplicationContext(appCxt);
    assertNotNull(handlerMapping);
    assertEquals(Integer.MAX_VALUE, handlerMapping.getOrder());
    handler = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
    assertNotNull(handler.getHandler());
  }