1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.webjars.internal

File WebJarsResourceReferenceHandler.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
14
6
1
123
71
8
0.57
2.33
6
1.33

Classes

Class Line # Actions
WebJarsResourceReferenceHandler 50 14 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.webjars.internal;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.io.InputStreamReader;
25    import java.io.StringWriter;
26    import java.util.Arrays;
27    import java.util.List;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Singleton;
32   
33    import org.xwiki.classloader.ClassLoaderManager;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.resource.ResourceReferenceHandlerException;
36    import org.xwiki.resource.ResourceType;
37    import org.xwiki.resource.servlet.AbstractServletResourceReferenceHandler;
38    import org.xwiki.velocity.VelocityManager;
39   
40    /**
41    * Handles {@code webjars} Resource References.
42    *
43    * @version $Id: 50f2b818855f82f367a89bb1952fc96e736ef309 $
44    * @since 6.1M2
45    * @see WebJarsResourceReferenceResolver for the URL format handled
46    */
47    @Component
48    @Named("webjars")
49    @Singleton
 
50    public class WebJarsResourceReferenceHandler extends AbstractServletResourceReferenceHandler<WebJarsResourceReference>
51    {
52    /**
53    * Prefix for locating resource files (JavaScript, CSS) in the classloader.
54    */
55    private static final String WEBJARS_RESOURCE_PREFIX = "META-INF/resources/webjars/";
56   
57    /**
58    * The encoding used when evaluating WebJar (text) resources.
59    */
60    private static final String UTF8 = "UTF-8";
61   
62    /**
63    * Used to evaluate the Velocity code from the WebJar resources.
64    */
65    @Inject
66    private VelocityManager velocityManager;
67   
68    @Inject
69    private ClassLoaderManager classLoaderManager;
70   
 
71  14721 toggle @Override
72    public List<ResourceType> getSupportedResourceReferences()
73    {
74  14724 return Arrays.asList(WebJarsResourceReference.TYPE);
75    }
76   
 
77  1654 toggle @Override
78    protected InputStream getResourceStream(WebJarsResourceReference resourceReference)
79    {
80  1662 String resourcePath = String.format("%s%s", WEBJARS_RESOURCE_PREFIX, getResourceName(resourceReference));
81  1666 return getClassLoader(resourceReference.getNamespace()).getResourceAsStream(resourcePath);
82    }
83   
 
84  4521 toggle @Override
85    protected String getResourceName(WebJarsResourceReference resourceReference)
86    {
87  4532 return resourceReference.getResourceName();
88    }
89   
 
90  3384 toggle @Override
91    protected boolean isResourceCacheable(WebJarsResourceReference resourceReference)
92    {
93  3384 return !Boolean.valueOf(resourceReference.getParameterValue("evaluate"));
94    }
95   
 
96  1666 toggle @Override
97    protected InputStream filterResource(WebJarsResourceReference resourceReference, InputStream resourceStream)
98    throws ResourceReferenceHandlerException
99    {
100  1666 if (!isResourceCacheable(resourceReference)) {
101  1205 String resourceName = getResourceName(resourceReference);
102  1206 try {
103    // Evaluates the given resource using Velocity.
104  1206 StringWriter writer = new StringWriter();
105  1206 this.velocityManager.getVelocityEngine().evaluate(this.velocityManager.getVelocityContext(), writer,
106    resourceName, new InputStreamReader(resourceStream, UTF8));
107  1205 return new ByteArrayInputStream(writer.toString().getBytes(UTF8));
108    } catch (Exception e) {
109  1 throw new ResourceReferenceHandlerException(
110    String.format("Failed to evaluate the Velocity code from WebJar resource [%s]", resourceName), e);
111    }
112    }
113  460 return super.filterResource(resourceReference, resourceStream);
114    }
115   
116    /**
117    * @return the Class Loader from which to look for WebJars resources
118    */
 
119  1661 toggle protected ClassLoader getClassLoader(String namespace)
120    {
121  1666 return this.classLoaderManager.getURLClassLoader(namespace, true);
122    }
123    }