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

br.com.objectos.rio.http.HttpServerBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.rio.http;

import java.io.IOException;
import java.util.Objects;

import br.com.objectos.core.util.ImmutableList;
import br.com.objectos.io.Directory;
import br.com.objectos.rio.http.HttpServerBuilderDsl.HttpServerBuilderAt;
import br.com.objectos.rio.http.HttpServerBuilderDsl.HttpServerBuilderWhen;
import br.com.objectos.rio.http.HttpServerBuilderDsl.HttpServerBuilderWhenAt;

/**
 * @author [email protected] (Marcio Endo)
 */
public class HttpServerBuilder {

  private int port = 80;
  private final ImmutableList.Builder routeList = ImmutableList.builder();

  public HttpServerBuilderDsl port(int port) {
    this.port = port;
    return new Dsl();
  }

  private class Dsl
      implements
      HttpServerBuilderDsl {

    @Override
    public HttpServer build() {
      try {
        Channel channel = Channel.get(port);
        RouteRegistry routeRegistry = RouteRegistry.of(routeList.build());
        return new HttpServer(channel, routeRegistry);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    @Override
    public HttpServerBuilderAt at(String path) {
      Objects.requireNonNull(path);
      Path thePath = Path.parse(path);
      return new At(this, thePath);
    }

    @Override
    public HttpServerBuilderWhen when(Method method) {
      Objects.requireNonNull(method);
      return new When(this, method);
    }

  }

  private class At implements HttpServerBuilderAt {

    private final Dsl dsl;
    private final Path path;

    public At(Dsl dsl, Path path) {
      this.dsl = dsl;
      this.path = path;
    }

    @Override
    public HttpServerBuilderDsl root(Directory directory) {
      PathSpec.Builder pathSpec = PathSpec.builder();
      while (path.hasNextPart()) {
        pathSpec.fixed(path.nextPart());
      }
      pathSpec.catchAllArgument();

      routeList.add(new PathSpecRoute(
          Method.GET,
          pathSpec.build(),
          new DirectoryRouteExecutor(directory)));
      return dsl;
    }

  }

  private class When implements HttpServerBuilderWhen {

    private final Dsl dsl;
    private final Method method;

    public When(Dsl dsl, Method method) {
      this.dsl = dsl;
      this.method = method;
    }

    @Override
    public HttpServerBuilderWhenAt at(String path) {
      return new HttpServerBuilderWhenAt() {
        @Override
        public HttpServerBuilderDsl execute(RouteExecutor executor) {
          PathSpec.Builder pathSpec = PathSpec.builder();

          Path parsedPath = Path.parse(path);
          while (parsedPath.hasNextPart()) {
            pathSpec.fixed(parsedPath.nextPart());
          }

          routeList.add(new PathSpecRoute(
              method,
              pathSpec.build(),
              executor));
          return dsl;
        }
      };
    }

    @Override
    public HttpServerBuilderDsl execute(RouteExecutor executor) {
      Objects.requireNonNull(executor);
      routeList.add(new MethodRoute(method, executor));
      return dsl;
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy