View Javadoc
1   /*
2    * Copyright (C) 2003-2011 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (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 Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.javascript;
18  
19  import java.io.Reader;
20  import java.io.StringReader;
21  
22  import javax.jcr.Node;
23  
24  import org.exoplatform.portal.resource.Resource;
25  import org.exoplatform.portal.resource.ResourceResolver;
26  import org.exoplatform.services.log.ExoLogger;
27  import org.exoplatform.services.log.Log;
28  import org.exoplatform.services.wcm.portal.LivePortalManagerService;
29  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30  import org.exoplatform.web.application.javascript.JavascriptConfigService;
31  
32  /**
33   * Created by The eXo Platform SAS
34   * Author : Nguyen Anh Vu
35   *          anhvurz90@gmail.com
36   *          vuna@exoplatform.com
37   * Nov 15, 2011
38   */
39  public class WCMJavascriptResourceResolver  implements ResourceResolver {
40  
41    private static final Log LOG = ExoLogger.getLogger(WCMJavascriptResourceResolver.class.getName());
42  
43    private LivePortalManagerService livePortalManagerService_;
44    private JavascriptConfigService javascriptConfigService_;
45  
46    public WCMJavascriptResourceResolver(LivePortalManagerService livePortalManagerService,
47                                         JavascriptConfigService javascriptConfigService) {
48      this.livePortalManagerService_ = livePortalManagerService;
49      this.javascriptConfigService_ = javascriptConfigService;
50    }
51  
52    @Override
53    public Resource resolve(String path) throws NullPointerException {
54      if(!path.matches(XJavascriptService.JS_PATH_REGEXP)) return null;
55      String[] elements = path.split("/");
56      String portalName = elements[4];
57      try {
58        Node portalNode = livePortalManagerService_.getLivePortal(WCMCoreUtils.getSystemSessionProvider(), portalName);
59        final String jsData = WCMCoreUtils.getSiteGlobalActiveJs(portalNode);
60        if(jsData == null)
61          return null;
62        return new Resource(path) {
63          public Reader read() {
64            return new StringReader(jsData);
65          }
66        };
67      } catch(Exception e) {
68        if (LOG.isErrorEnabled()) {
69          LOG.error("Unexpected error happens", e);
70        }
71      }
72      return null;
73    }
74  
75  }