Ejemplo n.º 1
0
  /*
   * (non-Javadoc)
   *
   * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
   * javax.servlet.ServletResponse, javax.servlet.FilterChain)
   */
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    if (PropertiesUtil.getObject(SysParameterEnum.STATIC_PAGE_SUPPORT, Boolean.class)) {
      HttpServletRequest hreq = (HttpServletRequest) request;

      String str_uri = hreq.getRequestURI();
      // System.out.println("hreq.getRequestURL() = " +
      // hreq.getRequestURL());
      // System.out.println(" hreq.getRealPath = " +
      // hreq.getRealPath("/"));
      // System.out.println(" hreq.getRemoteAddr= " +
      // hreq.getRemoteAddr());
      // System.out.println("str_uri =================== " + str_uri);
      // System.out.println("context path = " +
      // config.getServletContext().getContextPath());

      String prefixUriPath = supportURL(str_uri, contextPath);
      if (prefixUriPath != null) {
        HttpServletResponse hresp = (HttpServletResponse) response;
        String name = str_uri.substring(str_uri.lastIndexOf("/") + 1);

        if (AppUtils.isBlank(name)) {
          name = "index";
        }

        String fileName = name + ".html";
        String chrrentShopName = ThreadLocalContext.getCurrentShopName(hreq, hresp);
        String htmPath = HTML_PATH + chrrentShopName;

        // full name
        StringBuilder sb =
            new StringBuilder(htmPath).append(prefixUriPath).append("/").append(fileName);
        String fullName = sb.toString();

        // to which html
        sb.setLength(0);
        String toHtmlPage =
            sb.append(contextPath)
                .append("/")
                .append(AttributeKeys.HTML_COMMON)
                .append(chrrentShopName)
                .append(prefixUriPath)
                .append("/")
                .append(fileName)
                .toString();
        File file = new File(fullName);
        if (!file.exists()) {
          ServletResponse newResponse = new CharResponseWrapper(hresp);
          chain.doFilter(request, newResponse);
          String text = newResponse.toString();
          if (text != null) {
            FileProcessor.writeFile(text, htmPath + prefixUriPath, fileName);
            hresp.sendRedirect(toHtmlPage);
            // request.getRequestDispatcher(toHtmlPage).forward(hreq,
            // hresp);
          }
        } else {
          hresp.sendRedirect(toHtmlPage);
          // request.getRequestDispatcher(toHtmlPage).forward(hreq,
          // hresp);
        }

      } else {
        chain.doFilter(request, response);
      }
    } else {
      chain.doFilter(request, response);
    }
  }