Skip to content

Commit 159bb53

Browse files
committed
wer
1 parent f8660aa commit 159bb53

12 files changed

Lines changed: 124 additions & 36 deletions

File tree

modules/packed-incubator/packed-incubator-web/src/main/java/app/packed/web/HttpContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import app.packed.bean.BeanTrigger.AutoInject;
1919
import app.packed.binding.Key;
2020
import app.packed.context.Context;
21+
import app.packed.web.session.SessionContext;
2122
import internal.app.packed.bean.scanning.IntrospectorOnContextService;
2223
import internal.app.packed.extension.base.BaseExtensionBeanIntrospector;
2324
/**
@@ -26,6 +27,8 @@
2627
@AutoInject(introspector = HttpContextBeanIntrospector.class, requiresContext = HttpContext.class)
2728
public interface HttpContext extends Context<WebExtension> {
2829

30+
SessionContext session();
31+
2932
/** Returns the current HTTP request. */
3033
HttpRequest request();
3134

modules/packed-incubator/packed-incubator-web/src/main/java/app/packed/web/HttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ final class HttpRequestBeanIntrospector extends BaseExtensionBeanIntrospector {
5656
/** {@inheritDoc} */
5757
@Override
5858
public void onExtensionService(Key<?> key, IntrospectorOnContextService service) {
59-
service.binder().bindOp(new Op1<HttpContext, HttpRequest>(f->f.request()) {});
59+
service.binder().bindOp(new Op1<>(HttpContext::request) {});
6060
}
6161
}

modules/packed-incubator/packed-incubator-web/src/main/java/app/packed/web/HttpResponse.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
import java.io.IOException;
1919

20+
import app.packed.bean.BeanIntrospector;
2021
import app.packed.bean.BeanTrigger.AutoInject;
2122
import app.packed.binding.Key;
23+
import app.packed.extension.BaseExtension;
2224
import app.packed.operation.Op1;
23-
import internal.app.packed.bean.scanning.IntrospectorOnContextService;
24-
import internal.app.packed.extension.base.BaseExtensionBeanIntrospector;
2525

2626
/**
2727
* Represents an HTTP response.
@@ -42,11 +42,11 @@ public interface HttpResponse {
4242
void write(String body, String contentType) throws IOException;
4343
}
4444

45-
final class HttpResponseBeanIntrospector extends BaseExtensionBeanIntrospector {
45+
final class HttpResponseBeanIntrospector extends BeanIntrospector<BaseExtension> {
4646

4747
/** {@inheritDoc} */
4848
@Override
49-
public void onExtensionService(Key<?> key, IntrospectorOnContextService service) {
50-
service.binder().bindOp(new Op1<HttpContext, HttpResponse>(f->f.response()) {});
49+
public void onExtensionService(Key<?> key, OnContextService service) {
50+
service.binder().bindOp(new Op1<>(HttpContext::response) {});
5151
}
5252
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2026 Kasper Nielsen.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package app.packed.web;
17+
18+
import app.packed.operation.OperationHandle;
19+
import app.packed.operation.OperationMirror;
20+
import internal.app.packed.web.WebGetOperationHandle;
21+
22+
/**
23+
* A mirror for a web operation.
24+
*/
25+
public class WebOperationMirror extends OperationMirror {
26+
27+
final WebGetOperationHandle handle;
28+
29+
public WebOperationMirror(OperationHandle<?> handle) {
30+
super(handle);
31+
this.handle = (WebGetOperationHandle) handle;
32+
}
33+
34+
/** {@return the URL pattern this operation responds to} */
35+
public String urlPattern() {
36+
return handle.urlPattern;
37+
}
38+
}

modules/packed-incubator/packed-incubator-web/src/main/java/app/packed/web/WebTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919

2020
import app.packed.application.App;
21+
import app.packed.application.ApplicationMirror;
2122
import app.packed.assembly.BaseAssembly;
2223

2324
/**
@@ -43,8 +44,17 @@ protected void build() {
4344

4445
public static class Handlers {
4546

47+
Handlers(ApplicationMirror am) {
48+
am.operations().forEach(c -> {
49+
if (c instanceof WebOperationMirror wom) {
50+
System.out.println(wom.urlPattern() + wom.contexts());
51+
}
52+
});
53+
}
54+
4655
@WebGet(url = "/json")
47-
public void json(HttpResponse ctx) throws IOException {
56+
public void json(HttpResponse ctx, HttpRequest request) throws IOException {
57+
//System.out.println(sc.getClass());
4858
ctx.write("{\"status\":\"ok\"}", "application/json");
4959
}
5060
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2026 Kasper Nielsen.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package app.packed.web.session;
17+
18+
import app.packed.bean.BeanTrigger.AutoInject;
19+
import app.packed.binding.Key;
20+
import app.packed.context.Context;
21+
import app.packed.operation.Op1;
22+
import app.packed.web.HttpContext;
23+
import app.packed.web.WebExtension;
24+
import internal.app.packed.bean.scanning.IntrospectorOnContextService;
25+
import internal.app.packed.extension.base.BaseExtensionBeanIntrospector;
26+
27+
/**
28+
*
29+
*/
30+
@AutoInject(introspector = SessionContextBeanIntrospector.class, requiresContext = { SessionContext.class, HttpContext.class })
31+
public interface SessionContext extends Context<WebExtension> {
32+
33+
}
34+
35+
final class SessionContextBeanIntrospector extends BaseExtensionBeanIntrospector {
36+
37+
/** {@inheritDoc} */
38+
@Override
39+
public void onExtensionService(Key<?> key, IntrospectorOnContextService service) {
40+
service.binder().bindOp(new Op1<HttpContext, SessionContext>(HttpContext::session) {});
41+
}
42+
}

modules/packed-incubator/packed-incubator-web/src/main/java/internal/app/packed/web/PackedHttpContext.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
import app.packed.web.HttpContext;
2828
import app.packed.web.HttpRequest;
2929
import app.packed.web.HttpResponse;
30+
import app.packed.web.session.SessionContext;
3031

3132
/**
3233
* Implementation of HttpContext wrapping a HttpExchange.
3334
*/
3435
public final class PackedHttpContext implements HttpContext {
3536

36-
private final HttpExchange exchange;
37+
final HttpExchange exchange;
3738
private final PackedHttpRequest request;
3839
private final PackedHttpResponse response;
3940

@@ -147,4 +148,10 @@ public void write(String body, String contentType) throws IOException {
147148
headersSent = true;
148149
}
149150
}
151+
152+
/** {@inheritDoc} */
153+
@Override
154+
public SessionContext session() {
155+
return new SessionContext() {};
156+
}
150157
}

modules/packed-incubator/packed-incubator-web/src/main/java/internal/app/packed/web/WebGetOperationHandle.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import app.packed.operation.OperationInstaller;
2424
import app.packed.web.HttpContext;
2525
import app.packed.web.WebGet;
26+
import app.packed.web.WebOperationMirror;
2627
import internal.app.packed.extension.base.BaseExtensionOperationHandle;
2728

2829
/**
@@ -42,6 +43,11 @@ protected OperationConfiguration newOperationConfiguration() {
4243
return new OperationConfiguration(this);
4344
}
4445

46+
@Override
47+
public WebOperationMirror newOperationMirror() {
48+
return new WebOperationMirror(this);
49+
}
50+
4551
/**
4652
* Called when a @WebGet annotation is found on a method.
4753
*/

modules/packed-incubator/packed-incubator-web/src/main/java/internal/app/packed/web/WebServerSidehandle.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,16 @@
2828
import app.packed.lifecycle.Start;
2929
import app.packed.lifecycle.Stop;
3030
import app.packed.web.HttpContext;
31-
import app.packed.web.HttpRequest;
32-
import app.packed.web.HttpResponse;
3331

3432
/**
3533
* Per-operation sidehandle that manages HTTP handler registration.
3634
*/
37-
public final class WebServerSidehandle implements HttpContext {
35+
public final class WebServerSidehandle {
3836

3937
private final String urlPattern;
4038
private final WebHandlerInvoker invoker;
4139
private final WebServerManager serverManager;
4240

43-
/** Thread-local to hold current context during request handling. */
44-
private static final ThreadLocal<PackedHttpContext> currentContext = new ThreadLocal<>();
45-
4641
public WebServerSidehandle(
4742
@SidehandleBinding(Kind.CONSTANT) String urlPattern,
4843
@SidehandleBinding(Kind.OPERATION_INVOKER) WebHandlerInvoker invoker,
@@ -64,10 +59,10 @@ protected void onStop() {
6459

6560
private void handleRequest(HttpExchange exchange) {
6661
PackedHttpContext ctx = new PackedHttpContext(exchange);
67-
currentContext.set(ctx);
6862
try {
69-
invoker.invoke(this);
63+
invoker.invoke(ctx);
7064
} catch (Throwable e) {
65+
e.printStackTrace();
7166
try {
7267
String error = "Internal Server Error: " + e.getMessage();
7368
byte[] bytes = error.getBytes(StandardCharsets.UTF_8);
@@ -79,25 +74,12 @@ private void handleRequest(HttpExchange exchange) {
7974
// Ignore if we can't send error response
8075
}
8176
} finally {
82-
currentContext.remove();
8377
exchange.close();
8478
}
8579
}
8680

87-
@Override
88-
public HttpRequest request() {
89-
PackedHttpContext ctx = currentContext.get();
90-
return ctx != null ? ctx.request() : null;
91-
}
92-
93-
@Override
94-
public HttpResponse response() {
95-
PackedHttpContext ctx = currentContext.get();
96-
return ctx != null ? ctx.response() : null;
97-
}
98-
9981
/** Interface for invoking the handler method. */
10082
public interface WebHandlerInvoker {
101-
void invoke(WebServerSidehandle context) throws Throwable;
83+
void invoke(HttpContext context) throws Throwable;
10284
}
10385
}

modules/packed/src/main/java/app/packed/context/UnavilableContextException.java renamed to modules/packed/src/main/java/app/packed/context/ContextNotAvailableException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
//NotInContextException
3939

4040
// Hvad med generic ting der ikke passer
41-
public class UnavilableContextException extends BuildException {
41+
public class ContextNotAvailableException extends BuildException {
4242

4343
private static final long serialVersionUID = 1L;
4444

4545
/**
4646
* @param message
4747
*/
48-
public UnavilableContextException(String message) {
48+
public ContextNotAvailableException(String message) {
4949
super(message);
5050
}
5151
}

0 commit comments

Comments
 (0)