public NettyHandlerAdapter(Stopper stopper, Handler handler, LaunchConfig launchConfig) {
    this.handlers = new Handler[] {handler};
    this.return404 = Handlers.notFound();
    RegistryBuilder registryBuilder =
        Registries.registry()
            // If you update this list, update the class level javadoc on Context.
            .add(Stopper.class, stopper)
            .add(MimeTypes.class, new ActivationBackedMimeTypes())
            .add(
                PublicAddress.class,
                new DefaultPublicAddress(
                    launchConfig.getPublicAddress(),
                    launchConfig.getSSLContext() == null ? "http" : "https"))
            .add(Redirector.class, new DefaultRedirector())
            .add(ClientErrorHandler.class, new DefaultClientErrorHandler())
            .add(ServerErrorHandler.class, new DefaultServerErrorHandler())
            .add(LaunchConfig.class, launchConfig)
            .add(FileRenderer.class, new DefaultFileRenderer())
            .add(CharSequenceRenderer.class, new DefaultCharSequenceRenderer())
            .add(FormParser.class, FormParser.multiPart())
            .add(FormParser.class, FormParser.urlEncoded())
            .add(HttpClient.class, HttpClients.httpClient(launchConfig));

    if (launchConfig.isHasBaseDir()) {
      registryBuilder.add(FileSystemBinding.class, launchConfig.getBaseDir());
    }

    this.registry = registryBuilder.build();

    this.addResponseTimeHeader = launchConfig.isTimeResponses();
    this.compressResponses = launchConfig.isCompressResponses();
    this.applicationConstants =
        new DefaultContext.ApplicationConstants(launchConfig, new DefaultRenderController());
    this.execController = launchConfig.getExecController();
  }
Esempio n. 2
0
  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();
  }