示例#1
0
 /**
  * 从msg的[pathArr]中定位子Action的actName
  *
  * @param msg
  * @param pathNum
  * @param defaultStr
  * @return subact 子Action的actName
  */
 public static final String actPath(ActionMsg msg, int pathNum, String defaultStr) {
   // FIXME 测试时多计算了Servlet
   pathNum = pathNum + rootNum;
   String[] pathArr = (String[]) msg.getData("[pathArr]");
   String subact = (pathArr.length <= pathNum) ? defaultStr : pathArr[pathNum];
   return subact;
 }
示例#2
0
  /** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    // 全程使用utf-8
    setCharset("utf-8", req, resp);

    String requrl = req.getRequestURI();
    // requrl = (requrl == null) ? "" : requrl.substring(1);
    String[] pathArr = requrl.split("\\/");
    int rn = rootNum + 1;
    // [root]为默认Action
    String actName = (pathArr.length <= rn) ? "[root]" : pathArr[rn];
    // TODO 过滤静态请求,此处如果配合前端的web server过滤可省去
    if (staticPath.containsKey(actName)) {
      //			String encoding = req.getHeader("Accept-Encoding");
      //			if (encoding != null && encoding.indexOf("gzip") != -1){
      //				HttpServletResponseWrapper wrapper = new GZIPServletResponseWrapper(resp);
      //			    chain.doFilter(req, wrapper);
      //			    wrapper.getOutputStream().close();
      //			}else{
      //				chain.doFilter(request, response);
      //			}

      chain.doFilter(request, response);
      return;
    }

    if (isStop) {
      // FIXME 穿透控制,注意这里一定要加ip控制
      if (req.getParameter("keelcontrolsall") == null) {
        resp.setStatus(500);
        resp.getWriter().print("System under maintenance, please come back later.");
        return;
      }
    }
    //		chain.doFilter(request, response);

    try {
      // pathArr[0]为域名,pathArr[1]为第一个路径,后面继续为路径,最后面为参数
      // String[] pathArr = requrl.substring(requrl.indexOf("//")+2).split("\\/");
      //			int rn = 1+rootNum;
      //			String actName = (pathArr.length <= rn) ? "" : pathArr[rn];
      ActionMsg msg = new HttpActionMsg(actName, req, resp);
      Action action = ActionManager.findAction(actName);
      if (action == null) {
        // 如果存在以*为name的Action,则此Action为默认Action,可匹配所有未匹配到的Action请求
        if (hasDefaultAction) {
          // 加入actName,以便defaultAction使用
          msg.addData("[actName]", actName);
          action = this.defaultAction;
        } else {
          resp.setStatus(404);
          resp.getWriter().print("404 - 2");
          return;
        }
      }
      msg.addData("[pathArr]", pathArr);
      // msg.addData("[prefix]", staticPrefix);
      // 执行action
      msg = action.act(msg);
      // 不处理
      if (msg.getData("[none]") != null) {
        return;
      }
      // 是否打印
      else if (msg.getData("[print]") != null) {
        resp.getWriter().print(msg.getData("[print]"));
        return;
      }
      // 是否发向JSP
      else if (msg.getData("[jsp]") != null) {
        String to = (String) msg.getData("[jsp]");
        //				Object o = msg.getData("[jspAttr]");
        //				if (o != null) {
        //					req.setAttribute("[jspAttr]", o);
        //				}
        req.setAttribute("[jspAttr]", msg);
        RequestDispatcher rd = req.getRequestDispatcher(to);
        rd.forward(req, resp);
        return;
      }
      // 是否本地跳转
      else if (msg.getData("[redirect]") != null) {
        String redirect = KFilter.getPrefix() + (String) msg.getData("[redirect]");
        resp.sendRedirect(redirect);
        return;
      }
      // 是否直接跳转
      else if (msg.getData("[goto]") != null) {
        String link = (String) msg.getData("[goto]");
        resp.sendRedirect(link);
        return;
      }
      // 继续走chain
      //			else if(msg.getData("[chain]") != null){
      //				chain.doFilter(request, response);
      //				return;
      //			}
      else {
        resp.setStatus(404);
        // resp.getWriter().print("404 - 3");
        return;
      }
    } catch (Exception e) {
      log.error("KFilter error!", e);
      resp.setStatus(404);
      resp.getWriter().print("500 - Error! please contact [email protected] ");
      return;
    }
  }