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

Commit 5007b80

Browse files
committed
Converted SimpleHttpHandler to interface
1 parent 2aea0cf commit 5007b80

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

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/SimpleHttpExchangeAuthenticator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
/**
44
* Before each request is processed, the authenticator determines whether to cancel the request or not. The authenticator may also choose to process the request ahead of the handler. By default it returns true.
55
*
6+
* @deprecated Use {@link com.sun.net.httpserver.Authenticator} instead
7+
*
68
* @see SimpleHttpExchange
79
* @since 01.00.00
810
* @version 02.00.00
911
*/
12+
@Deprecated
1013
public interface SimpleHttpExchangeAuthenticator {
1114

1215
/**

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
223223
*
224224
* @see HttpContext
225225
* @see #createContext(String, HttpHandler)
226+
* @see #createContext(String, SimpleHttpHandler)
226227
* @see #removeContext(String)
227228
* @see #removeContext(HttpContext)
228229
* @since 02.00.00
@@ -242,13 +243,34 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
242243
* @see HttpContext
243244
* @see HttpHandler
244245
* @see #createContext(String)
246+
* @see #createContext(String, SimpleHttpHandler)
245247
* @see #removeContext(String)
246248
* @see #removeContext(HttpContext)
247249
* @since 02.00.00
248250
* @author Ktt Development
249251
*/
250252
public abstract HttpContext createContext(final String context, final HttpHandler handler);
251253

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

254276
/**

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,17 @@ 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+
//
120130

121131
private String generateRandomContext(){
122132
String targetContext;

0 commit comments

Comments
 (0)