protected void handle( final HttpServerRequest vertxRequest, final InputStream inputStream ) { URI uri = getAbsoluteURI(vertxRequest); boolean isSecure = "https".equalsIgnoreCase(uri.getScheme()); UriBuilder baseUriBuilder = UriBuilder.fromUri(uri) .replacePath(getBaseUri().getPath()) .replaceQuery(null); // Create the jersey request final ContainerRequest jerseyRequest = new ContainerRequest( baseUriBuilder.build(), uri, vertxRequest.method().name(), new DefaultSecurityContext(isSecure), new MapPropertiesDelegate()); handle(vertxRequest, inputStream, jerseyRequest); }
@Override public void handle(Void aVoid) { if (next >= requestProcessors.size()) { done.handle(null); } else { callVertxRequestProcessor(next, vertxRequest, jerseyRequest, done); } } });
@Override public ApplicationHandlerDelegate getDelegate() { return getContainer().getApplicationHandlerDelegate(); }
protected void handle( final HttpServerRequest vertxRequest, final InputStream inputStream ) { URI uri = getAbsoluteURI(vertxRequest); boolean isSecure = "https".equalsIgnoreCase(uri.getScheme()); UriBuilder baseUriBuilder = UriBuilder.fromUri(uri) .replacePath(baseUri.getPath()) .replaceQuery(null); // Create the jersey request final ContainerRequest jerseyRequest = new ContainerRequest( baseUriBuilder.build(), uri, vertxRequest.method(), new DefaultSecurityContext(isSecure), new MapPropertiesDelegate()); handle(vertxRequest, inputStream, jerseyRequest); }
/** * {@inheritDoc} */ @Override public void handle(final HttpServerRequest vertxRequest) { // Wait for the body for jersey to handle form/json/xml params if (shouldReadData(vertxRequest)) { if (logger.isDebugEnabled()) { logger.debug("DefaultJerseyHandler - handle request and read body: " + vertxRequest.method() + " " + vertxRequest.uri()); } final Buffer body = Buffer.buffer(); vertxRequest.handler(buffer -> { body.appendBuffer(buffer); if (body.length() > getMaxBodySize()) { throw new RuntimeException("The input stream has exceeded the max allowed body size " + getMaxBodySize() + "."); } }); vertxRequest.endHandler(aVoid -> { InputStream inputStream = new ByteArrayInputStream(body.getBytes()); DefaultJerseyHandler.this.handle(vertxRequest, inputStream); }); } else { if (logger.isDebugEnabled()) { logger.debug("DefaultJerseyHandler - handle request: " + vertxRequest.method() + " " + vertxRequest.uri()); } DefaultJerseyHandler.this.handle(vertxRequest, null); } }
if (shouldReadData(vertxRequest)) { if (logger.isDebugEnabled()) { logger.debug("DefaultJerseyHandler - handle request and read body: " + vertxRequest.method() + " " + vertxRequest.uri()); logger.debug("DefaultJerseyHandler - handle request: " + vertxRequest.method() + " " + vertxRequest.uri()); DefaultJerseyHandler.this.handle(vertxRequest, null);
@Override public void handle(Void event) { InputStream inputStream = new ByteArrayInputStream(body.getBytes()); DefaultJerseyHandler.this.handle(vertxRequest, inputStream); } });
vertxRequest.pause(); callVertxRequestProcessor(0, vertxRequest, jerseyRequest, aVoid -> { getDelegate().handle(jerseyRequest); }); } else { getDelegate().handle(jerseyRequest);
@Test public void testGetAbsoluteURI() throws Exception { URI uri; String absoluteUri = "http://0.0.0.0:80/test"; String host = "test.englishtown.com"; String badUrl = "http://test.englishtown.com/test?a=b=c|d=e"; HttpServerRequest request = mock(HttpServerRequest.class); when(request.absoluteURI()).thenReturn(absoluteUri).thenThrow(new IllegalArgumentException()); when(request.uri()).thenReturn(badUrl); when(request.headers()).thenReturn(headers); when(headers.get(eq(HttpHeaders.HOST))).thenReturn(host); uri = jerseyHandler.getAbsoluteURI(request); assertEquals("http://test.englishtown.com/test", uri.toString()); uri = jerseyHandler.getAbsoluteURI(request); assertEquals("http://test.englishtown.com/test?a=b%3Dc%7Cd%3De", uri.toString()); }
result = jerseyHandler.shouldReadData(request); assertFalse(result); result = jerseyHandler.shouldReadData(request); assertFalse(result); result = jerseyHandler.shouldReadData(request); assertTrue(result); result = jerseyHandler.shouldReadData(request); assertTrue(result); result = jerseyHandler.shouldReadData(request); assertTrue(result); result = jerseyHandler.shouldReadData(request); assertTrue(result); result = jerseyHandler.shouldReadData(request); assertTrue(result); result = jerseyHandler.shouldReadData(request); assertTrue(result); result = jerseyHandler.shouldReadData(request); assertTrue(result); result = jerseyHandler.shouldReadData(request); assertTrue(result);
@Test public void testGetBaseURI() throws Exception { URI baseUri = URI.create("/test"); when(options.getBaseUri()).thenReturn(baseUri); URI result = jerseyHandler.getBaseUri(); assertEquals(baseUri, result); }
@Override protected WhenJerseyServer getWhenJerseyServer() { ContainerResponseWriterProvider provider = new VertxResponseWriterProvider( vertx, new ArrayList<>(), new ArrayList<>()); DefaultJerseyOptions options = new DefaultJerseyOptions(vertx); VertxContainer container = new DefaultVertxContainer(vertx, options, null, null); JerseyHandler handler = new DefaultJerseyHandler(() -> container, provider, new ArrayList<>()); JerseyServer server = new DefaultJerseyServer(handler, container, () -> options); When when = WhenFactory.createSync(); return new DefaultWhenJerseyServer(vertx, () -> server, when); }
/** * {@inheritDoc} */ @Override public void handle(final HttpServerRequest vertxRequest) { // Wait for the body for jersey to handle form/json/xml params if (shouldReadData(vertxRequest)) { if (logger.isDebugEnabled()) { logger.debug("DefaultJerseyHandler - handle request and read body: " + vertxRequest.method() + " " + vertxRequest.uri()); } final Buffer body = Buffer.buffer(); vertxRequest.handler(buffer -> { body.appendBuffer(buffer); if (body.length() > getMaxBodySize()) { throw new RuntimeException("The input stream has exceeded the max allowed body size " + getMaxBodySize() + "."); } }); vertxRequest.endHandler(aVoid -> { InputStream inputStream = new ByteArrayInputStream(body.getBytes()); DefaultJerseyHandler.this.handle(vertxRequest, inputStream); }); } else { if (logger.isDebugEnabled()) { logger.debug("DefaultJerseyHandler - handle request: " + vertxRequest.method() + " " + vertxRequest.uri()); } DefaultJerseyHandler.this.handle(vertxRequest, null); } }
@Test public void testHandle() throws Exception { when(request.method()).thenReturn(HttpMethod.GET); when(request.headers()).thenReturn(mock(MultiMap.class)); jerseyHandler.handle(request); verify(applicationHandlerDelegate).handle(any(ContainerRequest.class)); }
vertxRequest.pause(); callVertxRequestProcessor(0, vertxRequest, jerseyRequest, aVoid -> { getDelegate().handle(jerseyRequest); }); } else { getDelegate().handle(jerseyRequest);
@Before public void setUp() { when(container.getOptions()).thenReturn(options); when(container.getVertx()).thenReturn(vertx); when(container.getApplicationHandlerDelegate()).thenReturn(applicationHandlerDelegate); when(applicationHandlerDelegate.getServiceLocator()).thenReturn(serviceLocator); when(options.getMaxBodySize()).thenReturn(1024); when(options.getBaseUri()).thenReturn(URI.create("/test")); when(request.absoluteURI()).thenReturn(URI.create("http://test.englishtown.com/test").toString()); when(request.response()).thenReturn(response); when(request.headers()).thenReturn(headers); JsonArray resources = new JsonArray().add("com.englishtown.vertx.jersey.resources"); config.put(DefaultJerseyOptions.CONFIG_RESOURCES, resources); ContainerResponseWriterProvider provider = mock(ContainerResponseWriterProvider.class); when(provider.get(any(HttpServerRequest.class), any(ContainerRequest.class))).thenReturn(mock(ContainerResponseWriter.class)); when(serviceLocator.<Ref<Vertx>>getService((new TypeLiteral<Ref<Vertx>>() { }).getType())).thenReturn(vertxRef); when(serviceLocator.<Ref<Container>>getService((new TypeLiteral<Ref<Container>>() { }).getType())).thenReturn(containerRef); jerseyHandler = new DefaultJerseyHandler(() -> container, provider, requestProcessors); }
protected void handle( final HttpServerRequest vertxRequest, final InputStream inputStream ) { URI uri = getAbsoluteURI(vertxRequest); boolean isSecure = "https".equalsIgnoreCase(uri.getScheme()); UriBuilder baseUriBuilder = UriBuilder.fromUri(uri) .replacePath(getBaseUri().getPath()) .replaceQuery(null); // Create the jersey request final ContainerRequest jerseyRequest = new ContainerRequest( baseUriBuilder.build(), uri, vertxRequest.method().name(), new DefaultSecurityContext(isSecure), new MapPropertiesDelegate()); handle(vertxRequest, inputStream, jerseyRequest); }
@Test public void testHandle_RequestProcessors() throws Exception { when(request.headers()).thenReturn(mock(MultiMap.class)); when(request.method()).thenReturn(HttpMethod.GET); InputStream inputStream = null; VertxRequestProcessor rp1 = mock(VertxRequestProcessor.class); VertxRequestProcessor rp2 = mock(VertxRequestProcessor.class); requestProcessors.add(rp1); requestProcessors.add(rp2); jerseyHandler.handle(request, inputStream); verify(rp1).process(any(HttpServerRequest.class), any(ContainerRequest.class), endHandlerCaptor.capture()); endHandlerCaptor.getValue().handle(null); verify(rp2).process(any(HttpServerRequest.class), any(ContainerRequest.class), endHandlerCaptor.capture()); endHandlerCaptor.getValue().handle(null); verify(applicationHandlerDelegate).handle(any(ContainerRequest.class)); }
protected void callVertxRequestProcessor( int index, final HttpServerRequest vertxRequest, final ContainerRequest jerseyRequest, final Handler<Void> done ) { if (index >= requestProcessors.size()) { done.handle(null); } VertxRequestProcessor processor = requestProcessors.get(index); final int next = index + 1; try { processor.process(vertxRequest, jerseyRequest, aVoid -> { if (next >= requestProcessors.size()) { done.handle(null); } else { callVertxRequestProcessor(next, vertxRequest, jerseyRequest, done); } }); } catch (Throwable t) { logger.error("VertxRequestProcessor " + processor.getClass().getSimpleName() + " threw exception: " + t.getMessage(), t); vertxRequest.response() .setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()) .end(); } }
@Override public ApplicationHandlerDelegate getDelegate() { return getContainer().getApplicationHandlerDelegate(); }