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

ratpack.file.internal.DefaultFileHandlerSpec 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.file.internal;

import com.google.common.collect.ImmutableList;
import ratpack.file.BaseDirRequiredException;
import ratpack.file.FileHandlerSpec;
import ratpack.func.Action;
import ratpack.handling.Handler;
import ratpack.handling.Handlers;
import ratpack.server.ServerConfig;

public class DefaultFileHandlerSpec implements FileHandlerSpec {

  private String path;
  private String dir;
  private ImmutableList indexFiles = ImmutableList.of();

  @Override
  public FileHandlerSpec path(String path) {
    this.path = path;
    return this;
  }

  @Override
  public FileHandlerSpec dir(String dir) {
    this.dir = dir;
    return this;
  }

  @Override
  public FileHandlerSpec indexFiles(String... indexFiles) {
    this.indexFiles = ImmutableList.copyOf(indexFiles);
    return this;
  }

  public static Handler build(ServerConfig serverConfig, Action config) throws Exception {
    if (!serverConfig.isHasBaseDir()) {
      throw new BaseDirRequiredException("no base dir set for application");
    }
    DefaultFileHandlerSpec spec = new DefaultFileHandlerSpec();
    config.execute(spec);
    Handler handler = new FileHandler(spec.indexFiles, !serverConfig.isDevelopment());
    if (spec.dir != null) {
      handler = Handlers.fileSystem(serverConfig, spec.dir, handler);
    }
    if (spec.path != null) {
      handler = Handlers.prefix(spec.path, handler);
    }
    return handler;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy