001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.view.tiles3; 018 019import java.io.IOException; 020import java.net.URL; 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.Collections; 024import java.util.Locale; 025 026import javax.servlet.ServletContext; 027 028import org.apache.tiles.request.ApplicationResource; 029import org.apache.tiles.request.locale.URLApplicationResource; 030import org.apache.tiles.request.servlet.ServletApplicationContext; 031 032import org.springframework.core.io.Resource; 033import org.springframework.core.io.support.ResourcePatternResolver; 034import org.springframework.lang.Nullable; 035import org.springframework.util.CollectionUtils; 036import org.springframework.util.ObjectUtils; 037import org.springframework.web.context.support.ServletContextResourcePatternResolver; 038 039/** 040 * Spring-specific subclass of the Tiles ServletApplicationContext. 041 * 042 * @author Rossen Stoyanchev 043 * @author Juergen Hoeller 044 * @since 3.2 045 */ 046public class SpringWildcardServletTilesApplicationContext extends ServletApplicationContext { 047 048 private final ResourcePatternResolver resolver; 049 050 051 public SpringWildcardServletTilesApplicationContext(ServletContext servletContext) { 052 super(servletContext); 053 this.resolver = new ServletContextResourcePatternResolver(servletContext); 054 } 055 056 057 @Override 058 @Nullable 059 public ApplicationResource getResource(String localePath) { 060 Collection<ApplicationResource> urlSet = getResources(localePath); 061 if (!CollectionUtils.isEmpty(urlSet)) { 062 return urlSet.iterator().next(); 063 } 064 return null; 065 } 066 067 @Override 068 @Nullable 069 public ApplicationResource getResource(ApplicationResource base, Locale locale) { 070 Collection<ApplicationResource> urlSet = getResources(base.getLocalePath(locale)); 071 if (!CollectionUtils.isEmpty(urlSet)) { 072 return urlSet.iterator().next(); 073 } 074 return null; 075 } 076 077 @Override 078 public Collection<ApplicationResource> getResources(String path) { 079 Resource[] resources; 080 try { 081 resources = this.resolver.getResources(path); 082 } 083 catch (IOException ex) { 084 ((ServletContext) getContext()).log("Resource retrieval failed for path: " + path, ex); 085 return Collections.emptyList(); 086 } 087 if (ObjectUtils.isEmpty(resources)) { 088 ((ServletContext) getContext()).log("No resources found for path pattern: " + path); 089 return Collections.emptyList(); 090 } 091 092 Collection<ApplicationResource> resourceList = new ArrayList<>(resources.length); 093 for (Resource resource : resources) { 094 try { 095 URL url = resource.getURL(); 096 resourceList.add(new URLApplicationResource(url.toExternalForm(), url)); 097 } 098 catch (IOException ex) { 099 // Shouldn't happen with the kind of resources we're using 100 throw new IllegalArgumentException("No URL for " + resource, ex); 101 } 102 } 103 return resourceList; 104 } 105 106}