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:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Checks etag and sends back HTTP status 304 if not modified. If modified sets
 * content type and content length, adds cache headers (
 * {@link #addCacheHeaders(HttpServletResponse, String, Integer)}), writes the data
 * into the {@link HttpServletResponse#getOutputStream()} and flushes it.
 *
 * @param request the HTTP servlet request
 * @param response the HTTP servlet response
 * @param data the response data//from   ww  w  .  j ava 2  s. com
 * @param contentType the content type of the data (i.e.
 * "application/javascript;charset=UTF-8")
 * @throws IOException
 */
public static void handleCacheableResponse(HttpServletRequest request, HttpServletResponse response,
        byte[] data, String contentType) throws IOException {
    String ifNoneMatch = request.getHeader("If-None-Match");
    String etag = "\"0" + DigestUtils.md5DigestAsHex(data) + "\"";

    if (etag.equals(ifNoneMatch)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }

    response.setContentType(contentType);
    response.setContentLength(data.length);

    addCacheHeaders(response, etag, 6);

    @SuppressWarnings("resource")
    ServletOutputStream out = response.getOutputStream();
    out.write(data);
    out.flush();
}

From source file:BrowserDetect.java

/**
 * Check that the user-agent string indicates some minimum browser level
 * @param request The browsers request//from  w w w. j a va2s.c o m
 * @param requiredUserAgent The UA required. Currently this is major version only
 * @param requiredVersion The version required, or -1 if versions are not important
 * @return true iff the browser matches the spec.
 */
public static boolean atLeast(HttpServletRequest request, UserAgent requiredUserAgent, int requiredVersion) {
    String userAgent = request.getHeader("user-agent");
    int realVersion;

    switch (requiredUserAgent) {
    case IE:
        realVersion = getMajorVersionAssumingIE(userAgent);
        break;

    case Gecko:
        realVersion = getMajorVersionAssumingGecko(userAgent);
        break;

    case Opera:
        realVersion = getMajorVersionAssumingOpera(userAgent);
        break;

    case AppleWebKit:
        realVersion = getMajorVersionAssumingAppleWebKit(userAgent);
        break;

    default:
        throw new UnsupportedOperationException("Detection of " + requiredUserAgent + " is not supported yet.");
    }

    return realVersion >= requiredVersion;
}

From source file:com.ziduye.base.web.Servlets.java

/**
 * ?Ajax/*from   w  w  w  .jav  a  2  s. c o m*/
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {
    String accept = request.getHeader("accept");
    String xRequestedWith = request.getHeader("X-Requested-With");

    // ?
    return ((accept != null && accept.indexOf("application/json") != -1
            || (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)));
}

From source file:com.openkm.util.FormatUtil.java

/**
 * Detect if the current browser is a mobile one
 *///w  w w . j a va  2  s.  c  o m
public static final boolean isMobile(HttpServletRequest request) {
    String userAgent = request.getHeader("user-agent").toLowerCase();
    return userAgent.contains("android") || userAgent.contains("iphone") || userAgent.contains("ipad")
            || userAgent.contains("blackberry");
}

From source file:com.emaxcore.emaxdata.common.web.Servlets.java

/**
 * ?? If-None-Match Header, Etag?.//from w w  w  . jav a 2  s .com
 *
 * Etag, checkIfNoneMatchfalse, 304 not modify status.
 *
 * @param etag ETag.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {
    String headerValue = request.getHeader(HttpHeaders.IF_NONE_MATCH);
    if (headerValue != null) {
        boolean conditionSatisfied = false;
        if (!"*".equals(headerValue)) {
            StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");
            while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                String currentToken = commaTokenizer.nextToken();
                if (currentToken.trim().equals(etag)) {
                    conditionSatisfied = true;
                }
            }
        } else {
            conditionSatisfied = true;
        }

        if (conditionSatisfied) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.setHeader(HttpHeaders.ETAG, etag);
            return false;
        }
    }
    return true;
}

From source file:com.hbs.common.josn.JSONUtil.java

static boolean isGzipInRequest(HttpServletRequest request) {
    String header = request.getHeader("Accept-Encoding");
    return header != null && header.indexOf("gzip") >= 0;
}

From source file:com.asual.summer.core.RequestFilter.java

static String getHeader(HttpServletRequest request, String name) {
    if ("Accept".equals(name) && RequestUtils.isWebKit()) {
        return "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    }//  w w  w .j a  v  a 2s. c om
    return request.getHeader(name);
}

From source file:jp.go.nict.langrid.servicesupervisor.ServiceSupervisor.java

private static String getProtocol(HttpServletRequest request) {
    String p = request.getHeader("X-Langrid-Protocol");
    if (p != null)
        return p;
    else/*w  w w . j  a v a2  s .com*/
        return Protocols.DEFAULT;
}

From source file:org.artifactory.webapp.servlet.RequestUtils.java

public static boolean isAuthHeaderPresent(HttpServletRequest request) {
    String header = request.getHeader("Authorization");
    if (header != null && header.startsWith("Basic ")) {
        String auth = header.substring(6);
        return !"Og==".equals(auth);
    }// www.  j a v  a 2 s  .  c o m

    return false;
}

From source file:com.ziduye.base.web.Servlets.java

/**
 * ?//  w ww  .  j a  v  a2  s  .c om
 * @param request
 * @param uri
 * @return
 */
public static boolean isPhoneRequest(HttpServletRequest request, String uri) {
    String userAgent = request.getHeader("USER-AGENT").toLowerCase();
    if (null == userAgent) {
        userAgent = "";
    }
    Matcher matcherPhone = phonePat.matcher(userAgent);
    if (matcherPhone.find()) {
        return true;
    } else {
        return false;
    }
}