Thomas Vachuska
Committed by Ray Milkey

Adding format easier to parse for OSX app.

Change-Id: Ibbf7f9ff7453b4e879da9c8c9215d8bb429a524b
...@@ -26,7 +26,9 @@ import javax.servlet.http.HttpServletResponse; ...@@ -26,7 +26,9 @@ import javax.servlet.http.HttpServletResponse;
26 import java.io.IOException; 26 import java.io.IOException;
27 import java.io.PrintWriter; 27 import java.io.PrintWriter;
28 import java.text.SimpleDateFormat; 28 import java.text.SimpleDateFormat;
29 +import java.util.ArrayList;
29 import java.util.Date; 30 import java.util.Date;
31 +import java.util.List;
30 32
31 import static com.google.common.base.Strings.isNullOrEmpty; 33 import static com.google.common.base.Strings.isNullOrEmpty;
32 34
...@@ -37,27 +39,22 @@ public class WardenServlet extends HttpServlet { ...@@ -37,27 +39,22 @@ public class WardenServlet extends HttpServlet {
37 39
38 static Warden warden; 40 static Warden warden;
39 41
42 + private SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
43 +
40 @Override 44 @Override
41 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 45 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
42 throws ServletException, IOException { 46 throws ServletException, IOException {
43 resp.setContentType("text/plain; charset=UTF-8"); 47 resp.setContentType("text/plain; charset=UTF-8");
44 - SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
45 -
46 try (PrintWriter out = resp.getWriter()) { 48 try (PrintWriter out = resp.getWriter()) {
47 - for (String cellName : warden.getCells()) { 49 + if (req.getPathInfo().endsWith("data")) {
48 - Reservation reservation = warden.currentCellReservation(cellName); 50 + String userName = req.getParameter("user");
49 - if (reservation != null) { 51 + if (userName != null) {
50 - long expiration = reservation.time + reservation.duration * 60_000; 52 + printUserInfo(out, userName);
51 - long remaining = (expiration - System.currentTimeMillis()) / 60_000;
52 - out.println(String.format("%-14s\t%-10s\t%s\t%s\t%s mins (%s remaining)",
53 - cellName + "-" + reservation.cellSpec,
54 - reservation.userName,
55 - fmt.format(new Date(reservation.time)),
56 - fmt.format(new Date(expiration)),
57 - reservation.duration, remaining));
58 } else { 53 } else {
59 - out.println(String.format("%-10s\t%-10s", cellName, "available")); 54 + printAvailability(out);
60 } 55 }
56 + } else {
57 + printAvailabilityText(out);
61 } 58 }
62 } catch (Exception e) { 59 } catch (Exception e) {
63 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR); 60 resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
...@@ -65,6 +62,52 @@ public class WardenServlet extends HttpServlet { ...@@ -65,6 +62,52 @@ public class WardenServlet extends HttpServlet {
65 } 62 }
66 } 63 }
67 64
65 + private void printUserInfo(PrintWriter out, String userName) {
66 + Reservation reservation = warden.currentUserReservation(userName);
67 + out.println(getCellStatus(null, reservation));
68 + }
69 +
70 + private void printAvailability(PrintWriter out) {
71 + List<String> list = new ArrayList<>(warden.getCells());
72 + list.sort(String::compareTo);
73 + for (String cellName : list) {
74 + Reservation reservation = warden.currentCellReservation(cellName);
75 + out.println(getCellStatus(cellName, reservation));
76 + }
77 + }
78 +
79 + private String getCellStatus(String cellName, Reservation reservation) {
80 + if (reservation != null) {
81 + long expiration = reservation.time + reservation.duration * 60_000;
82 + long remaining = (expiration - System.currentTimeMillis()) / 60_000;
83 + return String.format("%s,%s,%s,%s", reservation.cellName,
84 + reservation.cellSpec, reservation.userName, remaining);
85 + } else if (cellName != null) {
86 + return String.format("%s", cellName);
87 + }
88 + return null;
89 + }
90 +
91 + private void printAvailabilityText(PrintWriter out) {
92 + List<String> list = new ArrayList<>(warden.getCells());
93 + list.sort(String::compareTo);
94 + for (String cellName : list) {
95 + Reservation reservation = warden.currentCellReservation(cellName);
96 + if (reservation != null) {
97 + long expiration = reservation.time + reservation.duration * 60_000;
98 + long remaining = (expiration - System.currentTimeMillis()) / 60_000;
99 + out.println(String.format("%-14s\t%-10s\t%s\t%s\t%s mins (%s remaining)",
100 + cellName + "-" + reservation.cellSpec,
101 + reservation.userName,
102 + fmt.format(new Date(reservation.time)),
103 + fmt.format(new Date(expiration)),
104 + reservation.duration, remaining));
105 + } else {
106 + out.println(String.format("%-10s\t%-10s", cellName, "available"));
107 + }
108 + }
109 + }
110 +
68 @Override 111 @Override
69 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 112 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
70 throws ServletException, IOException { 113 throws ServletException, IOException {
......