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.mtt.myapp.common.util.FileDownloadUtil.java

/**
 * Provide file download from the given file path.
 * @param response {@link javax.servlet.http.HttpServletResponse}
 * @param desFile file path/*w w w .  j av  a 2s  .com*/
 * @return true if succeeded
 */
public static boolean downloadFile(HttpServletResponse response, File desFile) {
    if (desFile == null || !desFile.exists()) {
        return false;
    }
    boolean result = true;
    response.reset();
    response.addHeader("Content-Disposition", "attachment;filename=" + desFile.getName());
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Length", "" + desFile.length());
    InputStream fis = null;
    byte[] buffer = new byte[FILE_DOWNLOAD_BUFFER_SIZE];
    OutputStream toClient = null;
    try {
        fis = new BufferedInputStream(new FileInputStream(desFile));
        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:" + desFile.getAbsolutePath(), e);
        result = false;
    } catch (IOException e) {
        LOGGER.error("read file error:" + desFile.getAbsolutePath(), e);
        result = false;
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(toClient);
    }
    return result;
}

From source file:com.xsw.utils.Servlets.java

/**
 * ?Header.//from  w  w w  . jav a2s  . co  m
 */
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:cn.com.qiqi.order.utils.Servlets.java

/**
 * ?Header.//w  w w .  j a  v a 2  s  .  co  m
 */
public static void setNoCacheHeader(HttpServletResponse response) {
    // Http 1.0 header
    response.setDateHeader(HttpHeaders.EXPIRES, 1L);
    response.addHeader(HttpHeaders.PRAGMA, "no-cache");
    // Http 1.1 header
    response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0");
}

From source file:com.sobey.framework.utils.Servlets.java

/**
 * ?Header./*from   w w  w  . ja va 2 s .c  o m*/
 */
public static void setNoCacheHeader(HttpServletResponse response) {

    // Http 1.0 header

    response.setDateHeader(HttpHeaders.EXPIRES, 1L);

    response.addHeader(HttpHeaders.PRAGMA, "no-cache");

    // Http 1.1 header

    response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0");
}

From source file:com.cnksi.core.web.utils.Servlets.java

/**
 * ?Header.//from   www  .j  av a  2 s  . co m
 */
public static void setNoCacheHeader(HttpServletResponse response) {

    // Http 1.0 header
    response.setDateHeader(HttpHeaders.EXPIRES, 1L);
    response.addHeader(HttpHeaders.PRAGMA, "no-cache");
    // Http 1.1 header
    response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0");
}

From source file:co.com.realtech.mariner.util.jsf.file.FileDownloader.java

/**
 * Descargar excel a travs del FacesContext.
 *
 * @param context/*from   w ww .jav a2s.c o  m*/
 * @param bytes
 * @param nombreArchivo
 */
private static void descargarArchivo(FacesContext context, byte[] bytes, String nombreArchivo,
        String extension) {
    ExternalContext externalContext = context.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    try {
        try (ServletOutputStream servletOutputStream = response.getOutputStream()) {
            response.addHeader("Content-Type", "application/" + extension);
            response.addHeader("Content-Disposition",
                    "attachment; filename=" + nombreArchivo + "." + extension);
            response.setContentLength(bytes.length);
            response.setContentType("application/" + extension);
            servletOutputStream.write(bytes);
            servletOutputStream.flush();
            context.responseComplete();
        }
    } catch (Exception e) {
        System.out.println("Error enviando reporte al cliente, error causado por " + e);
    }
}

From source file:jp.go.nict.langrid.foundation.servlet.ResponseProcessor.java

private static void addLangridHeaders(Map<String, List<String>> src, HttpServletResponse dst) {
    for (Map.Entry<String, List<String>> e : src.entrySet()) {
        if (e.getKey().startsWith("X-LanguageGrid-")) {
            for (String v : e.getValue()) {
                dst.addHeader(e.getKey(), v);
            }//from w w w .j av  a 2  s .  com
        }
    }
}

From source file:com.kenlin.awsec2offering.App.java

public static void addCORSHeaders(HttpServletResponse resp) {
    resp.addHeader("Access-Control-Allow-Origin", "*");
    resp.addHeader("Access-Control-Allow-Methods", "GET");
    resp.addHeader("Access-Control-Allow-Headers", "Content-Type");
}

From source file:at.gv.egiz.bku.online.webapp.MoccaParameterBean.java

public static void setP3PHeader(Configuration config, HttpServletResponse response) {
    if (config.getBoolean(ENABLE_P3P_HEADER, false)) {
        // Set P3P Policy Header
        response.addHeader("P3P", P3P_POLICY);
    }//from ww  w  .  ja v  a 2 s .c o m
}

From source file:gov.nih.nci.caarray.web.helper.DownloadHelper.java

/**
 * Zips the selected files and writes the result to the servlet output stream. Also sets content type and
 * disposition appropriately./*from  w w  w.j  a va  2  s  . co m*/
 * 
 * @param files the files to zip and send
 * @param baseFilename the filename w/o the suffix to use for the archive file. This filename will be set as the
 *            Content-disposition header
 * @throws IOException if there is an error writing to the stream
 */
public static void downloadFiles(Collection<CaArrayFile> files, String baseFilename) throws IOException {
    final HttpServletResponse response = ServletActionContext.getResponse();

    try {
        final PackagingInfo info = getPreferedPackageInfo(files, baseFilename);
        response.setContentType(info.getMethod().getMimeType());
        response.addHeader("Content-disposition", "filename=\"" + info.getName() + "\"");

        final List<CaArrayFile> sortedFiles = new ArrayList<CaArrayFile>(files);
        Collections.sort(sortedFiles, CAARRAYFILE_NAME_COMPARATOR_INSTANCE);
        final OutputStream sos = response.getOutputStream();
        final OutputStream closeShield = new CloseShieldOutputStream(sos);
        final ArchiveOutputStream arOut = info.getMethod().createArchiveOutputStream(closeShield);
        try {
            for (final CaArrayFile f : sortedFiles) {
                fileAccessUtils.addFileToArchive(f, arOut);
            }
        } finally {
            // note that the caller's stream is shielded from the close(),
            // but this is the only way to finish and flush the (gzip) stream.
            try {
                arOut.close();
            } catch (final Exception e) {
                LOG.error(e);
            }
        }
    } catch (final Exception e) {
        LOG.error("Error streaming download of files " + files, e);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}