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

Commit 41d72fb

Browse files
committed
Started HttpSess impl
1 parent 9e238c1 commit 41d72fb

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.kttdevelopment.simplehttpserver;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
5+
import java.util.*;
6+
7+
/**
8+
* This class assigns {@link HttpSession} to every client.
9+
*
10+
* @since 03.03.00
11+
* @version 03.03.00
12+
* @author Ktt Development
13+
*/
14+
public class HttpSessionHandler {
15+
16+
private Map<String,HttpSession> sessions = new HashMap<>();
17+
18+
private final String cookie;
19+
20+
/**
21+
* Creates a session handler using the cookie <code>__session-id</code>.
22+
*
23+
* @since 03.03.00
24+
* @author Ktt Development
25+
*/
26+
public HttpSessionHandler(){
27+
cookie = "__session-id";
28+
}
29+
30+
/**
31+
* Creates a session handler, storing the id at the specified cookie.
32+
*
33+
* @param cookie cookie to store session id
34+
*
35+
* @since 03.03.00
36+
* @author Ktt Development
37+
*/
38+
public HttpSessionHandler(final String cookie){
39+
this.cookie = cookie;
40+
}
41+
42+
/**
43+
* Assigns a session id to a client. It is important to make sure duplicate ids do not occur.
44+
*
45+
* @param exchange http exchange
46+
* @return session id
47+
*
48+
* @since 03.03.00
49+
* @author Ktt Development
50+
*/
51+
public synchronized String assignSessionID(final HttpExchange exchange){
52+
String id;
53+
do id = UUID.randomUUID().toString();
54+
while(sessions.containsKey(id));
55+
return id;
56+
}
57+
58+
/**
59+
* Assigns a session to the client
60+
* @param exchange
61+
*/
62+
public synchronized final void assignSession(final HttpExchange exchange){
63+
final String sessionId;
64+
final HttpSession session;
65+
66+
final String rcookies = exchange.getRequestHeaders().getFirst("Cookie");
67+
final Map<String,String> cookies = new HashMap<>();
68+
69+
if(rcookies != null && !rcookies.isEmpty()){
70+
final String[] pairs = rcookies.split("; ");
71+
for(final String pair : pairs){
72+
final String[] value = pair.split("=");
73+
cookies.put(value[0],value[1]);
74+
}
75+
}
76+
77+
if((sessionId = cookies.get(cookie)) == null || !sessions.containsKey(sessionId)){
78+
session = new HttpSession() {
79+
80+
private final String sessionID;
81+
private final long creationTime;
82+
private long lastAccessTime;
83+
84+
{
85+
sessionID = assignSessionID(exchange);
86+
creationTime = System.currentTimeMillis();
87+
lastAccessTime = creationTime;
88+
sessions.put(sessionID,this);
89+
}
90+
91+
@Override
92+
public final String getSessionID(){
93+
return sessionID;
94+
}
95+
96+
//
97+
98+
@Override
99+
public final long getCreationTime(){
100+
return creationTime;
101+
}
102+
103+
@Override
104+
public final long getLastAccessTime(){
105+
return lastAccessTime;
106+
}
107+
108+
@Override
109+
public synchronized final void updateLastAccessTime(){
110+
lastAccessTime = System.currentTimeMillis();
111+
}
112+
113+
//
114+
115+
@SuppressWarnings("StringBufferReplaceableByString")
116+
@Override
117+
public final String toString(){
118+
final StringBuilder OUT = new StringBuilder();
119+
OUT.append("HttpSession") .append("{");
120+
OUT.append("sessionID") .append("=") .append(sessionID) .append(", ");
121+
OUT.append("creationTime") .append("=") .append(creationTime) .append(", ");
122+
OUT.append("lastAccessTime").append("=") .append(lastAccessTime);
123+
OUT.append("}");
124+
return OUT.toString();
125+
}
126+
127+
};
128+
129+
final SimpleHttpCookie out =
130+
new SimpleHttpCookie.Builder(cookie,session.getSessionID())
131+
.setPath("/")
132+
.setHttpOnly(true)
133+
.build();
134+
exchange.getResponseHeaders().add("Set-Cookie",out.toCookieHeaderString());
135+
}
136+
}
137+
138+
139+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
209209
*/
210210
public abstract Executor getExecutor();
211211

212+
//
213+
214+
public abstract void setHttpSessionHandler(final HttpSessionHandler sessionHandler);
215+
216+
public abstract HttpSessionHandler getHttpSessionHandler();
217+
212218
//
213219

214220
/**

0 commit comments

Comments
 (0)