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:com.dosport.system.utils.ServletUtils.java

/**
 * ?? If-None-Match Header, Etag?./*from   w ww  . ja v  a 2s . co  m*/
 * 
 * Etag, checkIfNoneMatchfalse, 304 not modify status.
 * 
 * @param etag
 *            ETag.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {
    String headerValue = request.getHeader("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("ETag", etag);
            return false;
        }
    }
    return true;
}

From source file:com.core.util.Servlets.java

/**
 * ?Ajax//from  w w  w .  j a  va  2  s.  co m
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {

    String accept = request.getHeader("accept");
    String xRequestedWith = request.getHeader("X-Requested-With");
    Principal principal = (Principal) SecurityUtils.getSubject().getPrincipal();
    // ?
    return ((accept != null && accept.indexOf("application/json") != -1
            || (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
            || (principal != null)));
}

From source file:net.duckling.ddl.web.bean.NginxAgent.java

/**
 * X-Accel-Redirectnignx/*from   ww w .jav a  2 s.c o  m*/
 * @param req
 * @param resp
 * @param fileName
 * @param fileSize
 * @param url
 */
public static void setRedirectUrl(HttpServletRequest req, HttpServletResponse resp, String fileName,
        long fileSize, String url) {
    if (fileSize > 0) {
        resp.addHeader(X_ACCEL_REDIRECT, url + "?agent="
                + Browser.recognizeBrowser(req.getHeader("USER-AGENT")).toString().toLowerCase());
    }

    // x_accel_redirect to nginx-clbs (gridfs) that
    // doesn't include Content-Disposition.
    // Set it here and the headers will be combined.
    resp.setHeader("Content-Disposition", Browser.encodeFileName(req.getHeader("USER-AGENT"), fileName));

}

From source file:org.ambraproject.wombat.util.HttpMessageUtil.java

/**
 * Checks to see if we should serve the contents of the requested object, or just return a 304 response with no body,
 * based on cache-related request headers.
 *
 * @param request      HttpServletRequest we will check for cache headers
 * @param lastModified last modified timestamp of the actual resource on the server
 * @param etag         etag generated from the actual resource on the server
 * @return true if we should serve the bytes of the resource, false if we should return 304.
 *///ww  w.j a  va  2 s.c o m
public static boolean checkIfModifiedSince(HttpServletRequest request, Long lastModified, String etag) {

    // Let the Etag-based header take precedence over If-Modified-Since.
    String etagFromRequest = request.getHeader("If-None-Match");
    if (etag != null && etagFromRequest != null) {
        return !etagFromRequest.equals(etag);
    } else {
        return lastModified == null || lastModified > request.getDateHeader("If-Modified-Since");
    }
}

From source file:com.fsnip.jg.common.web.Servlets.java

/**
 * ?Ajax/*from w ww . ja v 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 false;
    //      Principal principal = UserUtils.getPrincipal();
    //
    //      // ?
    //      return ((accept != null && accept.indexOf("application/json") != -1
    //         || (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
    //         || (principal != null && principal.isMobileLogin())));
}

From source file:com.wk.common.web.Servlets.java

/**
 * ?Ajax/*from w  w w .j  a  va 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");
    //      Principal principal = UserUtils.getPrincipal();

    // ?

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

From source file:de.mpg.escidoc.services.fledgeddata.webservice.oaiServlet.java

/**
 * Get a response Writer depending on acceptable encodings
 * @param request the servlet's request information
 * @param response the servlet's response information
 * @exception IOException an I/O error occurred
 *///from   w  w w . j a va  2s . c om
public static Writer getWriter(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Writer out;
    String encodings = request.getHeader("Accept-Encoding");

    if (encodings != null && encodings.indexOf("gzip") != -1) {
        response.setHeader("Content-Encoding", "gzip");
        out = new OutputStreamWriter(new GZIPOutputStream(response.getOutputStream()), "UTF-8");
    } else if (encodings != null && encodings.indexOf("deflate") != -1) {
        response.setHeader("Content-Encoding", "deflate");
        out = new OutputStreamWriter(new DeflaterOutputStream(response.getOutputStream()), "UTF-8");
    } else {
        out = response.getWriter();
    }
    return out;
}

From source file:com.mt86.jweb.common.web.Servlets.java

/**
 * ?Ajax/*from w  w w .  j  a  v  a2s . c o  m*/
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {

    String accept = request.getHeader("accept");
    String xRequestedWith = request.getHeader("X-Requested-With");
    Principal principal = UserUtils.getPrincipal();

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

From source file:club.crazypenguin.common.web.Servlets.java

/**
 * ?Ajax/*from   w  w w .j av a2  s.co  m*/
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {

    String accept = request.getHeader("accept");
    String xRequestedWith = request.getHeader("X-Requested-With");
    SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();

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

From source file:com.handpay.ibenefit.framework.util.WebUtils.java

public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {/*from   www  .  ja  va 2  s.co  m*/
    String headerValue = request.getHeader("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("ETag", etag);
            return false;
        }
    }
    return true;
}