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

File InternalSkinManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

54
87
14
1
330
230
43
0.49
6.21
14
3.07

Classes

Class Line # Actions
InternalSkinManager 59 87 0% 43 47
0.696774269.7%
 

Contributing tests

This file is covered by 39 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.rendering.syntax.SyntaxFactory;
45    import org.xwiki.security.authorization.ContextualAuthorizationManager;
46    import org.xwiki.security.authorization.Right;
47    import org.xwiki.skin.ResourceRepository;
48    import org.xwiki.skin.Skin;
49   
50    import com.xpn.xwiki.XWikiContext;
51    import com.xpn.xwiki.doc.XWikiDocument;
52   
53    /**
54    * @version $Id: ecd1523d68b33873c96f37636c2e59c391d9495f $
55    * @since 7.0M1
56    */
57    @Component(roles = InternalSkinManager.class)
58    @Singleton
 
59    public class InternalSkinManager implements Initializable
60    {
61    public static final String CKEY_SKIN = "skin";
62   
63    public static final String CKEY_PARENTSKIN = "baseskin";
64   
65    @Inject
66    private InternalSkinConfiguration skinConfiguration;
67   
68    @Inject
69    private Provider<XWikiContext> xcontextProvider;
70   
71    @Inject
72    private WikiSkinUtils wikiSkinUtils;
73   
74    @Inject
75    private Environment environment;
76   
77    @Inject
78    @Named("all")
79    private ConfigurationSource allConfiguration;
80   
81    @Inject
82    private ObservationManager observation;
83   
84    @Inject
85    private CacheManager cacheManager;
86   
87    @Inject
88    private ContextualAuthorizationManager authorization;
89   
90    @Inject
91    private Logger logger;
92   
93    @Inject
94    private SyntaxFactory syntaxFactory;
95   
96    private Cache<Skin> cache;
97   
 
98  99 toggle @Override
99    public void initialize() throws InitializationException
100    {
101    // Initialize cache
102  99 try {
103  99 this.cache = this.cacheManager.createNewCache(new LRUCacheConfiguration("skins", 100, 86400));
104    } catch (CacheException e) {
105  0 throw new InitializationException("Failed to initialize cache", e);
106    }
107   
108    // Initialize listener
109  99 this.observation.addListener(new AbstractEventListener("skins", new DocumentUpdatedEvent(),
110    new DocumentDeletedEvent(), new DocumentCreatedEvent())
111    {
 
112  3966 toggle @Override
113    public void onEvent(Event event, Object source, Object data)
114    {
115  3966 XWikiDocument document = (XWikiDocument) source;
116   
117  3966 if (document.getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null
118    || document.getOriginalDocument().getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null) {
119    // TODO: lower the granularity
120  0 InternalSkinManager.this.cache.removeAll();
121    }
122   
123    }
124    });
125    }
126   
 
127  303945 toggle public Skin getSkin(String id)
128    {
129  303949 if (StringUtils.isBlank(id)) {
130  295 return null;
131    }
132   
133  303648 Skin skin = this.cache.get(id);
134   
135  303646 if (skin == null) {
136  303650 skin = createSkin(id);
137    }
138   
139  303660 return skin;
140    }
141   
 
142  303635 toggle private Skin createSkin(String id)
143    {
144  303642 Skin skin;
145   
146  303673 if (this.wikiSkinUtils.isWikiSkin(id)) {
147  166 skin = new WikiSkin(id, this, this.skinConfiguration, this.wikiSkinUtils, this.logger, this.syntaxFactory);
148    } else {
149  303506 skin = new EnvironmentSkin(id, this, this.skinConfiguration, this.logger, this.syntaxFactory,
150    this.environment, this.xcontextProvider);
151    }
152   
153  303653 return skin;
154    }
155   
 
156  303568 toggle public Skin getCurrentSkin(boolean testRights)
157    {
158  303571 return getSkin(getCurrentSkinId(testRights));
159    }
160   
 
161  314687 toggle public String getCurrentSkinId(boolean testRights)
162    {
163  314705 String skin;
164   
165  314708 XWikiContext xcontext = this.xcontextProvider.get();
166   
167  314751 if (xcontext != null) {
168    // Try to get it from context
169  314746 skin = (String) xcontext.get(CKEY_SKIN);
170  314741 if (StringUtils.isNotEmpty(skin)) {
171  302998 return skin;
172    } else {
173  11739 skin = null;
174    }
175   
176    // Try to get it from URL
177  11741 if (xcontext.getRequest() != null) {
178  11720 skin = xcontext.getRequest().getParameter("skin");
179  11720 if (StringUtils.isNotEmpty(skin)) {
180  1226 return skin;
181    } else {
182  10484 skin = null;
183    }
184    }
185   
186    // Try to get it from preferences (user -> space -> wiki -> xwiki.properties)
187  10506 skin = this.allConfiguration.getProperty("skin");
188  10516 if (skin != null) {
189  0 return skin;
190    }
191    }
192   
193    // Try to get it from xwiki.cfg
194  10523 skin = getDefaultSkinId();
195   
196  10523 if (xcontext != null) {
197    // Check if current user have enough right to access the wiki skin (if it's a wiki skin)
198    // TODO: shouldn't we make sure anyone see the skin whatever right he have ?
199  10507 if (testRights) {
200  2619 XWikiDocument document = this.wikiSkinUtils.getSkinDocument(skin);
201  2618 if (document != null) {
202  9 if (!this.authorization.hasAccess(Right.VIEW, document.getDocumentReference())) {
203  0 this.logger.debug(
204    "Cannot access configured wiki skin [{}] due to access rights, using the default skin.",
205    skin);
206  0 skin = getDefaultSkinId();
207    }
208    }
209    }
210   
211    // Set found skin in the context
212  10509 xcontext.put(CKEY_SKIN, skin);
213    }
214   
215  10514 return skin;
216    }
217   
 
218  0 toggle public String getParentSkin(String skinId)
219    {
220  0 Skin skin = getSkin(skinId);
221  0 if (skin != null) {
222  0 ResourceRepository parent = skin.getParent();
223  0 if (parent != null) {
224  0 return parent.getId();
225    }
226    }
227   
228  0 return null;
229    }
230   
 
231  0 toggle public Skin getCurrentParentSkin(boolean testRights)
232    {
233  0 return getSkin(getCurrentParentSkinId(testRights));
234    }
235   
 
236  1113 toggle public String getCurrentParentSkinId(boolean testRights)
237    {
238    // From the context
239  1113 String baseSkin = getContextParentId();
240   
241    // From the skin
242  1091 if (baseSkin == null) {
243  1091 Skin skin = getCurrentSkin(testRights);
244  1114 if (skin != null) {
245  1113 ResourceRepository parent = skin.getParent();
246  1111 if (parent != null) {
247  1107 baseSkin = parent.getId();
248    }
249    }
250    }
251   
252    // From the configuration
253  1099 if (baseSkin == null) {
254  1092 baseSkin = getDefaultParentSkinId();
255    }
256   
257  1114 XWikiContext xcontext = this.xcontextProvider.get();
258   
259  1113 if (xcontext != null) {
260    // Check if current user have enough right to access the wiki skin (if it's a wiki skin)
261    // TODO: shouldn't we make sure anyone see the skin whatever right he have ?
262  1112 if (testRights) {
263  0 XWikiDocument document = this.wikiSkinUtils.getSkinDocument(baseSkin);
264  0 if (document != null) {
265  0 if (!this.authorization.hasAccess(Right.VIEW, document.getDocumentReference())) {
266  0 this.logger.debug(
267    "Cannot access configured wiki skin [{}] due to access rights, using the default skin.",
268    baseSkin);
269  0 baseSkin = getDefaultParentSkinId();
270    }
271    }
272    }
273   
274    // Set found skin in the context
275  1113 xcontext.put(CKEY_PARENTSKIN, baseSkin);
276    }
277   
278  1112 return baseSkin;
279    }
280   
 
281  1109 toggle public String getContextParentId()
282    {
283  1111 String parentId = null;
284   
285  1096 XWikiContext xcontext = this.xcontextProvider.get();
286   
287  1106 if (xcontext != null) {
288  1099 parentId = (String) xcontext.get(CKEY_PARENTSKIN);
289  1103 if (StringUtils.isEmpty(parentId)) {
290  1098 parentId = null;
291    }
292    }
293   
294  1102 return parentId;
295    }
296   
 
297  0 toggle public Skin getDefaultSkin()
298    {
299  0 return getSkin(getDefaultSkinId());
300    }
301   
 
302  10522 toggle public String getDefaultSkinId()
303    {
304  10522 String skin = this.skinConfiguration.getDefaultSkinId();
305   
306    // Fallback on default base skin
307  10522 if (skin == null) {
308  0 skin = getDefaultParentSkinId();
309    }
310   
311  10518 return skin;
312    }
313   
 
314  0 toggle public Skin getDefaultParentSkin()
315    {
316  0 return getSkin(this.skinConfiguration.getDefaultParentSkinId());
317    }
318   
 
319  2206 toggle public String getDefaultParentSkinId()
320    {
321  2214 String skin = this.skinConfiguration.getDefaultParentSkinId();
322   
323    // Fallback on default skin
324  2228 if (skin == null) {
325  0 skin = this.skinConfiguration.getDefaultSkinId();
326    }
327   
328  2229 return skin;
329    }
330    }