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:org.zkoss.zkgrails.ZKGrailsPageFilter.java

/** Write the original page data to the response. */
private void writeOriginal(HttpServletRequest request, HttpServletResponse response, Page page)
        throws IOException {
    response.setContentLength(page.getContentLength());
    if (request.getAttribute(USING_STREAM).equals(Boolean.TRUE)) {
        PrintWriter writer = new PrintWriter(response.getOutputStream());
        page.writePage(writer);//from w w  w  .j  a  va 2 s .  c  o m
        //flush writer to underlying outputStream
        writer.flush();
        response.getOutputStream().flush();
    } else {
        page.writePage(response.getWriter());
        response.getWriter().flush();
    }
}

From source file:com.adito.core.MessageResourceLoaderServlet.java

private void sendFile(InputStream in, long length, HttpServletResponse response) throws IOException {
    response.setHeader("Content-Type", "text/plain");
    response.setHeader("Content-Length", String.valueOf(length));
    response.setContentLength((int) length);
    Util.noCache(response);//from  w  w w  . ja  va 2  s. c o m
    try {
        Util.copy(in, response.getOutputStream());
        response.getOutputStream().flush();
    } catch (IOException ex) {
    } finally {
        Util.closeStream(in);
        Util.closeStream(response.getOutputStream());
    }

}

From source file:com.adito.extensions.actions.GetApplicationFileAction.java

private void sendFile(InputStream in, long length, HttpServletResponse response) throws IOException {
    Util.noCache(response);/*from   w ww.  java2s .co m*/

    response.setHeader("Content-type", "application/octet-stream");
    response.setContentLength((int) length);
    try {

        Util.copy(in, response.getOutputStream());

    } catch (IOException ex) {
    } finally {
        Util.closeStream(in);
        Util.closeStream(response.getOutputStream());
    }

}

From source file:ee.ria.xroad.proxy.testsuite.testcases.ServerProxyNoBoundary.java

@Override
public AbstractHandler getServerProxyHandler() {
    return new AbstractHandler() {
        @Override//  w w  w .  jav  a2s . co  m
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException {
            // Read all of the request.
            IOUtils.readLines(request.getInputStream());

            response.setContentType("multipart/mixed");
            response.setContentLength(1000);
            response.setHeader(HEADER_HASH_ALGO_ID, DEFAULT_DIGEST_ALGORITHM_ID);
            baseRequest.setHandled(true);
        }
    };
}

From source file:com.epam.ta.reportportal.ws.controller.impl.FileStorageController.java

/**
 * Copies provided {@link BinaryData} to Response
 * //from   w  w  w .j av a 2 s . c om
 * @param response
 * @param binaryData
 */
private void toResponse(HttpServletResponse response, BinaryData binaryData) {
    if (binaryData != null) {
        response.setContentType(binaryData.getContentType());
        response.setContentLength(binaryData.getLength().intValue());

        try {
            IOUtils.copy(binaryData.getInputStream(), response.getOutputStream());
        } catch (IOException e) {
            throw new ReportPortalException("Unable to retrieve binary data from data storage", e);
        }
    } else {
        response.setStatus(HttpStatus.NO_CONTENT.value());
    }
}

From source file:org.craftercms.engine.http.impl.HttpProxyImpl.java

protected void copyActualResponseBody(String responseBody, HttpServletResponse response) throws IOException {
    if (responseBody != null) {
        response.setContentLength(responseBody.length());
        OutputStream out = response.getOutputStream();
        out.write(responseBody.getBytes());
        out.flush();/*from  ww w .j  av  a2s.  c o m*/
    }
}

From source file:com.tasktop.c2c.server.web.proxy.HttpProxy.java

void copyProxyReponse(HttpMethod proxyResponse, HttpServletResponse response) throws IOException {
    copyProxyHeaders(proxyResponse.getResponseHeaders(), response);
    response.setContentLength(getResponseContentLength(proxyResponse));
    copy(proxyResponse.getResponseBodyAsStream(), response.getOutputStream());
    if (proxyResponse.getStatusLine() != null) {
        int statCode = proxyResponse.getStatusCode();
        response.setStatus(statCode);//from w  ww .  j  a  v a 2 s .c om
    }
}

From source file:cn.vlabs.clb.server.ui.frameservice.document.handler.GetDocContentHandler.java

private void writeFileContentToResponse(GridFSDBFile dbfile, HttpServletRequest request,
        HttpServletResponse response) {
    OutputStream os = null;// w  w w .j  a va  2 s .  c  o m
    try {
        long start = System.currentTimeMillis();
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) dbfile.getLength());
        response.setContentType("application/x-download");
        String filename = dbfile.getFilename();
        if (filename == null) {
            filename = "file" + dbfile.getId();
        }
        LOG.debug(dbfile.getFilename() + "," + dbfile.getLength());
        String headerValue = ResponseHeaderUtils.buildResponseHeader(request, filename, true);
        response.setHeader("Content-Disposition", headerValue);
        response.setHeader("Content-Length", dbfile.getLength() + "");
        os = response.getOutputStream();
        dbfile.writeTo(os);
        long end = System.currentTimeMillis();
        LOG.info("Read doc content using stream mode for doc [" + filename + "], use time " + (end - start)
                + "ms");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:werecloud.api.view.StringView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    if (model.containsKey("string")) {
        String data = model.get("string").toString();
        byte[] _data = data.getBytes();
        response.setContentType(getContentType());
        response.setContentLength(_data.length);
        ServletOutputStream out = response.getOutputStream();
        out.write(_data);/*from   ww w  .  ja  v a 2  s . c  o m*/
        out.flush();
        out.close();
        return;
    }
    throw new Exception("Could not find model.");
}

From source file:org.red5.server.net.servlet.StatisticsServlet.java

/** {@inheritDoc} */
@Override/*from www.  ja va  2s. c  om*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // Process request with XML-RPC server
    byte[] result = server.execute(request.getInputStream());
    response.setContentType("text/xml");
    response.setContentLength(result.length);
    OutputStream out = response.getOutputStream();
    out.write(result);
    out.close();
}