Example usage for javax.servlet.http HttpServletResponse setDateHeader

List of usage examples for javax.servlet.http HttpServletResponse setDateHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setDateHeader.

Prototype

public void setDateHeader(String name, long date);

Source Link

Document

Sets a response header with the given name and date-value.

Usage

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Adds Expires, ETag and Cache-Control response headers.
 *
 * @param response the HTTP servlet response
 * @param etag the calculated etag (md5) of the response
 * @param month number of months the response can be cached. Added to the Expires and
 * Cache-Control header. If null defaults to 6 months.
 *///from w w  w .  j a v  a 2  s  .  c o m
public static void addCacheHeaders(HttpServletResponse response, String etag, Integer month) {
    Assert.notNull(etag, "ETag must not be null");

    long seconds;
    if (month != null) {
        seconds = month * secondsInAMonth;
    } else {
        seconds = 6L * secondsInAMonth;
    }

    response.setDateHeader("Expires", System.currentTimeMillis() + seconds * 1000L);
    response.setHeader("ETag", etag);
    response.setHeader("Cache-Control", "public, max-age=" + seconds);
}

From source file:com.lushapp.common.web.utils.WebUtils.java

/**
 * Header.//from  w ww.  j av  a 2 s .  com
 */
public static void setNoCacheHeader(HttpServletResponse response) {
    //Http 1.0 header
    response.setDateHeader("Expires", 1L);
    response.addHeader("Pragma", "no-cache");
    //Http 1.1 header
    response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
}

From source file:org.nuxeo.theme.html.Utils.java

public static void setCacheHeaders(final HttpServletResponse response, final CachingDef caching) {
    if (caching != null) {
        final String lifetime = caching.getLifetime();
        if (lifetime != null) {
            final long now = System.currentTimeMillis();
            response.addHeader("Cache-Control", "max-age=" + lifetime);
            response.addHeader("Cache-Control", "must-revalidate");
            response.setDateHeader("Last-Modified", now);
            response.setDateHeader("Expires", now + new Long(lifetime) * 1000L);
        }/*from  w w w  . ja v  a 2s .co m*/
    }
}

From source file:org.openlegacy.tools.maven.SessionRunner.java

private static void handleJsFiles(HttpServletRequest request, HttpServletResponse response, String uri)
        throws IOException {
    PathMatchingResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
    Resource resource = pathResolver.getResource(uri);
    if (resource == null) {
        throw (new RuntimeException("Unable to find resource:" + uri));
    }/*from w w  w.  j  av  a 2  s.  c  o m*/
    response.setDateHeader(HEADER_EXPIRES, Long.MAX_VALUE);
    IOUtils.copy(resource.getInputStream(), response.getOutputStream());
    response.setStatus(HttpServletResponse.SC_OK);
    ((Request) request).setHandled(true);
}

From source file:com.igrow.mall.common.util.Struts2Utils.java

/**
 * ./*from w w  w .  j  a va2s  . c  om*/
        
 * eg.
 * render("text/plain", "hello", "encoding:GBK");
 * render("text/plain", "hello", "no-cache:false");
 * render("text/plain", "hello", "encoding:GBK", "no-cache:false");
 * 
 * @param headers ??header??"encoding:""no-cache:",UTF-8true.
 */
public static void render(final String contentType, final String content, final String... headers) {
    try {
        //?headers?
        String encoding = ENCODING_DEFAULT;
        boolean noCache = NOCACHE_DEFAULT;
        for (String header : headers) {
            String headerName = StringUtils.substringBefore(header, ":");
            String headerValue = StringUtils.substringAfter(header, ":");

            if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                encoding = headerValue;
            } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                noCache = Boolean.parseBoolean(headerValue);
            } else
                throw new IllegalArgumentException(headerName + "??header");
        }

        HttpServletResponse response = ServletActionContext.getResponse();

        //headers?
        String fullContentType = contentType + ";charset=" + encoding;
        response.setContentType(fullContentType);
        if (noCache) {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
        }

        response.getWriter().write(content);
        response.getWriter().flush();

    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:org.apdplat.platform.util.Struts2Utils.java

public static void render(final String contentType, final String content, final String... headers) {
    try {//from  w ww .j  a  v  a2 s  .c o  m
        //?headers?
        String encoding = ENCODING_DEFAULT;
        boolean noCache = NOCACHE_DEFAULT;
        for (String header : headers) {
            String headerName = StringUtils.substringBefore(header, ":");
            String headerValue = StringUtils.substringAfter(header, ":");

            if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                encoding = headerValue;
            } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                noCache = Boolean.parseBoolean(headerValue);
            } else {
                throw new IllegalArgumentException(headerName + "??header");
            }
        }

        HttpServletResponse response = ServletActionContext.getResponse();

        //headers?
        String fullContentType = contentType + ";charset=" + encoding;
        response.setContentType(fullContentType);
        if (noCache) {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
        }

        response.getWriter().write(content);
        response.getWriter().flush();

    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:org.apache.solr.servlet.cache.HttpCacheHeaderUtil.java

/**
 * Sets HTTP Response cache validator headers appropriately and
 * validates the HTTP Request against these using any conditional
 * request headers./*  ww  w  .  j av  a2  s.  c om*/
 *
 * If the request contains conditional headers, and those headers
 * indicate a match with the current known state of the system, this
 * method will return "true" indicating that a 304 Status code can be
 * returned, and no further processing is needed.
 *
 * 
 * @return true if the request contains conditional headers, and those
 *         headers indicate a match with the current known state of the
 *         system -- indicating that a 304 Status code can be returned to
 *         the client, and no further request processing is needed.  
 */
public static boolean doCacheHeaderValidation(final SolrQueryRequest solrReq, final HttpServletRequest req,
        final Method reqMethod, final HttpServletResponse resp) throws IOException {

    if (Method.POST == reqMethod || Method.OTHER == reqMethod) {
        return false;
    }

    final long lastMod = HttpCacheHeaderUtil.calcLastModified(solrReq);
    final String etag = HttpCacheHeaderUtil.calcEtag(solrReq);

    resp.setDateHeader("Last-Modified", lastMod);
    resp.setHeader("ETag", etag);

    if (checkETagValidators(req, resp, reqMethod, etag)) {
        return true;
    }

    if (checkLastModValidators(req, resp, lastMod)) {
        return true;
    }

    return false;
}

From source file:org.sakaiproject.evaluation.tool.utils.RenderingUtils.java

/**
 * Set the no-cache headers for this response
 * @param res the servlet response/*w  ww.j  a  v  a  2s.  com*/
 */
public static void setNoCacheHeaders(HttpServletResponse res) {
    long currentTime = System.currentTimeMillis();
    res.setDateHeader(Header.DATE.toString(), currentTime);
    res.setDateHeader(Header.EXPIRES.toString(), currentTime + 1000);

    res.setHeader(Header.CACHE_CONTROL.toString(), "no-cache");
    res.addHeader(Header.CACHE_CONTROL.toString(), "no-store");
    res.addHeader(Header.CACHE_CONTROL.toString(), "max-age=0");
    res.addHeader(Header.CACHE_CONTROL.toString(), "must-revalidate");
    res.addHeader(Header.CACHE_CONTROL.toString(), "private");
    res.addHeader(Header.CACHE_CONTROL.toString(), "s-maxage=0");
}

From source file:com.adnature.framework.util.Struts2Utils.java

/**
 * .//from w w w .ja  v a2  s  .c o  m
 * 
 * eg. <br/>
 * render("text/plain", "hello", "encoding:GBK"); <br/>
 * render("text/plain", "hello", "no-cache:false"); <br/>
 * render("text/plain", "hello", "encoding:GBK", "no-cache:false");
 * 
 * @param headers
 *            ??header??"encoding:""no-cache:",UTF-8true.
 */
public static void render(final String contentType, final String content, final String... headers) {
    try {
        // ?headers?
        String encoding = ENCODING_DEFAULT;
        boolean noCache = NOCACHE_DEFAULT;
        for (String header : headers) {
            String headerName = StringUtils.substringBefore(header, ":");
            String headerValue = StringUtils.substringAfter(header, ":");

            if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                encoding = headerValue;
            } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                noCache = Boolean.parseBoolean(headerValue);
            } else {
                throw new IllegalArgumentException(headerName + "??header");
            }
        }

        HttpServletResponse response = ServletActionContext.getResponse();

        // headers?
        String fullContentType = contentType + ";charset=" + encoding;
        response.setContentType(fullContentType);
        if (noCache) {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
        }

        response.getWriter().write(content);
        response.getWriter().flush();

    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:cn.guoyukun.spring.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String displayName,
        byte[] bytes) throws IOException {

    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;//from w w w. j  a v a  2  s . c  o  m
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) bytes.length);

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new ByteArrayInputStream(bytes));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}