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

me.xethh.libs.spring.web.security.toolkits.MutableHttpRequestWrapper Maven / Gradle / Ivy

The newest version!
package me.xethh.libs.spring.web.security.toolkits;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;

public class MutableHttpRequestWrapper extends HttpServletRequestWrapper implements MutableHttpRequest {
    private final Map customHeaders;

    public MutableHttpRequestWrapper(HttpServletRequest request) {
        super(request);
        this.customHeaders = new HashMap<>();
    }
    @Override
    public void putHeader(String name, String value){
        this.customHeaders.put(name, value);
    }

    @Override
    public String getHeader(String name) {
        // check the custom headers first
        String headerValue = customHeaders.get(name);

        if (headerValue != null){
            return headerValue;
        }
        // else return from into the original wrapped object
        return ((HttpServletRequest) getRequest()).getHeader(name);
    }

    @Override
    public Enumeration getHeaderNames() {
        // create a set of the custom header names
        Set set = new HashSet(customHeaders.keySet());

        // now add the headers from the wrapped request object
        @SuppressWarnings("unchecked")
        Enumeration e = ((HttpServletRequest) getRequest()).getHeaderNames();
        while (e.hasMoreElements()) {
            // add the names of the request headers into the list
            String n = e.nextElement();
            set.add(n);
        }

        // create an enumeration from the set and return
        return Collections.enumeration(set);
    }

    @Override
    public Enumeration getHeaders(String name) {
        Set set = new HashSet<>();
        String v = customHeaders.get(name);
        if(v!=null)
            set.add(v);
        Enumeration e = super.getHeaders(name);
        while (e.hasMoreElements()){
            String n = e.nextElement();
            set.add(n);
        }
        return Collections.enumeration(set);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy