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

Commit 94c76b5

Browse files
authored
Added get session & update
Added get session & update
2 parents bfc96d9 + e1c7405 commit 94c76b5

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public synchronized String assignSessionID(final HttpExchange exchange){
5656
}
5757

5858
/**
59-
* Assigns a session to the client. Session will only be saved client side if the exchange headers are sent using {@link HttpExchange#sendResponseHeaders(int, long)}.
59+
* Returns the session of the client or assigns one if it does not yet have one Session will only be saved client side if the exchange headers are sent using {@link HttpExchange#sendResponseHeaders(int, long)}.
6060
*
6161
* @param exchange http exchange
6262
*
6363
* @since 03.03.00
6464
* @author Ktt Development
6565
*/
66-
public synchronized final void assignSession(final HttpExchange exchange){
66+
public final HttpSession getSession(final HttpExchange exchange){
6767
final String sessionId;
6868
final HttpSession session;
6969

@@ -136,7 +136,10 @@ public final String toString(){
136136
.setHttpOnly(true)
137137
.build();
138138
exchange.getResponseHeaders().add("Set-Cookie",out.toCookieHeaderString());
139+
}else{
140+
session = sessions.get(sessionId);
139141
}
142+
return session;
140143
}
141144

142145
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.kttdevelopment.simplehttpserver;
22

3-
import com.sun.net.httpserver.HttpExchange;
43
import com.sun.net.httpserver.HttpHandler;
54

65
import java.io.IOException;
@@ -14,6 +13,7 @@
1413
* @version 03.03.00
1514
* @author Ktt Development
1615
*/
16+
1717
public interface SimpleHttpHandler extends HttpHandler {
1818

1919
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
215215

216216
public abstract HttpSessionHandler getHttpSessionHandler();
217217

218+
public abstract HttpSession getHttpSession(final HttpExchange exchange);
219+
218220
//
219221

220222
/**

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static SimpleHttpServer createSimpleHttpServer(final Integer port, final Integer
5050

5151
private void handle(final HttpExchange exchange){
5252
if(sessionHandler != null)
53-
sessionHandler.assignSession(exchange);
53+
sessionHandler.getSession(exchange).updateLastAccessTime();
5454
}
5555

5656
//
@@ -115,9 +115,12 @@ public final HttpSessionHandler getHttpSessionHandler(){
115115
return sessionHandler;
116116
}
117117

118-
//
118+
@Override
119+
public HttpSession getHttpSession(final HttpExchange exchange){
120+
return sessionHandler.getSession(exchange);
121+
}
119122

120-
//
123+
//
121124

122125
@Override
123126
public synchronized final HttpContext createContext(final String path){

0 commit comments

Comments
 (0)