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

SimpleHttpHandler now an interface; Authenticator added to #createContext #34

Merged
merged 7 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class HttpSession {
* @return a {@link HttpSession}
*
* @since 02.00.00
* @author KTt Development
* @author Ktt Development
*/
synchronized static HttpSession create(){
return HttpSessionImpl.createHttpSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SimpleHttpCookie {
/**
* 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.
*
* @deprecated Use {@link Builder} class instead. This method will be removed in the future.
* @deprecated Use {@link Builder} class instead. This method will be removed in the future
*
* @param name name of the cookie
* @param value value of the cookie
Expand Down Expand Up @@ -67,7 +67,7 @@ public SimpleHttpCookie(final String name, final String value, final String doma
/**
* Converts the cookie to a readable string for the cookie header.
*
* @deprecated Use {@link #toCookieHeaderString()} instead.
* @deprecated Use {@link #toCookieHeaderString()} instead
*
* @return cookie header
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
*
* @param key name of cookie to set
* @param value value of cookie
* @throws IllegalArgumentException if the cookie name is reserved by the server
*
* @see SimpleHttpCookie
* @see #setCookie(SimpleHttpCookie)
Expand All @@ -334,6 +335,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* Sets a cookie in the response header.
*
* @param cookie cookie to set
* @throws IllegalArgumentException if the cookie name is reserved by the server
*
* @see SimpleHttpCookie
* @see #setCookie(String, String)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ public synchronized final void setCookie(final String key, final String value){

@Override
public synchronized final void setCookie(final SimpleHttpCookie cookie){
getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString());
final String cstring = cookie.toCookieHeaderString();
if(cstring.startsWith("__session-id="))
throw new IllegalArgumentException("The cookie '__session-id' can not be set because it is reserved by the server");
getResponseHeaders().add("Set-Cookie",cstring);
}

//
Expand All @@ -325,11 +328,15 @@ public synchronized final void setCookie(final SimpleHttpCookie cookie){
public synchronized final HttpSession getHttpSession(){
final String sessionId;
final HttpSession session;

if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){
session = HttpSession.create();
setCookie(
new SimpleHttpCookie.Builder("__session-id",session.getSessionID()).build()
);
final SimpleHttpCookie cookie =
new SimpleHttpCookie.Builder("__session-id",session.getSessionID())
.setPath("/")
.setHttpOnly(true)
.build();
getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); // bypass implementation
}else{
session = HttpSession.sessions.get(sessionId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* @see HttpHandler
* @see SimpleHttpExchange
* @since 02.00.00
* @version 02.00.00
* @version 03.03.00
* @author Ktt Development
*/
public abstract class SimpleHttpHandler implements HttpHandler, SimpleHttpExchangeAuthenticator {

public interface SimpleHttpHandler {
/*
/**
* Encapsulates the {@link #handle(SimpleHttpExchange)} for the authenticator. Applications do not use this.
*
Expand All @@ -24,16 +24,16 @@ public abstract class SimpleHttpHandler implements HttpHandler, SimpleHttpExchan
*
* @since 02.00.00
* @author Ktt Development
*/
/
@Override
public final void handle(final HttpExchange exchange) throws IOException{
default final void handle(final HttpExchange exchange) throws IOException{
final SimpleHttpExchange sxe = SimpleHttpExchange.create(exchange);
if(authenticate(sxe))
handle(sxe);
else
sxe.close();
}

*/
/**
* Handlers the given request and generates a response <b>if no exceptions occur</b>.
*
Expand All @@ -43,6 +43,6 @@ public final void handle(final HttpExchange exchange) throws IOException{
* @since 02.00.00
* @author Ktt Development
*/
public abstract void handle(final SimpleHttpExchange exchange) throws IOException;
void handle(final SimpleHttpExchange exchange) throws IOException;

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.kttdevelopment.simplehttpserver;

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.*;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -223,6 +221,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
*
* @see HttpContext
* @see #createContext(String, HttpHandler)
* @see #createContext(String, SimpleHttpHandler)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
Expand All @@ -242,13 +241,100 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
* @see HttpContext
* @see HttpHandler
* @see #createContext(String)
* @see #createContext(String, SimpleHttpHandler)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 02.00.00
* @author Ktt Development
*/
public abstract HttpContext createContext(final String context, final HttpHandler handler);

/**
* Creates a context mapped to a specific {@link HttpHandler}.
*
* @param context the context
* @param handler the handler
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see SimpleHttpHandler
* @see #createContext(String)
* @see #createContext(String, HttpHandler)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 03.03.00
* @author Ktt Development
*/
public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler);

//

/**
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
*
* @param context the context
* @param authenticator authenticator
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see Authenticator
* @see #createContext(String, HttpHandler, Authenticator)
* @see #createContext(String, SimpleHttpHandler, Authenticator)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 03.03.00
* @author Ktt Development
*/
public abstract HttpContext createContext(final String context, final Authenticator authenticator);

/**
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
*
* @param context the context
* @param handler the handler
* @param authenticator authenticator
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see HttpHandler
* @see Authenticator
* @see #createContext(String, Authenticator)
* @see #createContext(String, SimpleHttpHandler, Authenticator)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 03.03.00
* @author Ktt Development
*/
public abstract HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator);

/**
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
*
* @param context the context
* @param handler the handler
* @param authenticator authenticator
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws NullPointerException if the context is null
*
* @see HttpContext
* @see SimpleHttpHandler
* @see Authenticator
* @see #createContext(String, Authenticator)
* @see #createContext(String, HttpHandler, Authenticator)
* @see #removeContext(String)
* @see #removeContext(HttpContext)
* @since 03.03.00
* @author Ktt Development
*/
public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler, final Authenticator authenticator);

//

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,40 @@ public synchronized final HttpContext createContext(final String path, final Htt
return context;
}

//
@Override
public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler){
if(!getContext(path).equals("/") && handler instanceof RootHandler)
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
final HttpContext context = server.createContext(getContext(path),(exchange) -> handler.handle(SimpleHttpExchange.create(exchange)));
contexts.put(context,context.getHandler());
return context;
}

//

@Override
public synchronized final HttpContext createContext(final String path, final Authenticator authenticator){
final HttpContext context = createContext(path);
context.setAuthenticator(authenticator);
return context;
}

@Override
public synchronized final HttpContext createContext(final String path, final HttpHandler handler, final Authenticator authenticator){
final HttpContext context = createContext(path,handler);
context.setAuthenticator(authenticator);
return context;
}

@Override
public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler, final Authenticator authenticator){
final HttpContext context = createContext(path,handler);
context.setAuthenticator(authenticator);
return context;
}


//

private String generateRandomContext(){
String targetContext;
Expand Down Expand Up @@ -151,12 +184,12 @@ public synchronized final HttpContext createTemporaryContext(final HttpHandler h

@Override
public synchronized final HttpContext createTemporaryContext(final String context){
return createContext(context, (exchange) -> removeContext(context));
return createContext(context, (HttpExchange exchange) -> removeContext(context));
}

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

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

@Override
public synchronized final HttpContext createTemporaryContext(final String context, final HttpHandler handler){
return createContext(context, (exchange) -> {
return createContext(context, (HttpExchange exchange) -> {
handler.handle(exchange);
removeContext(context);
});
}

@Override
public synchronized final HttpContext createTemporaryContext(final String context, final HttpHandler handler, final long maxTime){
final HttpContext httpContext = createContext(context, (exchange) -> {
final HttpContext httpContext = createContext(context, (HttpExchange exchange) -> {
handler.handle(exchange);
removeContext(context);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @version 02.00.00
* @author Ktt Development
*/
public class FileHandler extends SimpleHttpHandler {
public class FileHandler implements SimpleHttpHandler {

private final FileHandlerAdapter adapter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @version 02.00.00
* @author Ktt Development
*/
public class RedirectHandler extends SimpleHttpHandler {
public class RedirectHandler implements SimpleHttpHandler {

private final String link;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class SSEHandler extends SimpleHttpHandler {
public class SSEHandler implements SimpleHttpHandler {

private final List<OutputStream> listeners = new ArrayList<>();
private final AtomicInteger eventId = new AtomicInteger(-1);
Expand Down