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

br.com.objectos.rio.http.DirectoryRouteExecutor 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 br.com.objectos.io.Directory;
import br.com.objectos.io.File;
import br.com.objectos.io.Node;
import br.com.objectos.io.NodeVisitor;

/**
 * @author [email protected] (Marcio Endo)
 */
class DirectoryRouteExecutor implements NodeVisitor, RouteExecutor {

  private final Directory directory;

  public DirectoryRouteExecutor(Directory directory) {
    this.directory = directory;
  }

  @Override
  public Response execute(Request request, Arguments args) {
    String subpath = args.getString();
    Node node = directory.nodeAt(subpath);
    return node.accept(this);
  }

  @Override
  public Response visitDirectory(Directory directory) {
    return Response.builder()
        .sayNotFound()
        .build();
  }

  @Override
  public Response visitFile(File file) {
    try {
      return Response.builder()
          .sayOk()
          .contentType(ContentType.resolve(file))
          .contentLength(file.size())
          .messageBody(file)
          .build();
    } catch (IOException e) {
      return Response.builder()
          .sayError()
          .contentType(ContentType.textPlain())
          .messageBody(e.getMessage())
          .build();
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy