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: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 a  va2s  .  c  om
    }
    return requestHeaders;
}

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 ww w.  ja  v  a  2  s  . c  o m
 */
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();
}

From source file:org.piraso.web.base.WebEntryUtils.java

@SuppressWarnings("unchecked")
public static HttpRequestEntry toEntry(HttpServletRequest request) {
    HttpRequestEntry entry = new HttpRequestEntry(request.getRequestURI());

    entry.setMethod(request.getMethod());
    entry.setQueryString(request.getQueryString());
    entry.setRemoteAddr(request.getRemoteAddr());
    entry.setParameters(new LinkedHashMap<String, String[]>(request.getParameterMap()));
    entry.setServerName(request.getServerName());

    Enumeration<String> enumeration = request.getHeaderNames();
    while (enumeration.hasMoreElements()) {
        String name = enumeration.nextElement();
        String value = request.getHeader(name);
        entry.addHeader(name, value);//  ww w. j av a2s.com
    }

    if (!ArrayUtils.isEmpty(request.getCookies())) {
        for (Cookie cookie : request.getCookies()) {
            entry.addCookie(toEntry(cookie));
        }
    }

    return entry;
}

From source file:org.craftercms.commons.http.HttpUtils.java

/**
 * Creates a map from the headers in the specified request. Multi value headers will be in an array.
 *
 * @param request the request/* www .j  av  a  2  s.  c  o m*/
 */
public static Map<String, Object> createHeadersMap(HttpServletRequest request) {
    Map<String, Object> headersMap = new HashMap<>();
    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 {
            headersMap.put(headerName, headerValues.toArray(new String[headerValues.size()]));
        }
    }

    return headersMap;
}

From source file:org.eclipse.om2m.binding.http.RestHttpServlet.java

public static String httpRequestToString(HttpServletRequest request, String content) {
    String heads = "{\n";
    Enumeration<String> headerNames = request.getHeaderNames();

    while (headerNames.hasMoreElements()) {

        String headerName = headerNames.nextElement();
        heads += "\t" + (headerName) + ": ";

        Enumeration<String> headers = request.getHeaders(headerName);
        while (headers.hasMoreElements()) {
            String headerValue = headers.nextElement();
            heads += headerValue + "\n";
        }/* w  w w  .j a  va2  s . c o  m*/
    }
    heads += "}";
    return "HttpRequest [method=" + request.getMethod() + ", URI=" + request.getRequestURI()
            + ", representation=" + content + ", queryString=" + request.getQueryString() + ", headers :\n"
            + heads + "]";
}

From source file:de.betterform.agent.web.WebUtil.java

/**
 * copy all http headers from client request into betterForm context map as map HTTP_HEADERS. This map
 * will be picked up in AbstractHttpConnector to configure a proper request for the internal http-client.
 *//*w  w w  . jav a2  s  .c  o  m*/
public static void copyHttpHeaders(HttpServletRequest request, XFormsProcessor processor) {
    RequestHeaders httpHeaders = new RequestHeaders();

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        httpHeaders.addHeader(headerName, request.getHeader(headerName));
        if (WebUtil.LOGGER.isDebugEnabled()) {
            WebUtil.LOGGER
                    .debug("keeping httpheader: " + headerName + " value: " + request.getHeader(headerName));
        }

    }
    processor.setContextParam(AbstractHTTPConnector.HTTP_REQUEST_HEADERS, httpHeaders);
}

From source file:org.jxstar.control.action.ActionHelper.java

/**
 * ?//from   w  w  w  .  j a v  a2 s. com
 * @param request
 */
private static Map<String, String> getClientInfo(HttpServletRequest request) {
    Map<String, String> mpInfo = FactoryUtil.newMap();

    //??ID
    HttpSession session = request.getSession();
    mpInfo.put("session_id", session.getId());
    //??
    mpInfo.put("client_ip", request.getRemoteAddr());

    //??
    @SuppressWarnings("rawtypes")
    Enumeration em = request.getHeaderNames();
    while (em.hasMoreElements()) {
        String name = (String) em.nextElement();
        String value = request.getHeader(name);
        mpInfo.put(name.toLowerCase(), value);
    }

    return mpInfo;
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest3Test.java

static String headers(final HttpServletRequest request) {
    final StringBuilder text = new StringBuilder();
    text.append("Headers: ");
    final Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        final String name = headerNames.nextElement();
        text.append(name);/*from   w w w  .j  a v a  2 s. c o m*/
        text.append('=');
        final Enumeration<String> headers = request.getHeaders(name);
        while (headers.hasMoreElements()) {
            final String header = headers.nextElement();
            text.append(header);
            text.append(',');
        }
        text.append(';');
    }
    return text.toString();
}

From source file:org.occiware.mart.servlet.utils.ServletUtils.java

/**
 * Get headers from http javax servlet request.
 *
 * @param request/*from   ww w.  j  av a 2s. c o m*/
 * @return
 */
public static HeaderPojo getRequestHeaders(HttpServletRequest request) {

    HeaderPojo headerPojo;
    Map<String, List<String>> map = new LinkedHashMap<>();

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        List<String> elements = new LinkedList<>();
        String key = (String) headerNames.nextElement();
        Enumeration<String> values = request.getHeaders(key);
        while (values.hasMoreElements()) {
            elements.add(values.nextElement());
        }
        map.put(key, elements);
    }
    headerPojo = new HeaderPojo(map);

    return headerPojo;
}