Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
XWikiContextCacheKeyFactory | 39 | 5 | 0% | 1 | 0 |
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.lesscss.internal.cache; | |
21 | ||
22 | import javax.inject.Inject; | |
23 | import javax.inject.Provider; | |
24 | import javax.inject.Singleton; | |
25 | ||
26 | import org.xwiki.component.annotation.Component; | |
27 | ||
28 | import com.xpn.xwiki.XWikiContext; | |
29 | import com.xpn.xwiki.web.XWikiURLFactory; | |
30 | ||
31 | /** | |
32 | * Serialize to a string (a cache key) the current XWikiContext object with a selection of fields that the LESS cache | |
33 | * must handle. | |
34 | * | |
35 | * @version $Id: a40154dba0a3114fff74a843e5a20259e93c655d $ | |
36 | */ | |
37 | @Component(roles = XWikiContextCacheKeyFactory.class) | |
38 | @Singleton | |
39 | public class XWikiContextCacheKeyFactory | |
40 | { | |
41 | @Inject | |
42 | private Provider<XWikiContext> xcontextProvider; | |
43 | ||
44 | /** | |
45 | * @return the cache key corresponding to the current XWikiContext state | |
46 | */ | |
47 | 204 | ![]() |
48 | { | |
49 | 204 | XWikiContext xcontext = xcontextProvider.get(); |
50 | 204 | XWikiURLFactory urlFactory = xcontext.getURLFactory(); |
51 | ||
52 | // We serialize the class name of the current URLFactory. | |
53 | // Ex: - during HTML export, ExportURLFactory is used. | |
54 | // - for the standard 'view' action, XWikiDefaultURLFactory is used | |
55 | // - ... | |
56 | 204 | String urlFactoryName = urlFactory.getClass().getName(); |
57 | ||
58 | // We generate a fake URL with the current URL factory so that we take care of the internal state of that object | |
59 | // in our cache key. | |
60 | // Ex: - if the request comes from a file located in subdirectory, the generated URL will be: | |
61 | // '../style.css' | |
62 | // - if the request comes form a file located in a deeper subdirectory, the generated URL will be: | |
63 | // '../../style.css' | |
64 | // It is clear that we cannot cache the same results from a request coming from a subdirectory or an other, but | |
65 | // we have no API to get the internal state of the URL Factory. So we use this 'trick' to handle it. | |
66 | // Note: only the "path" part of the URL is needed. Otherwise, the cache cannot be share between requests | |
67 | // having 2 different hosts: e.g. http://localhost and http://external-url/ | |
68 | 204 | String urlFactoryGeneratedURL = urlFactory.createSkinURL("style.css", "skin", xcontext).getPath(); |
69 | ||
70 | 204 | return String.format("XWikiContext[URLFactory[%s, %s]]", urlFactoryName, urlFactoryGeneratedURL); |
71 | } | |
72 | } |