-
Notifications
You must be signed in to change notification settings - Fork 2
/
PrinterStatusServlet.java
134 lines (115 loc) · 4.63 KB
/
PrinterStatusServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.*;
import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import com.google.gson.Gson;
import org.apache.catalina.core.ApplicationPart;
import com.zebra.sdk.comm.*;
import com.zebra.sdk.printer.*;
@MultipartConfig
public class PrinterStatusServlet extends HttpServlet {
private List<String> getIpAddressList(String ipAddresses) {
return new ArrayList<String>(Arrays.asList(ipAddresses.split(",")));
}
private List<Connection> initiateConnections(List<String> ipAddressList) {
List<Connection> connections = new ArrayList<Connection>();
for (String ip : ipAddressList) {
Connection connection = new TcpConnection(ip, TcpConnection.DEFAULT_ZPL_TCP_PORT);
connections.add(connection);
}
return connections;
}
private List<ZebraPrinterStatus> getPrinterStatus(String ipAddresses) throws Exception {
String result = "";
String message = "";
boolean hasError = true;
List<String> ipAddressList = getIpAddressList(ipAddresses);
List<Connection> connections = initiateConnections(ipAddressList);
List<ZebraPrinterStatus> statuses = new ArrayList<ZebraPrinterStatus>();
ZebraPrinterStatus zbps;
for (Connection connection : connections) {
try {
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
PrinterStatus printerStatus = printer.getCurrentStatus();
if (printerStatus.isReadyToPrint) {
message = "Ready To Print";
hasError = false;
} else if (printerStatus.isPaused) {
message = "Cannot Print because the printer is paused.";
} else if (printerStatus.isHeadOpen) {
message = "Cannot Print because the printer head is open.";
} else if (printerStatus.isPaperOut) {
message = "Cannot Print because the paper is out.";
} else {
message = "Cannot Print.";
}
} catch (Exception e) {
message = e.toString();
} finally {
zbps = new ZebraPrinterStatus(((TcpConnection)connection).getAddress(), hasError, message);
connection.close();
}
statuses.add(zbps);
}
return statuses;
}
public ZebraPrinterStatus getDefaultStatus() {
return new ZebraPrinterStatus(null, true, "Please give me a few IP addresses to work with.");
}
public ZebraPrinterStatus getErrorStatus(String errorMessage) {
return new ZebraPrinterStatus(null, true, errorMessage);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String ipAddresses = request.getParameter("ip");
String output = "";
List<ZebraPrinterStatus> statuses;
PrintWriter out = response.getWriter();
Gson gson = new Gson();
response.setContentType("application/json");
try {
statuses = getPrinterStatus(ipAddresses);
output = gson.toJson(statuses);
} catch (NullPointerException npe) {
output = gson.toJson(getDefaultStatus());
} catch (Exception e) {
output = gson.toJson(getErrorStatus(e.toString()));
} finally {
out.println(output);
out.close();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
ApplicationPart alertMsgPart = (ApplicationPart)request.getPart("alertMsg");
String alertMsg = URLDecoder.decode(alertMsgPart.getString("UTF-8"), "UTF-8");
ApplicationPart uniqueIdPart = (ApplicationPart)request.getPart("uniqueId");
String uniqueId = URLDecoder.decode(uniqueIdPart.getString("UTF-8"), "UTF-8");
// String printerIpAddress = request.getHeader("Host");
String printerIpAddress = uniqueId;
ZebraPrinterStatus zbps = new ZebraPrinterStatus(printerIpAddress, !alertMsg.contains("ALERT CLEARED"), alertMsg);
// List<ZebraPrinterStatus> zbpsArray = new ArrayList<ZebraPrinterStatus>();
// zbpsArray.add(zbps);
Gson gson = new Gson();
String output = gson.toJson(zbps);
System.out.println(output);
URL url = new URL(WEBHOOK_URL);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(output);
wr.flush();
wr.close();
con.getResponseCode();
}
}