Example usage for javax.servlet.http HttpServletResponse getOutputStream

List of usage examples for javax.servlet.http HttpServletResponse getOutputStream

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse 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.ykun.commons.utils.excel.ExcelUtils.java

/**
 * xls//from ww w.j  a v  a  2s .c o  m
 *
 * @param <T>      the type parameter
 * @param response the response
 * @param list     the list
 * @param headers  the headers
 * @param fileName the file name
 * @throws IOException the io exception
 */
public static <T> void download(HttpServletResponse response, List<T> list, String[] headers, String fileName)
        throws IOException {
    setContentType(response, fileName);
    export(list, headers, response.getOutputStream());
}

From source file:cz.cas.lib.proarc.authentication.utils.AuthUtils.java

/**
 * Writes the authentication required status to the HTTP response.
 * @param response response//from  w ww.  j a  v a  2  s  . c  o  m
 * @throws IOException failure
 * @see #HEADER_AUTHENTICATE_TYPE
 * @see <a href='http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/Relogin.html'>SmartGWT Relogin</a>
 */
public static void setLoginRequiredResponse(HttpServletResponse response) throws IOException {
    response.setHeader(HEADER_AUTHENTICATE_TYPE, Authenticators.getInstance().getLoginType());
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.setContentType(MediaType.TEXT_HTML);
    InputStream res = ProarcAuthFilter.class.getResourceAsStream("loginRequiredMarker.html");
    try {
        IOUtils.copy(res, response.getOutputStream());
        res.close();
    } finally {
        IOUtils.closeQuietly(res);
    }
}

From source file:com.ykun.commons.utils.excel.ExcelUtils.java

/**
 * xls/*from   ww  w . j av  a 2  s  .  co m*/
 *
 * @param <T>      the type parameter
 * @param response the response
 * @param list     the list
 * @param headers  the headers
 * @param fileName the file name
 * @throws IOException the io exception
 */
public static <T> void download(HttpServletResponse response, List<T> list, List<String> headers,
        String fileName) throws IOException {
    setContentType(response, fileName);
    export(list, headers, response.getOutputStream());
}

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

public static HttpServletResponse mockResponse(OutputStream output) throws IOException {
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getOutputStream()).thenReturn(new MockServletOutputStream(output));
    return response;
}

From source file:com.runwaysdk.controller.ErrorUtility.java

public static boolean prepareThrowable(Throwable t, HttpServletRequest req, HttpServletResponse resp,
        Boolean isAsynchronus, boolean ignoreNotifications) throws IOException {
    return prepareThrowable(t, req, resp.getOutputStream(), resp, isAsynchronus, ignoreNotifications);
}

From source file:com.shz.foundation.utils.Servlets.java

/**
 * //from w  ww  .j av  a 2  s. com
 * @param response
 * @param image 
 * @param sufix ?
 */
public static void writeBufferedImage(HttpServletResponse response, BufferedImage image, String sufix)
        throws IOException {
    response.setHeader("Cache-Control", "private,no-cache,no-store");
    response.setContentType("image/" + sufix);
    ImageIO.write(image, sufix, response.getOutputStream());
    response.getOutputStream().close();
}

From source file:de.mpg.escidoc.services.fledgeddata.webservice.oaiServlet.java

/**
 * Get a response Writer depending on acceptable encodings
 * @param request the servlet's request information
 * @param response the servlet's response information
 * @exception IOException an I/O error occurred
 *//*from  www .  j  a v  a  2 s  . c  om*/
public static Writer getWriter(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Writer out;
    String encodings = request.getHeader("Accept-Encoding");

    if (encodings != null && encodings.indexOf("gzip") != -1) {
        response.setHeader("Content-Encoding", "gzip");
        out = new OutputStreamWriter(new GZIPOutputStream(response.getOutputStream()), "UTF-8");
    } else if (encodings != null && encodings.indexOf("deflate") != -1) {
        response.setHeader("Content-Encoding", "deflate");
        out = new OutputStreamWriter(new DeflaterOutputStream(response.getOutputStream()), "UTF-8");
    } else {
        out = response.getWriter();
    }
    return out;
}

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

public static HttpServletResponse mockResponse(final Map<String, List<String>> headers, OutputStream output)
        throws IOException {
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getOutputStream()).thenReturn(new MockServletOutputStream(output));
    if (headers != null) {
        Answer<Void> addHeaderAnswer = new Answer<Void>() {
            @Override/*from w ww.  j a  v  a 2 s .c o m*/
            public Void answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                List<String> value = headers.get((String) args[0]);
                if (value == null)
                    headers.put((String) args[0], value = new ArrayList<String>());
                value.add(args[1].toString());
                return null;
            }
        };
        Answer<Void> setHeaderAnswer = new Answer<Void>() {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                List<String> value = headers.get((String) args[0]);
                if (value == null)
                    headers.put((String) args[0], value = new ArrayList<String>());
                value.clear();
                value.add(args[1].toString());
                return null;
            }
        };
        doAnswer(setHeaderAnswer).when(response).setHeader(anyString(), anyString());
        doAnswer(setHeaderAnswer).when(response).setDateHeader(anyString(), anyLong());
        doAnswer(setHeaderAnswer).when(response).setIntHeader(anyString(), anyInt());
        doAnswer(addHeaderAnswer).when(response).addHeader(anyString(), anyString());
        doAnswer(addHeaderAnswer).when(response).addDateHeader(anyString(), anyLong());
        doAnswer(addHeaderAnswer).when(response).addIntHeader(anyString(), anyInt());
    }
    return response;
}

From source file:org.duracloud.duradmin.util.SpaceUtil.java

public static void streamToResponse(InputStream is, HttpServletResponse response, String mimetype,
        String contentLength) throws ContentStoreException, IOException {

    OutputStream outStream = response.getOutputStream();

    try {/*w w  w.  j  a v a 2  s . c om*/
        response.setContentType(mimetype);

        if (contentLength != null) {
            response.setContentLengthLong(Long.parseLong(contentLength));
        }
        byte[] buf = new byte[1024];
        int read = -1;
        while ((read = is.read(buf)) > 0) {
            outStream.write(buf, 0, read);
        }

        response.flushBuffer();
    } catch (Exception ex) {
        if (ex.getCause() instanceof ContentStateException) {
            response.reset();
            response.setStatus(HttpStatus.SC_CONFLICT);
            String message = "The requested content item is currently in long-term storage"
                    + " with limited retrieval capability. Please contact "
                    + "DuraCloud support (https://wiki.duraspace.org/x/6gPNAQ) "
                    + "for assistance in retrieving this content item.";
            //It is necessary to pad the message in order to force Internet Explorer to
            //display the server sent text rather than display the browser default error message.
            //If the message is less than 512 bytes, the browser will ignore the message.
            //c.f. http://support.microsoft.com/kb/294807
            message += StringUtils.repeat(" ", 512);
            outStream.write(message.getBytes());
        } else {
            throw ex;
        }
    } finally {
        try {
            outStream.close();
        } catch (Exception e) {
            log.warn("failed to close outputstream ( " + outStream + "): message=" + e.getMessage(), e);
        }
    }
}

From source file:com.yiji.openapi.sdk.util.Servlets.java

public static void writeResponse(HttpServletResponse response, String data) {
    OutputStream output = null;/*from   ww  w  .ja va  2  s.  c  om*/
    InputStream input = null;
    try {
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        output = response.getOutputStream();
        input = new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
        IOUtils.copy(input, output);
        output.flush();
    } catch (Exception e) {
        throw new RuntimeException("?(flushResponse):" + e.getMessage());
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
}