-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/buffer-max-size
- Loading branch information
Showing
10 changed files
with
273 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
avaje-jex/src/main/java/io/avaje/jex/routes/MultiHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.avaje.jex.routes; | ||
|
||
import io.avaje.jex.Context; | ||
import io.avaje.jex.ExchangeHandler; | ||
|
||
import java.io.IOException; | ||
|
||
final class MultiHandler implements ExchangeHandler { | ||
|
||
private final ExchangeHandler[] handlers; | ||
|
||
MultiHandler(ExchangeHandler[] handlers) { | ||
this.handlers = handlers; | ||
} | ||
|
||
@Override | ||
public void handle(Context ctx) throws IOException { | ||
for (ExchangeHandler handler : handlers) { | ||
handler.handle(ctx); | ||
if (ctx.responseSent()) { | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
avaje-jex/src/main/java/io/avaje/jex/routes/RouteIndexBuild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.avaje.jex.routes; | ||
|
||
import io.avaje.jex.ExchangeHandler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Build the RouteIndex. | ||
*/ | ||
final class RouteIndexBuild { | ||
|
||
/** | ||
* Partition entries by the number of path segments. | ||
*/ | ||
private final RouteIndexBuild.Entry[] entries = new RouteIndexBuild.Entry[6]; | ||
|
||
/** | ||
* Wildcard/splat based route entries. | ||
*/ | ||
private final List<SpiRoutes.Entry> wildcardEntries = new ArrayList<>(); | ||
|
||
RouteIndexBuild() { | ||
for (int i = 0; i < entries.length; i++) { | ||
entries[i] = new RouteIndexBuild.Entry(); | ||
} | ||
} | ||
|
||
private int index(int segmentCount) { | ||
return Math.min(segmentCount, 5); | ||
} | ||
|
||
void add(SpiRoutes.Entry entry) { | ||
if (entry.multiSlash()) { | ||
wildcardEntries.add(entry); | ||
} else { | ||
entries[index(entry.segmentCount())].add(entry); | ||
} | ||
} | ||
|
||
/** | ||
* Build and return the RouteIndex. | ||
*/ | ||
RouteIndex build() { | ||
final List<List<SpiRoutes.Entry>> pathEntries = new ArrayList<>(); | ||
for (Entry entry : entries) { | ||
pathEntries.add(entry.build()); | ||
} | ||
return new RouteIndex(wildcardEntries, pathEntries); | ||
} | ||
|
||
private static class Entry { | ||
|
||
private final List<SpiRoutes.Entry> list = new ArrayList<>(); | ||
private final Map<String,List<SpiRoutes.Entry>> pathMap = new HashMap<>(); | ||
|
||
void add(SpiRoutes.Entry entry) { | ||
if (entry.literal()) { | ||
// add literal paths to the beginning | ||
list.addFirst(entry); | ||
} else { | ||
pathMap.computeIfAbsent(entry.matchPath(), k -> new ArrayList<>(2)).add(entry); | ||
} | ||
} | ||
|
||
List<SpiRoutes.Entry> build() { | ||
List<SpiRoutes.Entry> result = new ArrayList<>(list.size() + pathMap.size()); | ||
result.addAll(list); | ||
pathMap.values().forEach(pathList -> { | ||
if (pathList.size() == 1) { | ||
result.add(pathList.getFirst()); | ||
} else { | ||
ExchangeHandler[] handlers = pathList.stream() | ||
.map(SpiRoutes.Entry::handler) | ||
.toList() | ||
.toArray(new ExchangeHandler[0]); | ||
result.add(pathList.getFirst().multiHandler(handlers)); | ||
} | ||
}); | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
avaje-jex/src/test/java/io/avaje/jex/jdk/MultiHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.avaje.jex.jdk; | ||
|
||
import io.avaje.jex.Jex; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.http.HttpResponse; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class MultiHandlerTest { | ||
|
||
static TestPair pair = init(); | ||
|
||
static TestPair init() { | ||
Jex app = Jex.create() | ||
.routing(routing -> routing | ||
.get("/hi", ctx4 -> { | ||
if (ctx4.header("Hx-Request") != null) { | ||
ctx4.text("HxResponse"); | ||
} | ||
}) | ||
.get("/hi", ctx -> ctx.text("NormalResponse")) | ||
.get("/hi/{id}", ctx3 -> { | ||
if (ctx3.header("Hx-Request") != null) { | ||
ctx3.text("HxResponse|" + ctx3.pathParam("id")); | ||
} | ||
}) | ||
.get("/hi/{id}", ctx2 -> { | ||
if (ctx2.header("H2-Request") != null) { | ||
ctx2.text("H2Response|" + ctx2.pathParam("id")); | ||
} | ||
}) | ||
.get("/hi/{id}", ctx1 -> ctx1.text("NormalResponse|" + ctx1.pathParam("id"))) | ||
); | ||
return TestPair.create(app); | ||
} | ||
|
||
@AfterAll | ||
static void end() { | ||
pair.shutdown(); | ||
} | ||
|
||
@Test | ||
void test() { | ||
HttpResponse<String> hres = pair.request().path("hi").GET().asString(); | ||
assertThat(hres.statusCode()).isEqualTo(200); | ||
assertThat(hres.body()).isEqualTo("NormalResponse"); | ||
|
||
HttpResponse<String> hxRes = pair.request() | ||
.header("Hx-Request", "true") | ||
.path("hi") | ||
.GET().asString(); | ||
assertThat(hxRes.statusCode()).isEqualTo(200); | ||
assertThat(hxRes.body()).isEqualTo("HxResponse"); | ||
} | ||
|
||
@Test | ||
void testWithPathParam() { | ||
HttpResponse<String> hres = pair.request().path("hi/42").GET().asString(); | ||
assertThat(hres.statusCode()).isEqualTo(200); | ||
assertThat(hres.body()).isEqualTo("NormalResponse|42"); | ||
|
||
HttpResponse<String> hxRes = pair.request() | ||
.header("Hx-Request", "true") | ||
.path("hi/42") | ||
.GET().asString(); | ||
assertThat(hxRes.statusCode()).isEqualTo(200); | ||
assertThat(hxRes.body()).isEqualTo("HxResponse|42"); | ||
|
||
HttpResponse<String> h2Res = pair.request() | ||
.header("H2-Request", "true") | ||
.path("hi/42") | ||
.GET().asString(); | ||
assertThat(h2Res.statusCode()).isEqualTo(200); | ||
assertThat(h2Res.body()).isEqualTo("H2Response|42"); | ||
} | ||
} |
Oops, something went wrong.