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:com.loadtesting.showcase.springmvc.filter.PatternFilter.java

/**
 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 *///  w  w w .jav  a  2  s  . c  om
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    ExampleKpiCapturers caps = new ExampleKpiCapturers(request, PatternLayer.list());
    WebProfilingFormat webFormat;
    try {
        caps.startDefaultCapturer();

        CountingOutputStream cos = new CountingOutputStream(response.getOutputStream());
        chain.doFilter(request, response);
        webFormat = new WebProfilingFormat(request.getContentLength(), cos.getByteCount());
    } finally {
        caps.endDefaultCapturer();
    }

    sendReports(webFormat, caps);
}

From source file:com.loadtesting.showcase.springmvc.filter.LoadTestFilter.java

/**
 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 *//*from  w w  w .  ja va 2s.  c om*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    ExampleKpiCapturers caps = new ExampleKpiCapturers(request, LoadTestLayer.list());
    WebProfilingFormat webFormat;
    try {
        caps.startDefaultCapturer();

        CountingOutputStream cos = new CountingOutputStream(response.getOutputStream());
        chain.doFilter(request, response);
        webFormat = new WebProfilingFormat(request.getContentLength(), cos.getByteCount());
    } finally {
        caps.endDefaultCapturer();
    }

    sendReports(webFormat, caps);
}

From source file:jp.co.opentone.bsol.linkbinder.view.filter.UploadFileFilter.java

private void writeResponse(ServletRequest req, ServletResponse res, UploadFileFilterResult result)
        throws IOException {
    res.setContentType("text/plain;charset=" + req.getCharacterEncoding());
    OutputStream out = res.getOutputStream();
    out.write(JSON.encode(result).getBytes(req.getCharacterEncoding()));
}

From source file:fr.putnami.pwt.plugin.ajaxbot.filter.AjaxPageFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;

    if (HttpMethod.GET.toString().equals(request.getMethod())
            && req.getParameter(QUERY_PARAM_ESCAPED_FRAGMENT) != null) {
        ByteStreams.copy(this.getHtmlStream(request), resp.getOutputStream());
    } else {/*from w w w . j a  va2 s .c  om*/
        chain.doFilter(req, resp);
    }
}

From source file:org.apache.cxf.fediz.spring.web.FederationSignOutCleanupFilter.java

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

    String wa = request.getParameter(FederationConstants.PARAM_ACTION);
    if (FederationConstants.ACTION_SIGNOUT_CLEANUP.equals(wa)) {
        if (request instanceof HttpServletRequest) {
            ((HttpServletRequest) request).getSession().invalidate();
        }/*from ww w .  j a  v a 2  s.  c  o  m*/

        final ServletOutputStream responseOutputStream = response.getOutputStream();
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("logout.jpg");
        if (inputStream == null) {
            LOG.warn("Could not write logout.jpg");
            return;
        }
        int read = 0;
        byte[] buf = new byte[1024];
        while ((read = inputStream.read(buf)) != -1) {
            responseOutputStream.write(buf, 0, read);
        }
        inputStream.close();
        responseOutputStream.flush();
    } else {
        chain.doFilter(request, response);
    }
}

From source file:se.inera.certificate.proxy.mappings.remote.RemoteDispatcher.java

private void sendResponseEntity(ServletResponse response, HttpEntity entity) throws IOException {
    // Entity can be null in case of 304 answer.
    if (entity != null) {
        Header contentType = entity.getContentType();

        if (contentType != null) {
            response.setContentType(contentType.getValue());
        }//w w  w  .j a v a  2 s .  c om
        copyLarge(entity.getContent(), response.getOutputStream());
    }
    response.getOutputStream().flush();
}

From source file:com.dianping.phoenix.router.ResponseFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (response instanceof HttpServletResponse && request instanceof HttpServletRequest) {
        HttpServletRequest hRequest = (HttpServletRequest) request;
        if (StringUtils.equalsIgnoreCase(SCRIPT_NAME, hRequest.getRequestURI())) {
            IOUtils.copy(this.getClass().getResourceAsStream(SCRIPT_NAME), response.getOutputStream());
            return;
        }//from  w  w w. j  a  v a  2 s .c  o m

        StatusAwareServletResponse statusAwareServletResponse = new StatusAwareServletResponse(
                (HttpServletResponse) response);

        chain.doFilter(request, statusAwareServletResponse);

        if (ACCEPTED_STATUS_CODE.contains(statusAwareServletResponse.getStatus())
                && (StringUtils.isBlank(statusAwareServletResponse.getContentType()) || ACCEPTED_CONTENT_TYPE
                        .contains(statusAwareServletResponse.getContentType().toLowerCase()))
                && hRequest.getHeader("x-requested-with") == null) {
            statusAwareServletResponse.getOutputStream().write(RESPONSE_APPEND_TEXT);
        }

    } else {
        chain.doFilter(request, response);
    }

}

From source file:com.medlog.webservice.lifecycle.Security.java

private void sendProcessingError(Throwable t, ServletResponse response) {
    String stackTrace = getStackTrace(t);

    if (stackTrace != null && !stackTrace.equals("")) {
        try {//from w w  w.j  a  v  a  2 s. c  om
            response.setContentType("text/html");
            PrintStream ps = new PrintStream(response.getOutputStream());
            PrintWriter pw = new PrintWriter(ps);
            pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N

            // PENDING! Localize this for next official release
            pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
            pw.print(stackTrace);
            pw.print("</pre></body>\n</html>"); //NOI18N
            pw.close();
            ps.close();
            response.getOutputStream().close();
        } catch (Exception ex) {
        }
    } else {
        try {
            PrintStream ps = new PrintStream(response.getOutputStream());
            t.printStackTrace(ps);
            ps.close();
            response.getOutputStream().close();
        } catch (Exception ex) {
        }
    }
}

From source file:com.metamesh.opencms.rfs.RfsFileLoader.java

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

    RfsFile rfsFile = (RfsFile) resource;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(rfsFile.getRfsPath());
        byte[] buffy = new byte[4096];
        int bytesRead;
        while ((bytesRead = fis.read(buffy)) >= 0) {
            res.getOutputStream().write(buffy, 0, bytesRead);
            res.getOutputStream().flush();
        }
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}