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.groupdocs.ui.Utils.java

public static int writeToResponse(InputStream input, ServletResponse response) {
    try {//from w w w .j  a v  a  2 s  . c  o m
        return Utils.copyStream(input, response.getOutputStream());
    } catch (IOException x) {
        throw new UncheckedIOException(x);
    }
}

From source file:com.tek271.reverseProxy.servlet.ContentUtils.java

public static void copyBinary(HttpEntity entity, ServletResponse response) {
    try {/*  w  ww  .  j ava2 s  . c o  m*/
        InputStream content = entity.getContent();
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(content, outputStream);
    } catch (IOException e) {
        Throwables.propagate(e);
    }
}

From source file:com.feilong.taglib.display.barcode.BarcodeServlet.java

/**
 * Encode./*w  w w  .  j a  va  2  s. c  o  m*/
 *
 * @param barcodeContentsAndConfig
 *            the barcode contents and config
 * @param response
 *            the response
 */
private static void render(BarcodeContentsAndConfig barcodeContentsAndConfig, ServletResponse response) {
    try {
        ServletOutputStream outputStream = response.getOutputStream();
        BarcodeEncodeUtil.encode(barcodeContentsAndConfig.getContents(), outputStream,
                barcodeContentsAndConfig.getBarcodeConfig());
    } catch (IOException e) {
        String message = Slf4jUtil.format("barcodeContentsAndConfig:{}",
                JsonUtil.format(barcodeContentsAndConfig));
        LOGGER.error(message, e);
        throw new UncheckedIOException(message, e);
    }
}

From source file:com.meltmedia.cadmium.servlets.ErrorPageFilterSelectionTest.java

/**
 * Creates a filter chain that sends a 200 response.
 * /*from  w  w w. j av a2  s .c om*/
 * @param content the content to write to the res object.
 * @return a filter chain that simulates a 200 response.
 */
public static FilterChain successFilterChain(final String content) {
    return new FilterChain() {
        @Override
        public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
            HttpServletResponse httpRes = (HttpServletResponse) res;
            httpRes.setStatus(200);
            OutputStream out = null;
            InputStream in = null;
            try {
                out = res.getOutputStream();
                in = new ByteArrayInputStream(content.getBytes());
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(out);
                IOUtils.closeQuietly(in);
            }
        }
    };
}

From source file:net.javacrumbs.test.AbstractServlet.java

/**
 * Copies backend response to servlet response.
 *
 * @param response//  w  w w  .  ja  v a 2 s .  c  om
 * @param resp
 * @throws IOException
 */
protected void copyResultToResponse(HttpResponse response, ServletResponse resp) throws IOException {
    resp.setContentType(CONTENT_TYPE);
    final ServletOutputStream outputStream = resp.getOutputStream();
    response.getEntity().writeTo(outputStream);
}

From source file:org.jasig.cas.web.LicenceFilter.java

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

    if (this.expiretime == 0l) {
        response.getOutputStream().println("License failed to load");
    } else if (this.expiretime <= new Date().getTime()) {
        response.getOutputStream().println("License has expired");
        return;//from   w ww. j a v  a  2  s.  com
    }
    chain.doFilter(request, response);
}

From source file:net.sf.j2ep.responsehandlers.ResponseHandlerBase.java

/**
 * Writes the entire stream from the method to the response
 * stream./*from  w ww.jav  a  2  s .c  om*/
 * 
 * @param response Response to send data to
 * @throws IOException An IOException is thrown when we are having problems with reading the streams
 */
protected void sendStreamToClient(ServletResponse response) throws IOException {
    InputStream streamFromServer = method.getResponseBodyAsStream();
    OutputStream responseStream = response.getOutputStream();

    if (streamFromServer != null) {
        byte[] buffer = new byte[1024];
        int read = streamFromServer.read(buffer);
        while (read > 0) {
            responseStream.write(buffer, 0, read);
            read = streamFromServer.read(buffer);
        }
        streamFromServer.close();

    }
    responseStream.flush();
    responseStream.close();
}

From source file:com.ibm.amc.feedback.FeedbackHandler.java

private void writeResponse(final ServletResponse response, final Queue<ActionStatusResponse> statuses) {
    try {/* w  ww . ja v  a2s .  c  om*/
        mapper.writeValue(response.getOutputStream(), statuses);
    } catch (Exception e) {
        logger.debug("writeResponse", "Exception writing response", e);
    }
}

From source file:org.ventiv.webjars.requirejs.servlet.RequireJsConfigServlet.java

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    if (res instanceof HttpServletResponse) {
        ((HttpServletResponse) res).setHeader("Content-Type", "application/javascript");
    }/*from   w w  w . j a  v  a2 s  .com*/

    res.getOutputStream().print("require.config(");
    if (prettyPrint)
        res.getOutputStream().print(mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(requireJsConfigBuilder.buildConfig()));
    else
        res.getOutputStream().print(mapper.writeValueAsString(requireJsConfigBuilder.buildConfig()));
    res.getOutputStream().print(");\n\n");

    // Append our special loader
    res.getOutputStream().println("var scripts = document.getElementsByTagName('script');");
    res.getOutputStream().println("for (var i = 0; i < scripts.length; i++) {");
    res.getOutputStream().println("    var script = scripts[i];");
    res.getOutputStream().println("    var src = script.getAttribute('src');");
    res.getOutputStream().println("    var dataMain = script.getAttribute('data-main');");
    res.getOutputStream().println("    var dataApp = script.getAttribute('data-app');");
    res.getOutputStream().println("    var baseUrl = script.getAttribute('data-base-url');");
    res.getOutputStream().println("    if (baseUrl)");
    res.getOutputStream().println("        require.config({ baseUrl: baseUrl });\n");
    res.getOutputStream().println("    if (dataMain && dataApp && src.indexOf(\"require\") > -1) {");
    res.getOutputStream().println("        require([dataApp], function() {});");
    res.getOutputStream().println("    }");
    res.getOutputStream().println("}");
}

From source file:edu.harvard.hms.dbmi.bd2k.i2b2proxy.filter.SessionFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
        throws IOException, ServletException {

    String user = validateAuthorizationHeader((HttpServletRequest) req);

    if (user == null) {
        ((HttpServletResponse) res).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        res.getOutputStream().write("{\"message\":\"Session is not authorized\"}".getBytes());
        res.getOutputStream().close();//from   www . j  a v a2 s  .com
        return;
    }

    HttpSession session = ((HttpServletRequest) req).getSession();
    session.setAttribute("user", user);

    fc.doFilter(req, res);
}