View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.resource;
20  
21  import java.io.InputStream;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.Set;
25  
26  import javax.faces.context.FacesContext;
27  import org.apache.myfaces.shared.resource.ResourceLoader;
28  import org.apache.myfaces.shared.resource.ResourceMeta;
29  import org.apache.myfaces.shared.resource.ResourceMetaImpl;
30  
31  /**
32   * A resource loader implementation which loads resources from the webapp root.
33   * It uses the methods on ExternalContext for handle resources.
34   * 
35   */
36  public class RootExternalContextResourceLoader extends ResourceLoader
37  {
38  
39      public RootExternalContextResourceLoader()
40      {
41          super("");
42      }
43  
44      protected Set<String> getResourcePaths(String path)
45      {
46          if (path.startsWith("/"))
47          {
48              return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(path);
49          }
50          else
51          {
52              return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths('/' + path);
53          }
54      }
55  
56      @Override
57      public String getResourceVersion(String path)
58      {
59          return null;
60      }
61  
62      @Override
63      public String getLibraryVersion(String path)
64      {
65          return null;
66      }
67  
68      //@Override
69      public URL getResourceURL(String resourceId)
70      {
71          try
72          {
73              if (resourceId.startsWith("/"))
74              {
75                  return FacesContext.getCurrentInstance().getExternalContext().getResource(
76                      resourceId);
77              }
78              else
79              {
80                  return FacesContext.getCurrentInstance().getExternalContext().getResource(
81                      '/' + resourceId);
82              }
83          }
84          catch (MalformedURLException e)
85          {
86              return null;
87          }
88      }
89      
90      @Override
91      public URL getResourceURL(ResourceMeta resourceMeta)
92      {
93          if (resourceMeta.getLocalePrefix() != null)
94          {
95              // the webapp root folder cannot be localized, 
96              // but if the resource lives in /contracts/[contract-name] it can be.
97              return null;
98          }
99          return getResourceURL(resourceMeta.getResourceIdentifier());
100     }
101 
102     @Override
103     public InputStream getResourceInputStream(ResourceMeta resourceMeta)
104     {
105         return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(
106             '/' + resourceMeta.getResourceIdentifier());
107     }
108 
109     @Override
110     public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
111                                            String resourceName, String resourceVersion)
112     {
113         return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, resourceVersion);
114     }
115 
116     @Override
117     public boolean libraryExists(String libraryName)
118     {
119         //No library can be created in root resolver
120         return false;
121     }
122 }