Skip to content

Commit

Permalink
Add hasBaseDir to LaunchConfig and use to throw BaseDirRequiredExcept…
Browse files Browse the repository at this point in the history
…ion (#299)
  • Loading branch information
rhart committed Apr 14, 2014
1 parent ddf8354 commit 781ef9c
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 27 deletions.
Expand Up @@ -20,31 +20,32 @@
import ratpack.file.FileSystemBinding;
import ratpack.handling.Context;
import ratpack.handling.Handler;
import ratpack.launch.LaunchConfig;
import ratpack.registry.Registries;

public class FileSystemBindingHandler implements Handler {

private final String path;
private final Handler handler;

public FileSystemBindingHandler(String path, Handler handler) {
this.path = path;
this.handler = handler;
public FileSystemBindingHandler(LaunchConfig launchConfig, String path, Handler handler) {
if (launchConfig.hasBaseDir()) {
this.path = path;
this.handler = handler;
} else {
throw new BaseDirRequiredException("An application base directory is required to use this handler");
}

// TODO - validate the path isn't escaping up with ../
}

public void handle(Context context) {
FileSystemBinding parentBinding = context.maybeGet(FileSystemBinding.class);
if (parentBinding == null) {
context.error(new BaseDirRequiredException("An application base directory is required to use this handler"));
FileSystemBinding binding = parentBinding.binding(path);
if (binding == null) {
context.clientError(404);
} else {
FileSystemBinding binding = parentBinding.binding(path);
if (binding == null) {
context.clientError(404);
} else {
context.insert(Registries.just(FileSystemBinding.class, binding), handler);
}
context.insert(Registries.just(FileSystemBinding.class, binding), handler);
}
}
}
10 changes: 6 additions & 4 deletions ratpack-core/src/main/java/ratpack/handling/Handlers.java
Expand Up @@ -66,13 +66,14 @@ public static Handler accepts(String... contentTypes) {
* <p>
* If no file can be found to serve, then control will be delegated to the next handler.
*
* @param launchConfig The application launch config
* @param path The relative path to the location of the assets to serve
* @param indexFiles The index files to try if the request is for a directory
* @return A handler
*/
public static Handler assets(String path, List<String> indexFiles) {
public static Handler assets(LaunchConfig launchConfig, String path, List<String> indexFiles) {
Handler handler = new AssetHandler(copyOf(indexFiles));
return fileSystem(path, handler);
return fileSystem(launchConfig, path, handler);
}

/**
Expand Down Expand Up @@ -166,12 +167,13 @@ public static Handler delete() {
* <p>
* The new file system binding will be created by the {@link ratpack.file.FileSystemBinding#binding(String)} method of the contextual binding.
*
* @param launchConfig The application launch config
* @param path The relative path to the new file system binding point
* @param handler The handler to execute with the new file system binding
* @return A handler
*/
public static Handler fileSystem(String path, Handler handler) {
return new FileSystemBindingHandler(path, handler);
public static Handler fileSystem(LaunchConfig launchConfig, String path, Handler handler) {
return new FileSystemBindingHandler(launchConfig, path, handler);
}

/**
Expand Down
Expand Up @@ -43,7 +43,7 @@ public DefaultChain(List<Handler> handlers, LaunchConfig launchConfig, @Nullable
}

public Chain assets(String path, String... indexFiles) {
return handler(Handlers.assets(path, indexFiles.length == 0 ? launchConfig.getIndexFiles() : copyOf(indexFiles)));
return handler(Handlers.assets(getLaunchConfig(), path, indexFiles.length == 0 ? launchConfig.getIndexFiles() : copyOf(indexFiles)));
}

@Override
Expand All @@ -60,11 +60,11 @@ public Chain delete(Handler handler) {
}

public Chain fileSystem(String path, Handler handler) {
return handler(Handlers.fileSystem(path, handler));
return handler(Handlers.fileSystem(getLaunchConfig(), path, handler));
}

public Chain fileSystem(String path, Action<? super Chain> action) throws Exception {
return handler(Handlers.fileSystem(path, chain(action)));
return handler(Handlers.fileSystem(getLaunchConfig(), path, chain(action)));
}

public Chain get(String path, Handler handler) {
Expand Down
7 changes: 7 additions & 0 deletions ratpack-core/src/main/java/ratpack/launch/LaunchConfig.java
Expand Up @@ -185,4 +185,11 @@ public interface LaunchConfig {
*/
public boolean isCompressResponses();

/**
* Whether or not the base dir of the application has been set.
*
* @return whether or not the base dir of the application has been set.
*/
public boolean hasBaseDir();

}
Expand Up @@ -166,5 +166,10 @@ public boolean isCompressResponses() {
return compressResponses;
}

@Override
public boolean hasBaseDir() {
return baseDir != null;
}


}
Expand Up @@ -119,4 +119,9 @@ public boolean isCompressResponses() {
return launchConfig.isCompressResponses();
}

@Override
public boolean hasBaseDir() {
return launchConfig.hasBaseDir();
}

}
Expand Up @@ -16,9 +16,11 @@

package ratpack.server;

import com.google.common.base.Throwables;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import ratpack.file.BaseDirRequiredException;
import ratpack.func.Factory;
import ratpack.func.Transformer;
import ratpack.handling.Handler;
Expand Down Expand Up @@ -87,6 +89,7 @@ private static Handler createHandler(LaunchConfig launchConfig, HandlerFactory h
try {
return handlerFactory.create(launchConfig);
} catch (Exception e) {
Throwables.propagateIfInstanceOf(e, BaseDirRequiredException.class);
throw new LaunchException("Could not create handler via handler factory: " + handlerFactory.getClass().getName(), e);
}
}
Expand Down
Expand Up @@ -48,7 +48,6 @@
import ratpack.http.*;
import ratpack.http.internal.*;
import ratpack.launch.LaunchConfig;
import ratpack.launch.NoBaseDirException;
import ratpack.registry.Registries;
import ratpack.registry.Registry;
import ratpack.registry.RegistryBuilder;
Expand All @@ -63,14 +62,11 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;

@ChannelHandler.Sharable
public class NettyHandlerAdapter extends SimpleChannelInboundHandler<FullHttpRequest> {
private final static Logger LOGGER = Logger.getLogger(NettyHandlerAdapter.class.getName());

private final Handler[] handlers;
private final Handler return404;

Expand Down Expand Up @@ -101,10 +97,8 @@ public NettyHandlerAdapter(Stopper stopper, Handler handler, LaunchConfig launch
.add(FormParser.class, FormParser.multiPart())
.add(FormParser.class, FormParser.urlEncoded());

try {
if (launchConfig.hasBaseDir()) {
registryBuilder.add(FileSystemBinding.class, launchConfig.getBaseDir());
} catch (NoBaseDirException e) {
LOGGER.warning("No base directory has been set for this application");
}

this.registry = registryBuilder.build();
Expand Down
Expand Up @@ -24,6 +24,7 @@
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.ResourceLeakDetector;
import ratpack.file.BaseDirRequiredException;
import ratpack.func.Transformer;
import ratpack.launch.LaunchConfig;
import ratpack.launch.LaunchException;
Expand Down Expand Up @@ -103,6 +104,7 @@ public void stop() {
}
running.set(true);
} catch (Exception e) {
Throwables.propagateIfInstanceOf(e, BaseDirRequiredException.class);
Throwables.propagateIfInstanceOf(e, LaunchException.class);
throw new LaunchException("Unable to launch due to exception", e);
} finally {
Expand Down
Expand Up @@ -18,6 +18,7 @@ package ratpack.groovy.test.embed

import org.junit.Rule
import org.junit.rules.TemporaryFolder
import ratpack.file.BaseDirRequiredException
import ratpack.test.embed.BaseDirBuilder
import ratpack.test.embed.PathBaseDirBuilder
import spock.lang.AutoCleanup
Expand Down Expand Up @@ -86,7 +87,7 @@ class ClosureBackedEmbeddedApplicationSpec extends Specification {
myapp.server.start()

then:
testHttpClient(myapp).get("static.text").statusCode == 500
thrown(BaseDirRequiredException)
}

}
2 changes: 1 addition & 1 deletion ratpack-site/src/ratpack/Ratpack.groovy
Expand Up @@ -122,7 +122,7 @@ ratpack {
def snapshot = get(PathBinding).boundTo == "snapshot"
(snapshot ? versions.snapshot : versions.current).subscribe { RatpackVersion version ->
if (version) {
respond Handlers.assets(version.version, launchConfig.indexFiles)
respond Handlers.assets(launchConfig, version.version, launchConfig.indexFiles)
} else {
clientError(404)
}
Expand Down
10 changes: 10 additions & 0 deletions ratpack-test/src/main/java/ratpack/test/MockLaunchConfig.java
Expand Up @@ -208,4 +208,14 @@ public boolean isCompressResponses() {
return false;
}

/**
* Returns false.
*
* @return false
*/
@Override
public boolean hasBaseDir() {
return false;
}

}

0 comments on commit 781ef9c

Please sign in to comment.