Commit 9f8a7d92 authored by Magne.Stenseng's avatar Magne.Stenseng
Browse files

Replace Handler.java

parent b4038162
......@@ -29,6 +29,7 @@ import inf226.storage.*;
import inf226.inchat.*;
import inf226.util.*;
/**
* The Hanlder class handles all HTTP and HTML components.
* Functions called display⋯ and print⋯ output HTML.
......@@ -43,6 +44,8 @@ public class Handler extends AbstractHandler
private final File landingpage = new File("index.html");
private final File script = new File("script.js");
private static InChat inchat;
private final DateTimeFormatter formatter =
......@@ -58,51 +61,44 @@ public class Handler extends AbstractHandler
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
System.err.println("Got a request for \"" + target + "\"");
final Map<String,Cookie> cookies = getCookies(request);
// Pages which do not require login
if (target.equals("/style.css")) {
HttpServletResponse response) throws IOException, ServletException {
System.err.println("Got a request for \"" + target + "\"");
final Map<String,Cookie> cookies = getCookies(request);
// Pages which do not require login
if (target.equals("/style.css")) {
serveFile(response,style,"text/css;charset=utf-8");
baseRequest.setHandled(true);
return;
} else if (target.equals("/login")) {
} else if (target.equals("/login")) {
serveFile(response,login, "text/html;charset=utf-8");
baseRequest.setHandled(true);
return;
} else if (target.equals("/register")) {
serveFile(response,register, "text/html;charset=utf-8");
baseRequest.setHandled(true);
return;
}else if (target.equals("/script.js")) {
serveFile(response,script, "application/javascript");
baseRequest.setHandled(true);
return;
}
} else if (target.equals("/register")) {
serveFile(response,register, "text/html;charset=utf-8");
baseRequest.setHandled(true);
return;
}else if (target.equals("/script.js")) {
serveFile(response,script, "application/javascript");
baseRequest.setHandled(true);
return;
}
// Attempt to create a session
Maybe.Builder<Stored<Session>> sessionBuilder
= new Maybe.Builder<Stored<Session>>();
Maybe.Builder<Stored<Session>> sessionBuilder = new Maybe.Builder<Stored<Session>>();
if(request.getParameter("register") != null) {
// Try to register a new user:
System.err.println("User registration.");
try {
String username = (new Maybe<String>
(request.getParameter("username"))).get();
String password = (new Maybe<String>
(request.getParameter("password"))).get();
System.err.println("Registering user: \"" + username
+ "\" with password \"" + password + "\"");
inchat.register(username,password).forEach(sessionBuilder);
String username = (new Maybe<String>(request.getParameter("username"))).get();
String password1 = (new Maybe<String>(request.getParameter("password"))).get();
Password password = new Password(password1);
System.err.println("Registering user: \"" + username + "\" with password \"" + password + "\"");
inchat.register(username,password.getPassword()).forEach(sessionBuilder);
} catch (Maybe.NothingException e) {
// Not enough data suppied for login
// Not enough data supplied for login
System.err.println("Broken usage of register");
}
} else if(request.getParameter("login") != null) {
......@@ -116,17 +112,15 @@ public class Handler extends AbstractHandler
(request.getParameter("password"))).get();
inchat.login(username,password).forEach(sessionBuilder);
} catch (Maybe.NothingException e) {
// Not enough data suppied for login
// Not enough data supplied for login
System.err.println("Broken usage of login");
}
} else {
// Final option is to restore a session from a cookie
final Maybe<Cookie> sessionCookie
= new Maybe<Cookie>(cookies.get("session"));
final Maybe<Stored<Session>> cookieSession
= sessionCookie.bind(c ->
inchat.restoreSession(UUID.fromString(c.getValue())));
final Maybe<Cookie> sessionCookie = new Maybe<Cookie>(cookies.get("session"));
final Maybe<Stored<Session>> cookieSession = sessionCookie.bind(c ->
inchat.restoreSession(UUID.fromString(c.getValue())));
cookieSession.forEach(sessionBuilder);
}
......@@ -135,10 +129,11 @@ public class Handler extends AbstractHandler
try {
final Stored<Session> session = sessionBuilder.getMaybe().get();
final Stored<Account> account = session.value.account;
// User is now logged in with a valid sesion.
// User is now logged in with a valid session.
// We set the session cookie to keep the user logged in:
response.addCookie(new Cookie("session",session.identity.toString()));
final PrintWriter out = response.getWriter();
// Handle a logged in request.
try {
......@@ -159,8 +154,7 @@ public class Handler extends AbstractHandler
}
if(request.getParameter("deletemessage") != null) {
UUID messageId =
UUID.fromString(Maybe.just(request.getParameter("message")).get());
UUID messageId = UUID.fromString(Maybe.just(request.getParameter("message")).get());
Stored<Channel.Event> message = inchat.getEvent(messageId).get();
channel = inchat.deleteEvent(channel, message);
}
......@@ -560,9 +554,9 @@ public class Handler extends AbstractHandler
= new AccountStorage(connection,userStore,channelStore);
SessionStorage sessionStore
= new SessionStorage(connection,accountStore);
inchat = new InChat(userStore,channelStore,
accountStore,sessionStore,connection);
inchat = new InChat(userStore,channelStore,accountStore,sessionStore,connection);
connection.setAutoCommit(false);
try {
final Stored<Session> admin = inchat.register("admin","pa$$w0rd").get();
final Stored<Channel> debug = inchat.createChannel(admin.value.account, "debug").get();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment