Example usage for javax.servlet.http HttpServletResponse setContentLengthLong

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

Introduction

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

Prototype

public void setContentLengthLong(long len);

Source Link

Document

Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.

Usage

From source file:org.duracloud.duradmin.util.SpaceUtil.java

public static void streamToResponse(InputStream is, HttpServletResponse response, String mimetype,
        String contentLength) throws ContentStoreException, IOException {

    OutputStream outStream = response.getOutputStream();

    try {//w ww  .  java 2 s . c  om
        response.setContentType(mimetype);

        if (contentLength != null) {
            response.setContentLengthLong(Long.parseLong(contentLength));
        }
        byte[] buf = new byte[1024];
        int read = -1;
        while ((read = is.read(buf)) > 0) {
            outStream.write(buf, 0, read);
        }

        response.flushBuffer();
    } catch (Exception ex) {
        if (ex.getCause() instanceof ContentStateException) {
            response.reset();
            response.setStatus(HttpStatus.SC_CONFLICT);
            String message = "The requested content item is currently in long-term storage"
                    + " with limited retrieval capability. Please contact "
                    + "DuraCloud support (https://wiki.duraspace.org/x/6gPNAQ) "
                    + "for assistance in retrieving this content item.";
            //It is necessary to pad the message in order to force Internet Explorer to
            //display the server sent text rather than display the browser default error message.
            //If the message is less than 512 bytes, the browser will ignore the message.
            //c.f. http://support.microsoft.com/kb/294807
            message += StringUtils.repeat(" ", 512);
            outStream.write(message.getBytes());
        } else {
            throw ex;
        }
    } finally {
        try {
            outStream.close();
        } catch (Exception e) {
            log.warn("failed to close outputstream ( " + outStream + "): message=" + e.getMessage(), e);
        }
    }
}

From source file:com.formkiq.web.config.TestDataController.java

/**
 * Provider Sample generic XML data with text/xml content type.
 * @param request {@link HttpServletRequest}
 * @param response {@link HttpServletResponse}
 * @throws IOException IOException//w w w  .ja v  a2  s . c  om
 */
@RequestMapping(value = "/testdata/sample-generic1.xml", method = RequestMethod.GET)
public void sampleGeneric1(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException {

    byte[] data = Resources.getResourceAsBytes("/testdata/sample-generic.xml");

    response.setContentType("text/xml");
    response.setContentLengthLong(data.length);
    IOUtils.write(data, response.getOutputStream());
}

From source file:com.formkiq.web.config.TestDataController.java

/**
 * Provider Sample generic XML data with text/xml content type.
 * @param request {@link HttpServletRequest}
 * @param response {@link HttpServletResponse}
 * @throws IOException IOException/*w  ww.  j av a  2s.c  o  m*/
 */
@RequestMapping(value = "/testdata/sample-generic2.xml", method = RequestMethod.GET)
public void sampleGeneric2(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException {

    byte[] data = Resources.getResourceAsBytes("/testdata/sample-generic.xml");

    response.setContentType("application/xml");
    response.setContentLengthLong(data.length);
    IOUtils.write(data, response.getOutputStream());
}

From source file:com.formkiq.web.config.TestDataController.java

/**
 * Provider Sample generic XML data with text/xml content type.
 * @param request {@link HttpServletRequest}
 * @param response {@link HttpServletResponse}
 * @throws IOException IOException// w  ww.j ava 2s . c o  m
 */
@RequestMapping(value = "/testdata/sample-generic1.json", method = RequestMethod.GET)
public void sampleGeneric3(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException {

    byte[] data = Resources.getResourceAsBytes("/testdata/sample-generic.json");

    response.setContentType("application/json");
    response.setContentLengthLong(data.length);
    IOUtils.write(data, response.getOutputStream());
}

From source file:org.eclipse.vorto.repository.web.AdminController.java

@RequestMapping(value = "/content", method = RequestMethod.GET)
@Secured("ROLE_ADMIN")
public void backupRepository(final HttpServletResponse response) throws Exception {
    byte[] backup = this.repositoryManager.backup();
    response.setHeader(CONTENT_DISPOSITION,
            ATTACHMENT_FILENAME + "vortobackup_" + SIMPLEDATEFORMAT.format(new Date()) + ".xml");
    response.setContentLengthLong(backup.length);
    response.setContentType(APPLICATION_OCTET_STREAM);
    try {//from w ww .ja v  a 2 s  .  co m
        IOUtils.copy(new ByteArrayInputStream(backup), response.getOutputStream());
        response.flushBuffer();
    } catch (IOException e) {
        throw new RuntimeException("Error copying file.", e);
    }
}

From source file:data.repository.pragma.PermanentRepoController.java

@RequestMapping(value = "/repo/find/data", method = RequestMethod.GET)
@ResponseBody/*w ww.ja va2s.c o m*/
public void DOfindData(@RequestParam(value = "ID", required = true) String ID, HttpServletResponse response) {
    // Connect to MongoDB and return DO data files as response
    // return
    try {
        GridFSDBFile doc = permanent_repository.findDOByID(ID);
        response.setContentType(doc.getContentType());
        response.setContentLengthLong(doc.getLength());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + doc.getFilename() + "\"");
        OutputStream out = response.getOutputStream();
        doc.writeTo(out);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:data.repository.pragma.DataObjectController.java

@RequestMapping(value = "/DO/find/data", method = RequestMethod.GET)
@ResponseBody//from   w  w w  .  ja  v  a2s . c  o  m
public void DOfindData(@RequestParam(value = "ID", required = true) String ID, HttpServletResponse response) {
    // Connect to MongoDB and return DO data files as response
    // return
    try {
        GridFSDBFile doc = Staging_repository.findDOByID(ID);
        response.setContentType(doc.getContentType());
        response.setContentLengthLong(doc.getLength());
        response.setHeader("Content-Disposition", "attachment; filename=\"" + doc.getFilename() + "\"");
        OutputStream out = response.getOutputStream();
        doc.writeTo(out);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.github.zhanhb.ckfinder.download.PathPartial.java

private void setContentLengthLong(HttpServletResponse response, long length) {
    if (HAS_METHOD_CONTENT_LENGTH_LONG) {
        response.setContentLengthLong(length);
    } else if (length <= Integer.MAX_VALUE) {
        response.setContentLength((int) length);
    } else {/*from  ww w  .  j ava  2s .  c  om*/
        response.setHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(length));
    }
}

From source file:com.formkiq.core.controller.admin.AdminController.java

/**
 * Download Form./*from w  w  w  .ja  v a  2s.c  o m*/
 * @param request {@link HttpServletRequest}
 * @param response {@link HttpServletResponse}
 * @param folder {@link String}
 * @param uuid {@link String}
 * @throws IOException IOException
 */
@Transactional
@RequestMapping("/download")
public void download(final HttpServletRequest request, final HttpServletResponse response,
        @RequestParam(value = "folder", required = true) final String folder,
        @RequestParam(value = "uuid", required = true) final String uuid) throws IOException {

    byte[] data = this.folderservice.findFormData(folder, uuid).getLeft();

    response.setContentType("application/zip");
    response.setHeader("Content-disposition", "attachment; filename=" + uuid + ".zip");
    response.setContentLengthLong(data.length);
    IOUtils.write(data, response.getOutputStream());
}