Skip to content

Commit 4928979

Browse files
authored
Add RequestContextResolver Feature (#204)
* now adds request resolver * Update BaseControllerWriter.java * add for other generators * works * lowercase b * Update JsonBUtil.java * pr comment * add ServerContext class to hold both req and res * prevent rebinding context * add hibernate plugin
1 parent 5fd620d commit 4928979

File tree

42 files changed

+650
-162
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+650
-162
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
fail-fast: false
1212
matrix:
13-
java_version: [11, 17, 19]
13+
java_version: [11, 17, 20]
1414
os: [ubuntu-latest]
1515

1616
steps:

http-api/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,26 @@
1515
<tag>avaje-http-parent-1.19</tag>
1616
</scm>
1717

18+
<dependencies>
19+
<dependency>
20+
<groupId>io.avaje</groupId>
21+
<artifactId>avaje-inject</artifactId>
22+
<version>9.0</version>
23+
<scope>provided</scope>
24+
<optional>true</optional>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-surefire-plugin</artifactId>
33+
<version>3.0.0</version>
34+
<configuration>
35+
<forkCount>0</forkCount>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
1840
</project>

http-api/src/main/java/io/avaje/http/api/Controller.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323

2424
/** Specify the path mapping request to the controller. */
2525
String value() default "";
26+
27+
/** Specify if the http request context should be instrumented via RequestContextResolver */
28+
boolean instrumentRequestContext() default false;
2629
}

http-api/src/main/java/io/avaje/http/api/Delete.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
*
2020
* }</pre>
2121
*/
22-
@Target(value=METHOD)
23-
@Retention(value=RUNTIME)
24-
@HttpMethod(value="DELETE")
22+
@Target(METHOD)
23+
@Retention(RUNTIME)
24+
@HttpMethod("DELETE")
2525
public @interface Delete {
2626

27-
/**
28-
* Specify the path.
29-
*/
27+
/** Specify the path. */
3028
String value() default "";
29+
30+
boolean instrumentRequestContext() default false;
3131
}

http-api/src/main/java/io/avaje/http/api/Get.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@
4949
*
5050
* }</pre>
5151
*/
52-
@Target(value=METHOD)
53-
@Retention(value=RUNTIME)
54-
@HttpMethod(value="GET")
52+
@Target(METHOD)
53+
@Retention(RUNTIME)
54+
@HttpMethod("GET")
5555
public @interface Get {
56+
57+
/** Specify the path. */
5658
String value() default "";
59+
60+
boolean instrumentRequestContext() default false;
5761
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package io.avaje.http.api;
22

3-
import java.lang.annotation.Retention;
4-
import java.lang.annotation.Target;
5-
63
import static java.lang.annotation.ElementType.METHOD;
74
import static java.lang.annotation.RetentionPolicy.RUNTIME;
85

9-
/**
10-
* Marks a method that handles HTTP PATCH requests.
11-
*/
12-
@Target(value = METHOD)
13-
@Retention(value = RUNTIME)
14-
@HttpMethod(value = "PATCH")
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
/** Marks a method that handles HTTP PATCH requests. */
10+
@Target(METHOD)
11+
@Retention(RUNTIME)
12+
@HttpMethod("PATCH")
1513
public @interface Patch {
14+
15+
/** Specify the path. */
1616
String value() default "";
17+
18+
boolean instrumentRequestContext() default false;
1719
}

http-api/src/main/java/io/avaje/http/api/Post.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
...
1717
* }
1818
*/
19-
@Target(value=METHOD)
20-
@Retention(value=RUNTIME)
21-
@HttpMethod(value="POST")
19+
@Target(METHOD)
20+
@Retention(RUNTIME)
21+
@HttpMethod("POST")
2222
public @interface Post {
23+
24+
/** Specify the path. */
2325
String value() default "";
26+
27+
boolean instrumentRequestContext() default false;
2428
}

http-api/src/main/java/io/avaje/http/api/Put.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
/**
1010
* Marks a method that handles HTTP PUT requests.
1111
*/
12-
@Target(value=METHOD)
13-
@Retention(value=RUNTIME)
14-
@HttpMethod(value="PUT")
12+
@Target(METHOD)
13+
@Retention(RUNTIME)
14+
@HttpMethod("PUT")
1515
public @interface Put {
16+
17+
/** Specify the path. */
1618
String value() default "";
19+
20+
boolean instrumentRequestContext() default false;
1721
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.avaje.http.api.context;
2+
3+
import java.util.Optional;
4+
import java.util.concurrent.Callable;
5+
import java.util.function.Supplier;
6+
7+
/**
8+
* The holder for the current request context that is bound to instrumented threads. Allowing lookup
9+
* of the current request if it is present. The Default implementation uses ThreadLocal. If you are
10+
* able, you should provide an implementation using ScopedValues.
11+
*/
12+
public interface RequestContextResolver {
13+
14+
/**
15+
* Wraps the execution of the given callable in request context processing.
16+
*
17+
* @param ctx The request context
18+
* @param callable The callable
19+
* @param <T> The return type of the callable
20+
* @return The return value of the callable
21+
* @throws Exception if the callable throws an exception
22+
*/
23+
<T> T callWith(ServerContext ctx, Callable<T> callable) throws Exception;
24+
25+
/**
26+
* Wraps the execution of the given supplier in request context processing.
27+
*
28+
* @param ctx The request context
29+
* @param supplier The supplier
30+
* @param <T> The return type of the supplier
31+
* @return The return value of the supplier
32+
*/
33+
<T> T supplyWith(ServerContext ctx, Supplier<T> supplier);
34+
35+
/**
36+
* Wraps the execution of the given runnable in request context processing.
37+
*
38+
* @param ctx The request context
39+
* @param runnable The runnable
40+
*/
41+
void runWith(ServerContext request, Runnable runnable);
42+
43+
/**
44+
* Retrieve the current server context.
45+
*
46+
* @return The request context if it is present
47+
*/
48+
Optional<ServerContext> currentRequest();
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.avaje.http.api.context;
2+
3+
/** Holder for the Server Request/Response Classes */
4+
public class ServerContext {
5+
6+
private final Object request;
7+
private final Object response;
8+
9+
public ServerContext(Object req, Object res) {
10+
request = req;
11+
response = res;
12+
}
13+
14+
/**
15+
* Retrieve the current server request.
16+
*
17+
* @return The request
18+
*/
19+
<T> T request() {
20+
return (T) request;
21+
}
22+
23+
/**
24+
* Retrieve the current server response.
25+
*
26+
* @return The request
27+
*/
28+
<T> T response() {
29+
return (T) response;
30+
}
31+
}

0 commit comments

Comments
 (0)