1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.skin

File EnvironmentSkin.java

 

Coverage histogram

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

Code metrics

16
38
8
1
161
104
17
0.45
4.75
8
2.12

Classes

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

Contributing tests

This file is covered by 20 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.configuration.BaseConfiguration;
29    import org.apache.commons.configuration.Configuration;
30    import org.apache.commons.configuration.ConfigurationException;
31    import org.apache.commons.configuration.PropertiesConfiguration;
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.rendering.syntax.SyntaxFactory;
37    import org.xwiki.skin.Resource;
38    import org.xwiki.skin.Skin;
39   
40    import com.xpn.xwiki.XWikiContext;
41   
42    /**
43    * Represents a skin stored in the file system.
44    *
45    * @version $Id: e6814017adecf4d009e0f6f1c273ec0eb1ac128c $
46    * @since 6.4M1
47    */
 
48    public class EnvironmentSkin extends AbstractSkin
49    {
50    private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentSkin.class);
51   
52    private Environment environment;
53   
54    private Configuration properties;
55   
56    private Provider<XWikiContext> xcontextProvider;
57   
 
58  303496 toggle public EnvironmentSkin(String id, InternalSkinManager skinManager, InternalSkinConfiguration configuration,
59    Logger logger, SyntaxFactory syntaxFactory, Environment environment, Provider<XWikiContext> xcontextProvider)
60    {
61  303505 super(id, skinManager, configuration, logger, syntaxFactory);
62   
63  303500 this.environment = environment;
64  303496 this.xcontextProvider = xcontextProvider;
65    }
66   
 
67  93101 toggle @Override
68    protected Skin createParent()
69    {
70  93098 Skin skin;
71   
72  93098 String parentId = getProperties().getString("parent");
73   
74  93104 if (parentId != null) {
75  93012 if (parentId.isEmpty()) {
76    // There is explicitly no parent (make sure to not fallback on default parent skin)
77  93007 skin = VOID;
78    } else {
79  0 skin = this.skinManager.getSkin(parentId);
80    }
81    } else {
82  97 skin = null;
83    }
84   
85  93101 return skin;
86    }
87   
 
88  104236 toggle public Configuration getProperties()
89    {
90  104252 if (this.properties == null) {
91  104188 URL url = this.environment.getResource(getSkinFolder() + "skin.properties");
92  104201 if (url != null) {
93  104093 try {
94  104099 this.properties = new PropertiesConfiguration(url);
95    } catch (ConfigurationException e) {
96  0 LOGGER.error("Failed to load skin [{}] properties file ([])", this.id, url,
97    ExceptionUtils.getRootCauseMessage(e));
98   
99  0 this.properties = new BaseConfiguration();
100    }
101    } else {
102  97 LOGGER.debug("No properties found for skin [{}]", this.id);
103   
104  97 this.properties = new BaseConfiguration();
105    }
106    }
107   
108  104251 return this.properties;
109    }
110   
 
111  243125 toggle public String getSkinFolder()
112    {
113  243125 return "/skins/" + this.id + '/';
114    }
115   
 
116  138944 toggle @Override
117    public Resource<?> getLocalResource(String resourceName)
118    {
119  138939 String resourcePath = getResourcePath(resourceName, false);
120   
121  138948 if (this.environment.getResource(resourcePath) != null) {
122  48065 return createResource(resourcePath, resourceName);
123    }
124   
125  90883 return null;
126    }
127   
 
128  48060 toggle protected AbstractEnvironmentResource createResource(String resourcePath, String resourceName)
129    {
130  48062 return new SkinEnvironmentResource(resourcePath, resourceName, this, this.environment, this.xcontextProvider);
131    }
132   
 
133  138936 toggle private String getResourcePath(String resource, boolean testExist)
134    {
135  138932 String skinFolder = getSkinFolder();
136  138943 String resourcePath = skinFolder + resource;
137   
138    // Prevent inclusion of templates from other directories
139  138937 Path normalizedResource = Paths.get(resourcePath).normalize();
140  138929 if (!normalizedResource.startsWith(skinFolder)) {
141  1 LOGGER.warn("Direct access to template file [{}] refused. Possible break-in attempt!", normalizedResource);
142   
143  1 return null;
144    }
145   
146  138914 if (testExist) {
147    // Check if the resource exist
148  0 if (this.environment.getResource(resourcePath) == null) {
149  0 return null;
150    }
151    }
152   
153  138923 return resourcePath;
154    }
155   
 
156  11142 toggle @Override
157    public String getOutputSyntaxString()
158    {
159  11145 return getProperties().getString("outputSyntax");
160    }
161    }