View Javadoc
1   /*
2    * Copyright (C) 2003-2012 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.handler;
18  
19  import java.io.PrintWriter;
20  
21  import javax.jcr.Node;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.apache.commons.lang3.StringUtils;
25  
26  import org.exoplatform.ecm.utils.MessageDigester;
27  import org.exoplatform.services.cache.CacheService;
28  import org.exoplatform.services.cache.ExoCache;
29  import org.exoplatform.services.jcr.ext.common.SessionProvider;
30  import org.exoplatform.services.security.ConversationState;
31  import org.exoplatform.services.security.IdentityConstants;
32  import org.exoplatform.services.wcm.portal.LivePortalManagerService;
33  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
34  import org.exoplatform.web.ControllerContext;
35  import org.exoplatform.web.WebRequestHandler;
36  import org.exoplatform.web.controller.QualifiedName;
37  
38  /**
39   * Created by The eXo Platform SAS
40   * Author : Dang Viet Ha
41   *          hadv@exoplatform.com
42   * Feb 20, 2012  
43   */
44  public class SiteJavascriptHandler extends WebRequestHandler {
45    
46    private ExoCache<String, Object> jsCache_;
47  
48    private String                   siteName_;
49  
50    private LivePortalManagerService livePortalManagerService_;
51  
52    public static final String       CACHE_REGION = "ecms.site.javascript.cache";
53  
54    @Override
55    public String getHandlerName() {
56      return "javascript";
57    }
58  
59    @Override
60    public synchronized boolean execute(ControllerContext context) throws Exception {
61      if (jsCache_ == null) {
62        jsCache_ = WCMCoreUtils.getService(CacheService.class)
63                               .getCacheInstance(SiteJavascriptHandler.CACHE_REGION);
64      }
65      siteName_ = context.getParameter(QualifiedName.create("gtn", "sitename"));
66  
67      String key = MessageDigester.getHash(siteName_);
68      String jsData = (String) jsCache_.get(key);
69      if (jsData == null) {
70        SessionProvider sessionProvider = WCMCoreUtils.getSystemSessionProvider();
71        livePortalManagerService_ = WCMCoreUtils.getService(LivePortalManagerService.class);
72        Node portalNode = null;
73        if ("shared".equals(siteName_)) {
74          portalNode = livePortalManagerService_.getLiveSharedPortal(sessionProvider);
75        } else {
76          portalNode = livePortalManagerService_.getLivePortal(sessionProvider, siteName_);  
77        }
78        jsData = WCMCoreUtils.getSiteGlobalActiveJs(portalNode, sessionProvider);
79        jsCache_.put(key, jsData);
80      }
81      HttpServletResponse res = context.getResponse();
82      res.setContentType("text/javascript");
83      PrintWriter out = res.getWriter();
84      out.println(jsData);
85      return true;
86    }
87    
88    @Override
89    protected boolean getRequiresLifeCycle() {
90      return true;
91    }
92  }