package se.chalmers.dat255.grupp12;
import static spark.Spark.*;
import com.google.gson.*;
import spark.*;
import java.io.IOException;
import java.sql.SQLException;
/**
* Created with IntelliJ IDEA.
* se.chalmers.dat255.grupp12.User: Hagej
* Date: 2013-09-12
* Time: 14:28
* To change this template use File | Settings | File Templates.
*/
public class Server {
private User user;
public static String lastResponse = "{}";
public static String lastRequest = "{}";
public Server() {
/**
* Authenticate user
* @param mail - e-mail address to verify
* @param token - oauth token from Google
*
* @return "Success" if authorization successful, "Fail" otherwise
*/
post(new Route("/auth") {
@Override
public Object handle(Request request, Response response) {
String mail = request.queryParams("mail");
String token = request.queryParams("token");
// Create a session unless one exists
Session login = request.session();
if (!login.isNew()) {
login.removeAttribute("user_id");
}
try {
if ((user = Authorization.authorize(mail, token)) != null) {
login = request.session();
login.attribute("user_id", user.getId());
System.out.println("Success! " + user.getMail());
return "Success";// + user.getMail();
} else {
System.out.println("Authorization failed: " + user.getMail());
return "Fail";//"Authorization failed, " + mail + " does not match token!";
}
} catch (IOException | SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
return e.getMessage();
}
}
});
before(new Filter() {
@Override
public void handle(Request request, Response response) {
Session session = request.session(false);
if (session != null && session.attribute("user_id") != null) {
try {
user = Database.getInstance().getUser((Integer) session.attribute("user_id"));
} catch (SQLException e) {
e.printStackTrace();
} catch (DataNotFoundException e) {
throw new IllegalStateException("User valid but not in db");
}
} else if (request.pathInfo().equals("/commit")) {
// If user is not trying to authenticate => stop!
halt(403);
}
}
});
get(new Route("/debug") {
@Override
public Object handle(Request request, Response response) {
return "{\"request\":" + lastRequest + ",\"response\":" + lastResponse + "}";
}
});
/**
* The commit route
* Everytime the client syncs with the server this route is used to
* modify the database by adding, removing and updating data on the database
* This is done through a commit controller that handles the incoming data
*
* @param modify - A String containing all the modifications done on the client
* @return - All the changes
*/
put(new JSONTransformer("/commit") {
@Override
public Object handle(Request request, Response response) {
String modification = request.queryParams("modify");
lastRequest = modification;
System.out.println("FROM " + user.getName().split(" ")[0] + ": " + modification);
CommitController commitController = new CommitController(user);
JsonArray element = new JsonParser().parse(modification).getAsJsonArray();
return commitController.getChanges(element);
}
});
// Filter to make sure that all response data is being sent with correct Content-Type and encoding.
after(new Filter() {
@Override
public void handle(Request request, Response response) {
response.header("Content-Type", "application/json; charset=utf-8");
}
});
}
}