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,14 +39,59 @@ public class WardenServlet extends HttpServlet { ...@@ -37,14 +39,59 @@ 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")) {
50 + String userName = req.getParameter("user");
51 + if (userName != null) {
52 + printUserInfo(out, userName);
53 + } else {
54 + printAvailability(out);
55 + }
56 + } else {
57 + printAvailabilityText(out);
58 + }
59 + } catch (Exception e) {
60 + resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
61 + e.printStackTrace();
62 + }
63 + }
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) {
48 Reservation reservation = warden.currentCellReservation(cellName); 95 Reservation reservation = warden.currentCellReservation(cellName);
49 if (reservation != null) { 96 if (reservation != null) {
50 long expiration = reservation.time + reservation.duration * 60_000; 97 long expiration = reservation.time + reservation.duration * 60_000;
...@@ -59,10 +106,6 @@ public class WardenServlet extends HttpServlet { ...@@ -59,10 +106,6 @@ public class WardenServlet extends HttpServlet {
59 out.println(String.format("%-10s\t%-10s", cellName, "available")); 106 out.println(String.format("%-10s\t%-10s", cellName, "available"));
60 } 107 }
61 } 108 }
62 - } catch (Exception e) {
63 - resp.setStatus(Response.SC_INTERNAL_SERVER_ERROR);
64 - e.printStackTrace();
65 - }
66 } 109 }
67 110
68 @Override 111 @Override
......