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

Commit 063ecce

Browse files
authored
Moved HttpSession to server; SimpleHttpHandler changed to interface.
Moved HttpSession to server; SimpleHttpHandler changed to interface.
2 parents 9e238c1 + 2d445c3 commit 063ecce

File tree

7 files changed

+196
-133
lines changed

7 files changed

+196
-133
lines changed

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,11 @@ public abstract class HttpSession {
1616
/**
1717
* Creates an empty {@link HttpSession}. Applications don't use this method.
1818
*
19-
* @see HttpSessionImpl#createHttpSession()
2019
* @since 02.00.00
2120
* @author Ktt Development
2221
*/
2322
HttpSession(){ }
2423

25-
//
26-
27-
/**
28-
* Creates a {@link HttpSession}.
29-
*
30-
* @return a {@link HttpSession}
31-
*
32-
* @since 02.00.00
33-
* @author Ktt Development
34-
*/
35-
synchronized static HttpSession create(){
36-
return HttpSessionImpl.createHttpSession();
37-
}
38-
3924
//
4025

4126
/**
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 final 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+
* 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)}.
60+
*
61+
* @param exchange http exchange
62+
*
63+
* @since 03.03.00
64+
* @author Ktt Development
65+
*/
66+
public final HttpSession getSession(final HttpExchange exchange){
67+
final String sessionId;
68+
final HttpSession session;
69+
70+
final String rcookies = exchange.getRequestHeaders().getFirst("Cookie");
71+
final Map<String,String> cookies = new HashMap<>();
72+
73+
if(rcookies != null && !rcookies.isEmpty()){
74+
final String[] pairs = rcookies.split("; ");
75+
for(final String pair : pairs){
76+
final String[] value = pair.split("=");
77+
cookies.put(value[0],value[1]);
78+
}
79+
}
80+
81+
if((sessionId = cookies.get(cookie)) == null || !sessions.containsKey(sessionId)){
82+
session = new HttpSession() {
83+
84+
private final String sessionID;
85+
private final long creationTime;
86+
private long lastAccessTime;
87+
88+
{
89+
sessionID = assignSessionID(exchange);
90+
creationTime = System.currentTimeMillis();
91+
lastAccessTime = creationTime;
92+
sessions.put(sessionID,this);
93+
}
94+
95+
@Override
96+
public final String getSessionID(){
97+
return sessionID;
98+
}
99+
100+
//
101+
102+
@Override
103+
public final long getCreationTime(){
104+
return creationTime;
105+
}
106+
107+
@Override
108+
public final long getLastAccessTime(){
109+
return lastAccessTime;
110+
}
111+
112+
@Override
113+
public synchronized final void updateLastAccessTime(){
114+
lastAccessTime = System.currentTimeMillis();
115+
}
116+
117+
//
118+
119+
@SuppressWarnings("StringBufferReplaceableByString")
120+
@Override
121+
public final String toString(){
122+
final StringBuilder OUT = new StringBuilder();
123+
OUT.append("HttpSession") .append("{");
124+
OUT.append("sessionID") .append("=") .append(sessionID) .append(", ");
125+
OUT.append("creationTime") .append("=") .append(creationTime) .append(", ");
126+
OUT.append("lastAccessTime").append("=") .append(lastAccessTime);
127+
OUT.append("}");
128+
return OUT.toString();
129+
}
130+
131+
};
132+
133+
final SimpleHttpCookie out =
134+
new SimpleHttpCookie.Builder(cookie,session.getSessionID())
135+
.setPath("/")
136+
.setHttpOnly(true)
137+
.build();
138+
exchange.getResponseHeaders().add("Set-Cookie",out.toCookieHeaderString());
139+
}else{
140+
session = sessions.get(sessionId);
141+
}
142+
return session;
143+
}
144+
145+
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,6 @@ static SimpleHttpExchange create(final HttpExchange exchange){
346346
*/
347347
public abstract void setCookie(final SimpleHttpCookie cookie);
348348

349-
//
350-
351-
/**
352-
* Returns the http session of the exchange if one exists; if one does not exist it will create one. <b>A new session will only persist if the exchange is sent.</b>
353-
*
354-
* @return http session
355-
*
356-
* @see HttpSession
357-
* @since 02.00.00
358-
* @author Ktt Development
359-
*/
360-
public abstract HttpSession getHttpSession();
361-
362349
//
363350

364351
/**

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,11 @@ public synchronized final void setCookie(final String key, final String value){
317317
@Override
318318
public synchronized final void setCookie(final SimpleHttpCookie cookie){
319319
final String cstring = cookie.toCookieHeaderString();
320-
if(cstring.startsWith("__session-id="))
321-
throw new IllegalArgumentException("The cookie '__session-id' can not be set because it is reserved by the server");
322320
getResponseHeaders().add("Set-Cookie",cstring);
323321
}
324322

325323
//
326-
327-
@Override
328-
public synchronized final HttpSession getHttpSession(){
329-
final String sessionId;
330-
final HttpSession session;
331-
332-
if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){
333-
session = HttpSession.create();
334-
final SimpleHttpCookie cookie =
335-
new SimpleHttpCookie.Builder("__session-id",session.getSessionID())
336-
.setPath("/")
337-
.setHttpOnly(true)
338-
.build();
339-
getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); // bypass implementation
340-
}else{
341-
session = HttpSession.sessions.get(sessionId);
342-
}
343-
return session;
344-
}
345-
324+
346325
@Override
347326
public final OutputStream getOutputStream(){
348327
return outputStream;

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,23 @@
1414
* @version 03.03.00
1515
* @author Ktt Development
1616
*/
17-
public interface SimpleHttpHandler {
18-
/*
17+
18+
public interface SimpleHttpHandler extends HttpHandler {
19+
1920
/**
20-
* Encapsulates the {@link #handle(SimpleHttpExchange)} for the authenticator. Applications do not use this.
21+
* Encapsulates the {@link #handle(SimpleHttpExchange)} for the authenticator. This method is reserved by the server; <b>do not override this</b>, it will break the {@link #handle(SimpleHttpExchange)} method.
2122
*
2223
* @param exchange client information
2324
* @throws IOException internal failure
2425
*
2526
* @since 02.00.00
2627
* @author Ktt Development
27-
/
28+
*/
2829
@Override
29-
default final void handle(final HttpExchange exchange) throws IOException{
30-
final SimpleHttpExchange sxe = SimpleHttpExchange.create(exchange);
31-
if(authenticate(sxe))
32-
handle(sxe);
33-
else
34-
sxe.close();
30+
default void handle(final HttpExchange exchange) throws IOException{
31+
handle(SimpleHttpExchange.create(exchange));
3532
}
36-
*/
33+
3734
/**
3835
* Handlers the given request and generates a response <b>if no exceptions occur</b>.
3936
*

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

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ 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+
218+
public abstract HttpSession getHttpSession(final HttpExchange exchange);
219+
212220
//
213221

214222
/**
@@ -221,7 +229,6 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
221229
*
222230
* @see HttpContext
223231
* @see #createContext(String, HttpHandler)
224-
* @see #createContext(String, SimpleHttpHandler)
225232
* @see #removeContext(String)
226233
* @see #removeContext(HttpContext)
227234
* @since 02.00.00
@@ -241,34 +248,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
241248
* @see HttpContext
242249
* @see HttpHandler
243250
* @see #createContext(String)
244-
* @see #createContext(String, SimpleHttpHandler)
245251
* @see #removeContext(String)
246252
* @see #removeContext(HttpContext)
247253
* @since 02.00.00
248254
* @author Ktt Development
249255
*/
250256
public abstract HttpContext createContext(final String context, final HttpHandler handler);
251257

252-
/**
253-
* Creates a context mapped to a specific {@link HttpHandler}.
254-
*
255-
* @param context the context
256-
* @param handler the handler
257-
* @return the http context associated with the context
258-
* @throws IllegalArgumentException if the context is invalid or taken
259-
* @throws NullPointerException if the context is null
260-
*
261-
* @see HttpContext
262-
* @see SimpleHttpHandler
263-
* @see #createContext(String)
264-
* @see #createContext(String, HttpHandler)
265-
* @see #removeContext(String)
266-
* @see #removeContext(HttpContext)
267-
* @since 03.03.00
268-
* @author Ktt Development
269-
*/
270-
public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler);
271-
272258
//
273259

274260
/**
@@ -283,7 +269,6 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
283269
* @see HttpContext
284270
* @see Authenticator
285271
* @see #createContext(String, HttpHandler, Authenticator)
286-
* @see #createContext(String, SimpleHttpHandler, Authenticator)
287272
* @see #removeContext(String)
288273
* @see #removeContext(HttpContext)
289274
* @since 03.03.00
@@ -305,36 +290,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
305290
* @see HttpHandler
306291
* @see Authenticator
307292
* @see #createContext(String, Authenticator)
308-
* @see #createContext(String, SimpleHttpHandler, Authenticator)
309293
* @see #removeContext(String)
310294
* @see #removeContext(HttpContext)
311295
* @since 03.03.00
312296
* @author Ktt Development
313297
*/
314298
public abstract HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator);
315299

316-
/**
317-
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
318-
*
319-
* @param context the context
320-
* @param handler the handler
321-
* @param authenticator authenticator
322-
* @return the http context associated with the context
323-
* @throws IllegalArgumentException if the context is invalid or taken
324-
* @throws NullPointerException if the context is null
325-
*
326-
* @see HttpContext
327-
* @see SimpleHttpHandler
328-
* @see Authenticator
329-
* @see #createContext(String, Authenticator)
330-
* @see #createContext(String, HttpHandler, Authenticator)
331-
* @see #removeContext(String)
332-
* @see #removeContext(HttpContext)
333-
* @since 03.03.00
334-
* @author Ktt Development
335-
*/
336-
public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler, final Authenticator authenticator);
337-
338300
//
339301

340302
/**

0 commit comments

Comments
 (0)