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.betaconceptframework.astroboa.console.filter.BinaryChannelDynamicFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest httpServletRequest = (HttpServletRequest) request;

    String absoluteBinaryChannelPath = createAbsoluteBinaryChannelPath(httpServletRequest.getContextPath(),
            httpServletRequest.getRequestURI());

    if (StringUtils.isNotBlank(absoluteBinaryChannelPath)) {

        InputStream contentAsStream = null;

        try {//from   w  w w .  j a  v a2s.co  m
            contentAsStream = new FileInputStream(absoluteBinaryChannelPath);

            if (contentAsStream != null) {
                ServletOutputStream servletOutputStream = response.getOutputStream();

                IOUtils.copy(contentAsStream, servletOutputStream);

                servletOutputStream.flush();

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            //Close Stream
            if (contentAsStream != null) {
                try {
                    contentAsStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else
                chain.doFilter(request, response);
        }
    } else
        chain.doFilter(request, response);

}

From source file:org.cruxframework.crux.core.declarativeui.filter.DeclarativeUIFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    if (production) {
        chain.doFilter(req, resp);//from   www  . j  a  v  a  2s .  c o  m
    } else {
        String requestedScreen = getRequestedScreen(req);
        if (requestedScreen != null) {
            CruxBridge.getInstance().registerLastPageRequested(requestedScreen);
            String module = null;
            int index = requestedScreen.indexOf("/");
            if (index > 0) {
                String relativeScreenId = Modules.getInstance().removeModuleIfPresent(requestedScreen);
                if (!relativeScreenId.equals(requestedScreen)) {
                    module = requestedScreen.substring(0, index);
                    requestedScreen = relativeScreenId;
                }
            }
            try {
                InputStream screenResource;
                if (module == null) {
                    screenResource = ScreenResourceResolverInitializer.getScreenResourceResolver()
                            .getScreenResource(requestedScreen);
                } else {
                    screenResource = ScreenResourceResolverInitializer.getScreenResourceResolver()
                            .getScreenResource(requestedScreen, module);
                }
                if (screenResource != null) {
                    StreamUtils.write(screenResource, resp.getOutputStream(), false);
                    return;
                } else {
                    log.info("The page [" + requestedScreen + "] is not declarative... accessing directly.");
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new ServletException(e.getMessage(), e);
            }
        }
        chain.doFilter(req, resp);
    }
}

From source file:org.fenixedu.bennu.core.filters.CloseTransactionFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    try {/* w w  w  .ja  va  2  s. co  m*/
        doChain(request, response, chain);
    } catch (Error t) {
        t.printStackTrace();
        InputStream resource = request.getServletContext().getResourceAsStream("/error.html");
        if (resource != null) {
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("text/html");
            IOUtils.copy(resource, outputStream);
            outputStream.flush();
        } else {
            throw t;
        }
    }
}

From source file:org.force66.mock.servletapi.MockFilterChain.java

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    Validate.notNull(request, "Null request not allowed.");
    Validate.notNull(response, "Null response not allowed.");

    if (this.runtimeException != null) {
        throw this.runtimeException;
    }/*from   w  ww  .  ja va2s .c  o m*/
    if (this.ioException != null) {
        throw this.ioException;
    }
    if (this.servletException != null) {
        throw this.servletException;
    }

    try {
        Thread.sleep(sleepTimeInMillis);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (contentType != null) {
        response.setContentType(contentType);
    }
    if (outputData != null) {
        response.setContentLength(outputData.toString().length());
        response.getOutputStream().write(outputData);
    }

    if (response.getOutputStream() != null) {
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

}

From source file:org.infoscoop.web.I18NFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    BufferHttpServletResponseWrapper bufResponse = new BufferHttpServletResponseWrapper(
            (HttpServletResponse) response);
    chain.doFilter(request, bufResponse);
    bufResponse.flushBuffer();/*from   w w w  . ja v  a  2  s . com*/
    String body = bufResponse.getStringContent();

    try {
        body = I18NUtil.resolve(type, body, request.getLocale());
    } catch (Exception e) {
        log.error("database error occurred. ", e);
    }

    if (bufResponse.getLocale() != null)
        response.setLocale(bufResponse.getLocale());
    response.setContentLength(body.getBytes("utf-8").length);

    Writer out = null;
    try {
        out = new OutputStreamWriter(response.getOutputStream(), "utf-8");
        out.write(body);
        out.flush();
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:org.ireland.jnetty.webapp.RequestDispatcherImpl.java

private void finishResponse(ServletResponse res) throws ServletException, IOException {

    if (res instanceof HttpServletResponseImpl) {
        res.flushBuffer(); // we sure that all data has already put to the ByteBuf?
    } else {//ww  w. j  a  v  a 2  s. c o  m
        try {
            OutputStream os = res.getOutputStream();
            os.flush();
            // os.close();
        } catch (Exception e) {
        }

        try {
            PrintWriter out = res.getWriter();
            out.flush();
            // out.close();
        } catch (Exception e) {
        }
    }
}

From source file:org.kuali.rice.devtools.pdle.PostDataLoadEncryptionServlet.java

@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    String attributesToEncryptPropertyFileName = request.getParameter(ATTRIBUTES_TO_ENCRYPT_PROPERTIES);
    if (StringUtils.isBlank(attributesToEncryptPropertyFileName)) {
        throw new IllegalArgumentException(
                "No valid " + ATTRIBUTES_TO_ENCRYPT_PROPERTIES + " parameter was passed to this Servlet.");
    }//from   w  ww .j  av  a 2 s .c o m
    boolean checkOjbEncryptConfig = true;
    String checkOjbEncryptConfigValue = request.getParameter(CHECK_OJB_ENCRYPT_CONFIG);
    if (!StringUtils.isBlank(checkOjbEncryptConfigValue)) {
        checkOjbEncryptConfig = Boolean.valueOf(checkOjbEncryptConfigValue).booleanValue();
    }
    execute(attributesToEncryptPropertyFileName, checkOjbEncryptConfig);
    response.getOutputStream().write(("<html><body><p>Successfully encrypted attributes as defined in: "
            + attributesToEncryptPropertyFileName + "</p></body></html>").getBytes());
}

From source file:org.onehippo.forge.hst.pdf.renderer.servlet.HtmlPDFRenderingFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(response instanceof HttpServletResponse)) {
        chain.doFilter(request, response);
        return;/*ww w .ja  v a 2s .  c  o  m*/
    }

    if (response.isCommitted()) {
        log.warn("The servlet response is already committed for the request: '{}'.",
                ((HttpServletRequest) request).getRequestURI());
        chain.doFilter(request, response);
        return;
    }

    ContentCapturingHttpServletResponse capturingResponse = null;
    InputStream htmlInputStream = null;
    Reader htmlReader = null;
    OutputStream pdfOutputStream = null;

    try {
        capturingResponse = new ContentCapturingHttpServletResponse((HttpServletResponse) response);
        chain.doFilter(request, capturingResponse);

        if (capturingResponse.isWrittenToPrintWriter()) {
            htmlReader = new CharArrayReader(capturingResponse.toCharArray());
        } else {
            htmlInputStream = new ByteArrayInputStream(capturingResponse.toByteArray());
            String characterEncoding = StringUtils.defaultIfBlank(capturingResponse.getCharacterEncoding(),
                    "UTF-8");
            htmlReader = new InputStreamReader(htmlInputStream, characterEncoding);
        }

        capturingResponse.close();
        capturingResponse = null;

        response.setContentType("application/pdf");

        StringBuilder headerValue = new StringBuilder("attachment");
        headerValue.append("; filename=\"").append(getDocumentFilename((HttpServletRequest) request))
                .append("\"");
        ((HttpServletResponse) response).addHeader("Content-Disposition", headerValue.toString());

        pdfOutputStream = response.getOutputStream();
        pdfRenderer.renderHtmlToPDF(htmlReader, true, pdfOutputStream,
                getDocumentURL((HttpServletRequest) request),
                getExternalLinkBaseURL((HttpServletRequest) request));
    } catch (Exception e) {

    } finally {
        if (capturingResponse != null) {
            capturingResponse.close();
        }

        IOUtils.closeQuietly(pdfOutputStream);
        IOUtils.closeQuietly(htmlReader);
        IOUtils.closeQuietly(htmlInputStream);
    }
}

From source file:org.opencms.jsp.jsonpart.CmsJsonPartFilter.java

/**
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 *//*from  ww w  .  j  ava2  s.  c o m*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (isJsonRequest(request)) {
        if (m_isNested.get() == null) {
            try {
                m_isNested.set(Boolean.TRUE);
                RequestWrapper reqWrapper = new RequestWrapper((HttpServletRequest) request);
                ResponseWrapper resWrapper = new ResponseWrapper((HttpServletResponse) response);
                chain.doFilter(reqWrapper, resWrapper);
                byte[] data = resWrapper.getBytes();
                String content = new String(data, resWrapper.getCharacterEncoding());
                String transformedContent = transformContent(content);
                byte[] transformedData = transformedContent.getBytes("UTF-8");
                response.setContentType("application/json; charset=UTF-8");
                response.setContentLength(transformedData.length);
                response.getOutputStream().write(transformedData);
                response.getOutputStream().flush();
            } finally {
                m_isNested.set(null);
            }
        }
    } else {
        chain.doFilter(request, response);
    }
}

From source file:org.opencms.loader.CmsDumpLoader.java

/**
 * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
 *///w  ww  .jav a 2 s.c  o m
public void service(CmsObject cms, CmsResource resource, ServletRequest req, ServletResponse res)
        throws CmsException, IOException {

    res.getOutputStream().write(cms.readFile(resource).getContents());
}