TilesConfigurer defensively expects null from getResources in case of no resources found

Also includes order preservation for resource results with Tiles 2 as well as retrieval failure logging with Tiles 3.

Issue: SPR-12362
This commit is contained in:
Juergen Hoeller 2014-10-23 17:23:52 +02:00
parent 1c217aae40
commit cb860364dd
4 changed files with 32 additions and 20 deletions

View File

@ -18,7 +18,7 @@ package org.springframework.web.servlet.view.tiles2;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.HashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
@ -26,6 +26,7 @@ import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.support.ServletContextResourcePatternResolver; import org.springframework.web.context.support.ServletContextResourcePatternResolver;
/** /**
@ -59,8 +60,8 @@ public class SpringWildcardServletTilesApplicationContext extends ServletTilesAp
public Set<URL> getResources(String path) throws IOException { public Set<URL> getResources(String path) throws IOException {
Set<URL> urlSet = null; Set<URL> urlSet = null;
Resource[] resources = this.resolver.getResources(path); Resource[] resources = this.resolver.getResources(path);
if (resources != null && resources.length > 0) { if (!ObjectUtils.isEmpty(resources)) {
urlSet = new HashSet<URL>(); urlSet = new LinkedHashSet<URL>(resources.length);
for (Resource resource : resources) { for (Resource resource : resources) {
urlSet.add(resource.getURL()); urlSet.add(resource.getURL());
} }

View File

@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.jsp.JspFactory; import javax.servlet.jsp.JspFactory;
@ -307,7 +308,10 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
try { try {
List<URL> result = new LinkedList<URL>(); List<URL> result = new LinkedList<URL>();
for (String definition : definitions) { for (String definition : definitions) {
result.addAll(applicationContext.getResources(definition)); Set<URL> resources = applicationContext.getResources(definition);
if (resources != null) {
result.addAll(resources);
}
} }
return result; return result;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,6 +37,7 @@ import org.springframework.web.context.support.ServletContextResourcePatternReso
* Spring-specific subclass of the Tiles ServletApplicationContext. * Spring-specific subclass of the Tiles ServletApplicationContext.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.2 * @since 3.2
*/ */
public class SpringWildcardServletTilesApplicationContext extends ServletApplicationContext { public class SpringWildcardServletTilesApplicationContext extends ServletApplicationContext {
@ -77,24 +78,26 @@ public class SpringWildcardServletTilesApplicationContext extends ServletApplica
resources = this.resolver.getResources(path); resources = this.resolver.getResources(path);
} }
catch (IOException ex) { catch (IOException ex) {
return Collections.<ApplicationResource> emptyList(); ((ServletContext) getContext()).log("Resource retrieval failed for path: " + path, ex);
return Collections.emptyList();
} }
Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>(); if (ObjectUtils.isEmpty(resources)) {
if (!ObjectUtils.isEmpty(resources)) { ((ServletContext) getContext()).log("No resources found for path pattern: " + path);
for (Resource resource : resources) { return Collections.emptyList();
URL url; }
try {
url = resource.getURL(); Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>(resources.length);
resourceList.add(new URLApplicationResource(url.toExternalForm(), url)); for (Resource resource : resources) {
} try {
catch (IOException ex) { URL url = resource.getURL();
// shouldn't happen with the kind of resources we're using resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
throw new IllegalArgumentException("No URL for " + resource.toString(), ex); }
} catch (IOException ex) {
// Shouldn't happen with the kind of resources we're using
throw new IllegalArgumentException("No URL for " + resource, ex);
} }
} }
return resourceList; return resourceList;
} }
} }

View File

@ -16,6 +16,7 @@
package org.springframework.web.servlet.view.tiles3; package org.springframework.web.servlet.view.tiles3;
import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.el.ArrayELResolver; import javax.el.ArrayELResolver;
@ -302,7 +303,10 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
if (definitions != null) { if (definitions != null) {
List<ApplicationResource> result = new LinkedList<ApplicationResource>(); List<ApplicationResource> result = new LinkedList<ApplicationResource>();
for (String definition : definitions) { for (String definition : definitions) {
result.addAll(applicationContext.getResources(definition)); Collection<ApplicationResource> resources = applicationContext.getResources(definition);
if (resources != null) {
result.addAll(resources);
}
} }
return result; return result;
} }