@Override public void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { String emailUser = request.getUserPrincipal().getName(); ServiceResponse<User> serviceResponse = userService.findByEmail(emailUser); String url = request.getRequestURL().toString(); HttpOutput out = (HttpOutput) response.getOutputStream(); if (serviceResponse.isOk()) { LOGGER.info(MessageFormat.format("{0} {1} {2} {3}", serviceResponse.getResult().getTenant().getDomainName(), url, response.getStatus(), out.getWritten())); } }
@Test public void send() throws Exception { byte[] bytes = {0}; CountDownLatch latch = new CountDownLatch(1); new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .expect(write(bytes)) .run(unit -> { new JettySse(unit.get(Request.class), unit.get(Response.class)) .send(Optional.of("1"), bytes).whenComplete((id, x) -> { if (x == null) { assertEquals("1", id.get()); latch.countDown(); } }); latch.await(); }); }
@Test public void sendFailure() throws Exception { byte[] bytes = {0}; IOException cause = new IOException("intentional error"); CountDownLatch latch = new CountDownLatch(1); new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .expect(unit -> { HttpOutput output = unit.get(HttpOutput.class); output.write(bytes); expectLastCall().andThrow(cause); }) .run(unit -> { new JettySse(unit.get(Request.class), unit.get(Response.class)) .send(Optional.of("1"), bytes).whenComplete((id, x) -> { if (x != null) { assertEquals(cause, x); latch.countDown(); } }); latch.await(); }); }
@Test public void sendBytes() throws Exception { byte[] bytes = "bytes".getBytes(); new MockUnit(ServletServletRequest.class, Request.class, Response.class, HttpOutput.class) .expect(servletRequest) .expect(unit -> { HttpOutput output = unit.get(HttpOutput.class); output.sendContent(unit.capture(ByteBuffer.class)); Response rsp = unit.get(Response.class); rsp.setHeader("Transfer-Encoding", null); expect(rsp.getHttpOutput()).andReturn(output); }) .run(unit -> { new JettyResponse(unit.get(ServletServletRequest.class), unit.get(Response.class)) .send(bytes); }, unit -> { assertArrayEquals(bytes, unit.captured(ByteBuffer.class).iterator().next().array()); }); }
@Test public void sendBuffer() throws Exception { byte[] bytes = "bytes".getBytes(); ByteBuffer buffer = ByteBuffer.wrap(bytes); new MockUnit(ServletServletRequest.class, Request.class, Response.class, HttpOutput.class) .expect(servletRequest) .expect(unit -> { HttpOutput output = unit.get(HttpOutput.class); output.sendContent(eq(buffer)); Response rsp = unit.get(Response.class); expect(rsp.getHttpOutput()).andReturn(output); }) .run(unit -> { new JettyResponse(unit.get(ServletServletRequest.class), unit.get(Response.class)) .send(buffer); }); }
@Test public void sendInputStream() throws Exception { new MockUnit(ServletServletRequest.class, Request.class, Response.class, HttpOutput.class, InputStream.class, AsyncContext.class) .expect(servletRequest) .expect(unit -> { unit.mockStatic(Channels.class); ReadableByteChannel channel = unit.mock(ReadableByteChannel.class); expect(Channels.newChannel(unit.get(InputStream.class))).andReturn(channel); HttpOutput output = unit.get(HttpOutput.class); output.sendContent(eq(channel), isA(JettyResponse.class)); Response rsp = unit.get(Response.class); expect(rsp.getHttpOutput()).andReturn(output); }) .expect(startAsync) .run(unit -> { new JettyResponse(unit.get(ServletServletRequest.class), unit.get(Response.class)) .send(unit.get(InputStream.class)); }); }
@Test public void sendSmallFileChannel() throws Exception { FileChannel channel = newFileChannel(1); new MockUnit(ServletServletRequest.class, Request.class, Response.class, HttpOutput.class, AsyncContext.class) .expect(servletRequest) .expect(unit -> { HttpOutput output = unit.get(HttpOutput.class); output.sendContent(eq(channel)); Response rsp = unit.get(Response.class); expect(rsp.getBufferSize()).andReturn(2); expect(rsp.getHttpOutput()).andReturn(output); }) .run(unit -> { new JettyResponse(unit.get(ServletServletRequest.class), unit.get(Response.class)) .send(channel); }); }
@Test public void sendLargeFileChannel() throws Exception { FileChannel channel = newFileChannel(10); new MockUnit(ServletServletRequest.class, Request.class, Response.class, HttpOutput.class, AsyncContext.class) .expect(servletRequest) .expect(unit -> { HttpOutput output = unit.get(HttpOutput.class); output.sendContent(eq(channel), isA(JettyResponse.class)); Response rsp = unit.get(Response.class); expect(rsp.getBufferSize()).andReturn(5); expect(rsp.getHttpOutput()).andReturn(output); }) .expect(startAsync) .run(unit -> { new JettyResponse(unit.get(ServletServletRequest.class), unit.get(Response.class)) .send(channel); }); }
@Test public void end() throws Exception { new MockUnit(ServletServletRequest.class, Request.class, Response.class, HttpOutput.class) .expect(servletRequest) .expect(unit -> { HttpOutput output = unit.get(HttpOutput.class); output.close(); Response rsp = unit.get(Response.class); expect(rsp.getHttpOutput()).andReturn(output); }) .expect(noAsyncStarted) .run(unit -> { new JettyResponse(unit.get(ServletServletRequest.class), unit.get(Response.class)) .end(); }); }
/** * @param response Http response. * @param type Type. * @param path Path to file. * @throws IOException If failed. */ private static void handleRequest(HttpServletResponse response, String type, String path) throws IOException { Path path0 = Paths.get(path); response.setContentType(type); response.setHeader("Content-Disposition", "attachment; filename=\"" + path0.getFileName() + "\""); try (HttpOutput out = (HttpOutput)response.getOutputStream()) { out.sendContent(FileChannel.open(path0, StandardOpenOption.READ)); } }
/** * @param response Http response. * @param type Type. * @param stream Stream. * @param attachmentName Attachment name. * @throws IOException If failed. */ private static void handleRequest(HttpServletResponse response, String type, InputStream stream, String attachmentName) throws IOException { response.setContentType(type); response.setHeader("Content-Disposition", "attachment; filename=\"" + attachmentName + "\""); try (HttpOutput out = (HttpOutput)response.getOutputStream()) { out.sendContent(stream); } }
@Test public void defaults() throws Exception { new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .run(unit -> { new JettySse(unit.get(Request.class), unit.get(Response.class)); }); }
@Test public void handshake() throws Exception { new MockUnit(Request.class, Response.class, HttpOutput.class, Runnable.class, AsyncContext.class, HttpChannel.class, Connector.class, Executor.class) .expect(httpOutput) .expect(unit -> { AsyncContext async = unit.get(AsyncContext.class); async.setTimeout(0L); Request req = unit.get(Request.class); expect(req.getAsyncContext()).andReturn(async); }) .expect(unit -> { Response rsp = unit.get(Response.class); rsp.setStatus(200); rsp.setHeader("Connection", "Close"); rsp.setContentType("text/event-stream; charset=utf-8"); rsp.flushBuffer(); HttpChannel channel = unit.get(HttpChannel.class); expect(rsp.getHttpChannel()).andReturn(channel); Connector connector = unit.get(Connector.class); expect(channel.getConnector()).andReturn(connector); Executor executor = unit.get(Executor.class); expect(connector.getExecutor()).andReturn(executor); executor.execute(unit.get(Runnable.class)); }) .run(unit -> { new JettySse(unit.get(Request.class), unit.get(Response.class)) .handshake(unit.get(Runnable.class)); }); }
@SuppressWarnings("resource") @Test public void shouldCloseEof() throws Exception { new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .run(unit -> { JettySse sse = new JettySse(unit.get(Request.class), unit.get(Response.class)); assertEquals(true, sse.shouldClose(new EofException())); }); }
@SuppressWarnings("resource") @Test public void shouldCloseBrokenPipe() throws Exception { new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .run(unit -> { JettySse sse = new JettySse(unit.get(Request.class), unit.get(Response.class)); assertEquals(true, sse.shouldClose(new IOException("broken pipe"))); }); }
@Test public void close() throws Exception { new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .expect(unit -> { Response rsp = unit.get(Response.class); rsp.closeOutput(); }) .run(unit -> { JettySse sse = new JettySse(unit.get(Request.class), unit.get(Response.class)); sse.close(); }); }
@Test public void ignoreClosedStream() throws Exception { new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .expect(unit -> { Response rsp = unit.get(Response.class); rsp.closeOutput(); }) .run(unit -> { JettySse sse = new JettySse(unit.get(Request.class), unit.get(Response.class)); sse.close(); sse.close(); }); }
@Test public void closeFailure() throws Exception { new MockUnit(Request.class, Response.class, HttpOutput.class) .expect(httpOutput) .expect(unit -> { Response rsp = unit.get(Response.class); rsp.closeOutput(); expectLastCall().andThrow(new EofException("intentional err")); }) .run(unit -> { JettySse sse = new JettySse(unit.get(Request.class), unit.get(Response.class)); sse.close(); }); }
private Block write(final byte[] bytes) { return unit -> { HttpOutput output = unit.get(HttpOutput.class); output.write(bytes); output.flush(); }; }
@Test public void failed() throws Exception { IOException cause = new IOException(); new MockUnit(ServletServletRequest.class, Request.class, Response.class, HttpOutput.class) .expect(servletRequest) .expect(unit -> { Logger log = unit.mock(Logger.class); log.error("execution of /path resulted in exception", cause); unit.mockStatic(LoggerFactory.class); expect(LoggerFactory.getLogger(org.jooby.Response.class)).andReturn(log); }) .expect(unit -> { HttpOutput output = unit.get(HttpOutput.class); output.close(); Response rsp = unit.get(Response.class); expect(rsp.getHttpOutput()).andReturn(output); }) .expect(noAsyncStarted) .expect(unit -> { ServletServletRequest req = unit.get(ServletServletRequest.class); expect(req.path()).andReturn("/path"); }) .run(unit -> { new JettyResponse(unit.get(ServletServletRequest.class), unit.get(Response.class)) .failed(cause); }); }
private HttpOutput sender() { return ((Response) rsp).getHttpOutput(); }