/* (non-Javadoc)
   * @see com.openedit.action.Command#execute(java.util.Map, java.util.Map)
   */
  public void execute(WebPageRequest inReq) throws OpenEditException {
    configure(inReq);
    Page page = (Page) inReq.getPage(); // urlUtils.requestPath();
    String requestPath = page.getPath();

    if (!inExcludeList(requestPath)) {
      Permission filter = inReq.getPage().getPermission("view");
      if ((filter != null)) {
        if (!filter.passes(inReq)) {
          if (isForbid()) {
            if (inReq.getResponse() != null) {
              inReq.getResponse().setStatus(HttpServletResponse.SC_FORBIDDEN);
              inReq.setHasRedirected(true);
            }
          } else {
            log.error("No view permission for " + page.getPath() + " sending redirect");
            inReq.putPageValue(
                "oe-exception", "You do not have permission to view " + page.getPath());

            // this is the original page someone might have been on. Used in login
            inReq.putSessionValue("originalEntryPage", inReq.getContentPage().getPath());
            String fullOriginalEntryPage = (String) inReq.getSessionValue("fullOriginalEntryPage");
            if (fullOriginalEntryPage == null) {
              inReq.putSessionValue("fullOriginalEntryPage", inReq.getPathUrlWithoutContext());
            }
            inReq.redirect(getLoginPath());
          }
        }
      } else {
        log.info("No view restrictions have been set for " + requestPath);
      }
    }
  }
  /** @see org.jpublish.ErrorHandler#handleError(JPublishError) */
  public boolean handleError(Throwable error, WebPageRequest context) {
    if (context.getResponse() != null
        && context.getResponse().getContentType() != null
        && !context.getResponse().getContentType().contains("json")) {
      return false;
    }

    OpenEditException exception = null;
    if (context != null) {
      try {
        if (!(error instanceof OpenEditException)) {
          exception = new OpenEditException(error); // we need the toStacktrace method
        } else {
          exception = (OpenEditException) error;
        }
        if (!context.hasRedirected() && context.getResponse() != null) {
          try {
            context.getResponse().setStatus(500);
          } catch (Exception ex) {
            // ignored
            log.debug("Ignored:" + ex);
          }
        }
        error.printStackTrace();
        String pathWithError = exception.getPathWithError();
        if (pathWithError == null) {
          pathWithError = context.getPage().getPath();
          exception.setPathWithError(pathWithError);
        }
        context.putPageValue("editPath", exception.getPathWithError());
        context.putPageValue(
            "oe-exception", exception); // must be a top level thing since we create a new context
        PageStreamer pages = (PageStreamer) context.getPageValue(PageRequestKeys.PAGES);

        Writer out = context.getWriter();
        out.append("{ \"reponse\": {\n");
        out.append(" \"status\":\"error\",");
        out.append("{ \"path\":\"" + pathWithError + "\",");
        out.append("{ \"details\":\"" + error + "\"");
        out.append("\n}");
        // error.printStackTrace( new PrintWriter( writer ) );
        out.flush();
      } catch (Exception ex) {
        // Do not throw an error here is it will be infinite
        log.error(ex);
        ex.printStackTrace();
        try {
          context.getWriter().write("Check error logs: " + ex);
          // throw new OpenEditRuntimeException(ex);
        } catch (Throwable ex1) {
          log.error(ex1);
        }
      }
      return true;
    }
    return false;
  }
  /* (non-Javadoc)
   * @see com.openedit.command.Command#load(com.anthonyeden.lib.config.Configuration)
   */
  public void configure(WebPageRequest inReq) {
    fieldLoginPath = inReq.findValue("login-path");

    Configuration element = inReq.getCurrentAction().getConfig();
    PageSettings settings = inReq.getPage().getPageSettings();
    for (Iterator iter = element.getChildren("exclude").iterator(); iter.hasNext(); ) {
      Configuration excludeElem = (Configuration) iter.next();
      String path = excludeElem.getValue();
      path = settings.replaceProperty(path);
      getExcludes().add(path);
    }
    String forbid = element.getAttribute("forbid");
    setForbid(Boolean.valueOf(forbid));
  }