開発メモ

開発用のメモです。

特定の アトリビュートを無視する HttpSession

import java.util.Enumeration;
import java.util.Vector;

import org.apache.catalina.session.StandardSession;
import org.apache.commons.lang3.StringUtils;

public class HttpSessionCatalinaWrapper extends StandardSession {

    private String[] ignoreNames;

    public HttpSessionCatalinaWrapper(StandardSession manager, String... ignoreNames) {
        super(manager.getManager());
        this.ignoreNames = ignoreNames;
    }

    @Override
    public Enumeration<String> getAttributeNames() {
        Enumeration<String> attributeNames = super.getAttributeNames();
        Vector<String> retAttributeNames = new Vector<>();
        while (attributeNames.hasMoreElements()) {
            String name = attributeNames.nextElement();
            boolean add = true;
            for (String ignoreName : ignoreNames) {
                if (!(add &= StringUtils.containsNone(name, ignoreName))) {
                    break;
                }
            }
            if (add) {
                retAttributeNames.addElement(name);
            }
        }
        return retAttributeNames.elements();
    }

    @Override
    public Object getAttribute(String name) {
        boolean add = true;
        for (String ignoreName : ignoreNames) {
            if (!(add &= StringUtils.containsNone(name, ignoreName))) {
                return null;
            }
        }
        return super.getAttribute(name);
    }
}
Twitter: @asahina_alice