Skip to content

Commit d53b67f

Browse files
committed
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 (cherry picked from commit c8b8dc5)
1 parent f812998 commit d53b67f

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

spring-webmvc-tiles3/src/main/java/org/springframework/web/servlet/view/tiles3/SpringWildcardServletTilesApplicationContext.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
* Spring-specific subclass of the Tiles ServletApplicationContext.
3838
*
3939
* @author Rossen Stoyanchev
40+
* @author Juergen Hoeller
4041
* @since 3.2
4142
*/
4243
public class SpringWildcardServletTilesApplicationContext extends ServletApplicationContext {
@@ -77,24 +78,26 @@ public Collection<ApplicationResource> getResources(String path) {
7778
resources = this.resolver.getResources(path);
7879
}
7980
catch (IOException ex) {
80-
return Collections.<ApplicationResource> emptyList();
81+
((ServletContext) getContext()).log("Resource retrieval failed for path: " + path, ex);
82+
return Collections.emptyList();
8183
}
82-
Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>();
83-
if (!ObjectUtils.isEmpty(resources)) {
84-
for (Resource resource : resources) {
85-
URL url;
86-
try {
87-
url = resource.getURL();
88-
resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
89-
}
90-
catch (IOException ex) {
91-
// shouldn't happen with the kind of resources we're using
92-
throw new IllegalArgumentException("No URL for " + resource.toString(), ex);
93-
}
84+
if (ObjectUtils.isEmpty(resources)) {
85+
((ServletContext) getContext()).log("No resources found for path pattern: " + path);
86+
return Collections.emptyList();
87+
}
88+
89+
Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>(resources.length);
90+
for (Resource resource : resources) {
91+
try {
92+
URL url = resource.getURL();
93+
resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
94+
}
95+
catch (IOException ex) {
96+
// Shouldn't happen with the kind of resources we're using
97+
throw new IllegalArgumentException("No URL for " + resource, ex);
9498
}
9599
}
96100
return resourceList;
97101
}
98102

99103
}
100-

spring-webmvc-tiles3/src/main/java/org/springframework/web/servlet/view/tiles3/TilesConfigurer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.servlet.view.tiles3;
1818

19+
import java.util.Collection;
1920
import java.util.LinkedList;
2021
import java.util.List;
2122
import javax.el.ArrayELResolver;
@@ -285,7 +286,10 @@ protected List<ApplicationResource> getSources(ApplicationContext applicationCon
285286
if (definitions != null) {
286287
List<ApplicationResource> result = new LinkedList<ApplicationResource>();
287288
for (String definition : definitions) {
288-
result.addAll(applicationContext.getResources(definition));
289+
Collection<ApplicationResource> resources = applicationContext.getResources(definition);
290+
if (resources != null) {
291+
result.addAll(resources);
292+
}
289293
}
290294
return result;
291295
}

spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles2/SpringTilesApplicationContextFactory.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@
1919
import java.io.IOException;
2020
import java.net.URL;
2121
import java.util.Enumeration;
22-
import java.util.HashSet;
2322
import java.util.LinkedHashMap;
23+
import java.util.LinkedHashSet;
2424
import java.util.Map;
2525
import java.util.Set;
2626
import javax.servlet.ServletContext;
@@ -32,6 +32,8 @@
3232

3333
import org.springframework.core.io.Resource;
3434
import org.springframework.core.io.support.ResourcePatternResolver;
35+
import org.springframework.util.CollectionUtils;
36+
import org.springframework.util.ObjectUtils;
3537
import org.springframework.web.context.support.ServletContextResourcePatternResolver;
3638

3739
/**
@@ -89,7 +91,7 @@ public Map<String, String> getInitParams() {
8991
public URL getResource(String path) throws IOException {
9092
URL retValue = null;
9193
Set<URL> urlSet = getResources(path);
92-
if (urlSet != null && !urlSet.isEmpty()) {
94+
if (!CollectionUtils.isEmpty(urlSet)) {
9395
retValue = urlSet.iterator().next();
9496
}
9597
return retValue;
@@ -99,8 +101,8 @@ public URL getResource(String path) throws IOException {
99101
public Set<URL> getResources(String path) throws IOException {
100102
Set<URL> urlSet = null;
101103
Resource[] resources = this.resolver.getResources(path);
102-
if (resources != null && resources.length > 0) {
103-
urlSet = new HashSet<URL>();
104+
if (!ObjectUtils.isEmpty(resources)) {
105+
urlSet = new LinkedHashSet<URL>(resources.length);
104106
for (Resource resource : resources) {
105107
urlSet.add(resource.getURL());
106108
}

spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.Properties;
28+
import java.util.Set;
2829
import javax.servlet.ServletContext;
2930
import javax.servlet.jsp.JspFactory;
3031

@@ -420,7 +421,10 @@ protected List<URL> getSourceURLs(TilesApplicationContext applicationContext,
420421
try {
421422
List<URL> result = new LinkedList<URL>();
422423
for (String definition : definitions) {
423-
result.addAll(applicationContext.getResources(definition));
424+
Set<URL> resources = applicationContext.getResources(definition);
425+
if (resources != null) {
426+
result.addAll(resources);
427+
}
424428
}
425429
return result;
426430
}

0 commit comments

Comments
 (0)