Example usage for javax.servlet.http HttpServletRequest getHeaders

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

Introduction

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

Prototype

public Enumeration<String> getHeaders(String name);

Source Link

Document

Returns all the values of the specified request header as an Enumeration of String objects.

Usage

From source file:org.craftercms.core.util.HttpServletUtils.java

public static Map<String, Object> createHeadersMap(HttpServletRequest request) {
    Map<String, Object> headersMap = new HashMap<String, Object>();
    for (Enumeration headerNameEnum = request.getHeaderNames(); headerNameEnum.hasMoreElements();) {
        String headerName = (String) headerNameEnum.nextElement();
        List<String> headerValues = new ArrayList<String>();

        CollectionUtils.addAll(headerValues, request.getHeaders(headerName));

        if (headerValues.size() == 1) {
            headersMap.put(headerName, headerValues.get(0));
        } else {/*from  w  ww.ja  v  a2 s .  c om*/
            headersMap.put(headerName, headerValues.toArray(new String[headerValues.size()]));
        }
    }

    return headersMap;
}

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);
            }//from ww w. java 2 s  . c o  m

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

    return headers;
}

From source file:edu.cornell.mannlib.vitro.webapp.web.MiscWebUtils.java

public static void debugPrintHeaders(HttpServletRequest req) {
    Enumeration hnames = req.getHeaderNames();
    while (hnames.hasMoreElements()) {
        String name = (String) hnames.nextElement();
        log.debug("header " + name);
        String value = req.getHeader(name);
        log.debug("    " + value);
        Enumeration values = req.getHeaders(name);
        if (values == null) {
            log.debug("    enumeration was null");
        } else {//from w w  w  . j  av  a  2s.  c o m
            log.debug("    enumeration values");
            while (values.hasMoreElements()) {
                String val = (String) values.nextElement();
                log.debug("    " + value);
            }
        }
    }
}

From source file:org.apache.solr.servlet.cache.HttpCacheHeaderUtil.java

/**
 * Check for etag related conditional headers and set status 
 * //  ww w .ja va 2 s. c  om
 * @return true if no request processing is necessary and HTTP response status has been set, false otherwise.
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static boolean checkETagValidators(final HttpServletRequest req, final HttpServletResponse resp,
        final Method reqMethod, final String etag) throws IOException {

    // First check If-None-Match because this is the common used header
    // element by HTTP clients
    final List<String> ifNoneMatchList = Collections.list(req.getHeaders("If-None-Match"));
    if (ifNoneMatchList.size() > 0 && isMatchingEtag(ifNoneMatchList, etag)) {
        if (reqMethod == Method.GET || reqMethod == Method.HEAD) {
            sendNotModified(resp);
        } else {
            sendPreconditionFailed(resp);
        }
        return true;
    }

    // Check for If-Match headers
    final List<String> ifMatchList = Collections.list(req.getHeaders("If-Match"));
    if (ifMatchList.size() > 0 && !isMatchingEtag(ifMatchList, etag)) {
        sendPreconditionFailed(resp);
        return true;
    }

    return false;
}

From source file:org.apache.shindig.gadgets.servlet.ServletUtil.java

/**
 * Returns an HttpRequest object encapsulating the servlet request.
 * NOTE: Request parameters are not explicitly taken care of, instead we copy
 * the InputStream and query parameters separately.
 *
 * @param servletReq The http servlet request.
 * @return An HttpRequest object with all the information provided by the
 *   servlet request./* w ww.ja  v  a2 s.  co  m*/
 * @throws IOException In case of errors.
 */
public static HttpRequest fromHttpServletRequest(HttpServletRequest servletReq) throws IOException {
    HttpRequest req = new HttpRequest(new UriBuilder(servletReq).toUri());

    Enumeration<?> headerNames = servletReq.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        Object obj = headerNames.nextElement();
        if (obj instanceof String) {
            String headerName = (String) obj;

            Enumeration<?> headerValues = servletReq.getHeaders(headerName);
            while (headerValues.hasMoreElements()) {
                obj = headerValues.nextElement();
                if (obj instanceof String) {
                    req.addHeader(headerName, (String) obj);
                }
            }
        }
    }

    req.setMethod(servletReq.getMethod());
    if ("POST".equalsIgnoreCase(req.getMethod())) {
        req.setPostBody(servletReq.getInputStream());
    }
    req.setParam(REMOTE_ADDR_KEY, servletReq.getRemoteAddr());
    return req;
}

From source file:org.zoxweb.server.http.servlet.HTTPServletUtil.java

public static List<GetNameValue<String>> extractRequestHeaders(HttpServletRequest req) {
    ArrayList<GetNameValue<String>> ret = new ArrayList<GetNameValue<String>>();

    Enumeration<String> headerNames = req.getHeaderNames();
    for (; headerNames.hasMoreElements();) {
        String headerName = headerNames.nextElement();
        Enumeration<String> headerValues = req.getHeaders(headerName);
        for (; headerValues.hasMoreElements();) {
            ret.add(new NVPair(headerName, headerValues.nextElement()));
        }/*w  ww . j a  va  2 s. c  o  m*/
    }

    return ret;
}

From source file:org.cruxframework.crux.core.server.rest.spi.HttpUtil.java

@SuppressWarnings("unchecked")
public static MultivaluedMap<String, String> extractRequestHeaders(HttpServletRequest request) {
    Headers<String> requestHeaders = new Headers<String>();

    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        Enumeration<String> headerValues = request.getHeaders(headerName);
        while (headerValues.hasMoreElements()) {
            String headerValue = headerValues.nextElement();
            // System.out.println("ADDING HEADER: " + headerName +
            // " value: " + headerValue);
            requestHeaders.add(headerName, headerValue);
        }/*from w w  w  .  j ava2  s .  c  o m*/
    }
    return requestHeaders;
}

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  w w .  j  av  a2 s  . c  o  m*/
        }
    }
}

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

private static void addCustomGetHeaders(HttpServletRequest request, HttpGet httpGet) {
    httpGet.addHeader("preferred-role", request.getHeader("preferred-role"));
    httpGet.addHeader("X-Source", request.getHeader("X-Source"));
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        Enumeration enumerationOfHeaderValues = request.getHeaders(headerName);
        while (enumerationOfHeaderValues.hasMoreElements()) {
            String headerValue = (String) enumerationOfHeaderValues.nextElement();
            if (headerName.equalsIgnoreCase("accept")) {
                httpGet.setHeader(headerName, headerValue);
            }/*from  w w w.j a v  a  2 s  .  co  m*/
        }

    }
}

From source file:org.ambraproject.rhino.rest.LoggingInterceptor.java

/**
 * Build a message describing a request.
 *
 * @param request the request to describe
 * @return the description//from  www  . ja  va  2 s . c  om
 */
private static String describe(HttpServletRequest request) {
    StringBuilder message = new StringBuilder();
    message.append(request.getMethod()).append(' ').append(request.getRequestURI());
    String queryString = request.getQueryString();
    if (queryString != null) {
        message.append(" ? ").append(queryString);
    }
    message.append('\n');

    // Append a list of headers
    for (Enumeration headerNames = request.getHeaderNames(); headerNames.hasMoreElements();) {
        String headerName = (String) headerNames.nextElement();
        message.append(INDENT).append(asStringLiteral(headerName)).append(": ");
        Enumeration<?> headers = request.getHeaders(headerName);
        Iterator<String> headerStrings = Iterators.transform(Iterators.forEnumeration(headers),
                LoggingInterceptor::asStringLiteral);
        JOINER.appendTo(message, headerStrings);
        message.append('\n');
    }

    return message.toString();
}