Example usage for javax.servlet.http HttpServletRequest getHeaderNames

List of usage examples for javax.servlet.http HttpServletRequest getHeaderNames

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getHeaderNames.

Prototype

public Enumeration<String> getHeaderNames();

Source Link

Document

Returns an enumeration of all the header names this request contains.

Usage

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * ?header????/*from w w  w.  j a v a2  s .  co m*/
 * @param req
 * @param name
 * @return
 */
public static String getHeader(HttpServletRequest req, String name) {
    String value = req.getHeader(name);
    if (value != null)
        return value;
    Enumeration names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String n = (String) names.nextElement();
        if (n.equalsIgnoreCase(name)) {
            return req.getHeader(n);
        }
    }
    return null;
}

From source file:com.itude.mobile.mobbl.server.util.HeaderUtil.java

public static void printRequestHeaders(Logger logger, HttpServletRequest request) {
    if (request == null)
        throw new IllegalArgumentException("Request can't be null");
    if (logger == null)
        throw new IllegalArgumentException("Logger can't be null");

    logger.debug("List of request parameters:");

    @SuppressWarnings("unchecked")
    Enumeration<String> requestHeaders = request.getHeaderNames();
    if (requestHeaders != null) {
        while (requestHeaders.hasMoreElements()) {
            String header = requestHeaders.nextElement();

            @SuppressWarnings("unchecked")
            Enumeration<String> headers = request.getHeaders(header);
            if (headers != null) {
                while (headers.hasMoreElements())
                    logger.debug(header + ":" + headers.nextElement());
            }//  w ww.  j a  v a 2s . c  om
        }
    }
}

From source file:org.dataone.annotator.store.AnnotatorRestServlet.java

private static void debugHeaders(HttpServletRequest request) {
    Enumeration<String> headers = request.getHeaderNames();
    while (headers.hasMoreElements()) {
        String name = (String) headers.nextElement();
        String value = request.getHeader(name);
        log.debug("Header: " + name + "=" + value);
        //System.out.println("Header: " + name + "=" + value);

    }//from   w  w w.j av a 2  s  .  c o  m
}

From source file:com.tek271.reverseProxy.servlet.ProxyFilter.java

@SuppressWarnings({ "unchecked" })
private static void addCustomHeaders(HttpServletRequest original, HttpEntityEnclosingRequest request,
        String... skipElements) {
    Enumeration<String> en = original.getHeaderNames();
    while (en.hasMoreElements()) {
        String name = en.nextElement();
        if (contains(skipElements, name)) {
            continue;
        } else if ("X-HTTP-Method-Override".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-Source".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("preferred-role".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-File-Name".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-Requested-With".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-Requested-By".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("Content-type".equalsIgnoreCase(name)) {
            request.setHeader(name, original.getHeader(name));
        }//from   w ww  .j  a va 2 s . c  om

    }
}

From source file:com.threewks.thundr.request.servlet.ServletSupport.java

public static Map<String, List<String>> getHeaderMap(HttpServletRequest req) {
    Map<String, List<String>> headerMap = new LinkedHashMap<String, List<String>>();
    Iterable<String> iterable = Expressive.<String>iterable(req.getHeaderNames());
    for (String name : iterable) {
        headerMap.put(name, getHeaders(name, req));
    }//from  ww w  . jav a  2s  .c  om
    return headerMap;
}

From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java

public static Map<String, List<String>> convertFieldEnumerationToMap(HttpServletRequest request) {
    Map<String, List<String>> headers = new HashMap<String, List<String>>();

    for (Enumeration<String> enumeration = request.getHeaderNames(); enumeration.hasMoreElements();) {
        String name = enumeration.nextElement();
        Enumeration headerNames = request.getHeaders(name);

        while (headerNames.hasMoreElements()) {
            List<String> list = headers.get(name);

            if (list == null) {
                list = new ArrayList<String>();
                headers.put(name, list);
            }/*  w  w w .  ja  va  2 s  .c  o m*/

            list.add((String) headerNames.nextElement());
        }
    }

    return headers;
}

From source file:jeeves.server.sources.ServiceRequestFactory.java

/**
 * Build up a map of the HTTP headers./*ww w  .ja v  a2 s  .co  m*/
 * @param req The web request
 * @return Map of header keys and values.
 */
@SuppressWarnings("unchecked")
private static Map<String, String> extractHeaders(HttpServletRequest req) {
    Map<String, String> headerMap = new HashMap<String, String>();
    for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements();) {
        String key = e.nextElement();
        headerMap.put(key, req.getHeader(key));
    }
    // The remote user needs to be saved as a header also
    if (req.getRemoteUser() != null) {
        headerMap.put("REMOTE_USER", req.getRemoteUser());
    }
    return headerMap;
}

From source file:com.groupon.odo.HttpUtilities.java

/**
 * Obtain newline-delimited headers from request
 *
 * @param request/*from  w w  w  .  ja  v a  2s.co m*/
 * @return
 */
public static String getHeaders(HttpServletRequest request) {
    String headerString = "";
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        if (headerString.length() != 0)
            headerString += "\n";

        String name = headerNames.nextElement();
        headerString += name + ": " + request.getHeader(name);
    }

    return headerString;
}

From source file:io.apiman.test.common.mock.EchoResponse.java

/**
 * Create an echo response from the inbound information in the http server
 * request.//from w  w w  .jav  a2s .  co m
 * @param request
 * @param withBody 
 * @return a new echo response
 */
public static EchoResponse from(HttpServletRequest request, boolean withBody) {
    EchoResponse response = new EchoResponse();
    response.setMethod(request.getMethod());
    response.setResource(request.getRequestURI());
    response.setUri(request.getRequestURI());
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String name = headerNames.nextElement();
        String value = request.getHeader(name);
        response.getHeaders().put(name, value);
    }
    if (withBody) {
        long totalBytes = 0;
        InputStream is = null;
        try {
            is = request.getInputStream();
            MessageDigest sha1 = MessageDigest.getInstance("SHA1"); //$NON-NLS-1$
            byte[] data = new byte[1024];
            int read = 0;
            while ((read = is.read(data)) != -1) {
                sha1.update(data, 0, read);
                totalBytes += read;
            }
            ;

            byte[] hashBytes = sha1.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < hashBytes.length; i++) {
                sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            String fileHash = sb.toString();

            response.setBodyLength(totalBytes);
            response.setBodySha1(fileHash);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return response;
}

From source file:org.apache.qpid.server.management.plugin.HttpManagementUtil.java

public static OutputStream getOutputStream(final HttpServletRequest request, final HttpServletResponse response,
        HttpManagementConfiguration managementConfiguration) throws IOException {
    OutputStream outputStream;/*  w ww.  j  av  a 2 s .  co  m*/
    if (managementConfiguration.isCompressResponses()
            && Collections.list(request.getHeaderNames()).contains(ACCEPT_ENCODING_HEADER)
            && request.getHeader(ACCEPT_ENCODING_HEADER).contains(GZIP_CONTENT_ENCODING)) {
        outputStream = new GZIPOutputStream(response.getOutputStream());
        response.setHeader(CONTENT_ENCODING_HEADER, GZIP_CONTENT_ENCODING);
    } else {
        outputStream = response.getOutputStream();
    }
    return outputStream;
}