All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.manydesigns.portofino.interceptors.AccessLoggerInterceptor Maven / Gradle / Ivy

There is a newer version: 4.2.12
Show newest version
package com.manydesigns.portofino.interceptors;

import com.manydesigns.portofino.pageactions.log.LogAccesses;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.ExecutionContext;
import net.sourceforge.stripes.controller.Interceptor;
import net.sourceforge.stripes.controller.Intercepts;
import net.sourceforge.stripes.controller.LifecycleStage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

@Intercepts(LifecycleStage.EventHandling)
public class AccessLoggerInterceptor implements Interceptor {

    public static final Logger logger = LoggerFactory.getLogger(AccessLoggerInterceptor.class);

    @Override
    public Resolution intercept(ExecutionContext context) throws Exception {
        Object bean = context.getActionBean();
        Method handler = context.getHandler();
        if(isToBeLogged(bean, handler)) {
            logger.info(
                    "ActionBean, method " + handler.getName() +
                    ", event " + context.getActionBeanContext().getEventName() +
                    ", query string " + context.getActionBeanContext().getRequest().getQueryString());
        }
        return context.proceed();
    }

    public static boolean isToBeLogged(Object resource, Method handler) {
        if (resource != null) {
            Boolean log = null;
            Class resourceClass = resource.getClass();

            LogAccesses annotation;
            if(handler != null) {
                annotation = handler.getAnnotation(LogAccesses.class);
                if(annotation != null) {
                    log = annotation.value();
                }
            }
            if(log == null) {
                annotation = resourceClass.getAnnotation(LogAccesses.class);
                log = (annotation != null && annotation.value());
            }
            return log;
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy