Example usage for javax.servlet.http HttpServletResponse setContentLength

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

Introduction

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

Prototype

public void setContentLength(int 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:it.eng.spagobi.kpi.service.KpiExporterAction.java

/**
 * This action is called by the user who wants to export the result of a Kpi in PDF
 * /*from  w  w w .j a va 2s  . c om*/
 */

public void service(SourceBean serviceRequest, SourceBean serviceResponse) throws Exception {

    File tmpFile = null;
    logger.debug("IN");
    HttpServletRequest httpRequest = getHttpRequest();
    HttpSession session = httpRequest.getSession();

    this.freezeHttpResponse();

    try {
        // get KPI result
        List<KpiResourceBlock> listKpiBlocks = (List<KpiResourceBlock>) session.getAttribute("KPI_BLOCK");

        //List<KpiResourceBlock> listKpiBlocks=executeKpi(objectId);

        // recover BiObject Name
        Object idObject = serviceRequest.getAttribute(SpagoBIConstants.OBJECT_ID);
        if (idObject == null) {
            logger.error("Document id not found");
            return;
        }

        Integer id = Integer.valueOf(idObject.toString());
        BIObject document = DAOFactory.getBIObjectDAO().loadBIObjectById(id);
        String docName = document.getName();

        //Recover user Id
        HashedMap parameters = new HashedMap();
        String userId = null;
        Object userIdO = serviceRequest.getAttribute("user_id");
        if (userIdO != null)
            userId = userIdO.toString();

        it.eng.spagobi.engines.exporters.KpiExporter exporter = new KpiExporter();
        tmpFile = exporter.getKpiReportPDF(listKpiBlocks, document, userId);

        String outputType = "PDF";

        String mimeType = "application/pdf";

        logger.debug("Report exported succesfully");

        HttpServletResponse response = getHttpResponse();
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "filename=\"report." + outputType + "\";");
        response.setContentLength((int) tmpFile.length());

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile));
        int b = -1;
        while ((b = in.read()) != -1) {
            response.getOutputStream().write(b);
        }
        response.getOutputStream().flush();
        in.close();
        logger.debug("OUT");

    } catch (Throwable e) {
        logger.error("An exception has occured", e);
        throw new Exception(e);
    } finally {

        tmpFile.delete();

    }
}

From source file:de.hybris.platform.b2bdocumentsfilter.B2BDocumentsSecureMediaFilter.java

private void sendData(final HttpServletResponse httpResponse, final InputStream mediaStream, final int length)
        throws IOException {
    httpResponse.setContentLength(length);

    try {//from w ww  . j av  a 2 s  . co  m
        final OutputStream out = httpResponse.getOutputStream();
        IOUtils.copyLarge(mediaStream, out);
    } finally {
        if (mediaStream != null) {
            mediaStream.close();
        }
        if (mediaStream != null) {
            mediaStream.close();
        }
    }
}

From source file:it.eng.spagobi.kpi.service.KpiXmlExporterAction.java

/**
 * This action is called by the user who wants to export the result of a Kpi in XML
 * /*from  w  w  w .  java2  s .  c o m*/
 */

public void service(SourceBean serviceRequest, SourceBean serviceResponse) throws Exception {

    File tmpFile = null;
    logger.debug("IN");
    HttpServletRequest httpRequest = getHttpRequest();
    HttpSession session = httpRequest.getSession();

    this.freezeHttpResponse();

    try {
        // get KPI result
        List<KpiResourceBlock> listKpiBlocks = (List<KpiResourceBlock>) session.getAttribute("KPI_BLOCK");
        String title = (String) session.getAttribute("TITLE");
        String subtitle = (String) session.getAttribute("SUBTITLE");
        if (title == null)
            title = "";
        if (subtitle == null)
            subtitle = "";

        // recover BiObject Name
        Object idObject = serviceRequest.getAttribute(SpagoBIConstants.OBJECT_ID);
        if (idObject == null) {
            logger.error("Document id not found");
            return;
        }

        Integer id = Integer.valueOf(idObject.toString());
        BIObject document = DAOFactory.getBIObjectDAO().loadBIObjectById(id);
        String docName = document.getName();

        //Recover user Id
        HashedMap parameters = new HashedMap();
        String userId = null;
        Object userIdO = serviceRequest.getAttribute("user_id");
        if (userIdO != null)
            userId = userIdO.toString();

        it.eng.spagobi.engines.exporters.KpiExporter exporter = new KpiExporter();
        tmpFile = exporter.getKpiExportXML(listKpiBlocks, document, userId);

        String outputType = "XML";

        String mimeType = "text/xml";

        logger.debug("Report exported succesfully");

        HttpServletResponse response = getHttpResponse();
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "filename=\"report." + outputType + "\";");
        response.setContentLength((int) tmpFile.length());

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile));
        int b = -1;
        while ((b = in.read()) != -1) {
            response.getOutputStream().write(b);
        }
        response.getOutputStream().flush();
        in.close();
        logger.debug("OUT");

    } catch (Throwable e) {
        logger.error("An exception has occured", e);
        throw new Exception(e);
    } finally {

        tmpFile.delete();

    }
}

From source file:cc.kune.core.server.manager.file.EntityLogoDownloadManager.java

/**
 * Reply./* ww w .  ja va2  s  . co  m*/
 *
 * @param resp
 *          the resp
 * @param logo
 *          the logo
 * @param mime
 *          the mime
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
private void reply(final HttpServletResponse resp, final byte[] logo, final String mime) throws IOException {
    resp.setContentLength(logo.length);
    resp.setContentType(mime);
    CacheUtils.setCache1Day(resp);
    resp.getOutputStream().write(logo);
}

From source file:com.enonic.cms.server.service.portal.mvc.controller.PortalRenderResponseServer.java

private void writeContent(HttpServletResponse httpResponse, byte[] content) throws IOException {
    httpResponse.setContentLength(content.length);

    OutputStream out = httpResponse.getOutputStream();
    out.write(content);/*from www.jav  a2  s  .  c o  m*/
}

From source file:org.darwinathome.server.controller.WebstartController.java

@RequestMapping("/{jarFile}.jar")
public void jarFile(@PathVariable String jarFile, HttpServletResponse response) {
    log.info("Request for " + jarFile);
    String realFileName = servletContext.getRealPath(jarFile + ".jar");
    File realFile = new File(realFileName);
    response.setContentType("application/java-archive");
    response.setContentLength((int) realFile.length());
    try {/*ww  w.  j  a v a 2 s .c o  m*/
        InputStream in = new FileInputStream(realFileName);
        OutputStream out = response.getOutputStream();
        try {
            byte[] buffer = new byte[8192];
            int read;
            while ((read = in.read(buffer)) > 0) {
                out.write(buffer, 0, read);
            }
        } finally {
            in.close();
        }
    } catch (SocketException e) {
        log.info("While writing jar: " + e.toString());
    } catch (EOFException e) {
        log.info("Broken pipe while requesting " + jarFile);
    } catch (IOException e) {
        log.warn("Problem requesting " + jarFile, e);
    }
}

From source file:com.trailmagic.image.ui.ImageManifestationController.java

@RequestMapping("/mf/by-id/{imageId}")
public ModelAndView imageById(HttpServletResponse res, @PathVariable("imageId") Long imageId) throws Exception {
    Map<String, Object> model = new HashMap<String, Object>();

    HeavyImageManifestation mf = imageManifestationRepository.getHeavyById(imageId);

    java.io.InputStream dataStream = mf.getData().getBinaryStream();
    res.setContentLength((int) mf.getData().length());

    log.debug("Passing manifestation data stream to view (type: " + dataStream.getClass() + ")");
    model.put(InputStreamView.STREAM_KEY, dataStream);
    model.put(InputStreamView.CONTENT_TYPE_KEY, mf.getFormat());
    if (mf.getName() != null) {
        model.put(InputStreamView.CONTENT_DISPOSITION_KEY, "inline; filename=" + mf.getName() + ";");
    }// ww w. j a va2 s  . co m
    return new ModelAndView(new InputStreamView(), model);
}

From source file:ar.com.fdvs.dj.output.MemoryReportWriter.java

public void writeTo(final HttpServletResponse _response) throws IOException, JRException {
    LOGGER.info("entering MemoryReportWriter.writeTo()");
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, stream);
    exporter.exportReport();/*w  w w .j a  v  a 2 s.c  om*/
    _response.setContentLength(stream.size());
    copyStreams(new ByteArrayInputStream(stream.toByteArray()), _response.getOutputStream());
}

From source file:com.sshdemo.common.report.manage.web.ParameterSetAction.java

private void buildChart() {
    PrintWriter pw = null;/*from  w  w w.  j  a v  a 2s.co  m*/
    InputStream in = null;
    try {
        ChartReport chart = reportFac.findChartReportById(reportId);

        HttpServletResponse response = ServletActionContext.getResponse();
        pw = response.getWriter();

        response.reset();// 
        response.setContentLength(0);
        byte[] bytes = chartFactory.export(chart, paraMap);
        response.setContentLength(bytes.length);
        response.setHeader("Content-Type", "image/png");
        in = new ByteArrayInputStream(bytes);
        int len = 0;
        while ((len = in.read()) > -1) {
            pw.write(len);
        }
        pw.flush();
    } catch (Exception e) {
        // log.error(e.toString());
    } finally {
        if (pw != null) {
            try {
                pw.close();
                pw = null;
            } catch (Exception e) {
            }
        }
        if (in != null) {
            try {
                in.close();
                in = null;
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.openmrs.module.clinicalsummary.web.controller.evaluator.EvaluatePatientController.java

/**
 * Attach the file to the servlet response body
 *
 * @param response the response//w w  w.j  a v a 2 s.  co  m
 * @param file     the file
 * @throws Exception
 */
private void attachAndPurgeFile(final HttpServletResponse response, final File file) throws Exception {
    response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
    response.setContentType(MimeConstants.MIME_PDF);
    response.setContentLength((int) file.length());

    FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());

    if (!file.delete()) {
        log.info("Deleting temporary file failed!");
    }
}