Search in sources :

Example 76 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class ChatServlet method doPost.

/**
 * This function fires when a user submits the form on the chat page. It gets the logged-in
 * username from the session, the conversation title from the URL, and the chat message from the
 * submitted form data. It creates a new Message from that data, adds it to the model, and then
 * redirects back to the chat page.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        // user is not logged in, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    User user = userStore.getUser(username);
    if (user == null) {
        // user was not found, don't let them add a message
        response.sendRedirect("/login");
        return;
    }
    String requestUrl = request.getRequestURI();
    String conversationTitle = requestUrl.substring("/chat/".length());
    Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
    if (conversation == null) {
        // couldn't find conversation, redirect to conversation list
        response.sendRedirect("/conversations");
        return;
    }
    String messageContent = request.getParameter("message");
    // this removes any HTML from the message content
    String cleanedMessageContent = Jsoup.clean(messageContent, Whitelist.none());
    Message message = new Message(UUID.randomUUID(), conversation.getId(), user.getId(), cleanedMessageContent, Instant.now());
    messageStore.addMessage(message);
    // redirect to a GET request
    response.sendRedirect("/chat/" + conversationTitle);
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation)

Example 77 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class LoginServlet method doPost.

/**
 * This function fires when a user submits the login form. It gets the username and password from
 * the submitted form data, checks that they're valid, and either adds the user to the session
 * so we know the user is logged in or shows an error to the user.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if (userStore.isUserRegistered(username)) {
        User user = userStore.getUser(username);
        if (BCrypt.checkpw(password, user.getPassword())) {
            request.getSession().setAttribute("user", username);
            response.sendRedirect("/conversations");
        } else {
            request.setAttribute("error", "Invalid password.");
            request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
        }
    } else {
        request.setAttribute("error", "That username was not found.");
        request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
    }
}
Also used : User(codeu.model.data.User)

Example 78 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class ConversationServlet method doPost.

/**
 * This function fires when a user submits the form on the conversations page. It gets the
 * logged-in username from the session and the new conversation title from the submitted form
 * data. It uses this to create a new Conversation object that it adds to the model.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        // user is not logged in, don't let them create a conversation
        response.sendRedirect("/conversations");
        return;
    }
    User user = userStore.getUser(username);
    if (user == null) {
        // user was not found, don't let them create a conversation
        System.out.println("User not found: " + username);
        response.sendRedirect("/conversations");
        return;
    }
    String conversationTitle = request.getParameter("conversationTitle");
    if (!conversationTitle.matches("[\\w*]*")) {
        request.setAttribute("error", "Please enter only letters and numbers.");
        request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
        return;
    }
    if (conversationStore.isTitleTaken(conversationTitle)) {
        // conversation title is already taken, just go into that conversation instead of creating a
        // new one
        response.sendRedirect("/chat/" + conversationTitle);
        return;
    }
    Conversation conversation = new Conversation(UUID.randomUUID(), user.getId(), conversationTitle, Instant.now());
    conversationStore.addConversation(conversation);
    response.sendRedirect("/chat/" + conversationTitle);
}
Also used : User(codeu.model.data.User) Conversation(codeu.model.data.Conversation)

Example 79 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by maksymko.

the class ProfileServlet method doGet.

/**
 * This function fires when a user navigates to the profile page. It gets the conversation title from
 * the URL, finds the corresponding Conversation, to show a list of all of the conversations a user has participated in
 * It then forwards to profile.jsp for rendering.
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String requestUrl = request.getRequestURI();
    String username = requestUrl.substring("/profile/".length());
    User user = userStore.getUser(username);
    if (user == null) {
        // user is not found
        request.setAttribute("error", "Sorry, user " + username + " not found.");
        request.getRequestDispatcher("/WEB-INF/view/profile.jsp").forward(request, response);
        return;
    }
    request.setAttribute("view_user", user);
    request.getRequestDispatcher("/WEB-INF/view/profile.jsp").forward(request, response);
}
Also used : User(codeu.model.data.User)

Example 80 with User

use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.

the class ChatServletTest method testDoPost_ConversationNotFound.

@Test
public void testDoPost_ConversationNotFound() throws IOException, ServletException {
    Mockito.when(mockRequest.getRequestURI()).thenReturn("/chat/test_conversation");
    Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
    User fakeUser = new User(UUID.randomUUID(), "test_username", "test_password", Instant.now());
    Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
    Mockito.when(mockConversationStore.getConversationWithTitle("test_conversation")).thenReturn(null);
    chatServlet.doPost(mockRequest, mockResponse);
    Mockito.verify(mockMessageStore, Mockito.never()).addMessage(Mockito.any(Message.class));
    Mockito.verify(mockResponse).sendRedirect("/conversations");
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Test(org.junit.Test)

Aggregations

User (codeu.model.data.User)91 Test (org.junit.Test)60 Conversation (codeu.model.data.Conversation)36 Message (codeu.model.data.Message)23 UserStore (codeu.model.store.basic.UserStore)9 UUID (java.util.UUID)9 HttpSession (javax.servlet.http.HttpSession)8 Activity (codeu.model.data.Activity)7 Instant (java.time.Instant)7 ArrayList (java.util.ArrayList)7 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)2 Entity (com.google.appengine.api.datastore.Entity)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 Comparator (java.util.Comparator)2 ActivityStore (codeu.model.store.basic.ActivityStore)1 OutputSettings (org.jsoup.nodes.Document.OutputSettings)1