Coverage Report - org.apache.myfaces.resource.ExternalContextContractResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
ExternalContextContractResourceLoader
0%
0/60
0%
0/40
4.5
 
 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  0
     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  0
     protected static final Pattern RESOURCE_VERSION_CHECKER = Pattern.compile("/\\p{Digit}+(_\\p{Digit}*)*\\..*");
 52  
 
 53  
     public ExternalContextContractResourceLoader(String prefix)
 54  
     {
 55  0
         super(prefix);
 56  0
     }
 57  
 
 58  
     protected Set<String> getResourcePaths(String contractName, String path)
 59  
     {
 60  0
         return FacesContext.getCurrentInstance().getExternalContext().getResourcePaths(
 61  
             getPrefix() + '/' + contractName + '/' + path);
 62  
     }
 63  
 
 64  
     @Override
 65  
     public String getResourceVersion(String path, String contractName)
 66  
     {
 67  0
         String resourceVersion = null;
 68  0
         Set<String> resourcePaths = this.getResourcePaths(contractName, path);
 69  0
         if (getPrefix() != null)
 70  
         {
 71  0
             path = getPrefix() + '/' + path;
 72  
         }
 73  
 
 74  0
         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  0
             for (String resourcePath : resourcePaths)
 83  
             {
 84  0
                 String version = "";
 85  0
                 if (path.length() < resourcePath.length()) 
 86  
                 {
 87  0
                     version = resourcePath.substring(path.length());
 88  
                 }
 89  
 
 90  0
                 if (RESOURCE_VERSION_CHECKER.matcher(version).matches())
 91  
                 {
 92  0
                     version = version.substring(1, version.lastIndexOf('.'));
 93  0
                     if (resourceVersion == null)
 94  
                     {
 95  0
                         resourceVersion = version;
 96  
                     }
 97  0
                     else if (getVersionComparator().compare(resourceVersion, version) < 0)
 98  
                     {
 99  0
                         resourceVersion = version;
 100  
                     }
 101  
                 }
 102  0
             }
 103  
             //Since it is a directory and no version was found, set as invalid
 104  0
             if (resourceVersion == null)
 105  
             {
 106  0
                 resourceVersion = VERSION_INVALID;
 107  
             }
 108  
         }
 109  0
         return resourceVersion;
 110  
     }
 111  
 
 112  
     @Override
 113  
     public String getLibraryVersion(String path, String contractName)
 114  
     {
 115  0
         String libraryVersion = null;
 116  0
         Set<String> libraryPaths = this.getResourcePaths(contractName, path);
 117  0
         path = getPrefix() + '/' + path;
 118  0
         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  0
             for (Iterator<String> it = libraryPaths.iterator(); it.hasNext();)
 127  
             {
 128  0
                 String libraryPath = it.next();
 129  0
                 String version = "";
 130  0
                 if (path.length() < libraryPath.length())
 131  
                 {
 132  0
                     version = libraryPath.substring(path.length());
 133  
                 }
 134  
 
 135  0
                 if (VERSION_CHECKER.matcher(version).matches())
 136  
                 {
 137  0
                     version = version.substring(1, version.length() - 1);
 138  0
                     if (libraryVersion == null)
 139  
                     {
 140  0
                         libraryVersion = version;
 141  
                     }
 142  0
                     else if (getVersionComparator().compare(libraryVersion, version) < 0)
 143  
                     {
 144  0
                         libraryVersion = version;
 145  
                     }
 146  
                 }
 147  0
             }
 148  
         }
 149  0
         return libraryVersion;
 150  
     }
 151  
 
 152  
     @Override
 153  
     public URL getResourceURL(ResourceMeta resourceMeta)
 154  
     {
 155  
         try
 156  
         {
 157  0
             return FacesContext.getCurrentInstance().getExternalContext().getResource(
 158  
                 getPrefix() + '/' + resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier());
 159  
         }
 160  0
         catch (MalformedURLException e)
 161  
         {
 162  0
             return null;
 163  
         }
 164  
     }
 165  
 
 166  
     @Override
 167  
     public InputStream getResourceInputStream(ResourceMeta resourceMeta)
 168  
     {
 169  0
         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  0
         return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, 
 178  
             resourceVersion, contractName);
 179  
     }
 180  
 
 181  
     @Override
 182  
     public boolean libraryExists(String libraryName, String contractName)
 183  
     {
 184  0
         if (getPrefix() != null && !"".equals(getPrefix()))
 185  
         {
 186  
             try
 187  
             {
 188  0
                 URL url =
 189  
                     FacesContext.getCurrentInstance().getExternalContext().getResource(
 190  
                         getPrefix() + '/' + contractName + '/' + libraryName);
 191  0
                 if (url != null)
 192  
                 {
 193  0
                     return true;
 194  
                 }
 195  
             }
 196  0
             catch (MalformedURLException e)
 197  
             {
 198  0
                 return false;
 199  0
             }
 200  
         }
 201  
         else
 202  
         {
 203  
             try
 204  
             {
 205  
 
 206  0
                 URL url = FacesContext.getCurrentInstance().
 207  
                     getExternalContext().getResource(contractName + '/' +libraryName);
 208  
 
 209  0
                 if (url != null)
 210  
                 {
 211  0
                     return true;
 212  
                 }
 213  
             }
 214  0
             catch (MalformedURLException e)
 215  
             {
 216  0
                 return false;
 217  0
             }
 218  
         }
 219  0
         return false;
 220  
     }
 221  
     
 222  
 }