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.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/*from w w w . j  a  va 2s . 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";
        }/*from  www.  j  a v a 2  s. co m*/
    }
    heads += "}";
    return "HttpRequest [method=" + request.getMethod() + ", URI=" + request.getRequestURI()
            + ", representation=" + content + ", queryString=" + request.getQueryString() + ", headers :\n"
            + heads + "]";
}

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  ww  . j  a va2s  .  c om
        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:framework.GlobalHelpers.java

public static List<String> headers(String name) {
    // TODO Move this code somewhere else
    HttpServletRequest req = req().getRequest();
    return Collections.list(req.getHeaders(name));
}

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

/**
 * Get headers from http javax servlet request.
 *
 * @param request//from w  w w.  j a  va2 s . c  om
 * @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;
}

From source file:org.openlaszlo.utils.LZHttpUtils.java

/** Add request headers into method. 
 *
 * @param req http servlet request object
 * @param method method to insert request headers into 
 *//*  w ww.ja v a 2s.co m*/
static public void proxyRequestHeaders(HttpServletRequest req, HttpMethodBase method) {
    mLogger.debug("proxyRequestHeaders");

    // Copy all headers, if the servlet container allows, otherwise just
    // copy the cookie header and log a message.
    Enumeration<?> headerNames = req.getHeaderNames();
    if (headerNames != null) {

        // Connection header tokens not to forward
        Enumeration<?> connEnum = req.getHeaders(CONNECTION);

        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            if (allowForward(key, connEnum)) {
                String val = (String) req.getHeader(key);
                method.addRequestHeader(key, val);
                mLogger.debug("  " + key + "=" + val);
            }
        }

    } else {
        mLogger.warn(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="Can't get header names to proxy request headers"
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(LZHttpUtils.class.getName(), "051018-237"));

        Enumeration<?> cookieEnum = req.getHeaders(COOKIE);
        if (cookieEnum != null) {
            while (cookieEnum.hasMoreElements()) {
                String val = (String) cookieEnum.nextElement();
                method.addRequestHeader(COOKIE, val);
                mLogger.debug("  Cookie=" + val);
            }
        }

        Enumeration<?> authEnum = req.getHeaders(AUTHORIZATION);
        if (authEnum != null) {
            while (authEnum.hasMoreElements()) {
                String val = (String) authEnum.nextElement();
                method.addRequestHeader(AUTHORIZATION, val);
                mLogger.debug("  Authorization=" + val);
            }
        }
    }
}

From source file:com.indeed.imhotep.web.QueryServlet.java

private static String getForwardedForIPAddress(final HttpServletRequest req,
        final String forwardForHeaderName) {
    final Enumeration headers = req.getHeaders(forwardForHeaderName);
    String value = null;//  w  w w .  j  a  v a2 s  .  com
    while (headers.hasMoreElements()) {
        value = (String) headers.nextElement();
    }
    return value;
}

From source file:edu.usu.sdl.openstorefront.security.HeaderRealm.java

public static boolean handleHeaderLogin(HttpServletRequest request) {
    boolean loginSuccessful = false;

    final String STUB_HEADER = "X_STUBHEADER_X";
    if (isUsingHeaderRealm()) {
        HeaderAuthToken headerAuthToken = new HeaderAuthToken();
        headerAuthToken.setRequest(request);
        headerAuthToken//from w ww .  j a v a 2 s .c  om
                .setAdminGroupName(PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_ADMIN_GROUP));
        headerAuthToken.setEmail(request
                .getHeader(PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_EMAIL, STUB_HEADER)));
        headerAuthToken.setFirstname(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_FIRSTNAME, STUB_HEADER)));

        Enumeration<String> groupValues = request
                .getHeaders(PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_GROUP, STUB_HEADER));
        StringBuilder group = new StringBuilder();
        while (groupValues.hasMoreElements()) {
            group.append(groupValues.nextElement());
            group.append(" | ");
        }

        headerAuthToken.setGroup(group.toString());
        headerAuthToken.setGuid(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_LDAPGUID, STUB_HEADER)));
        headerAuthToken.setLastname(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_LASTNAME, STUB_HEADER)));
        headerAuthToken.setOrganization(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_ORGANIZATION, STUB_HEADER)));
        headerAuthToken.setUsername(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_USERNAME, STUB_HEADER)));

        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.login(headerAuthToken);
            loginSuccessful = true;
        } catch (Exception ex) {
            log.log(Level.WARNING, "Login failed on header; Check configuration, if needed", ex);
        }
    }

    return loginSuccessful;
}

From source file:ca.uhn.fhir.rest.server.RestfulServerUtils.java

public static Set<String> parseAcceptHeaderAndReturnHighestRankedOptions(HttpServletRequest theRequest) {
    Set<String> retVal = new HashSet<String>();

    Enumeration<String> acceptValues = theRequest.getHeaders(Constants.HEADER_ACCEPT);
    if (acceptValues != null) {
        float bestQ = -1f;
        while (acceptValues.hasMoreElements()) {
            String nextAcceptHeaderValue = acceptValues.nextElement();
            Matcher m = ACCEPT_HEADER_PATTERN.matcher(nextAcceptHeaderValue);
            float q = 1.0f;
            while (m.find()) {
                String contentTypeGroup = m.group(1);
                if (isNotBlank(contentTypeGroup)) {

                    String name = m.group(3);
                    String value = m.group(4);
                    if (name != null && value != null) {
                        if ("q".equals(name)) {
                            try {
                                q = Float.parseFloat(value);
                                q = Math.max(q, 0.0f);
                            } catch (NumberFormatException e) {
                                ourLog.debug("Invalid Accept header q value: {}", value);
                            }//from  ww w.  j av  a 2  s  . co  m
                        }
                    }

                    if (q > bestQ) {
                        retVal.clear();
                        bestQ = q;
                    }

                    if (q == bestQ) {
                        retVal.add(contentTypeGroup.trim());
                    }

                }

                if (!",".equals(m.group(5))) {
                    break;
                }
            }

        }
    }

    return retVal;
}

From source file:flex.messaging.services.http.proxy.RequestFilter.java

/**
 * Copy HTTP request headers to the endpoint.
 * @param context the context//  w  w  w .ja  v a  2  s  . co m
 */
public static void copyHeadersToEndpoint(ProxyContext context) {
    HttpServletRequest clientRequest = FlexContext.getHttpRequest();
    if (clientRequest != null) {
        Enumeration headerNames = clientRequest.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = (String) headerNames.nextElement();
            if (RequestUtil.ignoreHeader(headerName, context)) {
                continue;
            }

            Enumeration headers = clientRequest.getHeaders(headerName);
            while (headers.hasMoreElements()) {
                String value = (String) headers.nextElement();
                context.getHttpMethod().addRequestHeader(headerName, value);

                if (Log.isInfo()) {
                    Log.getLogger(HTTPProxyService.LOG_CATEGORY)
                            .debug("-- Header in request: " + headerName + " : " + value);
                }
            }
        }
    }
}