Example usage for javax.servlet ServletResponse getOutputStream

List of usage examples for javax.servlet ServletResponse getOutputStream

Introduction

In this page you can find the example usage for javax.servlet ServletResponse getOutputStream.

Prototype

public ServletOutputStream getOutputStream() throws IOException;

Source Link

Document

Returns a ServletOutputStream suitable for writing binary data in the response.

Usage

From source file:org.xchain.namespaces.servlet.ResultCommand.java

public boolean execute(JXPathContext context) throws Exception {
    ServletResponse response = getResult(context);

    // set the result in the pipeline config's composite stage
    Result result = new StreamResult(response.getOutputStream());
    PipelineCommand.getPipelineConfig().getCompositeStage().setResult(result);

    // set the content type for the response
    ContentType contentType;/*from   w ww  . ja v  a2  s .  c o  m*/

    String requestedMediaType = hasMediaType() ? getMediaType(context)
            : PipelineCommand.getPipelineConfig().getCompositeStage().getMediaType();
    String requestedEncoding = PipelineCommand.getPipelineConfig().getCompositeStage().getEncoding();

    String charset = ContentType.DEFAULT_CHARSET;
    if (requestedEncoding != null) {
        charset = ContentType.ATTR_CHARSET + "=" + requestedEncoding;
    }

    if (requestedMediaType != null) {
        contentType = new ContentType(requestedMediaType + ";" + charset);
    } else {
        contentType = ContentType.TEXT_HTML;
    }

    // If the browser does not accept XHTML and we are requesting XHTML, default to HTML
    String acceptHeader = ((HttpServletRequest) getRequest(context)).getHeader("Accept");
    if (contentType.mediaTypeMatch(ContentType.TEXT_XHTML)
            && !ContentType.acceptsContentType(acceptHeader, ContentType.TEXT_XHTML)) {
        contentType = ContentType.TEXT_HTML;
    }

    response.setContentType(contentType.toString());

    if (getDisableCaching(context)) {
        disableCaching((HttpServletResponse) response);
    }

    return false;
}

From source file:org.yestech.lib.io.FileSystemFileDownloadFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    String fileName = request.getParameter("file");
    FileInputStream inputStream = null;
    ServletOutputStream outputStream = response.getOutputStream();
    if (StringUtils.isNotBlank(fileName)) {
        try {//from w  ww  .ja va2  s .c  om

            File downloadFile = new File(baseDirectory, fileName);
            response.setContentLength((int) downloadFile.length());
            inputStream = openInputStream(downloadFile);
            copy(inputStream, outputStream);
            outputStream.flush();
            if (deleteAfterDownload) {
                downloadFile.delete();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            closeQuietly(inputStream);
            closeQuietly(outputStream);
        }

    }
}

From source file:uk.gov.devonline.www.xforms.XFormsFilter.java

/**
 * The actual filtering method/* w w w  .  j a v  a 2s  .com*/
 * 
 * @see http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/Filter.html#doFilter(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest srvRequest, ServletResponse srvResponse, FilterChain filterChain)
        throws IOException, ServletException {

    /* before servlet request */
    if (isXFormUpdateRequest(srvRequest)) {
        LOG.info("Start Update XForm");
        updateXForm(srvRequest, srvResponse);
        LOG.info("End Update XForm");
    } else {

        /* do servlet request */
        LOG.info("Passing to Chain");
        BufferedHttpServletResponseWrapper bufResponse = new BufferedHttpServletResponseWrapper(
                (HttpServletResponse) srvResponse);
        filterChain.doFilter(srvRequest, bufResponse);
        LOG.info("Returned from Chain");

        /* after servlet request */
        if (hasXForm(bufResponse)) {
            bufResponse.getOutputStream().close();
            LOG.info("Start Render XForm");

            //remove DOCTYPE PI if it exists, Xerces in Chiba otherwise may try to download the system DTD (can cause latency problems)
            byte[] data = removeDocumentTypePI(bufResponse.getData());
            //correct the <xforms:instance> xmlns="" problem (workaround for namespace problems in eXist)
            data = correctInstanceXMLNS(data);

            renderXForm(new ByteArrayInputStream(data), srvRequest, srvResponse);
            LOG.info("End Render XForm");
        } else {
            srvResponse.getOutputStream().write(bufResponse.getData());
            srvResponse.getOutputStream().close();
        }
    }
}