├── README.md
├── .gitignore
├── src
└── main
│ ├── webapp
│ ├── WEB-INF
│ │ ├── index.yaml
│ │ └── appengine-web.xml
│ ├── css
│ │ ├── main.css
│ │ └── user-page.css
│ ├── user-page.html
│ ├── index.html
│ ├── aboutus.html
│ └── js
│ │ ├── navigation-loader.js
│ │ └── user-page-loader.js
│ └── java
│ └── com
│ └── google
│ └── codeu
│ ├── servlets
│ ├── LogoutServlet.java
│ ├── LoginStatusServlet.java
│ ├── LoginServlet.java
│ └── MessageServlet.java
│ └── data
│ ├── Message.java
│ └── Datastore.java
├── CONTRIBUTING.md
├── pom.xml
└── LICENSE
/README.md:
--------------------------------------------------------------------------------
1 | # codeu-starter-project
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | .settings
3 | .classpath
4 | .project
5 | .idea
6 | *.iml
7 | .DS_Store
8 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/index.yaml:
--------------------------------------------------------------------------------
1 | # Copyright 2019 Google Inc.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | indexes:
16 | - kind: Message
17 | properties:
18 | - name: user
19 | - name: timestamp
20 | direction: desc
21 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/appengine-web.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 | YOUR_PROJECT_ID
20 | 1
21 | false
22 | true
23 | java8
24 |
25 |
--------------------------------------------------------------------------------
/src/main/webapp/css/main.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /* Prevent the page from becoming too wide. */
18 | body {
19 | max-width: 600px;
20 | }
21 |
22 | /* Remove bullet points from navigation bar. */
23 | nav ul {
24 | list-style-type: none;
25 | padding-left: 0;
26 | }
27 |
28 | /* Keep navigation menu on one line. */
29 | nav li {
30 | display: inline-block;
31 | margin-right: 10px
32 | }
33 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to Contribute
2 |
3 | We'd love to accept your patches and contributions to this project. There are
4 | just a few small guidelines you need to follow.
5 |
6 | ## Contributor License Agreement
7 |
8 | Contributions to this project must be accompanied by a Contributor License
9 | Agreement. You (or your employer) retain the copyright to your contribution;
10 | this simply gives us permission to use and redistribute your contributions as
11 | part of the project. Head over to to see
12 | your current agreements on file or to sign a new one.
13 |
14 | You generally only need to submit a CLA once, so if you've already submitted one
15 | (even if it was for a different project), you probably don't need to do it
16 | again.
17 |
18 | ## Code reviews
19 |
20 | All submissions, including submissions by project members, require review. We
21 | use GitHub pull requests for this purpose. Consult
22 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
23 | information on using pull requests.
24 |
25 | ## Community Guidelines
26 |
27 | This project follows
28 | [Google's Open Source Community Guidelines](https://opensource.google.com/conduct/).
--------------------------------------------------------------------------------
/src/main/webapp/css/user-page.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /* Set the size of the message input to be 100 pixels high
18 | and will fill the width of its parent element. */
19 | #message-input {
20 | height: 100px;
21 | width: 100%;
22 | }
23 |
24 | /* Give each message a rounded border. */
25 | .message-div {
26 | border: thin solid black;
27 | border-radius: 5px;
28 | margin-top: 20px;
29 | }
30 |
31 | /* Add a bottom border to the message header. */
32 | .message-header {
33 | border-bottom: thin solid black;
34 | padding: 5px;
35 | }
36 |
37 | /* Give the message body a margin so the text
38 | isn't too close to the border. */
39 | .message-body {
40 | margin: 5px;
41 | }
42 |
43 | /* Use this to hide certain elements like forms if the
44 | user is not logged in or viewing their own page. */
45 | .hidden {
46 | display: none;
47 | }
--------------------------------------------------------------------------------
/src/main/webapp/user-page.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
This is the CodeU starter project. Click the links above to login and visit your page.
34 | You can post messages on your page, and you can visit other user pages if you have
35 | their URL.
36 |
This is your code now! Feel free to make changes, and don't be afraid to get creative!
37 | You could start by modifying this page to tell the world more about your team.
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/main/java/com/google/codeu/servlets/LogoutServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.codeu.servlets;
18 |
19 | import com.google.appengine.api.users.UserService;
20 | import com.google.appengine.api.users.UserServiceFactory;
21 | import java.io.IOException;
22 | import javax.servlet.annotation.WebServlet;
23 | import javax.servlet.http.HttpServlet;
24 | import javax.servlet.http.HttpServletRequest;
25 | import javax.servlet.http.HttpServletResponse;
26 |
27 | /**
28 | * Redirects the user to the Google logout page, which then redirects to the homepage.
29 | */
30 | @WebServlet("/logout")
31 | public class LogoutServlet extends HttpServlet {
32 |
33 | @Override
34 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
35 | UserService userService = UserServiceFactory.getUserService();
36 | String googleLogoutUrl = userService.createLogoutURL("/index.html");
37 | response.sendRedirect(googleLogoutUrl);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/google/codeu/data/Message.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.codeu.data;
18 |
19 | import java.util.UUID;
20 |
21 | /** A single message posted by a user. */
22 | public class Message {
23 |
24 | private UUID id;
25 | private String user;
26 | private String text;
27 | private long timestamp;
28 |
29 | /**
30 | * Constructs a new {@link Message} posted by {@code user} with {@code text} content. Generates a
31 | * random ID and uses the current system time for the creation time.
32 | */
33 | public Message(String user, String text) {
34 | this(UUID.randomUUID(), user, text, System.currentTimeMillis());
35 | }
36 |
37 | public Message(UUID id, String user, String text, long timestamp) {
38 | this.id = id;
39 | this.user = user;
40 | this.text = text;
41 | this.timestamp = timestamp;
42 | }
43 |
44 | public UUID getId() {
45 | return id;
46 | }
47 |
48 | public String getUser() {
49 | return user;
50 | }
51 |
52 | public String getText() {
53 | return text;
54 | }
55 |
56 | public long getTimestamp() {
57 | return timestamp;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/webapp/aboutus.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 |
21 | About Us
22 |
23 |
24 |
25 |
31 |
About Our Team
32 |
Teammate A Name
33 |
34 |
Summer Feelz:
35 |
Aspirational Hobby:
36 |
Ask me About:
37 |
38 |
Teammate B Name
39 |
40 |
Summer Feelz:
41 |
Aspirational Hobby:
42 |
Ask me About:
43 |
44 |
Teammate C Name
45 |
46 |
Summer Feelz:
47 |
Aspirational Hobby:
48 |
Ask me About:
49 |
50 |
Teammate D Name (delete if your team only has 3 students)
51 |
52 |
Summer Feelz:
53 |
Aspirational Hobby:
54 |
Ask me About:
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/src/main/java/com/google/codeu/servlets/LoginStatusServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.codeu.servlets;
18 |
19 | import com.google.appengine.api.users.UserService;
20 | import com.google.appengine.api.users.UserServiceFactory;
21 | import com.google.gson.JsonObject;
22 |
23 | import java.io.IOException;
24 | import javax.servlet.annotation.WebServlet;
25 | import javax.servlet.http.HttpServlet;
26 | import javax.servlet.http.HttpServletRequest;
27 | import javax.servlet.http.HttpServletResponse;
28 |
29 | /**
30 | * Returns login data as JSON, e.g. {"isLoggedIn": true, "username": "alovelace@codeustudents.com"}
31 | */
32 | @WebServlet("/login-status")
33 | public class LoginStatusServlet extends HttpServlet {
34 |
35 | @Override
36 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
37 |
38 | JsonObject jsonObject = new JsonObject();
39 |
40 | UserService userService = UserServiceFactory.getUserService();
41 | if (userService.isUserLoggedIn()) {
42 | jsonObject.addProperty("isLoggedIn", true);
43 | jsonObject.addProperty("username", userService.getCurrentUser().getEmail());
44 | } else {
45 | jsonObject.addProperty("isLoggedIn", false);
46 | }
47 |
48 | response.setContentType("application/json");
49 | response.getWriter().println(jsonObject.toString());
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/google/codeu/servlets/LoginServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | package com.google.codeu.servlets;
19 |
20 | import com.google.appengine.api.users.UserService;
21 | import com.google.appengine.api.users.UserServiceFactory;
22 | import java.io.IOException;
23 | import javax.servlet.annotation.WebServlet;
24 | import javax.servlet.http.HttpServlet;
25 | import javax.servlet.http.HttpServletRequest;
26 | import javax.servlet.http.HttpServletResponse;
27 |
28 | /**
29 | * Redirects the user to the Google login page or their page if they're already logged in.
30 | */
31 | @WebServlet("/login")
32 | public class LoginServlet extends HttpServlet {
33 |
34 | @Override
35 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
36 |
37 | UserService userService = UserServiceFactory.getUserService();
38 |
39 | // If the user is already logged in, redirect to their page
40 | if (userService.isUserLoggedIn()) {
41 | String user = userService.getCurrentUser().getEmail();
42 | response.sendRedirect("/user-page.html?user=" + user);
43 | return;
44 | }
45 |
46 | // Redirect to Google login page. That page will then redirect back to /login,
47 | // which will be handled by the above if statement.
48 | String googleLoginUrl = userService.createLoginURL("/login");
49 | response.sendRedirect(googleLoginUrl);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/webapp/js/navigation-loader.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /**
18 | * Adds a login or logout link to the page, depending on whether the user is
19 | * already logged in.
20 | */
21 | function addLoginOrLogoutLinkToNavigation() {
22 | const navigationElement = document.getElementById('navigation');
23 | if (!navigationElement) {
24 | console.warn('Navigation element not found!');
25 | return;
26 | }
27 |
28 | fetch('/login-status')
29 | .then((response) => {
30 | return response.json();
31 | })
32 | .then((loginStatus) => {
33 | if (loginStatus.isLoggedIn) {
34 | navigationElement.appendChild(createListItem(createLink(
35 | '/user-page.html?user=' + loginStatus.username, 'Your Page')));
36 |
37 | navigationElement.appendChild(
38 | createListItem(createLink('/logout', 'Logout')));
39 | } else {
40 | navigationElement.appendChild(
41 | createListItem(createLink('/login', 'Login')));
42 | }
43 | });
44 | }
45 |
46 | /**
47 | * Creates an li element.
48 | * @param {Element} childElement
49 | * @return {Element} li element
50 | */
51 | function createListItem(childElement) {
52 | const listItemElement = document.createElement('li');
53 | listItemElement.appendChild(childElement);
54 | return listItemElement;
55 | }
56 |
57 | /**
58 | * Creates an anchor element.
59 | * @param {string} url
60 | * @param {string} text
61 | * @return {Element} Anchor element
62 | */
63 | function createLink(url, text) {
64 | const linkElement = document.createElement('a');
65 | linkElement.appendChild(document.createTextNode(text));
66 | linkElement.href = url;
67 | return linkElement;
68 | }
69 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
20 | 4.0.0
21 |
22 | com.google.codeu
23 | codeu-starter-project
24 | 0.0.1-SNAPSHOT
25 | war
26 |
27 | codeu-starter-project
28 | http://maven.apache.org
29 |
30 |
31 | 1.8
32 | 1.8
33 | UTF-8
34 | false
35 |
36 |
37 |
38 |
39 |
40 | javax.servlet
41 | javax.servlet-api
42 | 3.1.0
43 | provided
44 |
45 |
46 |
47 | com.google.appengine
48 | appengine-api-1.0-sdk
49 | 1.9.59
50 |
51 |
52 |
53 | org.jsoup
54 | jsoup
55 | 1.8.3
56 |
57 |
58 |
59 | com.google.cloud
60 | google-cloud-datastore
61 | 1.52.0
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | com.google.appengine
70 | appengine-maven-plugin
71 | 1.9.59
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/src/main/java/com/google/codeu/servlets/MessageServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.codeu.servlets;
18 |
19 | import com.google.appengine.api.users.UserService;
20 | import com.google.appengine.api.users.UserServiceFactory;
21 | import com.google.codeu.data.Datastore;
22 | import com.google.codeu.data.Message;
23 | import com.google.gson.Gson;
24 | import java.io.IOException;
25 | import java.util.List;
26 | import javax.servlet.annotation.WebServlet;
27 | import javax.servlet.http.HttpServlet;
28 | import javax.servlet.http.HttpServletRequest;
29 | import javax.servlet.http.HttpServletResponse;
30 | import org.jsoup.Jsoup;
31 | import org.jsoup.safety.Whitelist;
32 |
33 | /** Handles fetching and saving {@link Message} instances. */
34 | @WebServlet("/messages")
35 | public class MessageServlet extends HttpServlet {
36 |
37 | private Datastore datastore;
38 |
39 | @Override
40 | public void init() {
41 | datastore = new Datastore();
42 | }
43 |
44 | /**
45 | * Responds with a JSON representation of {@link Message} data for a specific user. Responds with
46 | * an empty array if the user is not provided.
47 | */
48 | @Override
49 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
50 |
51 | response.setContentType("application/json");
52 |
53 | String user = request.getParameter("user");
54 |
55 | if (user == null || user.equals("")) {
56 | // Request is invalid, return empty array
57 | response.getWriter().println("[]");
58 | return;
59 | }
60 |
61 | List messages = datastore.getMessages(user);
62 | Gson gson = new Gson();
63 | String json = gson.toJson(messages);
64 |
65 | response.getWriter().println(json);
66 | }
67 |
68 | /** Stores a new {@link Message}. */
69 | @Override
70 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
71 |
72 | UserService userService = UserServiceFactory.getUserService();
73 | if (!userService.isUserLoggedIn()) {
74 | response.sendRedirect("/index.html");
75 | return;
76 | }
77 |
78 | String user = userService.getCurrentUser().getEmail();
79 | String text = Jsoup.clean(request.getParameter("text"), Whitelist.none());
80 |
81 | Message message = new Message(user, text);
82 | datastore.storeMessage(message);
83 |
84 | response.sendRedirect("/user-page.html?user=" + user);
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/src/main/java/com/google/codeu/data/Datastore.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.google.codeu.data;
18 |
19 | import com.google.appengine.api.datastore.DatastoreService;
20 | import com.google.appengine.api.datastore.DatastoreServiceFactory;
21 | import com.google.appengine.api.datastore.Entity;
22 | import com.google.appengine.api.datastore.PreparedQuery;
23 | import com.google.appengine.api.datastore.Query;
24 | import com.google.appengine.api.datastore.Query.FilterOperator;
25 | import com.google.appengine.api.datastore.Query.SortDirection;
26 | import java.util.ArrayList;
27 | import java.util.List;
28 | import java.util.UUID;
29 |
30 | /** Provides access to the data stored in Datastore. */
31 | public class Datastore {
32 |
33 | private DatastoreService datastore;
34 |
35 | public Datastore() {
36 | datastore = DatastoreServiceFactory.getDatastoreService();
37 | }
38 |
39 | /** Stores the Message in Datastore. */
40 | public void storeMessage(Message message) {
41 | Entity messageEntity = new Entity("Message", message.getId().toString());
42 | messageEntity.setProperty("user", message.getUser());
43 | messageEntity.setProperty("text", message.getText());
44 | messageEntity.setProperty("timestamp", message.getTimestamp());
45 |
46 | datastore.put(messageEntity);
47 | }
48 |
49 | /**
50 | * Gets messages posted by a specific user.
51 | *
52 | * @return a list of messages posted by the user, or empty list if user has never posted a
53 | * message. List is sorted by time descending.
54 | */
55 | public List getMessages(String user) {
56 | List messages = new ArrayList<>();
57 |
58 | Query query =
59 | new Query("Message")
60 | .setFilter(new Query.FilterPredicate("user", FilterOperator.EQUAL, user))
61 | .addSort("timestamp", SortDirection.DESCENDING);
62 | PreparedQuery results = datastore.prepare(query);
63 |
64 | for (Entity entity : results.asIterable()) {
65 | try {
66 | String idString = entity.getKey().getName();
67 | UUID id = UUID.fromString(idString);
68 | String text = (String) entity.getProperty("text");
69 | long timestamp = (long) entity.getProperty("timestamp");
70 |
71 | Message message = new Message(id, user, text, timestamp);
72 | messages.add(message);
73 | } catch (Exception e) {
74 | System.err.println("Error reading message.");
75 | System.err.println(entity.toString());
76 | e.printStackTrace();
77 | }
78 | }
79 |
80 | return messages;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/main/webapp/js/user-page-loader.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | // Get ?user=XYZ parameter value
18 | const urlParams = new URLSearchParams(window.location.search);
19 | const parameterUsername = urlParams.get('user');
20 |
21 | // URL must include ?user=XYZ parameter. If not, redirect to homepage.
22 | if (!parameterUsername) {
23 | window.location.replace('/');
24 | }
25 |
26 | /** Sets the page title based on the URL parameter username. */
27 | function setPageTitle() {
28 | document.getElementById('page-title').innerText = parameterUsername;
29 | document.title = parameterUsername + ' - User Page';
30 | }
31 |
32 | /**
33 | * Shows the message form if the user is logged in and viewing their own page.
34 | */
35 | function showMessageFormIfViewingSelf() {
36 | fetch('/login-status')
37 | .then((response) => {
38 | return response.json();
39 | })
40 | .then((loginStatus) => {
41 | if (loginStatus.isLoggedIn &&
42 | loginStatus.username == parameterUsername) {
43 | const messageForm = document.getElementById('message-form');
44 | messageForm.classList.remove('hidden');
45 | }
46 | });
47 | }
48 |
49 | /** Fetches messages and add them to the page. */
50 | function fetchMessages() {
51 | const url = '/messages?user=' + parameterUsername;
52 | fetch(url)
53 | .then((response) => {
54 | return response.json();
55 | })
56 | .then((messages) => {
57 | const messagesContainer = document.getElementById('message-container');
58 | if (messages.length == 0) {
59 | messagesContainer.innerHTML = '
This user has no posts yet.
';
60 | } else {
61 | messagesContainer.innerHTML = '';
62 | }
63 | messages.forEach((message) => {
64 | const messageDiv = buildMessageDiv(message);
65 | messagesContainer.appendChild(messageDiv);
66 | });
67 | });
68 | }
69 |
70 | /**
71 | * Builds an element that displays the message.
72 | * @param {Message} message
73 | * @return {Element}
74 | */
75 | function buildMessageDiv(message) {
76 | const headerDiv = document.createElement('div');
77 | headerDiv.classList.add('message-header');
78 | headerDiv.appendChild(document.createTextNode(
79 | message.user + ' - ' + new Date(message.timestamp)));
80 |
81 | const bodyDiv = document.createElement('div');
82 | bodyDiv.classList.add('message-body');
83 | bodyDiv.innerHTML = message.text;
84 |
85 | const messageDiv = document.createElement('div');
86 | messageDiv.classList.add('message-div');
87 | messageDiv.appendChild(headerDiv);
88 | messageDiv.appendChild(bodyDiv);
89 |
90 | return messageDiv;
91 | }
92 |
93 | /** Fetches data and populates the UI of the page. */
94 | function buildUI() {
95 | setPageTitle();
96 | showMessageFormIfViewingSelf();
97 | fetchMessages();
98 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------