Example usage for javax.servlet.http HttpServletResponse addHeader

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

Introduction

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

Prototype

public void addHeader(String name, String value);

Source Link

Document

Adds a response header with the given name and value.

Usage

From source file:com.hp.autonomy.searchcomponents.core.view.ViewContentSecurityPolicy.java

/**
 * Add content security policy headers to an HTTP response. These control what child content can be loaded from the
 * proxied document, reducing the risk of allowing users to serve arbitrary documents from the application domain.
 * @param response The HTTP response/* w ww  .  j  a v a2s .c om*/
 */
public static void addContentSecurityPolicy(final HttpServletResponse response) {
    // We need both headers to support all browsers
    response.addHeader("Content-Security-Policy", CONTENT_SECURITY_POLICY);
    response.addHeader("X-Content-Security-Policy", CONTENT_SECURITY_POLICY);
}

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

/**
 * X-Accel-Redirectnignx//from ww w .jav a2  s . com
 * @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:io.lavagna.web.security.SecurityFilter.java

/**
 *//*from ww w .  jav a  2s.co  m*/
// TODO check if the no caching directives could be removed/or at least
// changed... (?)
private static void addHeaders(HttpServletRequest req, HttpServletResponse res) {
    if (canApplyNoCachingHeaders(req)) {
        res.addHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
        res.addHeader("Expires", "0");
        res.addHeader("Pragma", "no-cache");
    }

    res.addHeader("X-Frame-Options", "SAMEORIGIN");
    res.addHeader("X-XSS-Protection", "1; mode=block");
    res.addHeader("x-content-type-options", "nosniff");
}

From source file:dk.dma.msinm.common.util.WebUtils.java

/**
 * Add headers to the response to ensure caching in the given duration
 * @param response the response/*w ww . j  a  v a2  s  .c o  m*/
 * @param seconds the number of seconds to cache the response
 * @return the response
 */
public static HttpServletResponse cache(HttpServletResponse response, int seconds) {
    long now = System.currentTimeMillis();
    response.addHeader("Cache-Control", "max-age=" + seconds);
    response.setDateHeader("Last-Modified", now);
    response.setDateHeader("Expires", now + seconds * 1000L);
    return response;
}

From source file:org.craftercms.core.util.HttpServletUtils.java

public static void disableCaching(HttpServletResponse response) {
    response.addHeader(PRAGMA_HEADER_NAME, "no-cache");
    response.addHeader(CACHE_CONTROL_HEADER_NAME, "no-cache, no-store, max-age=0");
    response.addDateHeader(EXPIRES_HEADER_NAME, 1L);
}

From source file:org.gwtwidgets.server.spring.ServletUtils.java

/**
 * Adjusts HTTP headers so that browsers won't cache response.
 * @param response//w  w  w.j a v  a 2  s .  c om
 * For more background see <a href="http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index2.html">this</a>.
 */
public static void disableResponseCaching(HttpServletResponse response) {
    response.setHeader("Expires", "Sat, 1 January 2000 12:00:00 GMT");
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    response.setHeader("Pragma", "no-cache");
}

From source file:Main.java

/**
 * Add headers to prevent browsers and proxies from caching this reply.
 * @param resp The response to add headers to
 *///  www .ja  v a2 s .co  m
public static void addNoCacheHeaders(HttpServletResponse resp) {
    // Set standard HTTP/1.1 no-cache headers.
    resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
    resp.addHeader("Cache-Control", "post-check=0, pre-check=0");

    // Set standard HTTP/1.0 no-cache header.
    resp.setHeader("Pragma", "no-cache");

    // Set to expire far in the past. Prevents caching at the proxy server
    resp.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
}

From source file:com.gc.core.framework.utils.web.ServletUtils.java

/**
 * ?Header./*w  w w .j  a v a 2s.c om*/
 */
public static void setDisableCacheHeader(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:com.dosport.system.utils.ServletUtils.java

/**
 * ?Header./*from  w w  w .ja va 2 s .  c  om*/
 */
public static void setDisableCacheHeader(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:com.iyonger.apm.web.util.FileDownloadUtils.java

/**
 * Download the given file to the given {@link HttpServletResponse}.
 *
 * @param response {@link HttpServletResponse}
 * @param file     file path/*  ww  w  . j av  a 2  s .c  om*/
 * @return true if succeeded
 */
public static boolean downloadFile(HttpServletResponse response, File file) {
    if (file == null || !file.exists()) {
        return false;
    }
    boolean result = true;
    response.reset();
    response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Length", "" + file.length());
    InputStream fis = null;
    byte[] buffer = new byte[FILE_DOWNLOAD_BUFFER_SIZE];
    OutputStream toClient = null;
    try {
        fis = new BufferedInputStream(new FileInputStream(file));
        toClient = new BufferedOutputStream(response.getOutputStream());
        int readLength;
        while (((readLength = fis.read(buffer)) != -1)) {
            toClient.write(buffer, 0, readLength);
        }
        toClient.flush();
    } catch (FileNotFoundException e) {
        LOGGER.error("file not found:" + file.getAbsolutePath(), e);
        result = false;
    } catch (IOException e) {
        LOGGER.error("read file error:" + file.getAbsolutePath(), e);
        result = false;
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(toClient);
    }
    return result;
}