Example usage for javax.servlet.http HttpServletResponse setHeader

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

Introduction

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

Prototype

public void setHeader(String name, String value);

Source Link

Document

Sets a response header with the given name and value.

Usage

From source file:be.fedict.eid.dss.webapp.DocumentViewerServlet.java

private void setResponseHeaders(HttpServletRequest request, HttpServletResponse response) {

    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http
    // 1.1/*ww  w  . j a v a 2s  .  co m*/
    if (!request.getScheme().equals("https")) {
        // else the download fails in IE
        response.setHeader("Pragma", "no-cache"); // http 1.0
    } else {
        response.setHeader("Pragma", "public");
    }
    response.setDateHeader("Expires", -1);

}

From source file:com.w3ma.m3u8web.controller.DownloadPlaylistController.java

@RequestMapping(value = "/playlist", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<ByteArrayResource> getPlaylist(final HttpServletResponse response) throws IOException {
    logger.debug("getPlaylist called");
    final HttpHeaders headers = new HttpHeaders();
    response.setHeader("Content-Disposition", "attachment; filename=playlist.m3u");
    return new ResponseEntity<>(
            new ByteArrayResource(playlistGeneratorService.getPlaylistByteArrayOutputStream().toByteArray()),
            headers, HttpStatus.OK);/* ww w.ja v a 2 s .c  o  m*/
}

From source file:org.openmrs.module.usagestatistics.web.view.csv.AbstractCSVView.java

/**
 * @see org.springframework.web.servlet.view.AbstractView
 */// w  ww  . ja va  2 s  . c  o  m
@Override
@SuppressWarnings("unchecked")
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    // Respond as a CSV file
    response.setContentType("text/csv");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + getFilename(model) + "\"");

    // Disable caching
    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache");

    // Write data
    writeValues(model, response.getWriter());
}

From source file:com.zlfun.framework.misc.UploadUtils.java

public static boolean download(HttpServletRequest request, HttpServletResponse response, String uuid) {

    InputStream bis;//from   w  ww. j  a v a  2  s .  c o m
    boolean ret = false;
    try {

        byte[] voiceData = FsUtils.readFromDisk(uuid);
        if (voiceData != null) {
            long p = 0L;
            long toLength = 0L;
            long contentLength = 0L;
            int rangeSwitch = 0; // 0,1,?bytes=27000-2,???bytes=27000-39000
            long fileLength;
            String rangBytes = "";
            fileLength = voiceData.length;

            // get file content
            bis = new ByteArrayInputStream(voiceData);

            // tell the client to allow accept-ranges
            response.reset();
            response.setHeader("Accept-Ranges", "bytes");

            // client requests a file block download start byte
            String range = request.getHeader("Range");
            if (range != null && range.trim().length() > 0 && !"null".equals(range)) {
                response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
                rangBytes = range.replaceAll("bytes=", "");
                if (rangBytes.endsWith("-")) { // bytes=270000-
                    rangeSwitch = 1;
                    p = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-")));
                    contentLength = fileLength - p; // 270000?bytes270000
                } else { // bytes=270000-320000
                    rangeSwitch = 2;
                    String temp1 = rangBytes.substring(0, rangBytes.indexOf("-"));
                    String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length());
                    p = Long.parseLong(temp1);
                    toLength = Long.parseLong(temp2);
                    contentLength = toLength - p + 1; // 
                    // 270000-320000
                    // 
                }
            } else {
                contentLength = fileLength;
            }

            // Content-Length?????
            // Content-Length: [?] - [?]
            response.setHeader("Content-Length", new Long(contentLength).toString());

            // 
            // ??:
            // Content-Range: bytes [?]-[? - 1]/[?]
            if (rangeSwitch == 1) {
                String contentRange = new StringBuffer("bytes ").append(new Long(p).toString()).append("-")
                        .append(new Long(fileLength - 1).toString()).append("/")
                        .append(new Long(fileLength).toString()).toString();
                response.setHeader("Content-Range", contentRange);
                bis.skip(p);
            } else if (rangeSwitch == 2) {
                String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString();
                response.setHeader("Content-Range", contentRange);
                bis.skip(p);
            } else {
                String contentRange = new StringBuffer("bytes ").append("0-").append(fileLength - 1).append("/")
                        .append(fileLength).toString();
                response.setHeader("Content-Range", contentRange);
            }

            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", getContentDisposition(request, uuid));

            OutputStream out = response.getOutputStream();

            int n = 0;
            long readLength = 0;
            int bsize = 1024;
            byte[] bytes = new byte[bsize];
            if (rangeSwitch == 2) {
                //  bytes=27000-39000 27000?
                while (readLength <= contentLength - bsize) {
                    n = bis.read(bytes);
                    readLength += n;
                    out.write(bytes, 0, n);
                }
                if (readLength <= contentLength) {
                    n = bis.read(bytes, 0, (int) (contentLength - readLength));
                    out.write(bytes, 0, n);
                }
            } else {
                while ((n = bis.read(bytes)) != -1) {
                    out.write(bytes, 0, n);
                }
            }
            out.flush();
            out.close();
            bis.close();
            return true;
        } else {
            return false;
        }
    } catch (IOException ex) {
        //  ClientAbortException 
        Logger.getLogger(UploadUtils.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch (Exception ex) {
        Logger.getLogger(UploadUtils.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }

}

From source file:controllers.CarPropertyController.java

@RequestMapping("/getXls")
public void getXls(Map<String, Object> model, HttpServletResponse response) throws Exception {
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=PROPERTY.xls");
    carPropertyService.getXls().write(response.getOutputStream());
}

From source file:cz.muni.fi.dndtroopsweb.security.ProtectFilter.java

private void response401(HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.setHeader("WWW-Authenticate", "Basic realm=\"type name and password\"");
    response.getWriter()/*w  w w. ja v a  2 s  .  co  m*/
            .println("<html><body><h1>401 Unauthorized Access</h1> Destruction in 5..4..3..</body></html>");
}

From source file:com.image32.demo.simpleapi.ContentHandler.java

private void httpHeaderNoCache(HttpServletResponse response) {
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain; charset=Shift_JIS");
    PrintWriter out = res.getWriter();
    res.setHeader("Content-Language", "ja");

    Locale locale = new Locale("ja", "");
    DateFormat full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    out.println("In Japanese:");

    try {//from   w  w  w .j  av  a2  s.  c  o  m
        FileInputStream fis = new FileInputStream(req.getRealPath("/HelloWorld.ISO-2022-JP"));
        InputStreamReader isr = new InputStreamReader(fis, "ISO-2022-JP");
        BufferedReader reader = new BufferedReader(isr);
        String line = null;
        while ((line = reader.readLine()) != null) {
            out.println(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    out.println(full.format(new Date()));
}

From source file:elw.web.ControllerElw.java

public static void storeContentHeaders(final FileBase fileBase, final HttpServletResponse resp) {
    final FileType fileType = IdNamed._.one(fileBase.getFileType());
    final String contentType;
    if (!fileType.getContentTypes().isEmpty()) {
        contentType = fileType.getContentTypes().get(0);
    } else {/* w  w w.ja v  a  2  s  .co m*/
        contentType = fileType.isBinary() ? "binary/octet-stream" : "text/plain";
    }

    if (fileType.isBinary()) {
        resp.setContentType(contentType);
    } else {
        resp.setContentType(contentType + "; charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
    }

    resp.setContentLength(fileBase.getCouchFile(FileBase.CONTENT).getLength().intValue());
    resp.setHeader("Content-Disposition", "attachment;");
}

From source file:core.NiprSyncController.java

@RequestMapping("/")
public void index(HttpServletRequest request, HttpServletResponse response) {

    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", "/login.html");
}