package edu.pdx.cs410J.servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;

/**
 * This servlet sets cookies on the client's browser to determine when the last
 * time the page was visited.
 */
public class LastVisitServlet extends HttpServlet {

  private static final String FIRST_VISIT_COOKIE = "firstVisited";

  private static final String LAST_VISIT_COOKIE = "lastVisited";

  private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter pw = response.getWriter();
    String now = DATE_FORMAT.format(new Date());
    Cookie firstVisitCookie = getCookie(request, FIRST_VISIT_COOKIE);
    Cookie lastVisitCookie;
    if (firstVisitCookie == null) {
      pw.println("<h1>Welcome to the Last Visit servlet</h1>");
      pw.println("<p>Check your browser cookies</p>");
      firstVisitCookie = new Cookie(FIRST_VISIT_COOKIE, now);
      firstVisitCookie.setComment("This first time you viewed this page");
      firstVisitCookie.setMaxAge(3600);
      response.addCookie(firstVisitCookie);

    } else {
      pw.println("<h1>Welcome back to the Last Visit servlet</h1>");
      pw.println("<p>You first visited on " + firstVisitCookie.getValue() + "</p>");

      lastVisitCookie = getCookie(request, LAST_VISIT_COOKIE);
      pw.println("<p>You were last here on " + lastVisitCookie.getValue() + "</p>");
    }

    lastVisitCookie = new Cookie(LAST_VISIT_COOKIE, now);
    lastVisitCookie.setMaxAge(3600);
    response.addCookie(lastVisitCookie);
  }

  /**
   * Returns the cookie from the given request with the given name
   *
   * @return <code>null</code> if no cookie with that name exists
   */
  private Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
      return null;
    }
    for (Cookie cookie : cookies) {
      if (cookie.getName().equals(name)) {
        return cookie;
      }
    }

    return null;
  }
}

@@ -27,16 +27,50 @@
 vlets;%0A%0A
+import javax.servlet.http.Cookie;%0A
 import j
@@ -197,81 +197,8 @@
 se;%0A
-import javax.servlet.http.Cookie;%0Aimport javax.servlet.ServletException;%0A
 impo
@@ -266,22 +266,25 @@
 va.t
-ext.DateFormat
+ime.LocalDateTime
 ;%0Aim
@@ -297,17 +297,37 @@
 ava.
-u
 ti
-l.Date
+me.format.DateTimeFormatter
 ;%0A%0A/
@@ -636,88 +636,17 @@
 %0A%0A  
-private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();%0A
+@Override
 %0A  p
@@ -733,26 +733,8 @@
 rows
- ServletException,
  IOE
@@ -795,50 +795,46 @@
     
-String now = DATE_FORMAT.format(new Date()
+LocalDateTime now = LocalDateTime.now(
 );%0A 
@@ -1106,28 +1106,36 @@
 sitCookie = 
-new 
+createSecure
 Cookie(FIRST
@@ -1234,48 +1234,8 @@
 %22);%0A
-      firstVisitCookie.setMaxAge(3600);%0A
     
@@ -1462,32 +1462,41 @@
 isitCookie = get
+LastVisit
 Cookie(request, 
@@ -1497,27 +1497,8 @@
 uest
-, LAST_VISIT_COOKIE
 );%0A 
@@ -1614,12 +1614,20 @@
 e = 
-new 
+createSecure
 Cook
@@ -1662,80 +1662,556 @@
     
-lastVisitCookie.setMaxAge(3600);%0A    response.addCookie(lastVisitCookie)
+response.addCookie(lastVisitCookie);%0A  %7D%0A%0A  private Cookie createSecureCookie(String name, LocalDateTime now) %7B%0A    Cookie cookie = new Cookie(name, now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));%0A    cookie.setSecure(true);%0A    cookie.setMaxAge(3600);%0A    return cookie;%0A  %7D%0A%0A  private Cookie getLastVisitCookie(HttpServletRequest request) %7B%0A    Cookie cookie = getCookie(request, LAST_VISIT_COOKIE);%0A    if (cookie == null) %7B%0A      throw new NullPointerException(%22Expected to have a %22 + LAST_VISIT_COOKIE + %22 cookie%22);%0A    %7D%0A    return cookie
 ;%0A  

Address #414 by using secure cookies. I also had to remove spaces from the value of the cookie, which apparently is required now.