Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit a4c211f

Browse files
committed
Added prehandle
1 parent bb63b67 commit a4c211f

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ static SimpleHttpServer createSimpleHttpServer(final Integer port, final Integer
3737

3838
private final HttpServer server = HttpServer.create();
3939

40-
private final HashMap<HttpContext,HttpHandler> contexts = new HashMap<>();
40+
private HttpSessionHandler sessionHandler;
41+
42+
private final Map<HttpContext,HttpHandler> contexts = new HashMap<>();
4143

4244
private boolean running = false;
4345

@@ -46,6 +48,11 @@ static SimpleHttpServer createSimpleHttpServer(final Integer port, final Integer
4648
server.bind(new InetSocketAddress(port),backlog != null ? backlog : 0);
4749
}
4850

51+
private void handle(final HttpExchange exchange){
52+
if(sessionHandler != null)
53+
sessionHandler.assignSession(exchange);
54+
}
55+
4956
//
5057

5158
@Override
@@ -98,30 +105,54 @@ public final Executor getExecutor(){
98105
return server.getExecutor();
99106
}
100107

108+
@Override
109+
public synchronized final void setHttpSessionHandler(final HttpSessionHandler sessionHandler){
110+
this.sessionHandler = sessionHandler;
111+
}
112+
113+
@Override
114+
public final HttpSessionHandler getHttpSessionHandler(){
115+
return sessionHandler;
116+
}
117+
118+
//
119+
101120
//
102121

103122
@Override
104123
public synchronized final HttpContext createContext(final String path){
105-
final HttpContext context = server.createContext(getContext(path));
106-
contexts.put(context,context.getHandler());
107-
return context;
124+
return createContext(path,(HttpExchange exchange) -> {});
108125
}
109126

110127
@Override
111128
public synchronized final HttpContext createContext(final String path, final HttpHandler handler){
112129
if(!getContext(path).equals("/") && handler instanceof RootHandler)
113130
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
114-
final HttpContext context = server.createContext(getContext(path),handler);
131+
132+
final HttpHandler wrapper = exchange -> {
133+
handle(exchange);
134+
handler.handle(exchange);
135+
};
136+
final HttpContext context = server.createContext(getContext(path),wrapper);
137+
115138
contexts.put(context,handler);
139+
116140
return context;
117141
}
118142

119143
@Override
120144
public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler){
121145
if(!getContext(path).equals("/") && handler instanceof RootHandler)
122146
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
123-
final HttpContext context = server.createContext(getContext(path),(exchange) -> handler.handle(SimpleHttpExchange.create(exchange)));
147+
148+
final HttpHandler wrapper = exchange -> {
149+
handle(exchange);
150+
handler.handle(SimpleHttpExchange.create(exchange));
151+
};
152+
final HttpContext context = server.createContext(getContext(path),wrapper);
153+
124154
contexts.put(context,context.getHandler());
155+
125156
return context;
126157
}
127158

@@ -249,11 +280,9 @@ public synchronized final void removeContext(final HttpContext context){
249280

250281
@Override
251282
public final HttpHandler getContextHandler(final String path){
252-
for(final HttpContext context : contexts.keySet()){
253-
if(context.getPath().equals(getContext(path))){
283+
for(final HttpContext context : contexts.keySet())
284+
if(context.getPath().equals(getContext(path)))
254285
return context.getHandler();
255-
}
256-
}
257286
return null;
258287
}
259288

0 commit comments

Comments
 (0)