org.springframework.web.servlet.resource.ResourceHttpRequestHandler Java Examples

The following examples show how to use org.springframework.web.servlet.resource.ResourceHttpRequestHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceHandlerRegistration.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Return a {@link ResourceHttpRequestHandler} instance.
 */
protected ResourceHttpRequestHandler getRequestHandler() {
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	if (this.resourceChainRegistration != null) {
		handler.setResourceResolvers(this.resourceChainRegistration.getResourceResolvers());
		handler.setResourceTransformers(this.resourceChainRegistration.getResourceTransformers());
	}
	handler.setLocationValues(this.locationValues);
	if (this.cacheControl != null) {
		handler.setCacheControl(this.cacheControl);
	}
	else if (this.cachePeriod != null) {
		handler.setCacheSeconds(this.cachePeriod);
	}
	return handler;
}
 
Example #2
Source File: ResourceHandlerRegistryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void resourceChainWithVersionResolver() throws Exception {
	VersionResourceResolver versionResolver = new VersionResourceResolver()
			.addFixedVersionStrategy("fixed", "/**/*.js")
			.addContentVersionStrategy("/**");

	this.registration.resourceChain(true).addResolver(versionResolver)
			.addTransformer(new AppCacheManifestTransformer());

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
	assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(3));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
	assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
 
Example #3
Source File: ResourceHandlerRegistryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void resourceChain() throws Exception {
	ResourceResolver mockResolver = Mockito.mock(ResourceResolver.class);
	ResourceTransformer mockTransformer = Mockito.mock(ResourceTransformer.class);
	this.registration.resourceChain(true).addResolver(mockResolver).addTransformer(mockTransformer);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
	CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
	assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
	assertThat(resolvers.get(1), Matchers.equalTo(mockResolver));
	assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(2));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.equalTo(mockTransformer));
}
 
Example #4
Source File: ResourceHandlerRegistration.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link ResourceHttpRequestHandler} instance.
 */
protected ResourceHttpRequestHandler getRequestHandler() {
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	if (this.resourceChainRegistration != null) {
		handler.setResourceResolvers(this.resourceChainRegistration.getResourceResolvers());
		handler.setResourceTransformers(this.resourceChainRegistration.getResourceTransformers());
	}
	handler.setLocations(this.locations);
	if (this.cacheControl != null) {
		handler.setCacheControl(this.cacheControl);
	}
	else if (this.cachePeriod != null) {
		handler.setCacheSeconds(this.cachePeriod);
	}
	return handler;
}
 
Example #5
Source File: ResourceHandlerRegistration.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a {@link ResourceHttpRequestHandler} instance.
 */
protected ResourceHttpRequestHandler getRequestHandler() {
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	if (this.resourceChainRegistration != null) {
		handler.setResourceResolvers(this.resourceChainRegistration.getResourceResolvers());
		handler.setResourceTransformers(this.resourceChainRegistration.getResourceTransformers());
	}
	handler.setLocations(this.locations);
	if (this.cacheControl != null) {
		handler.setCacheControl(this.cacheControl);
	}
	else if (this.cachePeriod != null) {
		handler.setCacheSeconds(this.cachePeriod);
	}
	return handler;
}
 
Example #6
Source File: ResourceHandlerRegistryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void urlResourceWithCharset() throws Exception {
	this.registration.addResourceLocations("[charset=ISO-8859-1]file:///tmp");
	this.registration.resourceChain(true);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	UrlResource resource = (UrlResource) handler.getLocations().get(1);
	assertEquals("file:/tmp", resource.getURL().toString());
	assertNotNull(handler.getUrlPathHelper());

	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	PathResourceResolver resolver = (PathResourceResolver) resolvers.get(resolvers.size()-1);
	Map<Resource, Charset> locationCharsets = resolver.getLocationCharsets();
	assertEquals(1, locationCharsets.size());
	assertEquals(StandardCharsets.ISO_8859_1, locationCharsets.values().iterator().next());
}
 
Example #7
Source File: ResourceHandlerRegistryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resourceChainWithVersionResolver() throws Exception {
	VersionResourceResolver versionResolver = new VersionResourceResolver()
			.addFixedVersionStrategy("fixed", "/**/*.js")
			.addContentVersionStrategy("/**");

	this.registration.resourceChain(true).addResolver(versionResolver)
			.addTransformer(new AppCacheManifestTransformer());

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
	assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(3));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
	assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
 
Example #8
Source File: ResourceHandlerRegistryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void resourceChain() throws Exception {
	ResourceResolver mockResolver = Mockito.mock(ResourceResolver.class);
	ResourceTransformer mockTransformer = Mockito.mock(ResourceTransformer.class);
	this.registration.resourceChain(true).addResolver(mockResolver).addTransformer(mockTransformer);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
	CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
	assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
	assertThat(resolvers.get(1), Matchers.equalTo(mockResolver));
	assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(2));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.equalTo(mockTransformer));
}
 
Example #9
Source File: ResourceHandlerRegistration.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Return a {@link ResourceHttpRequestHandler} instance.
 */
protected ResourceHttpRequestHandler getRequestHandler() {
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	if (this.resourceChainRegistration != null) {
		handler.setResourceResolvers(this.resourceChainRegistration.getResourceResolvers());
		handler.setResourceTransformers(this.resourceChainRegistration.getResourceTransformers());
	}
	handler.setLocationValues(this.locationValues);
	if (this.cacheControl != null) {
		handler.setCacheControl(this.cacheControl);
	}
	else if (this.cachePeriod != null) {
		handler.setCacheSeconds(this.cachePeriod);
	}
	return handler;
}
 
Example #10
Source File: ResourceHandlerRegistryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void urlResourceWithCharset() throws Exception {
	this.registration.addResourceLocations("[charset=ISO-8859-1]file:///tmp");
	this.registration.resourceChain(true);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	UrlResource resource = (UrlResource) handler.getLocations().get(1);
	assertEquals("file:/tmp", resource.getURL().toString());
	assertNotNull(handler.getUrlPathHelper());

	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	PathResourceResolver resolver = (PathResourceResolver) resolvers.get(resolvers.size()-1);
	Map<Resource, Charset> locationCharsets = resolver.getLocationCharsets();
	assertEquals(1, locationCharsets.size());
	assertEquals(StandardCharsets.ISO_8859_1, locationCharsets.values().iterator().next());
}
 
Example #11
Source File: ResourceHandlerRegistryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resourceChainWithVersionResolver() throws Exception {
	VersionResourceResolver versionResolver = new VersionResourceResolver()
			.addFixedVersionStrategy("fixed", "/**/*.js")
			.addContentVersionStrategy("/**");

	this.registration.resourceChain(true).addResolver(versionResolver)
			.addTransformer(new AppCacheManifestTransformer());

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
	assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(3));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
	assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
 
Example #12
Source File: ResourceHandlerRegistryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resourceChain() throws Exception {
	ResourceResolver mockResolver = Mockito.mock(ResourceResolver.class);
	ResourceTransformer mockTransformer = Mockito.mock(ResourceTransformer.class);
	this.registration.resourceChain(true).addResolver(mockResolver).addTransformer(mockTransformer);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
	CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
	assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
	assertThat(resolvers.get(1), Matchers.equalTo(mockResolver));
	assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(2));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.equalTo(mockTransformer));
}
 
Example #13
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResourcesWithResolversTransformers() throws Exception {
	loadBeanDefinitions("mvc-config-resources-chain.xml", 11);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertNotNull(mapping.getUrlMap().get("/resources/**"));
	ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"),
			ResourceHttpRequestHandler.class);
	assertNotNull(handler);

	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.instanceOf(CachingResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.instanceOf(VersionResourceResolver.class));
	assertThat(resolvers.get(2), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(3), Matchers.instanceOf(PathResourceResolver.class));

	CachingResourceResolver cachingResolver = (CachingResourceResolver) resolvers.get(0);
	assertThat(cachingResolver.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
	assertEquals("test-resource-cache", cachingResolver.getCache().getName());

	VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(1);
	assertThat(versionResolver.getStrategyMap().get("/**/*.js"),
			Matchers.instanceOf(FixedVersionStrategy.class));
	assertThat(versionResolver.getStrategyMap().get("/**"),
			Matchers.instanceOf(ContentVersionStrategy.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(3));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.instanceOf(CssLinkResourceTransformer.class));
	assertThat(transformers.get(2), Matchers.instanceOf(AppCacheManifestTransformer.class));

	CachingResourceTransformer cachingTransformer = (CachingResourceTransformer) transformers.get(0);
	assertThat(cachingTransformer.getCache(), Matchers.instanceOf(ConcurrentMapCache.class));
	assertEquals("test-resource-cache", cachingTransformer.getCache().getName());
}
 
Example #14
Source File: MVCConf.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ResourceHttpRequestHandler resourceHttpRequestHandler() {
    return new JarResourceHttpRequestHandler(
            "/resources/",
            "classpath:/resources/",
            "file:" + pluginsStatic,
            PREFIX + pluginsResources);
}
 
Example #15
Source File: WebMvcConfigurationSupportExtensionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void contentNegotiation() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
	NativeWebRequest webRequest = new ServletWebRequest(request);

	RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping();
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.xml");
	assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.rss");
	assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")),
			manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.atom");
	assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo");
	request.setParameter("f", "json");
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/resources/foo.gif");
	SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping();
	handlerMapping.setApplicationContext(this.context);
	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);
	ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());
}
 
Example #16
Source File: ResourceHandlerRegistry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a handler mapping with the mapped resource handlers; or {@code null} in case
 * of no registrations.
 */
protected AbstractHandlerMapping getHandlerMapping() {
	if (this.registrations.isEmpty()) {
		return null;
	}

	Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<String, HttpRequestHandler>();
	for (ResourceHandlerRegistration registration : this.registrations) {
		for (String pathPattern : registration.getPathPatterns()) {
			ResourceHttpRequestHandler handler = registration.getRequestHandler();
			handler.setServletContext(this.servletContext);
			handler.setApplicationContext(this.applicationContext);
			handler.setContentNegotiationManager(this.contentNegotiationManager);
			try {
				handler.afterPropertiesSet();
			}
			catch (Throwable ex) {
				throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
			}
			urlMap.put(pathPattern, handler);
		}
	}

	SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
	handlerMapping.setOrder(order);
	handlerMapping.setUrlMap(urlMap);
	return handlerMapping;
}
 
Example #17
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResourcesWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-resources-optional-attrs.xml");

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(5, mapping.getOrder());
	assertNotNull(mapping.getUrlMap().get("/resources/**"));

	ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"),
			ResourceHttpRequestHandler.class);
	assertNotNull(handler);
	assertEquals(3600, handler.getCacheSeconds());
}
 
Example #18
Source File: ResourceHandlerRegistry.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Return a handler mapping with the mapped resource handlers; or {@code null} in case of no registrations.
 */
protected AbstractHandlerMapping getHandlerMapping() {
	if (registrations.isEmpty()) {
		return null;
	}

	Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<String, HttpRequestHandler>();
	for (ResourceHandlerRegistration registration : this.registrations) {
		for (String pathPattern : registration.getPathPatterns()) {
			ResourceHttpRequestHandler handler = registration.getRequestHandler();
			handler.setServletContext(this.servletContext);
			handler.setApplicationContext(this.appContext);
			try {
				handler.afterPropertiesSet();
			}
			catch (Exception e) {
				throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", e);
			}
			urlMap.put(pathPattern, handler);
		}
	}

	SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
	handlerMapping.setOrder(order);
	handlerMapping.setUrlMap(urlMap);
	return handlerMapping;
}
 
Example #19
Source File: ResourcesBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private String registerResourceHandler(ParserContext parserContext, Element element, Object source) {
	String locationAttr = element.getAttribute("location");
	if (!StringUtils.hasText(locationAttr)) {
		parserContext.getReaderContext().error("The 'location' attribute is required.", parserContext.extractSource(element));
		return null;
	}

	ManagedList<String> locations = new ManagedList<String>();
	locations.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(locationAttr)));

	RootBeanDefinition resourceHandlerDef = new RootBeanDefinition(ResourceHttpRequestHandler.class);
	resourceHandlerDef.setSource(source);
	resourceHandlerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	resourceHandlerDef.getPropertyValues().add("locations", locations);

	String cacheSeconds = element.getAttribute("cache-period");
	if (StringUtils.hasText(cacheSeconds)) {
		resourceHandlerDef.getPropertyValues().add("cacheSeconds", cacheSeconds);
	}

	Element cacheControlElement = DomUtils.getChildElementByTagName(element, "cache-control");
	if (cacheControlElement != null) {
		CacheControl cacheControl = parseCacheControl(cacheControlElement);
		resourceHandlerDef.getPropertyValues().add("cacheControl", cacheControl);
	}

	Element resourceChainElement = DomUtils.getChildElementByTagName(element, "resource-chain");
	if (resourceChainElement != null) {
		parseResourceChain(resourceHandlerDef, parserContext, resourceChainElement, source);
	}

	String beanName = parserContext.getReaderContext().generateBeanName(resourceHandlerDef);
	parserContext.getRegistry().registerBeanDefinition(beanName, resourceHandlerDef);
	parserContext.registerComponent(new BeanComponentDefinition(resourceHandlerDef, beanName));
	return beanName;
}
 
Example #20
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml", 10);

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE - 1, mapping.getOrder());

	BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());

	ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
	assertNotNull(urlProvider);

	MappedInterceptor mappedInterceptor = appContext.getBean(MappedInterceptor.class);
	assertNotNull(urlProvider);
	assertEquals(ResourceUrlProviderExposingInterceptor.class, mappedInterceptor.getInterceptor().getClass());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/resources/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	for (HandlerInterceptor interceptor : chain.getInterceptors()) {
		interceptor.preHandle(request, response, chain.getHandler());
	}
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example #21
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResourcesWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-resources-optional-attrs.xml", 10);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(5, mapping.getOrder());
	assertNotNull(mapping.getUrlMap().get("/resources/**"));

	ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"),
			ResourceHttpRequestHandler.class);
	assertNotNull(handler);
	assertEquals(3600, handler.getCacheSeconds());
}
 
Example #22
Source File: AutoLoginInterceptor.java    From computoser with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
        Object handler) throws Exception {

    // don't handle ajax or resource requests
    String requestedWith = request.getHeader("X-Requested-With");
    if (handler instanceof ResourceHttpRequestHandler || (requestedWith != null && requestedWith.equals("XMLHttpRequest"))) {
        return true;
    }

    if (userContext.getUser() == null && request.getCookies() != null) {
        Cookie[] cookies = request.getCookies();

        String authToken = null;
        String series = null;

        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(SocialSignInAdapter.AUTH_TOKEN_COOKIE_NAME)) {
                authToken = cookie.getValue();
            } else if (cookie.getName().equals(SocialSignInAdapter.AUTH_TOKEN_SERIES_COOKIE_NAME)) {
                series = cookie.getValue();
            }
        }

        if (authToken != null && series != null) {
            User user = userService.rememberMeLogin(authToken, series);
            if (user != null) {
                adapter.signIn(user, response, false);
            }
        }
    }
    return true;
}
 
Example #23
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResourcesWithResolversTransformersCustom() throws Exception {
	loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml", 12);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertNotNull(mapping.getUrlMap().get("/resources/**"));
	ResourceHttpRequestHandler handler = appContext.getBean((String) mapping.getUrlMap().get("/resources/**"),
			ResourceHttpRequestHandler.class);
	assertNotNull(handler);

	assertThat(handler.getCacheControl().getHeaderValue(),
			Matchers.equalTo(CacheControl.maxAge(1, TimeUnit.HOURS)
					.sMaxAge(30, TimeUnit.MINUTES).cachePublic().getHeaderValue()));

	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers, Matchers.hasSize(3));
	assertThat(resolvers.get(0), Matchers.instanceOf(VersionResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.instanceOf(GzipResourceResolver.class));
	assertThat(resolvers.get(2), Matchers.instanceOf(PathResourceResolver.class));

	VersionResourceResolver versionResolver = (VersionResourceResolver) resolvers.get(0);
	assertThat(versionResolver.getStrategyMap().get("/**/*.js"),
			Matchers.instanceOf(FixedVersionStrategy.class));
	assertThat(versionResolver.getStrategyMap().get("/**"),
			Matchers.instanceOf(ContentVersionStrategy.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(2));
	assertThat(transformers.get(0), Matchers.instanceOf(CachingResourceTransformer.class));
	assertThat(transformers.get(1), Matchers.instanceOf(AppCacheManifestTransformer.class));
}
 
Example #24
Source File: ResourceHandlerRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void mapPathToLocation() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("GET");
	request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css");

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	handler.handleRequest(request, this.response);

	assertEquals("test stylesheet content", this.response.getContentAsString());
}
 
Example #25
Source File: ResourceHandlerRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resourceChainWithoutCaching() throws Exception {
	this.registration.resourceChain(false);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers, Matchers.hasSize(2));
	assertThat(resolvers.get(0), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(0));
}
 
Example #26
Source File: ResourceHandlerRegistry.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return a handler mapping with the mapped resource handlers; or {@code null} in case
 * of no registrations.
 */
@Nullable
protected AbstractHandlerMapping getHandlerMapping() {
	if (this.registrations.isEmpty()) {
		return null;
	}

	Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<>();
	for (ResourceHandlerRegistration registration : this.registrations) {
		for (String pathPattern : registration.getPathPatterns()) {
			ResourceHttpRequestHandler handler = registration.getRequestHandler();
			if (this.pathHelper != null) {
				handler.setUrlPathHelper(this.pathHelper);
			}
			if (this.contentNegotiationManager != null) {
				handler.setContentNegotiationManager(this.contentNegotiationManager);
			}
			handler.setServletContext(this.servletContext);
			handler.setApplicationContext(this.applicationContext);
			try {
				handler.afterPropertiesSet();
			}
			catch (Throwable ex) {
				throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
			}
			urlMap.put(pathPattern, handler);
		}
	}

	SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
	handlerMapping.setOrder(this.order);
	handlerMapping.setUrlMap(urlMap);
	return handlerMapping;
}
 
Example #27
Source File: ResourceHandlerRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resourceChainWithOverrides() throws Exception {
	CachingResourceResolver cachingResolver = Mockito.mock(CachingResourceResolver.class);
	VersionResourceResolver versionResolver = Mockito.mock(VersionResourceResolver.class);
	PathResourceResolver pathResourceResolver = new PathResourceResolver();
	CachingResourceTransformer cachingTransformer = Mockito.mock(CachingResourceTransformer.class);
	AppCacheManifestTransformer appCacheTransformer = Mockito.mock(AppCacheManifestTransformer.class);
	CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();

	this.registration.setCachePeriod(3600)
			.resourceChain(false)
				.addResolver(cachingResolver)
				.addResolver(versionResolver)
				.addResolver(pathResourceResolver)
				.addTransformer(cachingTransformer)
				.addTransformer(appCacheTransformer)
				.addTransformer(cssLinkTransformer);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(3));
	assertThat(resolvers.get(0), Matchers.sameInstance(cachingResolver));
	assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
	assertThat(resolvers.get(2), Matchers.sameInstance(pathResourceResolver));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(3));
	assertThat(transformers.get(0), Matchers.sameInstance(cachingTransformer));
	assertThat(transformers.get(1), Matchers.sameInstance(appCacheTransformer));
	assertThat(transformers.get(2), Matchers.sameInstance(cssLinkTransformer));
}
 
Example #28
Source File: ResourceHandlerRegistryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resourceChainWithOverrides() throws Exception {
	CachingResourceResolver cachingResolver = Mockito.mock(CachingResourceResolver.class);
	VersionResourceResolver versionResolver = Mockito.mock(VersionResourceResolver.class);
	WebJarsResourceResolver webjarsResolver = Mockito.mock(WebJarsResourceResolver.class);
	PathResourceResolver pathResourceResolver = new PathResourceResolver();
	CachingResourceTransformer cachingTransformer = Mockito.mock(CachingResourceTransformer.class);
	AppCacheManifestTransformer appCacheTransformer = Mockito.mock(AppCacheManifestTransformer.class);
	CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();

	this.registration.setCachePeriod(3600)
			.resourceChain(false)
				.addResolver(cachingResolver)
				.addResolver(versionResolver)
				.addResolver(webjarsResolver)
				.addResolver(pathResourceResolver)
				.addTransformer(cachingTransformer)
				.addTransformer(appCacheTransformer)
				.addTransformer(cssLinkTransformer);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.sameInstance(cachingResolver));
	assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
	assertThat(resolvers.get(2), Matchers.sameInstance(webjarsResolver));
	assertThat(resolvers.get(3), Matchers.sameInstance(pathResourceResolver));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(3));
	assertThat(transformers.get(0), Matchers.sameInstance(cachingTransformer));
	assertThat(transformers.get(1), Matchers.sameInstance(appCacheTransformer));
	assertThat(transformers.get(2), Matchers.sameInstance(cssLinkTransformer));
}
 
Example #29
Source File: ResourceHandlerRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resourceChainWithOverrides() throws Exception {
	CachingResourceResolver cachingResolver = Mockito.mock(CachingResourceResolver.class);
	VersionResourceResolver versionResolver = Mockito.mock(VersionResourceResolver.class);
	WebJarsResourceResolver webjarsResolver = Mockito.mock(WebJarsResourceResolver.class);
	PathResourceResolver pathResourceResolver = new PathResourceResolver();
	CachingResourceTransformer cachingTransformer = Mockito.mock(CachingResourceTransformer.class);
	AppCacheManifestTransformer appCacheTransformer = Mockito.mock(AppCacheManifestTransformer.class);
	CssLinkResourceTransformer cssLinkTransformer = new CssLinkResourceTransformer();

	this.registration.setCachePeriod(3600)
			.resourceChain(false)
				.addResolver(cachingResolver)
				.addResolver(versionResolver)
				.addResolver(webjarsResolver)
				.addResolver(pathResourceResolver)
				.addTransformer(cachingTransformer)
				.addTransformer(appCacheTransformer)
				.addTransformer(cssLinkTransformer);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers.toString(), resolvers, Matchers.hasSize(4));
	assertThat(resolvers.get(0), Matchers.sameInstance(cachingResolver));
	assertThat(resolvers.get(1), Matchers.sameInstance(versionResolver));
	assertThat(resolvers.get(2), Matchers.sameInstance(webjarsResolver));
	assertThat(resolvers.get(3), Matchers.sameInstance(pathResourceResolver));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(3));
	assertThat(transformers.get(0), Matchers.sameInstance(cachingTransformer));
	assertThat(transformers.get(1), Matchers.sameInstance(appCacheTransformer));
	assertThat(transformers.get(2), Matchers.sameInstance(cssLinkTransformer));
}
 
Example #30
Source File: ResourceHandlerRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resourceChainWithoutCaching() throws Exception {
	this.registration.resourceChain(false);

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	List<ResourceResolver> resolvers = handler.getResourceResolvers();
	assertThat(resolvers, Matchers.hasSize(2));
	assertThat(resolvers.get(0), Matchers.instanceOf(WebJarsResourceResolver.class));
	assertThat(resolvers.get(1), Matchers.instanceOf(PathResourceResolver.class));

	List<ResourceTransformer> transformers = handler.getResourceTransformers();
	assertThat(transformers, Matchers.hasSize(0));
}