Ktt-Development/simplehttpserver

#createContext optimization

Katsute opened this issue · 0 comments

Prerequisites

If all checks are not passed then the request will be closed

  • I have checked that no other similar feature request already exists
  • The feature request makes sense for the project

Proposal

Describe the feature that should be added
Change create context so they all call the last method, and only add authenticator if not null

Reasoning

Explain why this feature should be added
Code cleanup

@Override
public synchronized final HttpContext createContext(final String path){
return createContext(path,(HttpExchange exchange) -> {});
}
@Override
public synchronized final HttpContext createContext(final String path, final HttpHandler handler){
if(!getContext(path).equals("/") && handler instanceof RootHandler)
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
final HttpHandler wrapper = exchange -> {
handle(exchange);
handler.handle(exchange);
};
final HttpContext context = server.createContext(getContext(path),wrapper);
contexts.put(context,handler);
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;
}