org.qifu.base.interceptor.QueryParamInspectInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for org.qifu.base.interceptor.QueryParamInspectInterceptor.java

Source

/* 
 * Copyright 2012-2017 qifu of copyright Chen Xin Nien
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * -----------------------------------------------------------------------
 * 
 * author:    Chen Xin Nien
 * contact: chen.xin.nien@gmail.com
 * 
 */
package org.qifu.base.interceptor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.qifu.base.Constants;
import org.qifu.base.exception.ServiceException;
import org.qifu.base.model.ControllerMethodAuthority;
import org.qifu.sys.SysQueryParamInspectUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class QueryParamInspectInterceptor implements HandlerInterceptor {
    protected static Logger logger = Logger.getLogger(QueryParamInspectInterceptor.class);

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelView) throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        Method method = ((HandlerMethod) handler).getMethod();
        Annotation[] actionMethodAnnotations = method.getAnnotations();
        log(method, actionMethodAnnotations, request);
        return true;
    }

    private void log(Method method, Annotation[] annotations, HttpServletRequest request)
            throws ServiceException, Exception {
        String progId = "";
        for (Annotation anno : annotations) {
            if (anno instanceof ControllerMethodAuthority) {
                progId = ((ControllerMethodAuthority) anno).programId();
            }
        }
        if (StringUtils.isBlank(progId)) {
            return;
        }
        SysQueryParamInspectUtils.log(Constants.getSystem(), progId, method.getName(), request);
    }

}