public void onRequest(final IHttpExchange exchange) throws IOException {
IHttpRequest request = exchange.getRequest();
String authorization = request.getHeader("Authorization");
if (authorization != null) {
String[] s = authorization.split(" ");
if (!s[0].equalsIgnoreCase("BASIC")) {
exchange.sendError(401);
}
String decoded = new String(Base64.decodeBase64(s[1].getBytes()));
String[] userPasswordPair = decoded.split(":");
String authtoken = authenticator.login(userPasswordPair[0], userPasswordPair[1]);
request.removeHeader("Authorization");
request.setHeader("X-Authentication", authtoken);
IHttpResponseHandler respHdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
exchange.send(response);
}
public void onException(IOException ioe) {
}
};
exchange.forward(exchange.getRequest(), respHdl);
}
String authentication = request.getHeader("Authentication");
if (authentication == null) {
HttpResponse resp = new HttpResponse(401);
resp.setHeader("WWW-Authenticate", "basic");
exchange.send(resp);