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

Commit 9e238c1

Browse files
authored
SimpleHttpHandler now an interface; Authenticator added to #createContext
SimpleHttpHandler now an interface; Authenticator added to #createContext
2 parents 767c374 + cd3424a commit 9e238c1

File tree

11 files changed

+153
-50
lines changed

11 files changed

+153
-50
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public abstract class HttpSession {
3030
* @return a {@link HttpSession}
3131
*
3232
* @since 02.00.00
33-
* @author KTt Development
33+
* @author Ktt Development
3434
*/
3535
synchronized static HttpSession create(){
3636
return HttpSessionImpl.createHttpSession();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class SimpleHttpCookie {
3030
/**
3131
* Creates an HTTP cookie. All fields except for <code>name</code>, <code>secure</code>, <code>httpOnly</code>, and <code>value</code> can be set to null if unused.
3232
*
33-
* @deprecated Use {@link Builder} class instead. This method will be removed in the future.
33+
* @deprecated Use {@link Builder} class instead. This method will be removed in the future
3434
*
3535
* @param name name of the cookie
3636
* @param value value of the cookie
@@ -67,7 +67,7 @@ public SimpleHttpCookie(final String name, final String value, final String doma
6767
/**
6868
* Converts the cookie to a readable string for the cookie header.
6969
*
70-
* @deprecated Use {@link #toCookieHeaderString()} instead.
70+
* @deprecated Use {@link #toCookieHeaderString()} instead
7171
*
7272
* @return cookie header
7373
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
320320
*
321321
* @param key name of cookie to set
322322
* @param value value of cookie
323+
* @throws IllegalArgumentException if the cookie name is reserved by the server
323324
*
324325
* @see SimpleHttpCookie
325326
* @see #setCookie(SimpleHttpCookie)
@@ -334,6 +335,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
334335
* Sets a cookie in the response header.
335336
*
336337
* @param cookie cookie to set
338+
* @throws IllegalArgumentException if the cookie name is reserved by the server
337339
*
338340
* @see SimpleHttpCookie
339341
* @see #setCookie(String, String)

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ public synchronized final void setCookie(final String key, final String value){
316316

317317
@Override
318318
public synchronized final void setCookie(final SimpleHttpCookie cookie){
319-
getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString());
319+
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");
322+
getResponseHeaders().add("Set-Cookie",cstring);
320323
}
321324

322325
//
@@ -325,11 +328,15 @@ public synchronized final void setCookie(final SimpleHttpCookie cookie){
325328
public synchronized final HttpSession getHttpSession(){
326329
final String sessionId;
327330
final HttpSession session;
331+
328332
if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){
329333
session = HttpSession.create();
330-
setCookie(
331-
new SimpleHttpCookie.Builder("__session-id",session.getSessionID()).build()
332-
);
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
333340
}else{
334341
session = HttpSession.sessions.get(sessionId);
335342
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
* @see HttpHandler
1212
* @see SimpleHttpExchange
1313
* @since 02.00.00
14-
* @version 02.00.00
14+
* @version 03.03.00
1515
* @author Ktt Development
1616
*/
17-
public abstract class SimpleHttpHandler implements HttpHandler, SimpleHttpExchangeAuthenticator {
18-
17+
public interface SimpleHttpHandler {
18+
/*
1919
/**
2020
* Encapsulates the {@link #handle(SimpleHttpExchange)} for the authenticator. Applications do not use this.
2121
*
@@ -24,16 +24,16 @@ public abstract class SimpleHttpHandler implements HttpHandler, SimpleHttpExchan
2424
*
2525
* @since 02.00.00
2626
* @author Ktt Development
27-
*/
27+
/
2828
@Override
29-
public final void handle(final HttpExchange exchange) throws IOException{
29+
default final void handle(final HttpExchange exchange) throws IOException{
3030
final SimpleHttpExchange sxe = SimpleHttpExchange.create(exchange);
3131
if(authenticate(sxe))
3232
handle(sxe);
3333
else
3434
sxe.close();
3535
}
36-
36+
*/
3737
/**
3838
* Handlers the given request and generates a response <b>if no exceptions occur</b>.
3939
*
@@ -43,6 +43,6 @@ public final void handle(final HttpExchange exchange) throws IOException{
4343
* @since 02.00.00
4444
* @author Ktt Development
4545
*/
46-
public abstract void handle(final SimpleHttpExchange exchange) throws IOException;
46+
void handle(final SimpleHttpExchange exchange) throws IOException;
4747

4848
}

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

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.kttdevelopment.simplehttpserver;
22

3-
import com.sun.net.httpserver.HttpHandler;
4-
import com.sun.net.httpserver.HttpServer;
5-
import com.sun.net.httpserver.HttpContext;
3+
import com.sun.net.httpserver.*;
64

75
import java.io.IOException;
86
import java.net.InetSocketAddress;
@@ -223,6 +221,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
223221
*
224222
* @see HttpContext
225223
* @see #createContext(String, HttpHandler)
224+
* @see #createContext(String, SimpleHttpHandler)
226225
* @see #removeContext(String)
227226
* @see #removeContext(HttpContext)
228227
* @since 02.00.00
@@ -242,13 +241,100 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
242241
* @see HttpContext
243242
* @see HttpHandler
244243
* @see #createContext(String)
244+
* @see #createContext(String, SimpleHttpHandler)
245245
* @see #removeContext(String)
246246
* @see #removeContext(HttpContext)
247247
* @since 02.00.00
248248
* @author Ktt Development
249249
*/
250250
public abstract HttpContext createContext(final String context, final HttpHandler handler);
251251

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+
272+
//
273+
274+
/**
275+
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
276+
*
277+
* @param context the context
278+
* @param authenticator authenticator
279+
* @return the http context associated with the context
280+
* @throws IllegalArgumentException if the context is invalid or taken
281+
* @throws NullPointerException if the context is null
282+
*
283+
* @see HttpContext
284+
* @see Authenticator
285+
* @see #createContext(String, HttpHandler, Authenticator)
286+
* @see #createContext(String, SimpleHttpHandler, Authenticator)
287+
* @see #removeContext(String)
288+
* @see #removeContext(HttpContext)
289+
* @since 03.03.00
290+
* @author Ktt Development
291+
*/
292+
public abstract HttpContext createContext(final String context, final Authenticator authenticator);
293+
294+
/**
295+
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
296+
*
297+
* @param context the context
298+
* @param handler the handler
299+
* @param authenticator authenticator
300+
* @return the http context associated with the context
301+
* @throws IllegalArgumentException if the context is invalid or taken
302+
* @throws NullPointerException if the context is null
303+
*
304+
* @see HttpContext
305+
* @see HttpHandler
306+
* @see Authenticator
307+
* @see #createContext(String, Authenticator)
308+
* @see #createContext(String, SimpleHttpHandler, Authenticator)
309+
* @see #removeContext(String)
310+
* @see #removeContext(HttpContext)
311+
* @since 03.03.00
312+
* @author Ktt Development
313+
*/
314+
public abstract HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator);
315+
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+
252338
//
253339

254340
/**

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,40 @@ public synchronized final HttpContext createContext(final String path, final Htt
116116
return context;
117117
}
118118

119-
//
119+
@Override
120+
public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler){
121+
if(!getContext(path).equals("/") && handler instanceof RootHandler)
122+
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)));
124+
contexts.put(context,context.getHandler());
125+
return context;
126+
}
127+
128+
//
129+
130+
@Override
131+
public synchronized final HttpContext createContext(final String path, final Authenticator authenticator){
132+
final HttpContext context = createContext(path);
133+
context.setAuthenticator(authenticator);
134+
return context;
135+
}
136+
137+
@Override
138+
public synchronized final HttpContext createContext(final String path, final HttpHandler handler, final Authenticator authenticator){
139+
final HttpContext context = createContext(path,handler);
140+
context.setAuthenticator(authenticator);
141+
return context;
142+
}
143+
144+
@Override
145+
public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler, final Authenticator authenticator){
146+
final HttpContext context = createContext(path,handler);
147+
context.setAuthenticator(authenticator);
148+
return context;
149+
}
150+
151+
152+
//
120153

121154
private String generateRandomContext(){
122155
String targetContext;
@@ -151,12 +184,12 @@ public synchronized final HttpContext createTemporaryContext(final HttpHandler h
151184

152185
@Override
153186
public synchronized final HttpContext createTemporaryContext(final String context){
154-
return createContext(context, (exchange) -> removeContext(context));
187+
return createContext(context, (HttpExchange exchange) -> removeContext(context));
155188
}
156189

157190
@Override
158191
public synchronized final HttpContext createTemporaryContext(final String context, final long maxTime){
159-
final HttpContext httpContext = createContext(context, (exchange) -> removeContext(context));
192+
final HttpContext httpContext = createContext(context, (HttpExchange exchange) -> removeContext(context));
160193

161194
new Thread(() -> {
162195
try{
@@ -170,15 +203,15 @@ public synchronized final HttpContext createTemporaryContext(final String contex
170203

171204
@Override
172205
public synchronized final HttpContext createTemporaryContext(final String context, final HttpHandler handler){
173-
return createContext(context, (exchange) -> {
206+
return createContext(context, (HttpExchange exchange) -> {
174207
handler.handle(exchange);
175208
removeContext(context);
176209
});
177210
}
178211

179212
@Override
180213
public synchronized final HttpContext createTemporaryContext(final String context, final HttpHandler handler, final long maxTime){
181-
final HttpContext httpContext = createContext(context, (exchange) -> {
214+
final HttpContext httpContext = createContext(context, (HttpExchange exchange) -> {
182215
handler.handle(exchange);
183216
removeContext(context);
184217
});

src/main/java/com/kttdevelopment/simplehttpserver/handler/FileHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @version 02.00.00
2525
* @author Ktt Development
2626
*/
27-
public class FileHandler extends SimpleHttpHandler {
27+
public class FileHandler implements SimpleHttpHandler {
2828

2929
private final FileHandlerAdapter adapter;
3030

src/main/java/com/kttdevelopment/simplehttpserver/handler/RedirectHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @version 02.00.00
1616
* @author Ktt Development
1717
*/
18-
public class RedirectHandler extends SimpleHttpHandler {
18+
public class RedirectHandler implements SimpleHttpHandler {
1919

2020
private final String link;
2121

0 commit comments

Comments
 (0)