Search in sources :

Example 1 with AbsGlobalFilterHandler

use of com.creditease.monitor.globalfilter.AbsGlobalFilterHandler in project uavstack by uavorg.

the class GlobalFilterDispatchListener method handleEvent.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void handleEvent(InterceptContext context) {
    UAVServer.ServerVendor vendor = (ServerVendor) UAVServer.instance().getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR);
    Object httpReq = context.get(InterceptConstants.HTTPREQUEST);
    Object httpResp = context.get(InterceptConstants.HTTPRESPONSE);
    String url = null;
    /**
     * MSCP
     */
    if (vendor == ServerVendor.MSCP) {
    // TODO
    } else /**
     * JEE Application uses HttpServletRequest
     */
    {
        StringBuffer urlSB = (StringBuffer) ReflectionHelper.invoke("javax.servlet.http.HttpServletRequest", httpReq, "getRequestURL", null, null);
        if (urlSB != null) {
            url = urlSB.toString();
        }
    }
    if (url == null) {
        return;
    }
    if (MonitorServerUtil.isIncludeMonitorURL(url) == false) {
        return;
    }
    /**
     * NOTE: 目前的机制下,执行可能分为几种情况:
     *
     * 1. 执行1个BaseGlobalFilterHandler,就终止Handler Chain,也终止Filter
     * Chain,则该handler的isBlockHandlerChain和isBlockFilterChain都为true
     *
     * 2. 执行多个BaseGlobalFilterHandler(contextPath有重合或叠加),才终止Handler Chain,但不终止Filter
     * Chain,最后一个BaseGlobalFilterHandler的isBlockHandlerChain为true,
     * 所有的BaseGlobalFilterHandler的isBlockFilterChain为false
     *
     * 3. 执行多个BaseGlobalFilterHandler(contextPath有重合或叠加),才终止Handler Chain,,也终止Filter
     * Chain,最后一个BaseGlobalFilterHandler的isBlockHandlerChain为true,
     * 其中任何一个BaseGlobalFilterHandler的isBlockFilterChain为true
     *
     * 所以要注意,BaseGlobalFilterHandler的执行顺序与注册的顺序有关
     */
    boolean isBlockFilterChain = false;
    for (AbsGlobalFilterHandler handler : handlers) {
        String contextPath = handler.getContext();
        if (url.indexOf(contextPath) > -1) {
            try {
                handler.handle(httpReq, httpResp, context);
            } catch (Exception e) {
                logger.error("GlobalFilterDispatchListener Handler[" + handler.getClass().getName() + "] runs FAIL: url=" + url, e);
            }
            if (handler.isBlockFilterChain() == true) {
                isBlockFilterChain = true;
            }
            if (handler.isBlockHandlerChain() == true) {
                break;
            }
        }
    }
    if (isBlockFilterChain == true) {
        context.put(InterceptConstants.STOPREQUEST, new Object());
    }
}
Also used : ServerVendor(com.creditease.monitor.UAVServer.ServerVendor) ServerVendor(com.creditease.monitor.UAVServer.ServerVendor) UAVServer(com.creditease.monitor.UAVServer) AbsGlobalFilterHandler(com.creditease.monitor.globalfilter.AbsGlobalFilterHandler)

Aggregations

UAVServer (com.creditease.monitor.UAVServer)1 ServerVendor (com.creditease.monitor.UAVServer.ServerVendor)1 AbsGlobalFilterHandler (com.creditease.monitor.globalfilter.AbsGlobalFilterHandler)1