Example usage for javax.servlet.http HttpServletRequest getHeader

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

Introduction

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

Prototype

public String getHeader(String name);

Source Link

Document

Returns the value of the specified request header as a String.

Usage

From source file:cn.bc.web.util.WebUtils.java

public static String encodeFileName(HttpServletRequest request, String srcFileName) {
    boolean isIE = request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") != -1;
    return encodeFileName(isIE, srcFileName);
}

From source file:morph.plugin.views.annotation.ServletAnnotationMappingUtils.java

/**
 * Check whether the given request matches the specified header conditions.
 * @param headers the header conditions, following
 *                {@link org.springframework.web.bind.annotation.RequestMapping#headers() RequestMapping.headers()}
 * @param request the current HTTP request to check
 *///from   w  w w .j  a  va2 s . c  om
public static boolean checkHeaders(String[] headers, HttpServletRequest request) {
    if (!ObjectUtils.isEmpty(headers)) {
        for (String header : headers) {
            int separator = header.indexOf('=');
            if (separator == -1) {
                if (header.startsWith("!")) {
                    if (request.getHeader(header.substring(1)) != null) {
                        return false;
                    }
                } else if (request.getHeader(header) == null) {
                    return false;
                }
            } else {
                boolean negated = separator > 0 && header.charAt(separator - 1) == '!';
                String key = !negated ? header.substring(0, separator) : header.substring(0, separator - 1);
                String value = header.substring(separator + 1);
                if (isMediaTypeHeader(key)) {
                    List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(key));
                    List<MediaType> valueMediaTypes = MediaType.parseMediaTypes(value);
                    boolean found = false;
                    for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext()
                            && !found;) {
                        MediaType valueMediaType = valIter.next();
                        for (Iterator<MediaType> reqIter = requestMediaTypes.iterator(); reqIter.hasNext()
                                && !found;) {
                            MediaType requestMediaType = reqIter.next();
                            if (valueMediaType.includes(requestMediaType)) {
                                found = true;
                            }
                        }

                    }
                    if (!found) {
                        return negated;
                    }
                } else if (!value.equals(request.getHeader(key))) {
                    return negated;
                }
            }
        }
    }
    return true;
}

From source file:de.perdian.commons.servlet.ServletUtils.java

/**
 * Extracts the locales supported by the client that sent a given request.
 * According to RFC 2161 the result list will be sorted according to any
 * specified preferenc value (see the RFC for details)
 *
 * @param servletRequest//ww  w  .  ja  va2s.co m
 *     the request from which to extract the information
 * @param defaultLocales
 *     the locales to be returned if no locales could be extracted from the
 *     request
 * @return
 *     a {@code List} containing the accepted {@code java.util.Locale}
 *     objects. If no locales are found in the request, then the method will
 *     return the default locale list or an empty list if no default locales
 *     have been passed - never {@code null}
 */
public static List<Locale> extractAcceptedLocales(HttpServletRequest servletRequest,
        List<Locale> defaultLocales) {

    Map<Float, List<Locale>> localeValuesByPriority = new TreeMap<>((o1, o2) -> -1 * Float.compare(o1, o2));
    String headerValue = servletRequest.getHeader("accept-language");
    if (headerValue != null) {
        StringTokenizer headerTokenizer = new StringTokenizer(headerValue, ",");
        while (headerTokenizer.hasMoreElements()) {
            String header = headerTokenizer.nextToken().trim();
            int semicolonSeparator = header.indexOf(";");
            String localeValue = semicolonSeparator > 0 ? header.substring(0, semicolonSeparator) : header;
            Locale locale = LocaleUtils.toLocale(localeValue);
            if (locale != null) {
                Float qValue = Float.valueOf(1f);
                String qParameter = ";q=";
                int qParameterIndex = header.indexOf(qParameter);
                if (qParameterIndex > 0) {
                    try {
                        String qParameterValue = header.substring(qParameterIndex + qParameter.length());
                        qValue = Float.valueOf(qParameterValue);

                    } catch (Exception e) {
                        // Ignore here and use the default
                    }
                }
                List<Locale> targetList = localeValuesByPriority.get(qValue);
                if (targetList == null) {
                    targetList = new ArrayList<>();
                    localeValuesByPriority.put(qValue, targetList);
                }
                targetList.add(locale);
            }
        }
    }

    List<Locale> localeValues = new ArrayList<>();
    for (Map.Entry<Float, List<Locale>> localeValueEntry : localeValuesByPriority.entrySet()) {
        for (Locale locale : localeValueEntry.getValue()) {
            localeValues.add(locale);
        }
    }
    if (localeValues.isEmpty() && defaultLocales != null) {
        localeValues.addAll(defaultLocales);
    }
    return Collections.unmodifiableList(localeValues);

}

From source file:com.oneis.appserver.FileUploads.java

/**
 * Determine whether an HTTP request contains file uploads. If so, returns a
 * FileUploads object initialised for decoding the stream.
 *
 * @return FileUploads object to use to decode the streamed data.
 *//*w  w w  . j  a  v a  2s  . co  m*/
static public FileUploads createIfUploads(HttpServletRequest request) {
    String contentType = request.getHeader("Content-Type");
    if (contentType == null) {
        return null;
    }

    // Set up a parser for the various values
    ParameterParser paramParser = new ParameterParser();
    paramParser.setLowerCaseNames(true);

    // Decode content type
    Map contentTypeDecoded = paramParser.parse(contentType, PARAMPARSER_SEPERATORS);

    String boundaryStr = null;
    if (!contentTypeDecoded.containsKey("multipart/form-data")
            || (boundaryStr = (String) contentTypeDecoded.get("boundary")) == null) {
        // Wasn't a file upload
        return null;
    }

    byte[] boundary = null;
    try {
        boundary = boundaryStr.getBytes("ISO-8859-1");
    } catch (java.io.UnsupportedEncodingException e) {
        throw new RuntimeException("Expected charset ISO-8859-1 not installed");
    }
    if (boundaryStr == null || boundary.length < 2) {
        return null;
    }

    // Looks good... create an object to signal success
    return new FileUploads(boundary);
}

From source file:com.meltmedia.cadmium.servlets.FileServletTest.java

public static HttpServletRequest mockGetWithIfNoneMatch(String pathInfo, String eTag) {
    HttpServletRequest request = mockGet(pathInfo);
    when(request.getHeader(IF_NONE_MATCH_HEADER)).thenReturn(eTag);
    return request;
}

From source file:com.meltmedia.cadmium.servlets.FileServletTest.java

public static HttpServletRequest mockGetWithIfMatch(String pathInfo, String eTag) {
    HttpServletRequest request = mockGet(pathInfo);
    when(request.getHeader(IF_MATCH_HEADER)).thenReturn(eTag);
    return request;
}

From source file:com.meltmedia.cadmium.servlets.FileServletTest.java

public static HttpServletRequest mockGetWithRanges(String pathInfo, String eTag,
        final RangeSpec... rangeSpecs) {
    HttpServletRequest request = mockGet(pathInfo);
    when(request.getHeader(IF_RANGE_HEADER)).thenReturn(eTag);
    if (rangeSpecs.length > 0) {
        when(request.getHeader(RANGE_HEADER)).thenReturn(rangeHeader(rangeSpecs));
    }/*from w  w  w  .  jav a 2  s .co  m*/
    return request;
}

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

/**
 * ??/*from   w w w .j a  va  2s .  com*/
 * @param out
 * @param req
 */
public static void dumpHeaders(PrintStream out, HttpServletRequest req) {
    Enumeration names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        out.println(name + "=" + req.getHeader(name));
    }
}

From source file:com.icl.integrator.util.patch.ServletAnnotationMappingUtils.java

/**
 * Check whether the given request matches the specified header conditions.
 * @param headers the header conditions, following
 * {@link org.springframework.web.bind.annotation.RequestMapping#headers() RequestMapping.headers()}
 * @param request the current HTTP request to check
 *//*ww  w .j  a  v  a 2s.com*/
public static boolean checkHeaders(String[] headers, HttpServletRequest request) {
    if (!ObjectUtils.isEmpty(headers)) {
        for (String header : headers) {
            int separator = header.indexOf('=');
            if (separator == -1) {
                if (header.startsWith("!")) {
                    if (request.getHeader(header.substring(1)) != null) {
                        return false;
                    }
                } else if (request.getHeader(header) == null) {
                    return false;
                }
            } else {
                boolean negated = (separator > 0 && header.charAt(separator - 1) == '!');
                String key = !negated ? header.substring(0, separator) : header.substring(0, separator - 1);
                String value = header.substring(separator + 1);
                if (isMediaTypeHeader(key)) {
                    List<MediaType> requestMediaTypes = MediaType.parseMediaTypes(request.getHeader(key));
                    List<MediaType> valueMediaTypes = MediaType.parseMediaTypes(value);
                    boolean found = false;
                    for (Iterator<MediaType> valIter = valueMediaTypes.iterator(); valIter.hasNext()
                            && !found;) {
                        MediaType valueMediaType = valIter.next();
                        for (Iterator<MediaType> reqIter = requestMediaTypes.iterator(); reqIter.hasNext()
                                && !found;) {
                            MediaType requestMediaType = reqIter.next();
                            if (valueMediaType.includes(requestMediaType)) {
                                found = true;
                            }
                        }

                    }
                    if (negated) {
                        found = !found;
                    }
                    if (!found) {
                        return false;
                    }
                } else {
                    boolean match = value.equals(request.getHeader(key));
                    if (negated) {
                        match = !match;
                    }
                    if (!match) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

From source file:com.evolveum.midpoint.security.api.SecurityUtil.java

private static String getRemoteHostAddress(HttpServletRequest request) {
    for (String headerName : remoteHostAddressHeaders) {
        String header = request.getHeader(headerName);
        if (header != null) {
            return getAddressFromHeader(headerName, header);
        }//from w  ww . ja v a  2  s. c  om
    }
    return request.getRemoteAddr();
}