SiteJavascriptHandler.java

  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. import java.io.PrintWriter;

  19. import javax.jcr.Node;
  20. import javax.servlet.http.HttpServletResponse;

  21. import org.apache.commons.lang3.StringUtils;

  22. import org.exoplatform.ecm.utils.MessageDigester;
  23. import org.exoplatform.services.cache.CacheService;
  24. import org.exoplatform.services.cache.ExoCache;
  25. import org.exoplatform.services.jcr.ext.common.SessionProvider;
  26. import org.exoplatform.services.security.ConversationState;
  27. import org.exoplatform.services.security.IdentityConstants;
  28. import org.exoplatform.services.wcm.portal.LivePortalManagerService;
  29. import org.exoplatform.services.wcm.utils.WCMCoreUtils;
  30. import org.exoplatform.web.ControllerContext;
  31. import org.exoplatform.web.WebRequestHandler;
  32. import org.exoplatform.web.controller.QualifiedName;

  33. /**
  34.  * Created by The eXo Platform SAS
  35.  * Author : Dang Viet Ha
  36.  *          hadv@exoplatform.com
  37.  * Feb 20, 2012  
  38.  */
  39. public class SiteJavascriptHandler extends WebRequestHandler {
  40.  
  41.   private ExoCache<String, Object> jsCache_;

  42.   private String                   siteName_;

  43.   private LivePortalManagerService livePortalManagerService_;

  44.   public static final String       CACHE_REGION = "ecms.site.javascript.cache";

  45.   @Override
  46.   public String getHandlerName() {
  47.     return "javascript";
  48.   }

  49.   @Override
  50.   public synchronized boolean execute(ControllerContext context) throws Exception {
  51.     if (jsCache_ == null) {
  52.       jsCache_ = WCMCoreUtils.getService(CacheService.class)
  53.                              .getCacheInstance(SiteJavascriptHandler.CACHE_REGION);
  54.     }
  55.     siteName_ = context.getParameter(QualifiedName.create("gtn", "sitename"));

  56.     String key = MessageDigester.getHash(siteName_);
  57.     String jsData = (String) jsCache_.get(key);
  58.     if (jsData == null) {
  59.       SessionProvider sessionProvider = WCMCoreUtils.getSystemSessionProvider();
  60.       livePortalManagerService_ = WCMCoreUtils.getService(LivePortalManagerService.class);
  61.       Node portalNode = null;
  62.       if ("shared".equals(siteName_)) {
  63.         portalNode = livePortalManagerService_.getLiveSharedPortal(sessionProvider);
  64.       } else {
  65.         portalNode = livePortalManagerService_.getLivePortal(sessionProvider, siteName_);  
  66.       }
  67.       jsData = WCMCoreUtils.getSiteGlobalActiveJs(portalNode, sessionProvider);
  68.       jsCache_.put(key, jsData);
  69.     }
  70.     HttpServletResponse res = context.getResponse();
  71.     res.setContentType("text/javascript");
  72.     PrintWriter out = res.getWriter();
  73.     out.println(jsData);
  74.     return true;
  75.   }
  76.  
  77.   @Override
  78.   protected boolean getRequiresLifeCycle() {
  79.     return true;
  80.   }
  81. }