This project has retired. For details please refer to its Attic page.
WildcardServletApplicationContext xref
View Javadoc

1   /*
2    * $Id: WildcardServletApplicationContext.java 1306435 2012-03-28 15:39:11Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles.request.servlet.wildcard;
23  
24  import java.io.IOException;
25  import java.net.URL;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.Locale;
30  
31  import javax.servlet.ServletContext;
32  
33  import org.apache.tiles.request.ApplicationResource;
34  import org.apache.tiles.request.locale.URLApplicationResource;
35  import org.apache.tiles.request.servlet.ServletApplicationContext;
36  import org.springframework.core.io.Resource;
37  import org.springframework.core.io.support.ResourcePatternResolver;
38  import org.springframework.web.context.support.ServletContextResourcePatternResolver;
39  
40  /**
41   * Servlet-based implementation of the TilesApplicationContext interface that
42   * can resolve resources even using wildcards.
43   *
44   * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
45   */
46  public class WildcardServletApplicationContext extends ServletApplicationContext {
47  
48      /**
49       * The pattern resolver.
50       */
51      protected ResourcePatternResolver resolver;
52  
53      /**
54       * Constructor.
55       *
56       * @param servletContext The servlet context.
57       */
58      public WildcardServletApplicationContext(ServletContext servletContext) {
59          super(servletContext);
60          resolver = new ServletContextResourcePatternResolver(servletContext);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public ApplicationResource getResource(String localePath) {
66          ApplicationResource retValue = null;
67          Collection<ApplicationResource> urlSet = getResources(localePath);
68          if (urlSet != null && !urlSet.isEmpty()) {
69              retValue = urlSet.iterator().next();
70          }
71          return retValue;
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      public ApplicationResource getResource(ApplicationResource base, Locale locale) {
77          ApplicationResource retValue = null;
78          Collection<ApplicationResource> urlSet = getResources(base.getLocalePath(locale));
79          if (urlSet != null && !urlSet.isEmpty()) {
80              retValue = urlSet.iterator().next();
81          }
82          return retValue;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public Collection<ApplicationResource> getResources(String path) {
88          Resource[] resources;
89          try {
90              resources = resolver.getResources(path);
91          } catch (IOException e) {
92              return Collections.<ApplicationResource> emptyList();
93          }
94          Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>();
95          if (resources != null && resources.length > 0) {
96              for (int i = 0; i < resources.length; i++) {
97                  URL url;
98                  try {
99                      url = resources[i].getURL();
100                     resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
101                 } catch (IOException e) {
102                     // shouldn't happen with the kind of resources we're using
103                     throw new IllegalArgumentException("no URL for " + resources[i].toString(), e);
104                 }
105             }
106         }
107         return resourceList;
108     }
109 }