protected HttpSession getSession() {
   RequestContext reqCtx = RequestContext.lookup();
   if (reqCtx != null) {
     CommandRequest request = reqCtx.getRequest();
     if (request != null) {
       Panel currentPanel = RequestContext.lookup().getActivePanel();
       if (currentPanel != null) {
         return currentPanel.getPanelSession();
       } else {
         if (log.isDebugEnabled())
           log.debug("Using a PanelScoped bean outside a panel. Will default to SessionScoped.");
         return request.getSessionObject();
       }
     }
   }
   return null;
 }
 /**
  * Mapping to the controller servlet. By default it is Controller, but in case there is friendly
  * url, can be replaced by workspace/<friendly_url> It is NOT preceded by "/"
  *
  * @return Mapping to the controller servlet.
  */
 public String getBaseURI() {
   // Avoid an extra Controller in URL when it is already friendly
   Panel panel = RequestContext.lookup().getActivePanel();
   if (panel != null) { // There will be a friendly url here
     return getLinkToPage(panel.getSection(), true);
   }
   return getServletMapping();
 }
 protected String _getServletMapping() {
   HttpServletRequest request = RequestContext.lookup().getRequest().getRequestObject();
   if (request != null) {
     return request.getContextPath() + "/" + COMMAND_RUNNER;
   } else {
     // Do the best we can, this is a relative URL that might not work if AJAX handler does not
     // convert it to
     // absolute or a <base> tag is specified in the generated HTML
     return COMMAND_RUNNER;
   }
 }
 /**
  * Apply a final post-processing on generated URLs in order to add some extra information such as
  * CSRF tokens or propagate some behavioural parameters via URL-rewriting.
  */
 protected StringBuffer postProcessURL(StringBuffer url) {
   // Keep the embedded mode using URL rewriting.
   RequestContext reqCtx = RequestContext.lookup();
   if (reqCtx != null) {
     HttpServletRequest request = reqCtx.getRequest().getRequestObject();
     boolean embeddedMode = Boolean.parseBoolean(request.getParameter(Parameters.PARAM_EMBEDDED));
     String embeddedParam = Parameters.PARAM_EMBEDDED + "=true";
     if (embeddedMode && url.indexOf(embeddedParam) == -1) {
       url.append(url.indexOf("?") != -1 ? PARAM_SEPARATOR : "?");
       url.append(embeddedParam);
     }
   }
   // Add the CSRF protection token
   if (CSRFTokenProcessor.lookup().isEnabled()) {
     CSRFTokenGenerator csrfTokenGenerator = CSRFTokenGenerator.lookup();
     String token = csrfTokenGenerator.getLastToken();
     url.append(url.indexOf("?") != -1 ? PARAM_SEPARATOR : "?");
     url.append(csrfTokenGenerator.getTokenName()).append("=").append(token);
   }
   return url;
 }
  /**
   * Generate a link to the given JSP
   *
   * @param jsp The JSP file path relative to the webapp root.
   * @param paramsMap Extra params to be added to the URL.
   */
  public String getLinkToJsp(String jsp, Map paramsMap) {
    StringBuffer sb = new StringBuffer();
    sb.append(RequestContext.lookup().getRequest().getRequestObject().getContextPath());
    sb.append("/").append(JSP_PREFIX);
    if (!jsp.startsWith("/")) sb.append("/");
    sb.append(jsp);

    if (paramsMap != null && !paramsMap.isEmpty()) {
      String paramsMarkup = getParamsMarkup(paramsMap);
      sb.append("?").append(paramsMarkup);
    }
    return postProcessURL(sb).toString();
  }
 protected StringBuffer getRelativeLinkToWorkspace(
     WorkspaceImpl workspace, boolean allowFriendly, String lang) {
   StringBuffer sb = new StringBuffer();
   String friendlyUrl = workspace.getId();
   if (allowFriendly) {
     friendlyUrl = StringUtils.defaultIfEmpty(workspace.getFriendlyUrl(), workspace.getId());
   }
   sb.append(
           RequestContext.lookup().getRequest().getRequestObject().getContextPath()
               + "/"
               + FRIENDLY_PREFIX
               + "/")
       .append(lang)
       .append("/")
       .append(friendlyUrl);
   return sb;
 }
  /**
   * Get a permanent link to a given action on a bean
   *
   * @param bean Bean handler that will perform the action
   * @param action Bean's method that will be invoked
   * @param params Extra parameters for link
   * @return A link url to a bean action, independent on the page.
   */
  public String getPermanentLink(String bean, String action, Map params) {
    try {
      BeanHandler element = (BeanHandler) CDIBeanLocator.getBeanByNameOrType(bean);
      if (element == null) throw new RuntimeException("Bean '" + bean + "' not found.");

      StringBuffer sb = new StringBuffer();
      String base =
          RequestContext.lookup().getRequest().getRequestObject().getContextPath()
              + "/"
              + COMMAND_RUNNER;
      sb.append(base).append("?");
      params.put(FactoryURL.PARAMETER_BEAN, element.getBeanName());
      params.put(FactoryURL.PARAMETER_ACTION, action);
      sb.append(getParamsMarkup(params));
      element.setEnabledForActionHandling(true);
      return postProcessURL(sb).toString();
    } catch (ClassCastException cce) {
      log.error("Bean " + bean + " is not a BeanHandler.");
      return "#";
    }
  }
  /**
   * Generate a link to a factory component action
   *
   * @param beanName Factory component that will perform the action
   * @param action Bean's property (method) that will be invoked
   * @param params Extra parameters for link
   * @return A link url to a bean action
   */
  public String getMarkup(String beanName, String action, Map params) {
    try {
      if (params == null) params = new HashMap();
      Panel panel = RequestContext.lookup().getActivePanel();
      if (panel != null) {
        params.put(Parameters.DISPATCH_IDPANEL, panel.getPanelId());
        params.put(Parameters.DISPATCH_ACTION, "_factory");
      }

      StringBuffer sb = new StringBuffer();
      sb.append(_getServletMapping()).append("?");
      BeanHandler bean = (BeanHandler) CDIBeanLocator.getBeanByNameOrType(beanName);
      params.put(FactoryURL.PARAMETER_BEAN, bean.getBeanName());
      params.put(FactoryURL.PARAMETER_ACTION, bean.getActionName(action));
      sb.append(getParamsMarkup(params));
      bean.setEnabledForActionHandling(true);
      return postProcessURL(sb).toString();
    } catch (ClassCastException cce) {
      log.error("Bean " + beanName + " is not a BeanHandler.");
      return "#";
    }
  }
 protected Panel getPanel() {
   RequestContext reqCtx = RequestContext.getCurrentContext();
   CommandRequest request = reqCtx.getRequest();
   return (Panel) request.getRequestObject().getAttribute(Parameters.RENDER_PANEL);
 }