1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.skin;
18
19 import java.io.Reader;
20 import java.io.StringReader;
21 import java.util.Map;
22
23 import javax.jcr.Node;
24 import javax.jcr.PathNotFoundException;
25
26 import org.exoplatform.portal.resource.Resource;
27 import org.exoplatform.portal.resource.ResourceResolver;
28 import org.exoplatform.portal.resource.SkinConfig;
29 import org.exoplatform.portal.resource.SkinService;
30 import org.exoplatform.services.log.ExoLogger;
31 import org.exoplatform.services.log.Log;
32 import org.exoplatform.services.wcm.portal.LivePortalManagerService;
33 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
34
35
36
37
38
39
40
41 public class WCMSkinResourceResolver implements ResourceResolver {
42 private SkinService skinService;
43 private LivePortalManagerService livePortalService;
44
45
46 private static final Log LOG = ExoLogger.getLogger(WCMSkinResourceResolver.class.getName());
47
48 public WCMSkinResourceResolver(SkinService skinService, LivePortalManagerService livePortalService) {
49 this.skinService = skinService;
50 this.livePortalService = livePortalService;
51 }
52
53 public Resource resolve(String path) {
54 if(!path.matches(XSkinService.SKIN_PATH_REGEXP)) return null;
55
56 Map<String,String> params = XSkinService.getSkinParams(path);
57 String skinModule = params.get(XSkinService.MODULE_PARAM);
58 String siteName = params.get(XSkinService.SITENAME_PARAM);
59 String skinName = params.get(XSkinService.SKIN_PARAM);
60
61 if (!skinModule.matches(XSkinService.MODULE_NAME_REGEXP)) return null;
62
63 String cssPath = null;
64 SkinConfig portalSkinConfig = skinService.getSkin(skinModule,skinName);
65 if(portalSkinConfig != null) {
66 cssPath = portalSkinConfig.getCSSPath();
67 }
68
69 if(cssPath == null) {
70 for(SkinConfig skinConfig: skinService.getPortalSkins(skinName)) {
71 if(skinConfig.getModule().equals(skinModule)) {
72 cssPath = skinConfig.getCSSPath();
73 break;
74 }
75 }
76 }
77 try {
78 Node portalNode = livePortalService.getLivePortal(WCMCoreUtils.getSystemSessionProvider(), siteName);
79 String pureCssData = WCMCoreUtils.getSiteGlobalActiveStylesheet(portalNode);
80 final String cssData = pureCssData.replaceAll("@import(.*)[^/]*.css(.*);", "");
81 if(cssData == null)
82 return null;
83 return new Resource(cssPath) {
84 public Reader read() {
85 return new StringReader(cssData);
86 }
87 };
88 } catch(PathNotFoundException e) {
89 return null;
90 } catch(Exception e) {
91 if (LOG.isErrorEnabled()) {
92 LOG.error("Unexpected error happens", e);
93 }
94 }
95 return null;
96 }
97 }