View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
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   * Created by The eXo Platform SAS
37   * Author : Hoa Pham
38   *          hoa.phamvu@exoplatform.com
39   * Nov 25, 2008
40   */
41  public class WCMSkinResourceResolver implements ResourceResolver {
42    private SkinService skinService;
43    private LivePortalManagerService livePortalService;
44  
45    /** The log. */
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      //get css for shared portal if the portalName is shared Portal
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  }