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

File InternalSkinManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

54
87
14
1
326
227
43
0.49
6.21
14
3.07

Classes

Class Line # Actions
InternalSkinManager 58 87 0% 43 40
0.741935574.2%
 

Contributing tests

This file is covered by 42 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 javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Provider;
25    import javax.inject.Singleton;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.slf4j.Logger;
29    import org.xwiki.bridge.event.DocumentCreatedEvent;
30    import org.xwiki.bridge.event.DocumentDeletedEvent;
31    import org.xwiki.bridge.event.DocumentUpdatedEvent;
32    import org.xwiki.cache.Cache;
33    import org.xwiki.cache.CacheException;
34    import org.xwiki.cache.CacheManager;
35    import org.xwiki.cache.config.LRUCacheConfiguration;
36    import org.xwiki.component.annotation.Component;
37    import org.xwiki.component.phase.Initializable;
38    import org.xwiki.component.phase.InitializationException;
39    import org.xwiki.configuration.ConfigurationSource;
40    import org.xwiki.environment.Environment;
41    import org.xwiki.observation.AbstractEventListener;
42    import org.xwiki.observation.ObservationManager;
43    import org.xwiki.observation.event.Event;
44    import org.xwiki.security.authorization.ContextualAuthorizationManager;
45    import org.xwiki.security.authorization.Right;
46    import org.xwiki.skin.ResourceRepository;
47    import org.xwiki.skin.Skin;
48   
49    import com.xpn.xwiki.XWikiContext;
50    import com.xpn.xwiki.doc.XWikiDocument;
51   
52    /**
53    * @version $Id: 51ef9f448a24134528ec4a6d525b44924f94909f $
54    * @since 7.0M1
55    */
56    @Component(roles = InternalSkinManager.class)
57    @Singleton
 
58    public class InternalSkinManager implements Initializable
59    {
60    public static final String CKEY_SKIN = "skin";
61   
62    public static final String CKEY_PARENTSKIN = "baseskin";
63   
64    @Inject
65    private InternalSkinConfiguration skinConfiguration;
66   
67    @Inject
68    private Provider<XWikiContext> xcontextProvider;
69   
70    @Inject
71    private WikiSkinUtils wikiSkinUtils;
72   
73    @Inject
74    private Environment environment;
75   
76    @Inject
77    @Named("all")
78    private ConfigurationSource allConfiguration;
79   
80    @Inject
81    private ObservationManager observation;
82   
83    @Inject
84    private CacheManager cacheManager;
85   
86    @Inject
87    private ContextualAuthorizationManager authorization;
88   
89    @Inject
90    private Logger logger;
91   
92    private Cache<Skin> cache;
93   
 
94  118 toggle @Override
95    public void initialize() throws InitializationException
96    {
97    // Initialize cache
98  118 try {
99  118 this.cache = this.cacheManager.createNewCache(new LRUCacheConfiguration("skins", 100, 86400));
100    } catch (CacheException e) {
101  0 throw new InitializationException("Failed to initialize cache", e);
102    }
103   
104    // Initialize listener
105  118 this.observation.addListener(new AbstractEventListener("skins", new DocumentUpdatedEvent(),
106    new DocumentDeletedEvent(), new DocumentCreatedEvent())
107    {
 
108  8297 toggle @Override
109    public void onEvent(Event event, Object source, Object data)
110    {
111  8297 XWikiDocument document = (XWikiDocument) source;
112   
113  8297 if (document.getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null
114    || document.getOriginalDocument().getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null) {
115    // TODO: lower the granularity
116  4 InternalSkinManager.this.cache.removeAll();
117    }
118   
119    }
120    });
121    }
122   
 
123  5170790 toggle public Skin getSkin(String id)
124    {
125  5172087 if (StringUtils.isBlank(id)) {
126  301 return null;
127    }
128   
129  5172192 Skin skin = this.cache.get(id);
130   
131  5172522 if (skin == null) {
132  5173327 skin = createSkin(id);
133    }
134   
135  5172123 return skin;
136    }
137   
 
138  5173365 toggle private Skin createSkin(String id)
139    {
140  5173246 Skin skin;
141   
142  5170121 if (this.wikiSkinUtils.isWikiSkin(id)) {
143  3048996 skin = new WikiSkin(id, this, this.skinConfiguration, this.wikiSkinUtils, this.logger);
144    } else {
145  2123005 skin = new EnvironmentSkin(id, this, this.skinConfiguration, this.logger, this.environment,
146    this.xcontextProvider);
147    }
148   
149  5172214 return skin;
150    }
151   
 
152  4095764 toggle public Skin getCurrentSkin(boolean testRights)
153    {
154  4097097 return getSkin(getCurrentSkinId(testRights));
155    }
156   
 
157  4188291 toggle public String getCurrentSkinId(boolean testRights)
158    {
159  4188979 String skin;
160   
161  4189507 XWikiContext xcontext = this.xcontextProvider.get();
162   
163  4189556 if (xcontext != null) {
164    // Try to get it from context
165  4189256 skin = (String) xcontext.get(CKEY_SKIN);
166  4188569 if (StringUtils.isNotEmpty(skin)) {
167  369139 return skin;
168    } else {
169  3819923 skin = null;
170    }
171   
172    // Try to get it from URL
173  3819393 if (xcontext.getRequest() != null) {
174  3819736 skin = xcontext.getRequest().getParameter("skin");
175  3818535 if (StringUtils.isNotEmpty(skin)) {
176  693707 return skin;
177    } else {
178  3124509 skin = null;
179    }
180    }
181   
182    // Try to get it from preferences (user -> space -> wiki -> xwiki.properties)
183  3126374 skin = this.allConfiguration.getProperty("skin");
184  3126757 if (skin != null) {
185  3116969 return skin;
186    }
187    }
188   
189    // Try to get it from xwiki.cfg
190  9798 skin = getDefaultSkinId();
191   
192  9793 if (xcontext != null) {
193    // Check if current user have enough right to access the wiki skin (if it's a wiki skin)
194    // TODO: shouldn't we make sure anyone see the skin whatever right he have ?
195  9788 if (testRights) {
196  2921 XWikiDocument document = this.wikiSkinUtils.getSkinDocument(skin);
197  2923 if (document != null) {
198  9 if (!this.authorization.hasAccess(Right.VIEW, document.getDocumentReference())) {
199  0 this.logger.debug(
200    "Cannot access configured wiki skin [{}] due to access rights, using the default skin.",
201    skin);
202  0 skin = getDefaultSkinId();
203    }
204    }
205    }
206   
207    // Set found skin in the context
208  9788 xcontext.put(CKEY_SKIN, skin);
209    }
210   
211  9793 return skin;
212    }
213   
 
214  0 toggle public String getParentSkin(String skinId)
215    {
216  0 Skin skin = getSkin(skinId);
217  0 if (skin != null) {
218  0 ResourceRepository parent = skin.getParent();
219  0 if (parent != null) {
220  0 return parent.getId();
221    }
222    }
223   
224  0 return null;
225    }
226   
 
227  0 toggle public Skin getCurrentParentSkin(boolean testRights)
228    {
229  0 return getSkin(getCurrentParentSkinId(testRights));
230    }
231   
 
232  1358 toggle public String getCurrentParentSkinId(boolean testRights)
233    {
234    // From the context
235  1367 String baseSkin = getContextParentId();
236   
237    // From the skin
238  1364 if (baseSkin == null) {
239  1320 Skin skin = getCurrentSkin(testRights);
240  1325 if (skin != null) {
241  1323 ResourceRepository parent = skin.getParent();
242  1319 if (parent != null) {
243  1320 baseSkin = parent.getId();
244    }
245    }
246    }
247   
248    // From the configuration
249  1366 if (baseSkin == null) {
250  1042 baseSkin = getDefaultParentSkinId();
251    }
252   
253  1364 XWikiContext xcontext = this.xcontextProvider.get();
254   
255  1369 if (xcontext != null) {
256    // Check if current user have enough right to access the wiki skin (if it's a wiki skin)
257    // TODO: shouldn't we make sure anyone see the skin whatever right he have ?
258  1368 if (testRights) {
259  0 XWikiDocument document = this.wikiSkinUtils.getSkinDocument(baseSkin);
260  0 if (document != null) {
261  0 if (!this.authorization.hasAccess(Right.VIEW, document.getDocumentReference())) {
262  0 this.logger.debug(
263    "Cannot access configured wiki skin [{}] due to access rights, using the default skin.",
264    baseSkin);
265  0 baseSkin = getDefaultParentSkinId();
266    }
267    }
268    }
269   
270    // Set found skin in the context
271  1360 xcontext.put(CKEY_PARENTSKIN, baseSkin);
272    }
273   
274  1371 return baseSkin;
275    }
276   
 
277  1358 toggle public String getContextParentId()
278    {
279  1363 String parentId = null;
280   
281  1364 XWikiContext xcontext = this.xcontextProvider.get();
282   
283  1360 if (xcontext != null) {
284  1365 parentId = (String) xcontext.get(CKEY_PARENTSKIN);
285  1364 if (StringUtils.isEmpty(parentId)) {
286  1317 parentId = null;
287    }
288    }
289   
290  1365 return parentId;
291    }
292   
 
293  0 toggle public Skin getDefaultSkin()
294    {
295  0 return getSkin(getDefaultSkinId());
296    }
297   
 
298  9794 toggle public String getDefaultSkinId()
299    {
300  9801 String skin = this.skinConfiguration.getDefaultSkinId();
301   
302    // Fallback on default base skin
303  9799 if (skin == null) {
304  0 skin = getDefaultParentSkinId();
305    }
306   
307  9794 return skin;
308    }
309   
 
310  0 toggle public Skin getDefaultParentSkin()
311    {
312  0 return getSkin(this.skinConfiguration.getDefaultParentSkinId());
313    }
314   
 
315  2404 toggle public String getDefaultParentSkinId()
316    {
317  2415 String skin = this.skinConfiguration.getDefaultParentSkinId();
318   
319    // Fallback on default skin
320  2418 if (skin == null) {
321  0 skin = this.skinConfiguration.getDefaultSkinId();
322    }
323   
324  2416 return skin;
325    }
326    }