Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Predicate;

import io.avaje.jex.Context;
import io.avaje.jex.security.Role;
import io.avaje.jex.spi.JexPlugin;

/**
Expand Down Expand Up @@ -58,15 +59,16 @@ sealed interface Builder
permits StaticResourceHandlerBuilder {

/**
* Sets the HTTP path for the static resource handler.
* Sets the HTTP route for the static resource handler.
*
* @param path the HTTP path prefix
* @param roles the security roles for the route
* @return the updated configuration
*/
Builder httpPath(String path);
Builder route(String path, Role... roles);

/**
* Sets the index file to be served when a directory is requests.
* Sets the index file to be served when a directory is requested.
*
* @param directoryIndex the index file
* @return the updated configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.avaje.jex.staticcontent;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -11,7 +10,6 @@
import com.sun.net.httpserver.HttpExchange;

import io.avaje.jex.Context;
import io.avaje.jex.compression.CompressedOutputStream;
import io.avaje.jex.compression.CompressionConfig;

final class StaticFileHandler extends AbstractStaticHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.avaje.jex.ExchangeHandler;
import io.avaje.jex.Jex;
import io.avaje.jex.compression.CompressionConfig;
import io.avaje.jex.security.Role;

final class StaticResourceHandlerBuilder implements StaticContent.Builder, StaticContent {

Expand All @@ -29,6 +30,7 @@ final class StaticResourceHandlerBuilder implements StaticContent.Builder, Stati
private Predicate<Context> skipFilePredicate = NO_OP_PREDICATE;
private boolean isClasspath = true;
private boolean precompress;
private Role[] roles = {};

private StaticResourceHandlerBuilder(String root) {
this.root = root;
Expand All @@ -40,7 +42,7 @@ static StaticResourceHandlerBuilder builder(String root) {

@Override
public void apply(Jex jex) {
jex.get(path, createHandler(jex.config().compression()));
jex.get(path, createHandler(jex.config().compression()), roles);
}

@Override
Expand Down Expand Up @@ -73,8 +75,9 @@ ExchangeHandler createHandler(CompressionConfig compress) {
}

@Override
public StaticResourceHandlerBuilder httpPath(String path) {
public StaticResourceHandlerBuilder route(String path, Role... roles) {
this.path = path.endsWith("/") ? path + "*" : path;
this.roles = roles;
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion avaje-jex-static-content/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Defines the Static Content API for serving static resources with Jex - see {@link StaticContent}.
* Defines the Static Content API for serving static resources with Jex - see {@link io.avaje.jex.staticcontent.StaticContent}.
*
* <pre>{@code
* var staticContent = StaticContentService.createCP("/public").httpPath("/").directoryIndex("index.html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ static TestPair init() {

final Jex app =
Jex.create()
.plugin(defaultCP().httpPath("/index").build())
.plugin(defaultFile().httpPath("/indexFile").build())
.plugin(defaultCP().httpPath("/indexWild/*").build())
.plugin(defaultFile().httpPath("/indexWildFile/*").build())
.plugin(defaultCP().httpPath("/sus/").build())
.plugin(defaultFile().httpPath("/susFile/*").build())
.plugin(StaticContent.createCP("/logback.xml").httpPath("/single").build())
.plugin(defaultCP().route("/index").build())
.plugin(defaultFile().route("/indexFile").build())
.plugin(defaultCP().route("/indexWild/*").build())
.plugin(defaultFile().route("/indexWildFile/*").build())
.plugin(defaultCP().route("/sus/").build())
.plugin(defaultFile().route("/susFile/*").build())
.plugin(StaticContent.createCP("/logback.xml").route("/single").build())
.plugin(
StaticContent.createFile("src/test/resources/logback.xml")
.httpPath("/singleFile").build());
.route("/singleFile").build());

return TestPair.create(app);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ static TestPair init() {

final Jex app =
Jex.create()
.plugin(defaultCP().httpPath("/index").build())
.plugin(defaultFile().httpPath("/indexFile").build())
.plugin(defaultCP().httpPath("/indexWild/*").build())
.plugin(defaultFile().httpPath("/indexWildFile/*").build())
.plugin(defaultCP().httpPath("/sus/").build())
.plugin(defaultFile().httpPath("/susFile/*").build())
.plugin(StaticContent.createCP("/logback.xml").httpPath("/single").build())
.plugin(defaultCP().route("/index").build())
.plugin(defaultFile().route("/indexFile").build())
.plugin(defaultCP().route("/indexWild/*").build())
.plugin(defaultFile().route("/indexWildFile/*").build())
.plugin(defaultCP().route("/sus/").build())
.plugin(defaultFile().route("/susFile/*").build())
.plugin(StaticContent.createCP("/logback.xml").route("/single").build())
.plugin(
StaticContent.createFile("src/test/resources/logback.xml")
.httpPath("/singleFile").build());
.route("/singleFile").build());

return TestPair.create(app);
}
Expand Down
Loading