public static Registry buildBaseRegistry( RatpackServer ratpackServer, ExecController execController, ServerConfig serverConfig) { ErrorHandler errorHandler = serverConfig.isDevelopment() ? new DefaultDevelopmentErrorHandler() : new DefaultProductionErrorHandler(); RegistryBuilder baseRegistryBuilder; try { baseRegistryBuilder = Registry.builder() .add(ServerConfig.class, serverConfig) .add(ByteBufAllocator.class, PooledByteBufAllocator.DEFAULT) .add(ExecController.class, execController) .add(MimeTypes.class, new ActivationBackedMimeTypes()) .add( PublicAddress.class, new DefaultPublicAddress( serverConfig.getPublicAddress(), serverConfig.getSSLContext() == null ? HTTP_SCHEME : HTTPS_SCHEME)) .add(Redirector.class, new DefaultRedirector()) .add(ClientErrorHandler.class, errorHandler) .add(ServerErrorHandler.class, errorHandler) .with(new DefaultFileRenderer().register()) .with(new PromiseRenderer().register()) .with(new PublisherRenderer().register()) .with(new RenderableRenderer().register()) .with(new CharSequenceRenderer().register()) .add(FormParser.class, FormParser.multiPart()) .add(FormParser.class, FormParser.urlEncoded()) .add(FormNoOptParser.class, FormNoOptParser.multiPart()) .add(FormNoOptParser.class, FormNoOptParser.urlEncoded()) .add(RatpackServer.class, ratpackServer) // TODO remove Stopper, and just use RatpackServer instead (will need to update perf // and gradle tests) .add( Stopper.class, () -> uncheck( () -> { ratpackServer.stop(); return null; })) .add( HttpClient.class, HttpClient.httpClient( PooledByteBufAllocator.DEFAULT, serverConfig.getMaxContentLength())) .add( ServerSentEventStreamClient.class, ServerSentEventStreamClient.sseStreamClient(PooledByteBufAllocator.DEFAULT)) .add(HealthCheckResultsRenderer.class, new HealthCheckResultsRenderer()) .add(RequestId.Generator.class, new UuidBasedRequestIdGenerator()); addConfigObjects(serverConfig, baseRegistryBuilder); } catch (Exception e) { // Uncheck because it really shouldn't happen throw uncheck(e); } if (serverConfig.isHasBaseDir()) { baseRegistryBuilder.add(FileSystemBinding.class, serverConfig.getBaseDir()); } return baseRegistryBuilder.build(); }
@Provides HttpClient httpClient(ByteBufAllocator byteBufAllocator, ServerConfig serverConfig) { return HttpClient.httpClient(byteBufAllocator, serverConfig.getMaxContentLength()); }
public ReceivedResponse request(URI uri, Duration duration, Action<? super RequestSpec> action) throws Throwable { CountDownLatch latch = new CountDownLatch(1); AtomicReference<ExecResult<ReceivedResponse>> result = new AtomicReference<>(); try (ExecController execController = new DefaultExecController(2)) { execController .fork() .onComplete(e -> {}) .start( e -> { HttpClient.httpClient(UnpooledByteBufAllocator.DEFAULT, Integer.MAX_VALUE) .request(uri, action.prepend(s -> s.readTimeout(Duration.ofHours(1)))) .map( response -> { TypedData responseBody = response.getBody(); ByteBuf responseBodyBuffer = responseBody.getBuffer(); responseBodyBuffer = Unpooled.unreleasableBuffer(responseBodyBuffer.retain()); return new DefaultReceivedResponse( response.getStatus(), response.getHeaders(), new ByteBufBackedTypedData( responseBodyBuffer, responseBody.getContentType())); }) .connect( new Downstream<DefaultReceivedResponse>() { @Override public void success(DefaultReceivedResponse value) { result.set(ExecResult.of(Result.success(value))); latch.countDown(); } @Override public void error(Throwable throwable) { result.set(ExecResult.of(Result.error(throwable))); latch.countDown(); } @Override public void complete() { result.set(ExecResult.complete()); latch.countDown(); } }); }); try { if (!latch.await(duration.toNanos(), TimeUnit.NANOSECONDS)) { TemporalUnit unit = duration.getUnits().get(0); throw new IllegalStateException( "Request to " + uri + " took more than " + duration.get(unit) + " " + unit.toString() + " to complete"); } } catch (InterruptedException e) { throw Exceptions.uncheck(e); } return result.get().getValueOrThrow(); } }