diff --git a/portfolio/pom.xml b/portfolio/pom.xml index cef3db2..9022c5c 100644 --- a/portfolio/pom.xml +++ b/portfolio/pom.xml @@ -33,6 +33,11 @@ appengine-api-1.0-sdk 1.9.59 + + org.javatuples + javatuples + 1.2 + com.opencsv opencsv diff --git a/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java b/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java index fd7b30e..8a4666b 100644 --- a/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java +++ b/portfolio/src/main/java/com/google/sps/servlets/AuthenticationServlet.java @@ -27,16 +27,21 @@ public class AuthenticationServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { - response.setContentType("text/html"); + response.setContentType("application/json"); UserService userService = UserServiceFactory.getUserService(); String redirectUrl = "/index.html"; if (userService.isUserLoggedIn()) { + String userEmail = userService.getCurrentUser().getEmail(); String logoutUrl = userService.createLogoutURL(redirectUrl); - response.getWriter().println("

Logout here."); - } else { - String loginUrl = userService.createLoginURL(redirectUrl); - response.getWriter().println("

Login here."); + UserInfo userInfo = new UserInfo(); + userInfo.loggedIn(userEmail, logoutUrl); + JsonUtil.sendJson(response, userInfo); + return; } + String loginUrl = userService.createLoginURL(redirectUrl); + UserInfo userInfo = new UserInfo(); + userInfo.loggedOut(loginUrl); + JsonUtil.sendJson(response, userInfo); } - + } diff --git a/portfolio/src/main/java/com/google/sps/servlets/JsonUtil.java b/portfolio/src/main/java/com/google/sps/servlets/JsonUtil.java new file mode 100644 index 0000000..d79947a --- /dev/null +++ b/portfolio/src/main/java/com/google/sps/servlets/JsonUtil.java @@ -0,0 +1,29 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.sps.servlets; + +import com.google.gson.Gson; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + +public final class JsonUtil { + + public static void sendJson(HttpServletResponse response, Object object) throws IOException { + Gson gson = new Gson(); + String json = gson.toJson(object); + response.getWriter().println(json); + } + +} diff --git a/portfolio/src/main/java/com/google/sps/servlets/ListCommentsServlet.java b/portfolio/src/main/java/com/google/sps/servlets/ListCommentsServlet.java index 01f4369..242b0b2 100644 --- a/portfolio/src/main/java/com/google/sps/servlets/ListCommentsServlet.java +++ b/portfolio/src/main/java/com/google/sps/servlets/ListCommentsServlet.java @@ -18,12 +18,11 @@ import com.google.appengine.api.datastore.DatastoreServiceFactory; import com.google.appengine.api.datastore.Entity; import com.google.appengine.api.datastore.PreparedQuery; -import com.google.appengine.api.datastore.Query; import com.google.appengine.api.datastore.Query.SortDirection; -import com.google.gson.Gson; +import com.google.appengine.api.datastore.Query; import java.io.IOException; -import java.util.Iterator; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -39,7 +38,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro PreparedQuery results = prepareQuery(); int max = getMax(request); List comments = getCommentsToDisplay(results, max); - sendJson(response, comments); + JsonUtil.sendJson(response, comments); } private PreparedQuery prepareQuery() { @@ -60,16 +59,11 @@ private List getCommentsToDisplay(PreparedQuery results, int max) { Iterator iterator = results.asIterator(); for (int i = 0; (i < max) && (iterator.hasNext()); ++i) { Entity entity = iterator.next(); + String email = (String) entity.getProperty("email"); String comment = (String) entity.getProperty("comment"); - comments.add(comment); + comments.add(email + ": " + comment); } return comments; } - - private void sendJson(HttpServletResponse response, List comments) throws IOException { - Gson gson = new Gson(); - response.setContentType("application/json;"); - response.getWriter().println(gson.toJson(comments)); - } } diff --git a/portfolio/src/main/java/com/google/sps/servlets/NewCommentServlet.java b/portfolio/src/main/java/com/google/sps/servlets/NewCommentServlet.java index 7b6ae14..ab6836c 100644 --- a/portfolio/src/main/java/com/google/sps/servlets/NewCommentServlet.java +++ b/portfolio/src/main/java/com/google/sps/servlets/NewCommentServlet.java @@ -17,6 +17,8 @@ import com.google.appengine.api.datastore.DatastoreService; import com.google.appengine.api.datastore.DatastoreServiceFactory; import com.google.appengine.api.datastore.Entity; +import com.google.appengine.api.users.UserService; +import com.google.appengine.api.users.UserServiceFactory; import java.io.IOException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -29,17 +31,20 @@ public class NewCommentServlet extends HttpServlet { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { + UserService userService = UserServiceFactory.getUserService(); + String email = userService.getCurrentUser().getEmail(); String comment = request.getParameter("comment"); long timestamp = System.currentTimeMillis(); - Entity commentEntity = createEntity(comment, timestamp); + Entity commentEntity = createEntity(comment, timestamp, email); putEntity(commentEntity); response.sendRedirect("/index.html"); } - private Entity createEntity(String comment, long timestamp) { + private Entity createEntity(String comment, long timestamp, String email) { Entity entity = new Entity("Comment"); entity.setProperty("comment", comment); entity.setProperty("timestamp", timestamp); + entity.setProperty("email", email); return entity; } diff --git a/portfolio/src/main/java/com/google/sps/servlets/UserInfo.java b/portfolio/src/main/java/com/google/sps/servlets/UserInfo.java new file mode 100644 index 0000000..d37ac0e --- /dev/null +++ b/portfolio/src/main/java/com/google/sps/servlets/UserInfo.java @@ -0,0 +1,75 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.sps.servlets; + +/** + * Stores user information for authentication purposes. + */ +public class UserInfo { + + private String email; + private String loginUrl; + private String logoutUrl; + private boolean loggedIn; + + /** + * Store relevant logout information when user is logged in + * @param email + * @param logoutUrl + */ + public void loggedIn(String email, String logoutUrl) { + this.email = email; + this.logoutUrl = logoutUrl; + this.loggedIn = true; + } + + /** + * Store relevant login information when user is logged out + * @param loginUrl + */ + public void loggedOut(String loginUrl) { + this.loginUrl = loginUrl; + this.loggedIn = false; + } + + /** + * @return user email + */ + public String getEmail(){ + return email; + } + + /** + * @return url to log user in + */ + public String getLoginUrl(){ + return loginUrl; + } + + /** + * @return url to log user out + */ + public String getLogoutUrl(){ + return logoutUrl; + } + + /** + * @return user login status + */ + public boolean isLoggedIn() { + return loggedIn; + } + +} diff --git a/portfolio/src/main/webapp/libraries.js b/portfolio/src/main/webapp/libraries.js index 09627ca..80a48d2 100644 --- a/portfolio/src/main/webapp/libraries.js +++ b/portfolio/src/main/webapp/libraries.js @@ -146,9 +146,25 @@ function drawCoronavirusChart() { async function getLoginStatus() { const response = await fetch('/authentication'); - const responseHtml = await response.text(); - document.getElementById('login-status').innerHTML = responseHtml; - if (responseHtml.includes('Logout')) { - document.getElementById('comments-form').style.display = 'block'; + const json = await response.json(); + const loggedIn = json["loggedIn"]; + if (!loggedIn) { + const redirectUrl = json["loginUrl"]; + const html = loginHtml(redirectUrl); + document.getElementById('login-status').innerHTML = html; + return; } + const userEmail = json["email"]; + const redirectUrl = json["logoutUrl"]; + const html = logoutHtml(userEmail, redirectUrl); + document.getElementById('login-status').innerHTML = html; + document.getElementById('comments-form').style.display = "block"; +} + +function loginHtml(loginUrl) { + return `

Login here.`; +} + +function logoutHtml(userEmail, logoutUrl) { + return `

Hi ${userEmail}! Logout here.`; }