servidor http simples groovy (java) com autenticação básica

Execute um servidor da web simples do groovy:

def handler = new ScriptHttpHandler()
def server = HttpServer.create(new InetSocketAddress(8080), 0)
HttpContext context = server.createContext("/", handler)
context
.setAuthenticator(new BasicAuthAuthenticator())
server
.start()

Solicitar manipulador:

class ScriptHttpHandler implements HttpHandler {

@Override
void handle(HttpExchange exchange) {
exchange
.responseHeaders.set("Content-Type", "text/plain")
exchange
.sendResponseHeaders(200, 0)
exchange
.responseBody.withStream { OutputStream stream ->
doExecute
(exchange.requestBody, stream)
}
}

protected void doExecute(InputStream input, OutputStream output) {
....
}
}

Autenticação:

class BasicAuthAuthenticator extends BasicAuthenticator {

RemoteControlAuthenticator() {
super("enter password")
}

@Override
boolean checkCredentials(String user, String pwd) {
return user.equals("user") && pwd.equals("password")
}
}