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:InsertClobToMySqlServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String clobData = null;//from   w  ww.ja  va2 s .c om
    Connection conn = null;

    String id = "001";
    String name = "fileName";
    String fileAsURL = "http://yourwebsite/fileName.dat";
    ServletOutputStream out = response.getOutputStream();

    response.setContentType("text/html");
    out.println("<html><head><title>Insert Clob To MySql Servlet</title></head>");

    try {
        conn = getConnection();
        clobData = getClobsContentAsString(fileAsURL);
        insertCLOB(conn, id, name, clobData);
        out.println("<body><h4>OK: inserted a new record with id=" + id + "</h4></body></html>");
    } catch (Exception e) {
        e.printStackTrace();
        out.println("<body><h4>Error: " + e.getMessage() + "</h4></body></html>");
    }
}

From source file:cs544.wamp_blog_engine.controller.UserController.java

@RequestMapping(value = "/image/{id}", method = RequestMethod.GET)
public void getUserImage(Model model, @PathVariable int id, HttpServletResponse response) {
    try {/*from w  w w.  j  a v a2s. c  o m*/
        User u = userService.getUser(id);
        if (u != null) {
            OutputStream out = response.getOutputStream();
            out.write(u.getProfilepic());
            response.flushBuffer();
        }
    } catch (IOException ex) {
        Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.apache.hadoop.gateway.rm.dispatch.RMHaDispatchTest.java

@Test
public void testConnectivityFailure() throws Exception {
    String serviceName = "RESOURCEMANAGER";
    HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
    descriptor.addServiceConfig(/*  w w  w  .ja  v a 2s . c  om*/
            HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000", null, null));
    HaProvider provider = new DefaultHaProvider(descriptor);
    URI uri1 = new URI("http://unreachable-host");
    URI uri2 = new URI("http://reachable-host");
    ArrayList<String> urlList = new ArrayList<>();
    urlList.add(uri1.toString());
    urlList.add(uri2.toString());
    provider.addHaService(serviceName, urlList);
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);

    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME))
            .andReturn(provider).anyTimes();

    BasicHttpParams params = new BasicHttpParams();

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

    HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once();
    EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0))
            .once();
    EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1))
            .once();

    HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<ServletOutputStream>() {
        @Override
        public ServletOutputStream answer() throws Throwable {
            return new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    throw new IOException("unreachable-host");
                }

                @Override
                public void setWriteListener(WriteListener arg0) {
                }

                @Override
                public boolean isReady() {
                    return false;
                }
            };
        }
    }).once();
    EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse);
    Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName));
    RMHaDispatch dispatch = new RMHaDispatch();
    dispatch.setHttpClient(new DefaultHttpClient());
    dispatch.setHaProvider(provider);
    dispatch.init();
    long startTime = System.currentTimeMillis();
    try {
        dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse);
    } catch (IOException e) {
        //this is expected after the failover limit is reached
    }
    long elapsedTime = System.currentTimeMillis() - startTime;
    Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName));
    //test to make sure the sleep took place
    Assert.assertTrue(elapsedTime > 1000);
}

From source file:com.cloud.servlet.StaticResourceServletTest.java

private HttpServletResponse doGetTest(final String uri, final Map<String, String> headers)
        throws ServletException, IOException {
    final StaticResourceServlet servlet = Mockito.mock(StaticResourceServlet.class);
    Mockito.doCallRealMethod().when(servlet).doGet(Matchers.any(HttpServletRequest.class),
            Matchers.any(HttpServletResponse.class));
    final ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getRealPath(uri)).thenReturn(new File(rootDirectory, uri).getAbsolutePath());
    Mockito.when(servlet.getServletContext()).thenReturn(servletContext);

    final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getServletPath()).thenReturn(uri);
    Mockito.when(request.getHeader(Matchers.anyString())).thenAnswer(new Answer<String>() {

        @Override//from w w  w.  j  a  va  2  s. com
        public String answer(final InvocationOnMock invocation) throws Throwable {
            return headers.get(invocation.getArguments()[0]);
        }
    });
    final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    final ServletOutputStream responseBody = Mockito.mock(ServletOutputStream.class);
    Mockito.when(response.getOutputStream()).thenReturn(responseBody);
    servlet.doGet(request, response);
    return response;
}

From source file:org.craftercms.engine.http.impl.HttpProxyImpl.java

protected void copyActualResponseBody(String responseBody, HttpServletResponse response) throws IOException {
    if (responseBody != null) {
        response.setContentLength(responseBody.length());
        OutputStream out = response.getOutputStream();
        out.write(responseBody.getBytes());
        out.flush();/*w  w  w . j  a  v  a 2s.c  om*/
    }
}

From source file:com.netflix.hollow.diff.ui.HollowUIRouter.java

protected boolean serveResource(HttpServletRequest req, HttpServletResponse resp, String resourceName) {
    try {//from w w  w  .java 2  s.  c om
        if (req.getParameter("contentType") != null) {
            resp.setContentType(resourceName);
        }

        InputStream is = this.getClass().getResourceAsStream("/" + resourceName);

        IOUtils.copy(is, resp.getOutputStream());
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.qcadoo.view.internal.resource.module.UniversalResourceModule.java

@Override
public boolean serveResource(final HttpServletRequest request, final HttpServletResponse response) {
    Resource resource = getResourceFromURI(request.getRequestURI());
    if (resource != null && resource.exists()) {
        response.setContentType(getContentTypeFromURI(request));
        try {/*from   ww  w.j a  va  2s. co  m*/
            IOUtils.copy(resource.getInputStream(), response.getOutputStream());
        } catch (IOException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
        return true;
    } else {
        return false;
    }
}

From source file:com.thoughtworks.go.server.security.BasicAuthenticationFilter.java

private void generateResponse(HttpServletResponse httpResponse, String type, String msg) throws IOException {
    httpResponse.addHeader("WWW-Authenticate", "Basic realm=\"GoCD\"");
    httpResponse.setContentType(type);// w  ww .  ja  v  a 2  s  . c  om
    httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    httpResponse.getOutputStream().print(msg);
}