All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ratpack.server.internal.ServerRegistry Maven / Gradle / Ivy

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ratpack.server.internal;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import ratpack.config.ConfigObject;
import ratpack.error.ClientErrorHandler;
import ratpack.error.ServerErrorHandler;
import ratpack.error.internal.DefaultDevelopmentErrorHandler;
import ratpack.error.internal.DefaultProductionErrorHandler;
import ratpack.error.internal.ErrorHandler;
import ratpack.exec.ExecControl;
import ratpack.exec.ExecController;
import ratpack.exec.ExecInterceptor;
import ratpack.exec.internal.DefaultExecController;
import ratpack.file.FileSystemBinding;
import ratpack.file.MimeTypes;
import ratpack.file.internal.ActivationBackedMimeTypes;
import ratpack.file.internal.DefaultFileRenderer;
import ratpack.form.internal.FormNoOptParser;
import ratpack.form.internal.FormParser;
import ratpack.func.Function;
import ratpack.handling.Redirector;
import ratpack.handling.RequestLog;
import ratpack.handling.internal.DefaultRedirector;
import ratpack.handling.internal.DefaultRequestLog;
import ratpack.health.internal.HealthCheckResultsRenderer;
import ratpack.http.client.HttpClient;
import ratpack.registry.Registry;
import ratpack.registry.RegistryBuilder;
import ratpack.render.internal.CharSequenceRenderer;
import ratpack.render.internal.PromiseRenderer;
import ratpack.render.internal.PublisherRenderer;
import ratpack.render.internal.RenderableRenderer;
import ratpack.server.*;
import ratpack.sse.ServerSentEventStreamClient;

import static ratpack.util.Exceptions.uncheck;
import static ratpack.util.internal.ProtocolUtil.HTTPS_SCHEME;
import static ratpack.util.internal.ProtocolUtil.HTTP_SCHEME;

public abstract class ServerRegistry {
  public static Registry serverRegistry(RatpackServer ratpackServer, DefaultExecController execController, ServerConfig serverConfig, Function userRegistryFactory) {
    Registry baseRegistry = buildBaseRegistry(ratpackServer, execController, serverConfig);
    Registry userRegistry = buildUserRegistry(userRegistryFactory, baseRegistry);

    ImmutableList interceptors = ImmutableList.copyOf(userRegistry.getAll(ExecInterceptor.class));
    execController.getControl().setDefaultInterceptors(interceptors);

    return baseRegistry.join(userRegistry);
  }

  private static Registry buildUserRegistry(Function userRegistryFactory, Registry baseRegistry) {
    Registry userRegistry;
    try {
      userRegistry = userRegistryFactory.apply(baseRegistry);
    } catch (Exception e) {
      Throwables.propagateIfPossible(e);
      throw new StartupFailureException("Failed to build user registry", e);
    }
    return userRegistry;
  }

  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(ExecControl.class, execController.getControl())
        .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)
        .add(RequestLog.class, new DefaultRequestLog())
        .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(execController, PooledByteBufAllocator.DEFAULT, serverConfig.getMaxContentLength()))
        .add(ServerSentEventStreamClient.class, ServerSentEventStreamClient.sseStreamClient(execController, PooledByteBufAllocator.DEFAULT))
        .add(HealthCheckResultsRenderer.class, new HealthCheckResultsRenderer());

      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();
  }

  private static void addConfigObjects(ServerConfig serverConfig, RegistryBuilder baseRegistryBuilder) {
    for (ConfigObject configObject : serverConfig.getRequiredConfig()) {
      addConfigObject(baseRegistryBuilder, configObject);
    }
  }

  private static  void addConfigObject(RegistryBuilder baseRegistryBuilder, ConfigObject configObject) {
    baseRegistryBuilder.add(configObject.getType(), configObject.getObject());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy