開発メモ

開発用のメモです。

セッション IDをログに出力

package jp.mirageworld.webapps.interceptor;


import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


public class SessionIdInterceptor
        extends
            AbstractInterceptor {


    /** logger. */
    protected static final Logger logger;
    static {
        logger = LogManager.getLogger( SessionIdInterceptor.class );
    }

    @Override
    public void init() {


        logger.info( "INIT : " + getClass().getName() );
        super.init();
    }

    @Override
    public String intercept( ActionInvocation invocation )
            throws Exception {


        String sessionId = ServletActionContext.getRequest().getRequestedSessionId();
        StringBuffer uri = ServletActionContext.getRequest().getRequestURL();
        uri.append( "!JSESSIONID=" + StringUtils.defaultString( sessionId ) );

        logger.info( "INTERCEPTOR : " + getClass().getName() + " : START" );
        logger.info( "INTERCEPTOR : " + getClass().getName() + " : SESSION ID " + uri.toString() );
        logger.info( "INTERCEPTOR : " + getClass().getName() + " : END" );
        return invocation.invoke();
    }

    @Override
    public void destroy() {


        logger.info( "DESTROY: " + getClass().getName() );
        super.destroy();
    }

}
Twitter: @asahina_alice