package art.servers.controller;
|
|
|
import art.library.interop.InteropParameters;
|
import art.library.utils.licence.Licence;
|
import art.library.utils.resources.Resources;
|
import art.servers.Shared;
|
import art.servers.configuration.ConfigurationListenerHttp;
|
import art.servers.configuration.ConfigurationSecurity;
|
import art.servers.types.HttpAuthentication;
|
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpHandler;
|
import java.io.File;
|
import java.io.IOException;
|
import java.nio.file.Files;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
|
public class ControllerListenerWEB extends ControllerListenerHttpsWeb
|
{
|
private List<HttpAuthentication> autentications = new ArrayList<HttpAuthentication>();
|
|
|
public ControllerListenerWEB(ConfigurationListenerHttp configuration, ConfigurationSecurity configurationSecutiry)
|
{
|
super(configuration, configurationSecutiry);
|
}
|
|
|
|
public void contexts()
|
{
|
super.contexts();
|
server.createContext("/", new entry());
|
server.createContext("/connect", new connect());
|
server.createContext("/logout", new disconnect());
|
}
|
|
|
|
public class entry implements HttpHandler
|
{
|
public void handle(HttpExchange httpExchange) throws IOException
|
{
|
String language = null;
|
|
try
|
{
|
InteropParameters parameters = new InteropParameters(httpExchange.getRequestURI().getRawQuery());
|
language = (parameters.hasParameter("language") == true) ? (String)parameters.getParameterValue("language") : "";
|
|
byte[] data = null;
|
|
File file = new File("data/" + Shared.getApplicationName() + "/html/login.html");
|
|
if (file.exists() == true)
|
{
|
data = Files.readAllBytes(file.toPath());
|
}
|
else
|
{
|
data = Resources.getResourceBytes("data/" + Shared.getApplicationName() + "/html/login.html");
|
}
|
|
result(httpExchange, 200, data);
|
}
|
catch (Exception exception)
|
{
|
result(httpExchange, 400, language, exception);
|
}
|
}
|
}
|
|
|
|
protected class connect implements HttpHandler
|
{
|
public void handle(HttpExchange httpExchange) throws IOException
|
{
|
String language = null;
|
|
try
|
{
|
InteropParameters parameters = new InteropParameters(httpExchange.getRequestURI().getRawQuery());
|
language = (parameters.hasParameter("language") == true) ? (String)parameters.getParameterValue("language") : "";
|
String username = (String)parameters.getParameterValue("username");
|
String password = (String)parameters.getParameterValue("password");
|
|
HttpAuthentication authentication = new HttpAuthentication(username, password, httpExchange.getRemoteAddress().getHostName());
|
authentication.userAgent = httpExchange.getRequestHeaders().getFirst("User-Agent");
|
|
if ((username.equals("read")) && (password.equals(Licence.decrypt(security.readPassword))))
|
{
|
authentication.profileWebServer = HttpAuthentication.PROFILE_READ;
|
}
|
else if ((username.equals("write")) && (password.equals(Licence.decrypt(security.writePassword))))
|
{
|
authentication.profileWebServer = HttpAuthentication.PROFILE_WRITE;
|
}
|
else if ((username.equals("admin")) && (password.equals(Licence.decrypt(security.adminPassword))))
|
{
|
authentication.profileWebServer = HttpAuthentication.PROFILE_ADMIN;
|
}
|
else if ((username.equals("download")) && (password.equals(Licence.decrypt(security.downloadPassword))))
|
{
|
authentication.profileWebServer = HttpAuthentication.PROFILE_DOWNLOAD;
|
}
|
else
|
{
|
throw new Exception("Autentication error");
|
}
|
|
addAuthentication(authentication);
|
result(httpExchange, 200, new byte[0]);
|
}
|
catch (Exception exception)
|
{
|
result(httpExchange, 400, Shared.getMessage(language, "Autentication error"));
|
}
|
}
|
}
|
|
|
|
|
|
|
protected class disconnect implements HttpHandler
|
{
|
public void handle(HttpExchange httpExchange) throws IOException
|
{
|
String language = null;
|
|
try
|
{
|
InteropParameters parameters = new InteropParameters(httpExchange.getRequestURI().getRawQuery());
|
language = (parameters.hasParameter("language") == true) ? (String)parameters.getParameterValue("language") : "";
|
authenticationRemove(httpExchange);
|
byte[] data = null;
|
|
File file = new File("data/" + Shared.getApplicationName() + "/html/login.html");
|
|
if (file.exists() == true)
|
{
|
data = Files.readAllBytes(file.toPath());
|
}
|
else
|
{
|
data = Resources.getResourceBytes("data/" + Shared.getApplicationName() + "/html/login.html");
|
}
|
|
result(httpExchange, 200, data);
|
}
|
catch (Exception exception)
|
{
|
result(httpExchange, 400, language, exception);
|
}
|
}
|
}
|
|
|
|
public HttpAuthentication getAuthentication(HttpExchange httpExchange)
|
{
|
String address = httpExchange.getRemoteAddress().getHostName();
|
String userAgent = httpExchange.getRequestHeaders().getFirst("User-Agent");
|
|
|
for (HttpAuthentication current : autentications)
|
{
|
if ((current.userAgent.equals(userAgent)) && (current.address.equals(address)))
|
{
|
return current;
|
}
|
}
|
|
return null;
|
}
|
|
|
private void addAuthentication(HttpAuthentication authentication)
|
{
|
for (HttpAuthentication current : autentications)
|
{
|
if ((current.userAgent.equals(authentication.userAgent)) && (current.address.equals(authentication.address)))
|
{
|
autentications.remove(current);
|
}
|
}
|
autentications.add(authentication);
|
}
|
|
|
|
|
|
public boolean authenticationValid(HttpExchange httpExchange)
|
{
|
String address = httpExchange.getRemoteAddress().getHostName();
|
String userAgent = httpExchange.getRequestHeaders().getFirst("User-Agent");
|
|
|
for (HttpAuthentication current : autentications)
|
{
|
if ((current.userAgent.equals(userAgent)) && (current.address.equals(address)))
|
{
|
return true;
|
}
|
}
|
|
return false;
|
}
|
|
|
|
public boolean authenticationExpired(HttpExchange httpExchange)
|
{
|
String address = httpExchange.getRemoteAddress().getHostName();
|
String userAgent = httpExchange.getRequestHeaders().getFirst("User-Agent");
|
|
|
for (HttpAuthentication current : autentications)
|
{
|
if ((current.userAgent.equals(userAgent)) && (current.address.equals(address)))
|
{
|
// TODO
|
}
|
}
|
|
return false;
|
}
|
|
|
|
|
public void authenticationRemove(HttpExchange httpExchange)
|
{
|
String address = httpExchange.getRemoteAddress().getHostName();
|
String userAgent = httpExchange.getRequestHeaders().getFirst("User-Agent");
|
|
List<HttpAuthentication> removes = new ArrayList<HttpAuthentication>();
|
|
for (HttpAuthentication current : autentications)
|
{
|
if ((current.userAgent.equals(userAgent)) && (current.address.equals(address)))
|
{
|
removes.add(current);
|
}
|
}
|
|
for (HttpAuthentication remove : removes)
|
{
|
autentications.remove(remove);
|
}
|
}
|
|
|
}
|