servlet convert Request Headers - Java javax.servlet.http

Java examples for javax.servlet.http:HttpServletRequest

Description

servlet convert Request Headers

Demo Code


import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main{
    private static List<String> allowedHeaders = Arrays.asList("Accept",
            "Accept-Language", "Accept-Charset");
    public static Map<String, List<String>> convertRequestHeaders(
            HttpServletRequest req) {/*  ww w  .jav a 2  s .  c om*/
        Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();

        Enumeration enumeration = req.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();

            if (!"Host".equalsIgnoreCase(name)
                    && allowedHeaders.contains(name)) {
                List<String> headerValues = headers.get(name);
                if (headerValues == null) {
                    headerValues = new LinkedList<String>();
                    headers.put(name, headerValues);
                }
                headerValues.add(req.getHeader(name));
            }
        }
        return headers;
    }
    /**
     * Returns true if two strings are considered case blind equal, false
     * otherwise
     *
     * @param s1 the first string
     * @param s2 the second string
     * @return the equality of the two Strings
     */
    @SuppressWarnings({ "StringEquality" })
    public static boolean equalsIgnoreCase(String s1, String s2) {
        return s1 == s2 || s1 != null && s1.equalsIgnoreCase(s2);
    }
}

Related Tutorials