本文整理汇总了Java中org.springframework.web.servlet.resource.ResourceHttpRequestHandler类的典型用法代码示例。如果您正苦于以下问题:Java ResourceHttpRequestHandler类的具体用法?Java ResourceHttpRequestHandler怎么用?Java ResourceHttpRequestHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ResourceHttpRequestHandler类属于org.springframework.web.servlet.resource包,在下文中一共展示了ResourceHttpRequestHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resourceChain
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@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));
}
示例2: getRequestHandler
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
/**
* 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;
}
示例3: getMappingLocations
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
protected Map<String, List<Resource>> getMappingLocations(HandlerMapping mapping)
throws IllegalAccessException {
Map<String, List<Resource>> mappingLocations = new LinkedHashMap<String, List<Resource>>();
if (mapping instanceof SimpleUrlHandlerMapping) {
Field locationsField = ReflectionUtils
.findField(ResourceHttpRequestHandler.class, "locations");
locationsField.setAccessible(true);
for (Map.Entry<String, Object> entry : ((SimpleUrlHandlerMapping) mapping)
.getHandlerMap().entrySet()) {
ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) entry
.getValue();
mappingLocations.put(entry.getKey(),
(List<Resource>) locationsField.get(handler));
}
}
return mappingLocations;
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:19,代码来源:WebMvcAutoConfigurationTests.java
示例4: resourceChainWithVersionResolver
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@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));
}
示例5: preHandle
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
// filter resource requests
if (ClassUtils.isAssignableValue(ResourceHttpRequestHandler.class, handler)) {
return true;
}
UserSession systemSession;
try {
systemSession = trustedClientService.getSystemSession(idpConfig.getTrustedClientPassword());
AppContext.setSecurityContext(new SecurityContext(systemSession));
} catch (LoginException e) {
log.error("Unable to obtain system session", e);
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return false;
}
return true;
}
示例6: preHandle
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws ServletException {
if (BroadleafResourceHttpRequestHandler.class.isAssignableFrom(handler.getClass())) {
// Bundle resources are cached for ten years
BroadleafResourceHttpRequestHandler h = (BroadleafResourceHttpRequestHandler) handler;
if (h.isBundleRequest(request)) {
applyCacheSeconds(response, TEN_YEARS_SECONDS);
}
return true;
} else if (ResourceHttpRequestHandler.class.isAssignableFrom(handler.getClass())) {
// Non-bundle resources will not specify cache parameters - we will rely on the server responding
// with a 304 if the resource hasn't been modified.
return true;
} else {
// Non-resources (meaning requests that go to controllers) will apply the configured caching parameters.
return super.preHandle(request, response, handler);
}
}
示例7: preHandle
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws ServletException {
if (SparkResourceHttpRequestHandler.class.isAssignableFrom(handler.getClass())) {
// Bundle resources are cached for ten years
SparkResourceHttpRequestHandler h = (SparkResourceHttpRequestHandler) handler;
if (h.isBundleRequest(request)) {
applyCacheSeconds(response, TEN_YEARS_SECONDS);
}
return true;
} else if (ResourceHttpRequestHandler.class.isAssignableFrom(handler.getClass())) {
// Non-bundle resources will not specify cache parameters - we will rely on the server responding
// with a 304 if the resource hasn't been modified.
return true;
} else {
// Non-resources (meaning requests that go to controllers) will apply the configured caching parameters.
return super.preHandle(request, response, handler);
}
}
示例8: getHandlerMapping
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
/**
* 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 : registrations) {
for (String pathPattern : registration.getPathPatterns()) {
ResourceHttpRequestHandler requestHandler = registration.getRequestHandler();
requestHandler.setServletContext(servletContext);
requestHandler.setApplicationContext(applicationContext);
urlMap.put(pathPattern, requestHandler);
}
}
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setOrder(order);
handlerMapping.setUrlMap(urlMap);
return handlerMapping;
}
示例9: registerResourceHandler
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
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);
}
String beanName = parserContext.getReaderContext().generateBeanName(resourceHandlerDef);
parserContext.getRegistry().registerBeanDefinition(beanName, resourceHandlerDef);
parserContext.registerComponent(new BeanComponentDefinition(resourceHandlerDef, beanName));
return beanName;
}
示例10: getHandlerMapping
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
/**
* 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;
}
示例11: registerResourceHandler
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
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;
}
示例12: testResources
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@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);
}
示例13: testResourcesWithOptionalAttributes
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@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());
}
示例14: testResourcesWithResolversTransformers
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@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());
}
示例15: testResourcesWithResolversTransformersCustom
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; //导入依赖的package包/类
@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));
}