001/* 002 * Copyright 2002-2014 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; 025import javax.servlet.ServletContext; 026 027import org.apache.tiles.request.ApplicationResource; 028import org.apache.tiles.request.locale.URLApplicationResource; 029import org.apache.tiles.request.servlet.ServletApplicationContext; 030 031import org.springframework.core.io.Resource; 032import org.springframework.core.io.support.ResourcePatternResolver; 033import org.springframework.util.CollectionUtils; 034import org.springframework.util.ObjectUtils; 035import org.springframework.web.context.support.ServletContextResourcePatternResolver; 036 037/** 038 * Spring-specific subclass of the Tiles ServletApplicationContext. 039 * 040 * @author Rossen Stoyanchev 041 * @author Juergen Hoeller 042 * @since 3.2 043 */ 044public class SpringWildcardServletTilesApplicationContext extends ServletApplicationContext { 045 046 private final ResourcePatternResolver resolver; 047 048 049 public SpringWildcardServletTilesApplicationContext(ServletContext servletContext) { 050 super(servletContext); 051 this.resolver = new ServletContextResourcePatternResolver(servletContext); 052 } 053 054 055 @Override 056 public ApplicationResource getResource(String localePath) { 057 Collection<ApplicationResource> urlSet = getResources(localePath); 058 if (!CollectionUtils.isEmpty(urlSet)) { 059 return urlSet.iterator().next(); 060 } 061 return null; 062 } 063 064 @Override 065 public ApplicationResource getResource(ApplicationResource base, Locale locale) { 066 Collection<ApplicationResource> urlSet = getResources(base.getLocalePath(locale)); 067 if (!CollectionUtils.isEmpty(urlSet)) { 068 return urlSet.iterator().next(); 069 } 070 return null; 071 } 072 073 @Override 074 public Collection<ApplicationResource> getResources(String path) { 075 Resource[] resources; 076 try { 077 resources = this.resolver.getResources(path); 078 } 079 catch (IOException ex) { 080 ((ServletContext) getContext()).log("Resource retrieval failed for path: " + path, ex); 081 return Collections.emptyList(); 082 } 083 if (ObjectUtils.isEmpty(resources)) { 084 ((ServletContext) getContext()).log("No resources found for path pattern: " + path); 085 return Collections.emptyList(); 086 } 087 088 Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>(resources.length); 089 for (Resource resource : resources) { 090 try { 091 URL url = resource.getURL(); 092 resourceList.add(new URLApplicationResource(url.toExternalForm(), url)); 093 } 094 catch (IOException ex) { 095 // Shouldn't happen with the kind of resources we're using 096 throw new IllegalArgumentException("No URL for " + resource, ex); 097 } 098 } 099 return resourceList; 100 } 101 102}