protected void handle(HttpServletRequest hreq, HttpServletResponse hres, AnubisContext ctx)
      throws Exception {
    String pathInfo = hreq.getPathInfo();
    String path = hreq.getServletPath() + pathInfo;

    Project project = ctx.getProject();
    String urlPath = project.getUrl() + path;

    String themeName = pathInfo.substring(1, pathInfo.indexOf("/", 1));
    String resName = pathInfo.substring(pathInfo.indexOf("/", 1));

    ServletContext app = config.getServletContext();
    String mimeType = app.getMimeType(pathInfo);

    InputStream is = null;
    try {
      Theme theme = null;
      if (themeName.equals("system")) {
        theme = project.getSystemTheme();
      } else {
        theme = project.getThemes().get(themeName);
      }

      is = theme.getResource(resName);
      if (is != null) ResponseWriter.write(app, hreq, hres, mimeType, is);
    } catch (Exception e) {
      System.out.println("error theme resource " + e.getMessage());
    } finally {
      try {
        is.close();
      } catch (Exception ign) {;
      }
    }
  }
示例#2
0
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws IOException, ServletException {
    HttpServletRequest hreq = (HttpServletRequest) servletRequest;
    HttpServletResponse hres = (HttpServletResponse) servletResponse;
    AnubisContext.setContext(new WebAnubisContext(config.getServletContext(), hreq, hres));
    try {
      ServletContext app = config.getServletContext();
      String pattern = buildIgnoredPattern();

      if (!hreq.getServletPath().matches(pattern)) {
        // run the CMS file

        Project project = AnubisContext.getCurrentContext().getProject();
        Map params = CmsWebUtil.buildRequestParams(hreq);

        String mimeType = app.getMimeType(hreq.getServletPath());
        String ext = CmsWebConstants.PAGE_FILE_EXT;
        if (mimeType == null) {
          mimeType = "text/html";
        } else {
          // if mimetype is not null then automatically we consider it as media.
          ext = CmsWebConstants.MEDIA_FILE_EXT;
        }

        String spath = hreq.getServletPath();
        if (spath.equals("/")) {
          Folder folder = project.getFileManager().getFolder("/");
          File ff = ProjectUtils.findFirstVisibleFile(folder);
          if (ff != null) spath = ff.getPath();
        } else {
          if (spath.endsWith("/")) spath = spath.substring(0, spath.length() - 1);
        }

        String filename = spath + ext;

        FileInstance file = null;
        try {
          file = project.getFileManager().getFile(filename, params);
        } catch (com.rameses.anubis.FileNotFoundException fe) {
          hres.setStatus(HttpServletResponse.SC_NOT_FOUND);
          file = project.getFileManager().getFile("/404.pg", params);
          ResponseWriter.write(app, hreq, hres, mimeType, file.getContent());
          return;
        } catch (Exception e) {
          e.printStackTrace();
          throw new ServletException(e.getMessage());
        }

        if (file.getHref() != null) {
          hres.sendRedirect(file.getHref());
          return;
        }

        SessionContext ctx = AnubisContext.getCurrentContext().getSession();

        // set authenicated as true if there is sessionid
        boolean allow_access = true;
        if (file.isSecured()) {
          if (!ctx.isLoggedIn()) allow_access = false;
          else if (!ctx.checkFilePermission(file)) allow_access = false;
        }

        if (!allow_access) {
          String path = CmsWebConstants.LOGIN_PAGE_PATH;
          String requestPath = hreq.getRequestURI();
          String qry = hreq.getQueryString();
          if (qry != null && qry.trim().length() > 0) {
            requestPath += "?" + qry;
          }
          hres.sendRedirect(path + "?target=" + URLEncoder.encode(requestPath));
        } else {
          InputStream is = file.getContent();
          ResponseWriter.write(app, hreq, hres, mimeType, is);
        }
      } else {
        filterChain.doFilter(servletRequest, servletResponse);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      AnubisContext.removeContext();
    }
  }