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.Iterator;
25  import java.util.Set;
26  import java.util.regex.Pattern;
27  import javax.faces.context.FacesContext;
28  import org.apache.myfaces.shared.resource.ContractResourceLoader;
29  import org.apache.myfaces.shared.resource.ResourceMeta;
30  import org.apache.myfaces.shared.resource.ResourceMetaImpl;
31  
32  /**
33   *
34   * @author lu4242
35   */
36  public class ExternalContextContractResourceLoader extends ContractResourceLoader
37  {
38  
39      /**
40       * It checks version like this: /1/, /1_0/, /1_0_0/, /100_100/
41       * 
42       * Used on getLibraryVersion to filter resource directories
43       **/
44      protected static final Pattern VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*/");
45  
46      /**
47       * It checks version like this: /1.js, /1_0.js, /1_0_0.js, /100_100.js
48       * 
49       * Used on getResourceVersion to filter resources
50       **/
51      protected static final Pattern RESOURCE_VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*\\..*");
52  
53      public ExternalContextContractResourceLoader(String prefix)
54      {
55          super(prefix);
56      }
57  
58      protected Set<String> getResourcePaths(String contractName, String path)
59      {
60          return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(
61              getPrefix() + '/' + contractName + '/' + path);
62      }
63  
64      @Override
65      public String getResourceVersion(String path, String contractName)
66      {
67          String resourceVersion = null;
68          Set<String> resourcePaths = this.getResourcePaths(contractName, path);
69          if (getPrefix() != null)
70          {
71              path = getPrefix() + '/' + path;
72          }
73  
74          if (null != resourcePaths && !resourcePaths.isEmpty())
75          {
76              // resourceVersion = // execute the comment
77              // Look in the resourcePaths for versioned resources.
78              // If one or more versioned resources are found, take
79              // the one with the "highest" version number as the value
80              // of resourceVersion. If no versioned libraries
81              // are found, let resourceVersion remain null.
82              for (String resourcePath : resourcePaths)
83              {
84                  String version = "";
85                  if (path.length() < resourcePath.length()) 
86                  {
87                      version = resourcePath.substring(path.length());
88                  }
89  
90                  if (RESOURCE_VERSION_CHECKER.matcher(version).matches())
91                  {
92                      version = version.substring(1, version.lastIndexOf('.'));
93                      if (resourceVersion == null)
94                      {
95                          resourceVersion = version;
96                      }
97                      else if (getVersionComparator().compare(resourceVersion, version) < 0)
98                      {
99                          resourceVersion = version;
100                     }
101                 }
102             }
103             //Since it is a directory and no version was found, set as invalid
104             if (resourceVersion == null)
105             {
106                 resourceVersion = VERSION_INVALID;
107             }
108         }
109         return resourceVersion;
110     }
111 
112     @Override
113     public String getLibraryVersion(String path, String contractName)
114     {
115         String libraryVersion = null;
116         Set<String> libraryPaths = this.getResourcePaths(contractName, path);
117         path = getPrefix() + '/' + path;
118         if (null != libraryPaths && !libraryPaths.isEmpty())
119         {
120             // Look in the libraryPaths for versioned libraries.
121             // If one or more versioned libraries are found, take
122             // the one with the "highest" version number as the value
123             // of libraryVersion. If no versioned libraries
124             // are found, let libraryVersion remain null.
125 
126             for (Iterator<String> it = libraryPaths.iterator(); it.hasNext();)
127             {
128                 String libraryPath = it.next();
129                 String version = "";
130                 if (path.length() < libraryPath.length())
131                 {
132                     version = libraryPath.substring(path.length());
133                 }
134 
135                 if (VERSION_CHECKER.matcher(version).matches())
136                 {
137                     version = version.substring(1, version.length() - 1);
138                     if (libraryVersion == null)
139                     {
140                         libraryVersion = version;
141                     }
142                     else if (getVersionComparator().compare(libraryVersion, version) < 0)
143                     {
144                         libraryVersion = version;
145                     }
146                 }
147             }
148         }
149         return libraryVersion;
150     }
151 
152     @Override
153     public URL getResourceURL(ResourceMeta resourceMeta)
154     {
155         try
156         {
157             return FacesContext.getCurrentInstance().getExternalContext().getResource(
158                 getPrefix() + '/' + resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier());
159         }
160         catch (MalformedURLException e)
161         {
162             return null;
163         }
164     }
165 
166     @Override
167     public InputStream getResourceInputStream(ResourceMeta resourceMeta)
168     {
169         return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(
170             getPrefix() + '/' + resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier());
171     }
172 
173     @Override
174     public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
175                                            String resourceName, String resourceVersion, String contractName)
176     {
177         return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, 
178             resourceVersion, contractName);
179     }
180 
181     @Override
182     public boolean libraryExists(String libraryName, String contractName)
183     {
184         if (getPrefix() != null && !"".equals(getPrefix()))
185         {
186             try
187             {
188                 URL url =
189                     FacesContext.getCurrentInstance().getExternalContext().getResource(
190                         getPrefix() + '/' + contractName + '/' + libraryName);
191                 if (url != null)
192                 {
193                     return true;
194                 }
195             }
196             catch (MalformedURLException e)
197             {
198                 return false;
199             }
200         }
201         else
202         {
203             try
204             {
205 
206                 URL url = FacesContext.getCurrentInstance().
207                     getExternalContext().getResource(contractName + '/' +libraryName);
208 
209                 if (url != null)
210                 {
211                     return true;
212                 }
213             }
214             catch (MalformedURLException e)
215             {
216                 return false;
217             }
218         }
219         return false;
220     }
221     
222 }