開発メモ

開発用のメモです。

特定のパラメーター・アトリビュート・ヘッダーを除外する HttpServletRequest

パラメーター 対象 
hrdIgnoreNames header
prmIgnoreNames parameter
atrIgnoreNames attribute
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

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

public class HttpServletRequestCatalinaWrapper extends HttpServletRequestWrapper {

    private String[] hrdIgnoreNames;
    private String[] prmIgnoreNames;
    private String[] atrIgnoreNames;

    public HttpServletRequestCatalinaWrapper(Request request) {
        super(request);
    }

    public void setHrdIgnoreNames(String[] hrdIgnoreNames) {
        this.hrdIgnoreNames = hrdIgnoreNames;
    }

    public void setPrmIgnoreNames(String[] prmIgnoreNames) {
        this.prmIgnoreNames = prmIgnoreNames;
    }

    public void setAtrIgnoreNames(String[] atrIgnoreNames) {
        this.atrIgnoreNames = atrIgnoreNames;
    }

    @Override
    public Enumeration<String> getAttributeNames() {
        return purser(getAttributeNames(), atrIgnoreNames);
    }

    @Override
    public Object getAttribute(String name) {
        if (isNotIgnore(name, atrIgnoreNames)) {
            return super.getAttribute(name);
        }
        return null;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return purser(getParameterNames(), prmIgnoreNames);
    }

    @Override
    public String getParameter(String name) {
        if (isNotIgnore(name, prmIgnoreNames)) {
            return super.getParameter(name);
        }
        return null;
    }

    @Override
    public String[] getParameterValues(String name) {
        if (isNotIgnore(name, prmIgnoreNames)) {
            return super.getParameterValues(name);
        }
        return null;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Enumeration<String> params = super.getParameterNames();
        Map<String, String[]> parameters = super.getParameterMap();
        while (params.hasMoreElements()) {
            String name = params.nextElement();
            if (!isNotIgnore(name, prmIgnoreNames)) {
                parameters.remove(name);
            }
        }
        return parameters;
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        return purser(getHeaderNames(), hrdIgnoreNames);
    }

    @Override
    public String getHeader(String name) {
        if (isNotIgnore(name, hrdIgnoreNames)) {
            return super.getHeader(name);
        }
        return null;
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        if (isNotIgnore(name, hrdIgnoreNames)) {
            return super.getHeaders(name);
        }
        return null;
    }

    @Override
    public HttpSession getSession() {
        return new HttpSessionCatalinaWrapper((StandardSession) super.getSession(), atrIgnoreNames);
    }

    @Override
    public HttpSession getSession(boolean create) {
        return new HttpSessionCatalinaWrapper((StandardSession) super.getSession(create), atrIgnoreNames);
    }

    private boolean isNotIgnore(String name, String[] ignores) {
        for (String ignoreName : ignores) {
            if (!(StringUtils.containsNone(name, ignoreName))) {
                return false;
            }
        }
        return true;
    }

    private Enumeration<String> purser(Enumeration<String> src, String[] ignores) {
        Vector<String> desc = new Vector<>();
        while (src.hasMoreElements()) {
            String name = src.nextElement();
            boolean add = true;
            for (String ignoreName : ignores) {
                if (!(add &= StringUtils.containsNone(name, ignoreName))) {
                    break;
                }
            }
            if (add) {
                desc.addElement(name);
            }
        }
        return desc.elements();
    }
}
Twitter: @asahina_alice