1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.skin

File EnvironmentSkin.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

16
38
8
1
160
103
17
0.45
4.75
8
2.12

Classes

Class Line # Actions
EnvironmentSkin 47 38 0% 17 9
0.854838785.5%
 

Contributing tests

This file is covered by 23 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 com.xpn.xwiki.internal.skin;
21   
22    import java.net.URL;
23    import java.nio.file.Path;
24    import java.nio.file.Paths;
25   
26    import javax.inject.Provider;
27   
28    import org.apache.commons.configuration2.BaseConfiguration;
29    import org.apache.commons.configuration2.Configuration;
30    import org.apache.commons.configuration2.builder.fluent.Configurations;
31    import org.apache.commons.configuration2.ex.ConfigurationException;
32    import org.apache.commons.lang3.exception.ExceptionUtils;
33    import org.slf4j.Logger;
34    import org.slf4j.LoggerFactory;
35    import org.xwiki.environment.Environment;
36    import org.xwiki.skin.Resource;
37    import org.xwiki.skin.Skin;
38   
39    import com.xpn.xwiki.XWikiContext;
40   
41    /**
42    * Represents a skin stored in the file system.
43    *
44    * @version $Id: ab0098a4a52f229f811ad0f4b129f488a592b442 $
45    * @since 6.4M1
46    */
 
47    public class EnvironmentSkin extends AbstractSkin
48    {
49    private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentSkin.class);
50   
51    private Environment environment;
52   
53    private Configuration properties;
54   
55    private Provider<XWikiContext> xcontextProvider;
56   
 
57  2122096 toggle public EnvironmentSkin(String id, InternalSkinManager skinManager, InternalSkinConfiguration configuration,
58    Logger logger, Environment environment, Provider<XWikiContext> xcontextProvider)
59    {
60  2121352 super(id, skinManager, configuration, logger);
61   
62  2122604 this.environment = environment;
63  2122678 this.xcontextProvider = xcontextProvider;
64    }
65   
 
66  1211252 toggle @Override
67    protected Skin createParent()
68    {
69  1211736 Skin skin;
70   
71  1212143 String parentId = getProperties().getString("parent");
72   
73  1211037 if (parentId != null) {
74  1201815 if (parentId.isEmpty()) {
75    // There is explicitly no parent (make sure to not fallback on default parent skin)
76  1202267 skin = VOID;
77    } else {
78  0 skin = this.skinManager.getSkin(parentId);
79    }
80    } else {
81  9569 skin = null;
82    }
83   
84  1211165 return skin;
85    }
86   
 
87  1332708 toggle public Configuration getProperties()
88    {
89  1333679 if (this.properties == null) {
90  1333316 URL url = this.environment.getResource(getSkinFolder() + "skin.properties");
91  1333163 if (url != null) {
92  1323710 try {
93  1323928 this.properties = new Configurations().properties(url);
94    } catch (ConfigurationException e) {
95  0 LOGGER.error("Failed to load skin [{}] properties file ([])", this.id, url,
96    ExceptionUtils.getRootCauseMessage(e));
97   
98  0 this.properties = new BaseConfiguration();
99    }
100    } else {
101  9569 LOGGER.debug("No properties found for skin [{}]", this.id);
102   
103  9569 this.properties = new BaseConfiguration();
104    }
105    }
106   
107  1331554 return this.properties;
108    }
109   
 
110  2705137 toggle public String getSkinFolder()
111    {
112  2706407 return "/skins/" + this.id + '/';
113    }
114   
 
115  1371405 toggle @Override
116    public Resource<?> getLocalResource(String resourceName)
117    {
118  1372102 String resourcePath = getResourcePath(resourceName, false);
119   
120  1372367 if (this.environment.getResource(resourcePath) != null) {
121  529858 return createResource(resourcePath, resourceName);
122    }
123   
124  842648 return null;
125    }
126   
 
127  529786 toggle protected AbstractEnvironmentResource createResource(String resourcePath, String resourceName)
128    {
129  529941 return new SkinEnvironmentResource(resourcePath, resourceName, this, this.environment, this.xcontextProvider);
130    }
131   
 
132  1371895 toggle private String getResourcePath(String resource, boolean testExist)
133    {
134  1371814 String skinFolder = getSkinFolder();
135  1372728 String resourcePath = skinFolder + resource;
136   
137    // Prevent inclusion of templates from other directories
138  1372924 Path normalizedResource = Paths.get(resourcePath).normalize();
139  1372468 if (!normalizedResource.startsWith(skinFolder)) {
140  5 LOGGER.warn("Direct access to template file [{}] refused. Possible break-in attempt!", normalizedResource);
141   
142  5 return null;
143    }
144   
145  1372653 if (testExist) {
146    // Check if the resource exist
147  0 if (this.environment.getResource(resourcePath) == null) {
148  0 return null;
149    }
150    }
151   
152  1371930 return resourcePath;
153    }
154   
 
155  121016 toggle @Override
156    public String getOutputSyntaxString()
157    {
158  121237 return getProperties().getString("outputSyntax");
159    }
160    }