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

The following examples show how to use org.springframework.web.servlet.resource.CachingResourceTransformer. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
Source File: ResourceChainRegistration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public ResourceChainRegistration(boolean cacheResources, @Nullable Cache cache) {
	Assert.isTrue(!cacheResources || cache != null, "'cache' is required when cacheResources=true");
	if (cacheResources) {
		this.resolvers.add(new CachingResourceResolver(cache));
		this.transformers.add(new CachingResourceTransformer(cache));
	}
}
 
Example #9
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 #10
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 #11
Source File: ResourcesBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void parseResourceCache(ManagedList<? super Object> resourceResolvers,
		ManagedList<? super Object> resourceTransformers, Element element, Object source) {

	String resourceCache = element.getAttribute("resource-cache");
	if ("true".equals(resourceCache)) {
		ConstructorArgumentValues cavs = new ConstructorArgumentValues();

		RootBeanDefinition cachingResolverDef = new RootBeanDefinition(CachingResourceResolver.class);
		cachingResolverDef.setSource(source);
		cachingResolverDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingResolverDef.setConstructorArgumentValues(cavs);

		RootBeanDefinition cachingTransformerDef = new RootBeanDefinition(CachingResourceTransformer.class);
		cachingTransformerDef.setSource(source);
		cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingTransformerDef.setConstructorArgumentValues(cavs);

		String cacheManagerName = element.getAttribute("cache-manager");
		String cacheName = element.getAttribute("cache-name");
		if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
			RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
			cavs.addIndexedArgumentValue(0, cacheManagerRef);
			cavs.addIndexedArgumentValue(1, cacheName);
		}
		else {
			ConstructorArgumentValues cacheCavs = new ConstructorArgumentValues();
			cacheCavs.addIndexedArgumentValue(0, RESOURCE_CHAIN_CACHE);
			RootBeanDefinition cacheDef = new RootBeanDefinition(ConcurrentMapCache.class);
			cacheDef.setSource(source);
			cacheDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			cacheDef.setConstructorArgumentValues(cacheCavs);
			cavs.addIndexedArgumentValue(0, cacheDef);
		}
		resourceResolvers.add(cachingResolverDef);
		resourceTransformers.add(cachingTransformerDef);
	}
}
 
Example #12
Source File: ResourceChainRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected List<ResourceTransformer> getResourceTransformers() {
	if (this.hasVersionResolver  && !this.hasCssLinkTransformer) {
		List<ResourceTransformer> result = new ArrayList<ResourceTransformer>(this.transformers);
		boolean hasTransformers = !this.transformers.isEmpty();
		boolean hasCaching = hasTransformers && this.transformers.get(0) instanceof CachingResourceTransformer;
		result.add(hasCaching ? 1 : 0, new CssLinkResourceTransformer());
		return result;
	}
	return this.transformers;
}
 
Example #13
Source File: ResourceChainRegistration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public ResourceChainRegistration(boolean cacheResources, Cache cache) {
	Assert.isTrue(!cacheResources || cache != null, "'cache' is required when cacheResources=true");
	if (cacheResources) {
		this.resolvers.add(new CachingResourceResolver(cache));
		this.transformers.add(new CachingResourceTransformer(cache));
	}
}
 
Example #14
Source File: ResourcesBeanDefinitionParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void parseResourceCache(ManagedList<? super Object> resourceResolvers,
		ManagedList<? super Object> resourceTransformers, Element element, Object source) {

	String resourceCache = element.getAttribute("resource-cache");
	if ("true".equals(resourceCache)) {
		ConstructorArgumentValues cavs = new ConstructorArgumentValues();

		RootBeanDefinition cachingResolverDef = new RootBeanDefinition(CachingResourceResolver.class);
		cachingResolverDef.setSource(source);
		cachingResolverDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingResolverDef.setConstructorArgumentValues(cavs);

		RootBeanDefinition cachingTransformerDef = new RootBeanDefinition(CachingResourceTransformer.class);
		cachingTransformerDef.setSource(source);
		cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingTransformerDef.setConstructorArgumentValues(cavs);

		String cacheManagerName = element.getAttribute("cache-manager");
		String cacheName = element.getAttribute("cache-name");
		if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
			RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
			cavs.addIndexedArgumentValue(0, cacheManagerRef);
			cavs.addIndexedArgumentValue(1, cacheName);
		}
		else {
			ConstructorArgumentValues cacheCavs = new ConstructorArgumentValues();
			cacheCavs.addIndexedArgumentValue(0, RESOURCE_CHAIN_CACHE);
			RootBeanDefinition cacheDef = new RootBeanDefinition(ConcurrentMapCache.class);
			cacheDef.setSource(source);
			cacheDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			cacheDef.setConstructorArgumentValues(cacheCavs);
			cavs.addIndexedArgumentValue(0, cacheDef);
		}
		resourceResolvers.add(cachingResolverDef);
		resourceTransformers.add(cachingTransformerDef);
	}
}
 
Example #15
Source File: ResourceChainRegistration.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected List<ResourceTransformer> getResourceTransformers() {
	if (this.hasVersionResolver && !this.hasCssLinkTransformer) {
		List<ResourceTransformer> result = new ArrayList<ResourceTransformer>(this.transformers);
		boolean hasTransformers = !this.transformers.isEmpty();
		boolean hasCaching = hasTransformers && this.transformers.get(0) instanceof CachingResourceTransformer;
		result.add(hasCaching ? 1 : 0, new CssLinkResourceTransformer());
		return result;
	}
	return this.transformers;
}
 
Example #16
Source File: ResourceChainRegistration.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ResourceChainRegistration(boolean cacheResources, Cache cache) {
	Assert.isTrue(!cacheResources || cache != null, "'cache' is required when cacheResources=true");
	if (cacheResources) {
		this.resolvers.add(new CachingResourceResolver(cache));
		this.transformers.add(new CachingResourceTransformer(cache));
	}
}
 
Example #17
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 #18
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testResourcesWithResolversTransformersCustom() throws Exception {
	loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml");

	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(EncodedResourceResolver.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 #19
Source File: ResourcesBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void parseResourceCache(ManagedList<Object> resourceResolvers,
		ManagedList<Object> resourceTransformers, Element element, @Nullable Object source) {

	String resourceCache = element.getAttribute("resource-cache");
	if ("true".equals(resourceCache)) {
		ConstructorArgumentValues cargs = new ConstructorArgumentValues();

		RootBeanDefinition cachingResolverDef = new RootBeanDefinition(CachingResourceResolver.class);
		cachingResolverDef.setSource(source);
		cachingResolverDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingResolverDef.setConstructorArgumentValues(cargs);

		RootBeanDefinition cachingTransformerDef = new RootBeanDefinition(CachingResourceTransformer.class);
		cachingTransformerDef.setSource(source);
		cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingTransformerDef.setConstructorArgumentValues(cargs);

		String cacheManagerName = element.getAttribute("cache-manager");
		String cacheName = element.getAttribute("cache-name");
		if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
			RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
			cargs.addIndexedArgumentValue(0, cacheManagerRef);
			cargs.addIndexedArgumentValue(1, cacheName);
		}
		else {
			ConstructorArgumentValues cacheCavs = new ConstructorArgumentValues();
			cacheCavs.addIndexedArgumentValue(0, RESOURCE_CHAIN_CACHE);
			RootBeanDefinition cacheDef = new RootBeanDefinition(ConcurrentMapCache.class);
			cacheDef.setSource(source);
			cacheDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			cacheDef.setConstructorArgumentValues(cacheCavs);
			cargs.addIndexedArgumentValue(0, cacheDef);
		}
		resourceResolvers.add(cachingResolverDef);
		resourceTransformers.add(cachingTransformerDef);
	}
}
 
Example #20
Source File: ResourceChainRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected List<ResourceTransformer> getResourceTransformers() {
	if (this.hasVersionResolver && !this.hasCssLinkTransformer) {
		List<ResourceTransformer> result = new ArrayList<>(this.transformers);
		boolean hasTransformers = !this.transformers.isEmpty();
		boolean hasCaching = hasTransformers && this.transformers.get(0) instanceof CachingResourceTransformer;
		result.add(hasCaching ? 1 : 0, new CssLinkResourceTransformer());
		return result;
	}
	return this.transformers;
}
 
Example #21
Source File: ResourceChainRegistration.java    From java-technology-stack with MIT License 5 votes vote down vote up
public ResourceChainRegistration(boolean cacheResources, @Nullable Cache cache) {
	Assert.isTrue(!cacheResources || cache != null, "'cache' is required when cacheResources=true");
	if (cacheResources) {
		this.resolvers.add(new CachingResourceResolver(cache));
		this.transformers.add(new CachingResourceTransformer(cache));
	}
}
 
Example #22
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 #23
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResourcesWithResolversTransformersCustom() throws Exception {
	loadBeanDefinitions("mvc-config-resources-chain-no-auto.xml");

	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(EncodedResourceResolver.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: ResourcesBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void parseResourceCache(ManagedList<Object> resourceResolvers,
		ManagedList<Object> resourceTransformers, Element element, @Nullable Object source) {

	String resourceCache = element.getAttribute("resource-cache");
	if ("true".equals(resourceCache)) {
		ConstructorArgumentValues cargs = new ConstructorArgumentValues();

		RootBeanDefinition cachingResolverDef = new RootBeanDefinition(CachingResourceResolver.class);
		cachingResolverDef.setSource(source);
		cachingResolverDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingResolverDef.setConstructorArgumentValues(cargs);

		RootBeanDefinition cachingTransformerDef = new RootBeanDefinition(CachingResourceTransformer.class);
		cachingTransformerDef.setSource(source);
		cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		cachingTransformerDef.setConstructorArgumentValues(cargs);

		String cacheManagerName = element.getAttribute("cache-manager");
		String cacheName = element.getAttribute("cache-name");
		if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
			RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
			cargs.addIndexedArgumentValue(0, cacheManagerRef);
			cargs.addIndexedArgumentValue(1, cacheName);
		}
		else {
			ConstructorArgumentValues cacheCavs = new ConstructorArgumentValues();
			cacheCavs.addIndexedArgumentValue(0, RESOURCE_CHAIN_CACHE);
			RootBeanDefinition cacheDef = new RootBeanDefinition(ConcurrentMapCache.class);
			cacheDef.setSource(source);
			cacheDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			cacheDef.setConstructorArgumentValues(cacheCavs);
			cargs.addIndexedArgumentValue(0, cacheDef);
		}
		resourceResolvers.add(cachingResolverDef);
		resourceTransformers.add(cachingTransformerDef);
	}
}
 
Example #25
Source File: ResourceChainRegistration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected List<ResourceTransformer> getResourceTransformers() {
	if (this.hasVersionResolver && !this.hasCssLinkTransformer) {
		List<ResourceTransformer> result = new ArrayList<>(this.transformers);
		boolean hasTransformers = !this.transformers.isEmpty();
		boolean hasCaching = hasTransformers && this.transformers.get(0) instanceof CachingResourceTransformer;
		result.add(hasCaching ? 1 : 0, new CssLinkResourceTransformer());
		return result;
	}
	return this.transformers;
}
 
Example #26
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testResourcesWithResolversTransformers() throws Exception {
	loadBeanDefinitions("mvc-config-resources-chain.xml");

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

	assertNotNull(handler.getUrlPathHelper());

	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));

	PathResourceResolver pathResolver = (PathResourceResolver) resolvers.get(3);
	Map<Resource, Charset> locationCharsets = pathResolver.getLocationCharsets();
	assertEquals(1, locationCharsets.size());
	assertEquals(StandardCharsets.ISO_8859_1, locationCharsets.values().iterator().next());

	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 #27
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testResourcesWithResolversTransformers() throws Exception {
	loadBeanDefinitions("mvc-config-resources-chain.xml");

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

	assertNotNull(handler.getUrlPathHelper());

	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));

	PathResourceResolver pathResolver = (PathResourceResolver) resolvers.get(3);
	Map<Resource, Charset> locationCharsets = pathResolver.getLocationCharsets();
	assertEquals(1, locationCharsets.size());
	assertEquals(StandardCharsets.ISO_8859_1, locationCharsets.values().iterator().next());

	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());
}