public void doPresence(
      HttpServletRequest req,
      HttpServletResponse res,
      Session session,
      String siteId,
      String toolContextPath,
      String toolPathInfo)
      throws ToolException, IOException {
    // permission check - visit the site
    Site site = null;
    try {
      Set<SecurityAdvisor> advisors =
          (Set<SecurityAdvisor>) session.getAttribute("sitevisit.security.advisor");
      if (advisors != null) {
        for (SecurityAdvisor advisor : advisors) {
          SecurityService.pushAdvisor(advisor);
          // session.removeAttribute("sitevisit.security.advisor");
        }
      }
      site = SiteService.getSiteVisit(siteId);
    } catch (IdUnusedException e) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    } catch (PermissionException e) {
      // if not logged in, give them a chance
      if (session.getUserId() == null) {
        portal.doLogin(req, res, session, req.getPathInfo(), false);
      } else {
        portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      }
      return;
    }

    // get the skin for the site
    String skin = site.getSkin();

    // find the tool registered for this
    ActiveTool tool = ActiveToolManager.getActiveTool("sakai.presence");
    if (tool == null) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    }

    // form a placement based on the site and the fact that this is that
    // site's presence...
    // Note: the placement is transient, but will always have the same id
    // and context based on the siteId
    Placement placement =
        new org.sakaiproject.util.Placement(
            siteId + "-presence", tool.getId(), tool, null, siteId, null);

    portal.forwardTool(tool, req, res, placement, skin, toolContextPath, toolPathInfo);
  }
  public void doTool(
      HttpServletRequest req,
      HttpServletResponse res,
      Session session,
      String placementId,
      String toolContextPath,
      String toolPathInfo)
      throws ToolException, IOException {

    if (portal.redirectIfLoggedOut(res)) return;

    // find the tool from some site
    ToolConfiguration siteTool = SiteService.findTool(placementId);
    if (siteTool == null) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    }

    // Reset the tool state if requested
    if (portalService.isResetRequested(req)) {
      Session s = SessionManager.getCurrentSession();
      ToolSession ts = s.getToolSession(placementId);
      ts.clearAttributes();
      portalService.setResetState(null);
      M_log.debug("Tool state reset");
    }

    // find the tool registered for this

    ActiveTool tool = ActiveToolManager.getActiveTool(siteTool.getToolId());
    if (tool == null) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    }

    // permission check - visit the site (unless the tool is configured to
    // bypass)
    Site site = null;
    if (tool.getAccessSecurity() == Tool.AccessSecurity.PORTAL) {
      try {
        site = SiteService.getSiteVisit(siteTool.getSiteId());
      } catch (IdUnusedException e) {
        portal.doError(req, res, session, Portal.ERROR_WORKSITE);
        return;
      } catch (PermissionException e) {
        // if not logged in, give them a chance
        if (session.getUserId() == null) {
          portal.doLogin(req, res, session, URLUtils.getSafePathInfo(req), false);
        } else {
          portal.doError(req, res, session, Portal.ERROR_WORKSITE);
        }
        return;
      }
    }

    // Check to see if the tool is visible
    if (!ToolManager.isVisible(site, siteTool)) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    }

    if (portal.isPortletPlacement(siteTool)) {

      String siteType = portal.calcSiteType(siteTool.getSiteId());

      // form a context sensitive title
      String title =
          ServerConfigurationService.getString("ui.service", "Sakai")
              + " : "
              + portal.getSiteHelper().getUserSpecificSiteTitle(site, false)
              + " : "
              + siteTool.getTitle();

      PortalRenderContext rcontext =
          portal.startPageContext(siteType, title, siteTool.getSkin(), req);

      Map m = portal.includeTool(res, req, siteTool);
      rcontext.put("tool", m);

      portal.sendResponse(rcontext, res, "tool", null);

    } else {
      M_log.debug("forwardtool in ToolHandler");
      portal.forwardTool(
          tool, req, res, siteTool, siteTool.getSkin(), toolContextPath, toolPathInfo);
    }
  }
  /**
   * Process a tool request
   *
   * @param req
   * @param res
   * @param session
   * @param placementId
   * @param toolContextPath
   * @param toolPathInfo
   * @return true if the processing was successful, false if nt
   * @throws ToolException
   * @throws IOException
   */
  protected boolean doTool(
      HttpServletRequest req,
      HttpServletResponse res,
      Session session,
      String placementId,
      String toolContextPath,
      String toolPathInfo)
      throws ToolException, IOException {
    // find the tool from some site
    // TODO: all placements are from sites? -ggolden
    ToolConfiguration siteTool = SiteService.findTool(placementId);
    if (siteTool == null) return false;

    // find the tool registered for this
    ActiveTool tool = ActiveToolManager.getActiveTool(siteTool.getToolId());
    if (tool == null) return false;

    // permission check - visit the site (unless the tool is configured to
    // bypass)
    if (tool.getAccessSecurity() == Tool.AccessSecurity.PORTAL) {
      Site site = null;
      try {
        site = SiteService.getSiteVisit(siteTool.getSiteId());
      } catch (IdUnusedException e) {
        return false;
      } catch (PermissionException e) {
        // TODO: login here?
        return false;
      }
    }

    // if the path is not set, and we are expecting one, we need to compute
    // the path and redirect
    // we expect a path only if the tool has a registered home -ggolden
    if ((toolPathInfo == null) && (tool.getHome() != null)) {
      // what path? The one last visited, or home
      ToolSession toolSession = SessionManager.getCurrentSession().getToolSession(placementId);
      String redirectPath =
          (String) toolSession.getAttribute(ActiveTool.TOOL_ATTR_CURRENT_DESTINATION);
      if (redirectPath == null) {
        redirectPath = tool.getHome();
      }

      // redirect with this tool path
      String redirectUrl =
          ServerConfigurationService.getServerUrl() + toolContextPath + redirectPath;
      res.sendRedirect(res.encodeRedirectURL(redirectUrl));
      return true;
    }

    // store the path as the current path, if we are doing this
    if (tool.getHome() != null) {
      ToolSession toolSession = SessionManager.getCurrentSession().getToolSession(placementId);
      toolSession.setAttribute(ActiveTool.TOOL_ATTR_CURRENT_DESTINATION, toolPathInfo);
    }

    // prepare for the forward
    setupForward(req, res, siteTool, siteTool.getSkin());
    req.setAttribute(ToolURL.MANAGER, new ToolURLManagerImpl(res));

    // let the tool do the the work (forward)
    tool.forward(req, res, siteTool, toolContextPath, toolPathInfo);

    return true;
  }
  protected boolean sendToHelper(HttpServletRequest req, HttpServletResponse res, String target) {
    String path = req.getPathInfo();
    if (path == null) path = "/";

    // 0 parts means the path was just "/", otherwise parts[0] = "", parts[1] = item id, parts[2] if
    // present is "edit"...
    String[] parts = path.split("/");

    if (parts.length < 2) {
      return false;
    }

    if (!parts[1].endsWith(HELPER_EXT)) {
      return false;
    }

    ToolSession toolSession = SessionManager.getCurrentToolSession();

    Enumeration params = req.getParameterNames();
    while (params.hasMoreElements()) {
      String paramName = (String) params.nextElement();
      if (paramName.startsWith(HELPER_SESSION_PREFIX)) {
        String attributeName = paramName.substring(HELPER_SESSION_PREFIX.length());
        toolSession.setAttribute(attributeName, req.getParameter(paramName));
      }
    }

    // calc helper id
    int posEnd = parts[1].lastIndexOf(".");

    String helperId = target.substring(1, posEnd + 1);
    ActiveTool helperTool = ActiveToolManager.getActiveTool(helperId);

    // get the current location (if one doesn't exist) and save it for when we return from the
    // helper
    if (toolSession.getAttribute(helperTool.getId() + Tool.HELPER_DONE_URL) == null) {
      toolSession.setAttribute(
          helperTool.getId() + Tool.HELPER_DONE_URL,
          req.getContextPath() + req.getServletPath() + computeDefaultTarget(true));
    }
    toolSession.setAttribute(
        helperTool.getId() + "thetoolPath", req.getContextPath() + req.getServletPath());

    // saves the alternate done url map into a tool specific attribute
    if (toolSession.getAttribute(helperTool.getId() + ToolFinishedView.ALTERNATE_DONE_URL)
        == null) {
      toolSession.setAttribute(
          helperTool.getId() + ToolFinishedView.ALTERNATE_DONE_URL,
          toolSession.getAttribute(ToolFinishedView.ALTERNATE_DONE_URL));
      toolSession.setAttribute(
          helperTool.getId() + ToolFinishedView.ALTERNATE_DONE_URL_MAP,
          toolSession.getAttribute(ToolFinishedView.ALTERNATE_DONE_URL_MAP));
      toolSession.removeAttribute(ToolFinishedView.ALTERNATE_DONE_URL);
      toolSession.removeAttribute(ToolFinishedView.ALTERNATE_DONE_URL_MAP);
    }

    /*comment out for using the global parameter rather than tool-by-tool setting
        SessionState state = UsageSessionService.getSessionState(toolSession.getPlacementId());
    		boolean show_other_sites = ServerConfigurationService.getBoolean("syllabus.resources.show_all_collections.helper", true);
    		state.setAttribute("resources.allow_user_to_see_all_sites", (new Boolean(show_other_sites)).toString());
    		state.setAttribute("resources.user_chooses_to_see_other_sites", (new Boolean(show_other_sites)).toString());
    */
    String context = req.getContextPath() + req.getServletPath() + Web.makePath(parts, 1, 2);
    String toolPath = Web.makePath(parts, 2, parts.length);
    try {
      helperTool.help(req, res, context, toolPath);
    } catch (ToolException e) {
      throw new RuntimeException(e);
    }

    return true; // was handled as helper call
  }
  public void doTool(
      HttpServletRequest req,
      HttpServletResponse res,
      Session session,
      String placementId,
      String toolContextPath,
      String toolPathInfo)
      throws ToolException, IOException {

    if (portal.redirectIfLoggedOut(res)) return;

    // find the tool from some site
    ToolConfiguration siteTool = SiteService.findTool(placementId);
    if (siteTool == null) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    }

    // Reset the tool state if requested
    if ("true".equals(req.getParameter(portalService.getResetStateParam()))
        || "true".equals(portalService.getResetState())) {
      Session s = SessionManager.getCurrentSession();
      ToolSession ts = s.getToolSession(placementId);
      ts.clearAttributes();
    }

    // find the tool registered for this
    ActiveTool tool = ActiveToolManager.getActiveTool(siteTool.getToolId());
    if (tool == null) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    }

    // permission check - visit the site (unless the tool is configured to
    // bypass)
    Site site = null;
    if (tool.getAccessSecurity() == Tool.AccessSecurity.PORTAL) {
      try {
        Set<SecurityAdvisor> advisors =
            (Set<SecurityAdvisor>) session.getAttribute("sitevisit.security.advisor");
        if (advisors != null) {
          for (SecurityAdvisor advisor : advisors) {
            SecurityService.pushAdvisor(advisor);
            // session.removeAttribute("sitevisit.security.advisor");
          }
        }
        site = SiteService.getSiteVisit(siteTool.getSiteId());
      } catch (IdUnusedException e) {
        portal.doError(req, res, session, Portal.ERROR_WORKSITE);
        return;
      } catch (PermissionException e) {
        // if not logged in, give them a chance
        if (session.getUserId() == null) {
          portal.doLogin(req, res, session, req.getPathInfo(), false);
        } else {
          portal.doError(req, res, session, Portal.ERROR_WORKSITE);
        }
        return;
      }
    }

    // Check to see if the tool is visible
    if (!isToolVisible(site, siteTool)) {
      portal.doError(req, res, session, Portal.ERROR_WORKSITE);
      return;
    }

    if (portal.isPortletPlacement(siteTool)) {

      String siteType = portal.calcSiteType(siteTool.getSiteId());

      // form a context sensitive title
      String title =
          ServerConfigurationService.getString("ui.service")
              + " : "
              + site.getTitle()
              + " : "
              + siteTool.getTitle();

      PortalRenderContext rcontext =
          portal.startPageContext(siteType, title, siteTool.getSkin(), req);

      Map m = portal.includeTool(res, req, siteTool);
      rcontext.put("tool", m);

      portal.sendResponse(rcontext, res, "tool", null);

    } else {
      portal.forwardTool(
          tool, req, res, siteTool, siteTool.getSkin(), toolContextPath, toolPathInfo);
    }
  }