From ef39634e302748f3473aec6468cb7d06df92ced2 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 7 Jul 2015 23:59:35 -0700 Subject: [PATCH] Polish --- .../autoconfigure/web/ResourceProperties.java | 114 ++++++++---------- 1 file changed, 53 insertions(+), 61 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java index 34d4ad5654..d9fb396561 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java @@ -54,13 +54,11 @@ public class ResourceProperties implements ResourceLoaderAware { SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length); } - private static final String[] STATIC_INDEX_HTML_RESOURCES; - static { - STATIC_INDEX_HTML_RESOURCES = new String[RESOURCE_LOCATIONS.length]; - for (int i = 0; i < STATIC_INDEX_HTML_RESOURCES.length; i++) { - STATIC_INDEX_HTML_RESOURCES[i] = RESOURCE_LOCATIONS[i] + "index.html"; - } - } + /** + * Locations of static resources. Defaults to classpath:[/META-INF/resources/, + * /resources/, /static/, /public/] plus context:/ (the root of the servlet context). + */ + private String[] staticLocations = RESOURCE_LOCATIONS; /** * Cache period for the resources served by the resource handler, in seconds. @@ -74,12 +72,6 @@ public class ResourceProperties implements ResourceLoaderAware { private final Chain chain = new Chain(); - /** - * Locations of static resources. Defaults to classpath:[/META-INF/resources/, - * /resources/, /static/, /public/] plus context:/ (the root of the servlet context). - */ - private String[] staticLocations = RESOURCE_LOCATIONS; - private ResourceLoader resourceLoader; @Override @@ -87,6 +79,54 @@ public class ResourceProperties implements ResourceLoaderAware { this.resourceLoader = resourceLoader; } + public String[] getStaticLocations() { + return this.staticLocations; + } + + public void setStaticLocations(String[] staticLocations) { + this.staticLocations = staticLocations; + } + + public Resource getWelcomePage() { + for (String location : getStaticWelcomePageLocations()) { + Resource resource = this.resourceLoader.getResource(location); + if (resource.exists()) { + try { + resource.getURL(); + return resource; + } + catch (IOException ex) { + // Ignore + } + } + } + return null; + } + + private String[] getStaticWelcomePageLocations() { + String[] result = new String[this.staticLocations.length]; + for (int i = 0; i < result.length; i++) { + String location = this.staticLocations[i]; + if (!location.endsWith("/")) { + location = location + "/"; + } + result[i] = location + "index.html"; + } + return result; + } + + public List getFaviconLocations() { + List locations = new ArrayList( + CLASSPATH_RESOURCE_LOCATIONS.length + 1); + if (this.resourceLoader != null) { + for (String location : CLASSPATH_RESOURCE_LOCATIONS) { + locations.add(this.resourceLoader.getResource(location)); + } + } + locations.add(new ClassPathResource("/")); + return Collections.unmodifiableList(locations); + } + public Integer getCachePeriod() { return this.cachePeriod; } @@ -258,52 +298,4 @@ public class ResourceProperties implements ResourceLoaderAware { } - public String[] getStaticLocations() { - return this.staticLocations; - } - - public void setStaticLocations(String[] staticLocations) { - this.staticLocations = staticLocations; - } - - private String[] getStaticWelcomePageLocations() { - String[] result = new String[this.staticLocations.length]; - for (int i = 0; i < result.length; i++) { - String location = this.staticLocations[i]; - if (!location.endsWith("/")) { - location = location + "/"; - } - result[i] = location + "index.html"; - } - return result; - } - - public List getFaviconLocations() { - List locations = new ArrayList( - CLASSPATH_RESOURCE_LOCATIONS.length + 1); - if (this.resourceLoader != null) { - for (String location : CLASSPATH_RESOURCE_LOCATIONS) { - locations.add(this.resourceLoader.getResource(location)); - } - } - locations.add(new ClassPathResource("/")); - return Collections.unmodifiableList(locations); - } - - public Resource getWelcomePage() { - for (String location : getStaticWelcomePageLocations()) { - Resource resource = this.resourceLoader.getResource(location); - if (resource.exists()) { - try { - resource.getURL(); - return resource; - } - catch (IOException ex) { - // Ignore - } - } - } - return null; - } - }