Example usage for javax.servlet ServletOutputStream ServletOutputStream

List of usage examples for javax.servlet ServletOutputStream ServletOutputStream

Introduction

In this page you can find the example usage for javax.servlet ServletOutputStream ServletOutputStream.

Prototype


protected ServletOutputStream() 

Source Link

Document

Does nothing, because this is an abstract class.

Usage

From source file:org.nuxeo.ecm.core.io.download.TestDownloadService.java

@Test
public void testETagHeaderNoDigest() throws Exception {
    String blobValue = "Hello World";
    Blob blob = Blobs.createBlob(blobValue);
    blob.setFilename("myFile.txt");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getHeader("If-None-Match")).thenReturn("\"b10a8db164e0754105b7a99be72e3fe5\"");
    when(req.getMethod()).thenReturn("GET");

    HttpServletResponse resp = mock(HttpServletResponse.class);
    ServletOutputStream sos = new ServletOutputStream() {
        @Override// www  .j  av  a2s .c o m
        public void write(int b) throws IOException {
            out.write(b);
        }
    };
    @SuppressWarnings("resource")
    PrintWriter printWriter = new PrintWriter(sos);
    when(resp.getOutputStream()).thenReturn(sos);
    when(resp.getWriter()).thenReturn(printWriter);

    downloadService.downloadBlob(req, resp, null, null, blob, null, "test");

    verify(req, atLeastOnce()).getHeader("If-None-Match");
    assertEquals(0, out.toByteArray().length);
    verify(resp).sendError(HttpServletResponse.SC_NOT_MODIFIED);
}

From source file:org.nuxeo.ecm.core.io.download.TestDownloadService.java

@Test
@LocalDeploy("org.nuxeo.ecm.core.io.test:OSGI-INF/test-download-service-permission.xml")
public void testDownloadPermission() throws Exception {
    // blob to download
    String blobValue = "Hello World";
    Blob blob = Blobs.createBlob(blobValue);
    blob.setFilename("myfile.txt");
    blob.setDigest("12345");

    // mock request
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn("GET");

    // mock response
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletOutputStream sos = new ServletOutputStream() {
        @Override//  w  w  w.ja  v a2s.c om
        public void write(int b) throws IOException {
            out.write(b);
        }
    };
    @SuppressWarnings("resource")
    PrintWriter printWriter = new PrintWriter(sos);
    when(response.getOutputStream()).thenReturn(sos);
    when(response.getWriter()).thenReturn(printWriter);

    // mock document
    DocumentModel doc = mock(DocumentModel.class);
    when(doc.getPropertyValue("dc:format")).thenReturn("pdf");

    // extended infos with rendition
    String reason = "rendition";
    Map<String, Serializable> extendedInfos = Collections.singletonMap("rendition", "myrendition");

    // principal
    NuxeoPrincipal principal = new UserPrincipal("bob", Collections.singletonList("members"), false, false);

    // do tests while logged in
    LoginStack loginStack = ClientLoginModule.getThreadLocalLogin();
    loginStack.push(principal, null, null);
    try {
        // send download request for file:content, should be denied
        downloadService.downloadBlob(request, response, doc, "file:content", blob, null, reason, extendedInfos);
        assertEquals("", out.toString());
        verify(response, atLeastOnce()).sendError(403, "Permission denied");

        // but another xpath is allowed, per the javascript rule
        downloadService.downloadBlob(request, response, doc, "other:blob", blob, null, reason, extendedInfos);
        assertEquals(blobValue, out.toString());
    } finally {
        loginStack.pop();
    }
}

From source file:org.nuxeo.ecm.core.io.download.TestDownloadService.java

@Test
public void testTransientCleanup() throws IOException {
    // transfert temporary file into a blob
    Path path = Files.createTempFile("pfouh", "pfouh");
    FileBlob blob = new FileBlob("pfouh");
    Files.move(path, blob.getFile().toPath(), REPLACE_EXISTING);

    // store the blob for downloading
    String key = downloadService.storeBlobs(Collections.singletonList(blob));

    // mock request
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn("GET");

    // mock response
    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletOutputStream sos = new ServletOutputStream() {
        @Override/*from   w w w.j  a va 2 s  .com*/
        public void write(int b) throws IOException {
            out.write(b);
        }
    };
    @SuppressWarnings("resource")
    PrintWriter printWriter = new PrintWriter(sos);
    when(response.getOutputStream()).thenReturn(sos);
    when(response.getWriter()).thenReturn(printWriter);

    NuxeoPrincipal principal = new UserPrincipal("bob", Collections.singletonList("members"), false, false);

    // do tests while logged in
    LoginStack loginStack = ClientLoginModule.getThreadLocalLogin();
    loginStack.push(principal, null, null);

    try {
        downloadService.downloadBlob(request, response, key, "download");
    } finally {
        loginStack.pop();
    }

    // the file is gone
    assertFalse(blob.getFile().exists());
}

From source file:org.onehippo.forge.hst.pdf.renderer.servlet.ContentCapturingHttpServletResponse.java

public ServletOutputStream getOutputStream() throws IOException {
    if (servletOutputStream == null) {
        if (printWriter != null) {
            throw new IllegalStateException("getWriter() has already been called on this response");
        }//from  ww  w  .ja  v  a  2 s  .  com

        byteOutputBuffer = new ByteArrayOutputStreamBuffer();
        servletOutputStream = new ServletOutputStream() {
            @Override
            public void write(int b) throws IOException {
                byteOutputBuffer.write(b);
            }
        };
    }

    return servletOutputStream;
}

From source file:org.ovirt.vdsmfake.servlet.CustomHttpServletResponseWrapper.java

@Override
public ServletOutputStream getOutputStream() throws IOException {
    final ServletOutputStream fakeOutputStream = new ServletOutputStream() {
        @Override// ww w. ja  v  a 2  s. c  o  m
        public void write(int b) throws IOException {
            bos.write(b);
        }
    };

    return fakeOutputStream;
}

From source file:org.paxle.filter.webgraph.gui.PrefuseServletTest.java

public void testGenerateGraphImage() throws IOException, ServletException {
    final File testFile = new File(testDir, "test.png");
    if (testFile.exists())
        assertTrue(testFile.delete());//from   ww  w .  j  a  v  a2s.  c om

    assertTrue(testFile.createNewFile());
    final FileOutputStream fileOut = new FileOutputStream(testFile);
    checking(new Expectations() {
        {
            allowing(resp).setContentType("image/png");
            one(resp).getOutputStream();
            will(returnValue(new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    fileOut.write(b);
                }
            }));

            allowing(req).getParameter("view");
            will(returnValue("graph"));
        }
    });

    this.servlet.doGet(this.req, this.resp);

    fileOut.flush();
    fileOut.close();
}

From source file:org.paxle.filter.webgraph.gui.PrefuseServletTest.java

public void testGenerateGraphML() throws IOException, ServletException {
    final File testFile = new File(testDir, "test.xml");
    if (testFile.exists())
        assertTrue(testFile.delete());/*from  w w w  .j  a  va 2 s. c  om*/

    assertTrue(testFile.createNewFile());
    final FileOutputStream fileOut = new FileOutputStream(testFile);
    checking(new Expectations() {
        {
            allowing(resp).setContentType("text/xml");
            one(resp).getOutputStream();
            will(returnValue(new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    fileOut.write(b);
                }
            }));

            allowing(req).getParameter("view");
            will(returnValue("graphML"));
        }
    });

    this.servlet.doGet(this.req, this.resp);

    fileOut.flush();
    fileOut.close();
}

From source file:org.pentaho.platform.pdi.WebContextServletTest.java

@Before
public void setUp() throws Exception {
    webContextServlet = spy(new WebContextServlet());
    jsFile = new PlatformWebResource("analyzer", "scripts/includeMe.js");
    txtFile = new PlatformWebResource("analyzer", "scripts/includeMe.txt");

    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    when(mockRequest.getRequestURI()).thenReturn("fake/uri/" + WebContextServlet.WEB_CONTEXT_JS);
    when(mockRequest.getParameter(WebContextServlet.CONTEXT)).thenReturn("testContext");
    when(mockRequest.getParameter(WebContextServlet.LOCALE)).thenReturn("xp_TO");

    this.httpRequest = mockRequest;

    HttpServletResponse mockResponse = mock(HttpServletResponse.class);

    this.mockResponseOutputStream = new java.io.ByteArrayOutputStream();
    when(mockResponse.getOutputStream()).thenReturn(new ServletOutputStream() {
        @Override/*from  w ww  .j  ava  2  s  . c o m*/
        public void write(int b) throws IOException {
            WebContextServletTest.this.mockResponseOutputStream.write(b);
        }
    });

    this.httpResponse = mockResponse;
}

From source file:org.pentaho.platform.web.http.filters.PentahoWebContextFilterTest.java

@Before
public void setup() throws IOException, ServletException {
    String scheme = "https";
    String serverName = "di.pentaho.local";
    int port = 9055;

    this.serverAddress = scheme + "://" + serverName + ":" + port;

    this.contextRoot = "/the/context/root/";
    this.fullyQualifiedServerURL = this.serverAddress + this.contextRoot;

    this.mockRequest = mock(HttpServletRequest.class);

    when(this.mockRequest.getRequestURI()).thenReturn("/somewhere/" + PentahoWebContextFilter.WEB_CONTEXT_JS);

    when(this.mockRequest.getScheme()).thenReturn(scheme);
    when(this.mockRequest.getServerName()).thenReturn(serverName);
    when(this.mockRequest.getServerPort()).thenReturn(port);

    when(this.mockRequest.getHeader("referer")).thenReturn(this.serverAddress + "/some/app");

    this.mockResponse = mock(HttpServletResponse.class);
    this.mockResponseOutputStream = new java.io.ByteArrayOutputStream();
    when(this.mockResponse.getOutputStream()).thenReturn(new ServletOutputStream() {
        @Override/*  w ww. j ava  2s  .  com*/
        public void write(int b) throws IOException {
            PentahoWebContextFilterTest.this.mockResponseOutputStream.write(b);
        }
    });

    FilterConfig mockFilterConfig = mock(FilterConfig.class);

    this.pentahoWebContextFilter = spy(new PentahoWebContextFilter());

    IApplicationContext mockApplicationContext = mock(IApplicationContext.class);
    when(mockApplicationContext.getFullyQualifiedServerURL()).thenReturn(this.fullyQualifiedServerURL);
    doReturn(mockApplicationContext).when(this.pentahoWebContextFilter).getApplicationContext();

    IPentahoRequestContext mockRequestContext = mock(IPentahoRequestContext.class);
    when(mockRequestContext.getContextPath()).thenReturn(this.contextRoot);
    doReturn(mockRequestContext).when(this.pentahoWebContextFilter).getRequestContext();

    IPentahoSession mockSession = mock(IPentahoSession.class);
    doReturn(mockSession).when(this.pentahoWebContextFilter).getSession();

    IPluginManager mockPluginManager = mock(IPluginManager.class);
    doReturn(mockPluginManager).when(this.pentahoWebContextFilter).getPluginManager();

    this.pentahoWebContextFilter.init(mockFilterConfig);
}

From source file:org.pentaho.webcontext.core.impl.PentahoWebContextServletImplTest.java

@Before
public void setUp() throws Exception {
    webContextServlet = spy(new PentahoWebContextServletImpl());

    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    when(mockRequest.getRequestURI()).thenReturn("fake/uri/" + WEB_CONTEXT_JS);
    when(mockRequest.getParameter(LOCALE_REQUEST_PARAM)).thenReturn("xp_TO");

    this.httpRequest = mockRequest;

    HttpServletResponse mockResponse = mock(HttpServletResponse.class);

    this.mockResponseOutputStream = new java.io.ByteArrayOutputStream();
    when(mockResponse.getOutputStream()).thenReturn(new ServletOutputStream() {
        @Override//w w  w  . j  a  v a2s.  c o m
        public void write(int b) throws IOException {
            PentahoWebContextServletImplTest.this.mockResponseOutputStream.write(b);
        }
    });

    this.httpResponse = mockResponse;
}