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:oeg.licensius.service.ServiceCommons.java

/**
 * Determines whether a HTTP Servlet Request is demanding Turtle
 *
 * @param request HTTP request/* w w  w.j  a va2 s  .  c om*/
 * @retur true if demanding turtle
 */
public static boolean isJSON(HttpServletRequest request) {
    String uri = request.getRequestURI();
    Enumeration enume = request.getHeaderNames();
    while (enume.hasMoreElements()) {
        String hname = (String) enume.nextElement();
        Enumeration<String> enum2 = request.getHeaders(hname);
        while (enum2.hasMoreElements()) {
            String valor = enum2.nextElement();
            if (hname.equals("Accept")) {
                if (valor.contains("application/json")) {
                    return true;
                }
            }
        }
    }
    return false;

}

From source file:gov.nih.nci.cabig.ctms.web.WebTools.java

@SuppressWarnings({ "unchecked" })
public static SortedMap<String, String[]> headersToMap(final HttpServletRequest request) {
    return namedAttributesToMap(request.getHeaderNames(), new AttributeAccessor<String[]>() {
        @SuppressWarnings({ "unchecked" })
        public String[] getAttribute(String headerName) {
            Enumeration<String> values = request.getHeaders(headerName);
            List<String> valList = new ArrayList<String>();
            while (values.hasMoreElements()) {
                valList.add(values.nextElement());
            }/*  w w w  . ja  v  a  2  s .  c om*/
            return valList.toArray(new String[valList.size()]);
        }
    });
}

From source file:org.ambraproject.wombat.controller.FeedbackController.java

private static Map<String, String> getUserSessionAttributes(HttpServletRequest request) {
    Map<String, String> headers = new LinkedHashMap<>();

    for (String headerName : Collections.list(request.getHeaderNames())) {
        List<String> headerValues = Collections.list(request.getHeaders(headerName));
        headers.put(headerName, headerValues.stream().collect(joinWithComma()));
    }/*from  w w w . j  a  v a2  s.  co m*/

    headers.put("server-name", request.getServerName() + ":" + request.getServerPort());
    headers.put("remote-addr", request.getRemoteAddr());
    headers.put("local-addr", request.getLocalAddr() + ":" + request.getLocalPort());

    /*
     * Keeping this in case more values get passed from the client other than just the visible form
     * fields
     */
    for (String paramName : Collections.list(request.getParameterNames())) {
        String[] paramValues = request.getParameterValues(paramName);
        headers.put(paramName, Stream.of(paramValues).collect(joinWithComma()));
    }

    return headers;
}

From source file:org.opendatakit.odktables.util.ServiceUtils.java

@SuppressWarnings({ "rawtypes", "unused" })
public static void examineRequest(ServletContext sc, HttpServletRequest req) {
    Log logger = LogFactory.getLog(ServiceUtils.class);

    Enumeration headers = req.getHeaderNames();
    StringBuilder b = new StringBuilder();
    while (headers.hasMoreElements()) {
        String headerName = (String) headers.nextElement();
        Enumeration fieldValues = req.getHeaders(headerName);
        while (fieldValues.hasMoreElements()) {
            String fieldValue = (String) fieldValues.nextElement();
            b.append(headerName).append(": ").append(fieldValue).append("\n");
        }/*from  www  . jav  a2s. c om*/
    }

    String contentType = req.getContentType();
    logger.info("Content type: " + contentType);

    String charEncoding = req.getCharacterEncoding();
    logger.info("Character encoding: " + charEncoding);

    String headerSet = b.toString();
    logger.info("Headers: " + headerSet);

    Cookie[] cookies = req.getCookies();
    logger.info("Cookies: " + cookies);

    String method = req.getMethod();
    logger.info("Method: " + method);

    String ctxtPath = req.getContextPath();
    logger.info("Context Path: " + ctxtPath);

    String pathInfo = req.getPathInfo();
    logger.info("Path Info: " + pathInfo);

    String query = req.getQueryString();
    logger.info("Query String: " + query);

    String ace = req.getHeader(ApiConstants.ACCEPT_CONTENT_ENCODING_HEADER);
    boolean sessionId = req.isRequestedSessionIdValid();
}

From source file:com.pushinginertia.commons.net.util.HttpServletRequestUtils.java

/**
 * Converts all of the headers in a request to a newline-separated string of name-value pairs. This is required
 * because not all implementations of {@link javax.servlet.http.HttpServletRequest} implement the toString method.
 * @param req request received from the user agent
 * @return null if req is null// w w  w . ja va2s .c  om
 */
public static String toString(final HttpServletRequest req) {
    ValidateAs.notNull(req, "req");

    final StringBuilder sb = new StringBuilder();
    for (Enumeration<String> headerNames = req.getHeaderNames(); headerNames.hasMoreElements();) {
        final String headerName = headerNames.nextElement();
        for (Enumeration<String> headers = req.getHeaders(headerName); headers.hasMoreElements();) {
            final String value = headers.nextElement();
            sb.append(headerName).append('=').append(value).append('\n');
        }
    }
    return sb.toString();
}

From source file:org.nuxeo.ecm.core.opencmis.impl.server.NuxeoContentStream.java

public static boolean hasWantDigestRequestHeader(HttpServletRequest request, String digestAlgorithm) {
    if (request == null || digestAlgorithm == null) {
        return false;
    }/*from  w  w w . j av  a2  s.  c  o  m*/
    Enumeration<String> values = request.getHeaders(WANT_DIGEST_HEADER_NAME);
    if (values == null) {
        return false;
    }
    Iterator<String> it = Iterators.forEnumeration(values);
    while (it.hasNext()) {
        String value = it.next();
        int semicolon = value.indexOf(';');
        if (semicolon >= 0) {
            value = value.substring(0, semicolon);
        }
        if (value.equalsIgnoreCase(digestAlgorithm)) {
            return true;
        }
    }
    return false;
}

From source file:fr.paris.lutece.util.http.SecurityUtil.java

/**
 * Write request headers infos into the dump stringbuffer
 * @param sb The dump stringbuffer/*from w  w  w .  j  av a2  s . c o  m*/
 * @param request The HTTP request
 */
private static void dumpHeaders(StringBuffer sb, HttpServletRequest request) {
    Enumeration<String> values;
    String key;
    Enumeration<String> headers = request.getHeaderNames();

    while (headers.hasMoreElements()) {
        key = headers.nextElement();
        values = request.getHeaders(key);

        while (values.hasMoreElements()) {
            dumpVariable(sb, key, values.nextElement());
        }
    }
}

From source file:org.springframework.http.server.reactive.ServletServerHttpRequest.java

private static HttpHeaders initHeaders(HttpServletRequest request) {
    HttpHeaders headers = new HttpHeaders();
    for (Enumeration<?> names = request.getHeaderNames(); names.hasMoreElements();) {
        String name = (String) names.nextElement();
        for (Enumeration<?> values = request.getHeaders(name); values.hasMoreElements();) {
            headers.add(name, (String) values.nextElement());
        }//from w  ww  .jav  a 2 s  . c  o m
    }
    MediaType contentType = headers.getContentType();
    if (contentType == null) {
        String requestContentType = request.getContentType();
        if (StringUtils.hasLength(requestContentType)) {
            contentType = MediaType.parseMediaType(requestContentType);
            headers.setContentType(contentType);
        }
    }
    if (contentType != null && contentType.getCharset() == null) {
        String encoding = request.getCharacterEncoding();
        if (StringUtils.hasLength(encoding)) {
            Charset charset = Charset.forName(encoding);
            Map<String, String> params = new LinkedCaseInsensitiveMap<>();
            params.putAll(contentType.getParameters());
            params.put("charset", charset.toString());
            headers.setContentType(new MediaType(contentType.getType(), contentType.getSubtype(), params));
        }
    }
    if (headers.getContentLength() == -1) {
        int contentLength = request.getContentLength();
        if (contentLength != -1) {
            headers.setContentLength(contentLength);
        }
    }
    return headers;
}

From source file:net.yacy.http.ProxyHandler.java

/**
 * Convert ServletRequest header to modifiable YaCy RequestHeader
 *
 * @param request ServletRequest//from   w  w w  . java  2s. c  o m
 * @return RequestHeader created from ServletRequest
 */
public static RequestHeader convertHeaderFromJetty(HttpServletRequest request) {
    RequestHeader result = new RequestHeader();
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        Enumeration<String> headers = request.getHeaders(headerName);
        while (headers.hasMoreElements()) {
            String header = headers.nextElement();
            result.add(headerName, header);
        }
    }
    return result;
}

From source file:org.opendatakit.aggregate.odktables.impl.api.wink.ContentEncodingResponseFilter.java

/**
 * Returns an AcceptEncoding object if there is an Accept Encoding header.
 * /*from   www .j a  v  a2  s .  c  o  m*/
 * @param httpServletRequest
 * @return
 */
static AcceptEncoding getAcceptEncodingHeader(HttpServletRequest httpServletRequest) {
    logger.trace("getAcceptEncodingHeader({}) entry", httpServletRequest); //$NON-NLS-1$
    Enumeration<String> acceptEncodingEnum = httpServletRequest.getHeaders(HttpHeaders.ACCEPT_ENCODING);
    StringBuilder sb = new StringBuilder();
    if (acceptEncodingEnum.hasMoreElements()) {
        sb.append(acceptEncodingEnum.nextElement());
        while (acceptEncodingEnum.hasMoreElements()) {
            sb.append(","); //$NON-NLS-1$
            sb.append(acceptEncodingEnum.nextElement());
        }
        String acceptEncodingHeader = sb.toString();
        logger.trace("acceptEncodingHeader is {} so returning as AcceptEncodingHeader", //$NON-NLS-1$
                acceptEncodingHeader);
        return acceptEncodingHeaderDelegate.fromString(acceptEncodingHeader);
    }
    logger.trace("No Accept-Encoding header"); //$NON-NLS-1$
    logger.trace("getAcceptEncodingHeader() exit - returning null"); //$NON-NLS-1$
    return null;
}