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:org.apache.solr.servlet.cache.HttpCacheHeaderUtil.java

/**
 * Set the Cache-Control HTTP header (and Expires if needed)
 * based on the SolrConfig./*w ww. j av a  2 s. c  om*/
 * @param conf The config of the SolrCore handling this request
 * @param resp The servlet response object to modify
 * @param method The request method (GET, POST, ...) used by this request
 */
public static void setCacheControlHeader(final SolrConfig conf, final HttpServletResponse resp,
        final Method method) {
    // We do not emit HTTP header for POST and OTHER request types
    if (Method.POST == method || Method.OTHER == method) {
        return;
    }
    final String cc = conf.getHttpCachingConfig().getCacheControlHeader();
    if (null != cc) {
        resp.setHeader("Cache-Control", cc);
    }
    Long maxAge = conf.getHttpCachingConfig().getMaxAge();
    if (null != maxAge) {
        resp.setDateHeader("Expires", System.currentTimeMillis() + (maxAge * 1000L));
    }

    return;
}

From source file:org.opencms.flex.CmsFlexController.java

/**
 * Sets the "last modified" date header for a given http request.<p>
 * // ww  w.j  av  a 2s. c  o  m
 * @param res the response to set the "last modified" date header for
 * @param dateLastModified the date to set (if this is lower then 0, the current time is set)
 */
public static void setDateLastModifiedHeader(HttpServletResponse res, long dateLastModified) {

    if (dateLastModified > -1) {
        // set date last modified header (precision is only second, not millisecond
        res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, (dateLastModified / 1000) * 1000);
    } else {
        // this resource can not be optimized for "last modified", use current time as header
        res.setDateHeader(CmsRequestUtil.HEADER_LAST_MODIFIED, System.currentTimeMillis());
    }
}

From source file:com.ccf.util.ModDateHeaderUtil.java

/**
 * ??/*from  w w  w.j a  va 2 s.  c om*/
 * @param response
 */
public static void setLastModifiedHeader(HttpServletResponse response) {
    SimpleDateFormat df = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss");
    Calendar lastModifiedCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    String lastModified = df.format(lastModifiedCal.getTime());

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    response.setHeader("Date", df.format(cal.getTime()) + " GMT");
    cal.add(Calendar.DAY_OF_MONTH, 1);
    // response.setHeader("Expires",df.format(cal.getTime())+" GMT");
    response.setHeader("Retry-After", df.format(cal.getTime()) + " GMT");
    response.setHeader("Cache-Control", "public");
    //response.setHeader("Last-Modified",lastModified+" GMT");

    response.setDateHeader("Last-Modified", System.currentTimeMillis() - 60 * 60 * 1000);
    // Force clients to revalidate each time
    // See RFC 2616 (HTTP 1.1 spec) secs 14.21, 13.2.1
    response.setDateHeader("Expires", System.currentTimeMillis() + (expiresOffset * 1000));
    // We may also want this (See 13.2.1 and 14.9.4)
    // response.setHeader("Cache-Control","must-revalidate");
}

From source file:ru.org.linux.topic.TopicListController.java

/**
 * @param response/*from w w w . ja  v  a2  s  .co m*/
 * @param topicListForm
 */
private static void setExpireHeaders(HttpServletResponse response, TopicListRequest topicListForm) {
    if (topicListForm.getMonth() == null) {
        response.setDateHeader("Expires", System.currentTimeMillis() + 60 * 1000);
        response.setDateHeader("Last-Modified", System.currentTimeMillis());
    } else {
        long expires = System.currentTimeMillis() + 30 * 24 * 60 * 60 * 1000L;

        Calendar calendar = Calendar.getInstance();
        calendar.set(topicListForm.getYear(), topicListForm.getMonth() - 1, 1);
        calendar.add(Calendar.MONTH, 1);

        long lastmod = calendar.getTimeInMillis();

        if (lastmod < System.currentTimeMillis()) {
            response.setDateHeader("Expires", expires);
            response.setDateHeader("Last-Modified", lastmod);
        } else {
            response.setDateHeader("Expires", System.currentTimeMillis() + 60 * 1000);
            response.setDateHeader("Last-Modified", System.currentTimeMillis());
        }
    }

}

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

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();// w  w w  . j a va 2s.c om
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    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) file.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 FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:ch.entwine.weblounge.common.impl.request.Http11ProtocolHandler.java

/**
 * Method generateHeaders./*w  w w.  j a  v  a 2  s .  c o  m*/
 * 
 * @param resp
 * @param type
 */
public static void generateHeaders(HttpServletResponse resp, Http11ResponseType type) {

    /* generate headers only once! */
    if (type.headers)
        return;
    type.headers = true;

    /* adjust the statistics */
    ++stats[STATS_HEADER_GENERATED];
    incResponseStats(type.type, headerStats);

    /* set the date header */
    resp.setDateHeader(HEADER_DATE, type.time);

    /* check expires */
    if (type.expires > type.time + MS_PER_YEAR) {
        type.expires = type.time + MS_PER_YEAR;
        log.warn("Expiration date too far in the future. Adjusting.");
    }

    /* set the standard headers and status code */
    switch (type.type) {
    case RESPONSE_PARTIAL_CONTENT:
        if (type.expires > type.time)
            resp.setDateHeader(HEADER_EXPIRES, type.expires);
        if (type.modified > 0) {
            resp.setHeader(HEADER_ETAG, Http11Utils.calcETag(type.modified));
            resp.setDateHeader(HEADER_LAST_MODIFIED, type.modified);
        }
        if (type.size < 0 || type.from < 0 || type.to < 0 || type.from > type.to || type.to > type.size) {
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            break;
        }
        resp.setContentLength((int) type.size);
        resp.setHeader(HEADER_CONTENT_RANGE, "bytes " + type.from + "-" + type.to + "/" + type.size);
        resp.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
        break;

    case RESPONSE_OK:
        if (type.expires > type.time) {
            resp.setDateHeader(HEADER_EXPIRES, type.expires);
        } else if (type.expires == 0) {
            resp.setHeader(HEADER_CACHE_CONTROL, "no-cache");
            resp.setHeader(HEADER_PRAGMA, "no-cache");
            resp.setDateHeader(HEADER_EXPIRES, 0);
        }
        if (type.modified > 0) {
            resp.setHeader(HEADER_ETAG, Http11Utils.calcETag(type.modified));
            resp.setDateHeader(HEADER_LAST_MODIFIED, type.modified);
        }
        if (type.size >= 0)
            resp.setContentLength((int) type.size);
        resp.setStatus(HttpServletResponse.SC_OK);
        break;

    case RESPONSE_NOT_MODIFIED:
        if (type.expires > type.time)
            resp.setDateHeader(HEADER_EXPIRES, type.expires);
        if (type.modified > 0)
            resp.setHeader(HEADER_ETAG, Http11Utils.calcETag(type.modified));
        resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        break;

    case RESPONSE_METHOD_NOT_ALLOWED:
        resp.setHeader(HEADER_ALLOW, "GET, POST, HEAD");
        resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        break;

    case RESPONSE_PRECONDITION_FAILED:
        if (type.expires > type.time)
            resp.setDateHeader(HEADER_EXPIRES, type.expires);
        if (type.modified > 0) {
            resp.setHeader(HEADER_ETAG, Http11Utils.calcETag(type.modified));
            resp.setDateHeader(HEADER_LAST_MODIFIED, type.modified);
        }
        resp.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
        break;

    case RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE:
        if (type.expires > type.time)
            resp.setDateHeader(HEADER_EXPIRES, type.expires);
        if (type.modified > 0) {
            resp.setHeader(HEADER_ETAG, Http11Utils.calcETag(type.modified));
            resp.setDateHeader(HEADER_LAST_MODIFIED, type.modified);
        }
        if (type.size >= 0)
            resp.setHeader(HEADER_CONTENT_RANGE, "*/" + type.size);
        break;

    case RESPONSE_INTERNAL_SERVER_ERROR:
    default:
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.shredzone.cilla.view.interceptor.GenericViewInterceptor.java

@Override
public void onRequest(HttpServletRequest req, HttpServletResponse resp) {
    Date now = new Date();
    req.setAttribute("now", now);
    resp.setDateHeader("Date", now.getTime());
}

From source file:net.bafeimao.umbrella.web.controller.CaptchaController.java

@RequestMapping("")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setDateHeader("Expires", 0);
    // Set standard HTTP/1.1 no-cache headers.
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    // Set standard HTTP/1.0 no-cache header.
    response.setHeader("Pragma", "no-cache");
    // return a jpeg
    response.setContentType("image/jpeg");
    // create the text for the image
    String capText = captchaProducer.createText();
    // store the text in the session
    request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
    // create the image with the text
    BufferedImage bi = captchaProducer.createImage(capText);
    ServletOutputStream out = response.getOutputStream();
    // write the data out
    ImageIO.write(bi, "jpg", out);

    try {/* w  ww.j  a  v  a  2 s  .  c  o  m*/
        out.flush();
    } finally {
        out.close();
    }

    return null;
}

From source file:org.openmrs.module.metadatasharing.web.view.JsonView.java

/**
 * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map,
 *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *//*from  w  w  w .j  a v  a  2s.  co  m*/
@Override
protected void renderMergedOutputModel(@SuppressWarnings("rawtypes") Map model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache");
    response.setContentType("application/json");
    PrintWriter writer = response.getWriter();
    writer.write(serializer.serialize(getObjectToSerialize(model, request, response)));
}

From source file:org.openmrs.module.metadatasharing.web.view.XmlView.java

/**
 * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map,
 *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *//*w  ww .  j a  v a  2  s. co  m*/
@Override
protected void renderMergedOutputModel(@SuppressWarnings("rawtypes") Map model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache");
    response.setContentType("application/xml");
    PrintWriter writer = response.getWriter();
    writer.write(serializer.serialize(getObjectToSerialize(model, request, response)));
}