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:cherry.foundation.download.DownloadTemplateTest.java

private HttpServletResponse createMock() throws IOException {
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletOutputStream out = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(out);
    return response;
}

From source file:com.java2s.SendFilter.java

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

    //get the file name from the 'file' parameter
    String fileName = request.getParameter("file");
    if (fileName == null || fileName.equals(""))
        throw new ServletException("Invalid or non-existent file parameter in SendPdf component.");

    if (fileName.indexOf(".pdf") == -1)
        fileName = fileName + ".pdf";

    ServletOutputStream stream = null;// w  ww  .jav a2 s.c o m
    BufferedInputStream buf = null;
    HttpServletResponse httpResp = null;
    try {

        httpResp = (HttpServletResponse) response;
        stream = httpResp.getOutputStream();
        File pdf = new File(PDF_DIR + "/" + fileName);

        //set response headers
        httpResp.setContentType(PDF_CONTENT_TYPE);
        httpResp.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        httpResp.setContentLength((int) pdf.length());

        FileInputStream input = new FileInputStream(pdf);
        buf = new BufferedInputStream(input);
        int readBytes = 0;
        //read from the file; write to the ServletOutputStream
        while ((readBytes = buf.read()) != -1)
            stream.write(readBytes);

    } catch (Exception ioe) {

        //  throw new ServletException(ioe.getMessage());
        System.out.println(ioe.getMessage());

    } finally {

        if (buf != null)
            buf.close();
        if (stream != null) {
            stream.flush();
            //stream.close();
        }

    } //end finally
    chain.doFilter(request, httpResp);
}

From source file:com.kelveden.rastajax.servlet.DefaultJsonServlet.java

private void writeRepresentationToResponse(JsonNode representation, HttpServletResponse httpResponse)
        throws IOException {

    httpResponse.setContentType("application/json; charset=utf8");

    final OutputStream outputStream = httpResponse.getOutputStream();

    outputStream.write(representation.toString().getBytes("UTF-8"));
    outputStream.flush();/*www  .  j av a 2 s .  c o  m*/
}

From source file:org.openmrs.module.odkconnector.web.controller.concept.AutocompleteConceptController.java

@RequestMapping(method = RequestMethod.GET)
public void search(final @RequestParam(required = true, value = "searchTerm") String searchTerm,
        final HttpServletResponse response) throws Exception {
    List<Concept> concepts = Context.getConceptService().getConceptsByName(searchTerm);
    OutputStream stream = response.getOutputStream();

    JsonFactory f = new JsonFactory();
    JsonGenerator g = f.createJsonGenerator(stream, JsonEncoding.UTF8);
    g.useDefaultPrettyPrinter();/*from w  ww  .  j  a  v  a  2s.  c o m*/

    g.writeStartObject();
    g.writeArrayFieldStart("elements");

    for (Concept concept : concepts) {
        g.writeStartObject();
        g.writeStringField("uuid", concept.getUuid());
        g.writeStringField("name", concept.getDisplayString());
        g.writeEndObject();
    }
    g.writeEndArray();
    g.writeEndObject();

    g.close();
}

From source file:org.jasig.portlet.calendar.mvc.EmptyView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(),
            encoding);/*from  www.ja  v a  2  s  . c om*/
    generator.writeRaw("    ");
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.network.ServletReceiver.java

/** Send the given reponse to the client waiting for the given HttpServletResponse */
protected void respond(HttpServletResponse r, Response res) {
    try {//from ww  w  .ja va 2  s . co m
        OutputStream out = r.getOutputStream();
        ObjectOutputStream outputToApplet = new ObjectOutputStream(out);
        outputToApplet.writeObject(res);

        outputToApplet.flush();
        outputToApplet.close();
    } catch (IOException e) {
        log.error("Failed to respond to request", e);
    }
}

From source file:ru.ttk.baloo.rest.security.oauth.Logout.java

@Override
public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        Authentication authentication) throws IOException, ServletException {

    this.removeAccess(httpServletRequest);
    httpServletResponse.getOutputStream().write(BYE_MESSAGE.getBytes());
}

From source file:org.apache.hadoop.gateway.dispatch.HttpClientDispatchTest.java

@Test
public void testJiraKnox58() throws URISyntaxException, IOException {

    URI uri = new URI("http://unreachable-host");
    BasicHttpParams params = new BasicHttpParams();

    HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpUriRequest.class);
    EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes();
    EasyMock.expect(outboundRequest.getURI()).andReturn(uri).anyTimes();
    EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes();

    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);

    HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<ServletOutputStream>() {
        @Override/*from  w ww . ja  va 2  s .c om*/
        public ServletOutputStream answer() throws Throwable {
            return new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host");
                }
            };
        }
    });

    EasyMock.replay(outboundRequest, inboundRequest, outboundResponse);

    HttpClientDispatch dispatch = new HttpClientDispatch();
    try {
        dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
        fail("Should have thrown IOException");
    } catch (IOException e) {
        assertThat(e.getMessage(), not(containsString("unreachable-host")));
        assertThat(e, not(instanceOf(UnknownHostException.class)));
        assertThat("Message needs meaningful content.", e.getMessage().trim().length(), greaterThan(12));
    }
}

From source file:org.apache.servicemix.http.endpoints.SerializedMarshaler.java

public void sendOut(MessageExchange exchange, NormalizedMessage outMsg, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    if (outMsg.getContent() != null) {
        unmarshal(response.getOutputStream(), outMsg.getContent());
    }//from  w  w  w.  j a  va2s  .  c  o m
}

From source file:edu.usu.sdl.opencatalog.web.action.BaseAction.java

protected Resolution formLoadResults(final JsonFormLoad data) {
    return new StreamingResolution(MediaType.APPLICATION_JSON) {

        @Override/*from  w ww .  java2s  .  c o  m*/
        protected void stream(HttpServletResponse response) throws Exception {
            objectMapper.writeValue(response.getOutputStream(), data);
        }
    };
}