Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printer status special character support #689

Merged
merged 42 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
fd8f707
test commit
Vzor- Jul 9, 2020
4fa114e
test commit2
Vzor- Jul 9, 2020
83c4726
test commit
Vzor- Jul 9, 2020
42b9f61
test commit4
Vzor- Jul 9, 2020
c8b22f2
test commit5
Vzor- Jul 9, 2020
9e229e8
test commit6
Vzor- Jul 9, 2020
4d68045
test commit7
Vzor- Jul 9, 2020
50b0cb3
test commit8
Vzor- Jul 10, 2020
72cf896
test commit9
Vzor- Jul 10, 2020
5f8db62
test commit9
Vzor- Jul 10, 2020
0f09bc2
test commit9
Vzor- Jul 10, 2020
f606f98
test commit10
Vzor- Jul 10, 2020
d6ad650
test commit11
Vzor- Jul 10, 2020
951d9bd
fixes special printer names on macos and ubuntu
Vzor- Jul 10, 2020
735571e
fixes special printer names on macos and ubuntu
Vzor- Jul 10, 2020
bc390f4
Merge remote-tracking branch 'origin/master'
Vzor- Jul 10, 2020
a512f6f
even more test commits
Vzor- Jul 14, 2020
49ddd36
even more test commits 2
Vzor- Jul 14, 2020
b240c7b
cleanup #1
Vzor- Jul 14, 2020
b1afd68
cleanup #2
Vzor- Jul 14, 2020
c83e051
rework
Vzor- Jul 15, 2020
1fbe123
rework 2
Vzor- Jul 15, 2020
3144c8b
rework 3
Vzor- Jul 15, 2020
80d8098
rework 4
Vzor- Jul 15, 2020
0c88838
rework 5
Vzor- Jul 15, 2020
002d8c6
rework 6
Vzor- Jul 16, 2020
e839872
rework 7
Vzor- Jul 16, 2020
ba6c9a3
rework 8
Vzor- Jul 16, 2020
0d9e832
rework 9
Vzor- Jul 16, 2020
621228b
rework 10
Vzor- Jul 17, 2020
8eca26e
rework 11
Vzor- Jul 17, 2020
65104a8
rework 12
Vzor- Jul 17, 2020
762b88f
rework 13
Vzor- Jul 17, 2020
8fa7307
rework 14
Vzor- Jul 17, 2020
647c22a
rework 15
Vzor- Jul 17, 2020
1ef7f3d
rework 16
Vzor- Jul 17, 2020
2d2a10b
Refactor how "Description" is called
tresf Jul 17, 2020
f602103
Rename "description" in CupsStatusHandler
tresf Jul 17, 2020
1ec286e
Rename "description" in CupsStatusHandler again :)
tresf Jul 17, 2020
60f03b0
Added html4 escaping
Vzor- Jul 17, 2020
9bfb214
Mix-up fix
Vzor- Jul 17, 2020
0f8b7c0
sample escape fix
Vzor- Jul 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/qz/printer/info/NativePrinterMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qz.printer.PrintServiceMatcher;
import qz.utils.SystemUtilities;

import javax.print.PrintService;
import javax.print.attribute.standard.PrinterName;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
Expand All @@ -29,15 +31,15 @@ public static NativePrinterMap getInstance() {
return instance;
}

public String getPrinterId(String description) {
public String lookupPrinterId(String description) {
for(Map.Entry<String,NativePrinter> entry : entrySet()) {
NativePrinter info = entry.getValue();
if (info.getDescription().equals(description)) {
if (description.equals(info.getPrintService().value().getName())) {
return entry.getKey();
}
}
log.warn("Could not find printerId for " + description);
return description;
return null;
}

public ArrayList<PrintService> findMissing(PrintService[] services) {
Expand Down
19 changes: 16 additions & 3 deletions src/qz/printer/status/CupsStatusHandler.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package qz.printer.status;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -12,6 +15,7 @@
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.IOException;
Expand All @@ -20,6 +24,7 @@
* Created by kyle on 4/27/17.
*/
public class CupsStatusHandler extends AbstractHandler {
private static final Logger log = LoggerFactory.getLogger(CupsStatusHandler.class);

private static String lastGuid;

Expand Down Expand Up @@ -48,18 +53,25 @@ private void parseXML(XMLEventReader eventReader) throws XMLStreamException {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if ("description".equalsIgnoreCase(qName)) {
//This is the description of the rss message, NOT the description of the printer
if ("description".equalsIgnoreCase(startElement.getName().getLocalPart())) {
isDescription = true;
description = "";
}
if ("guid".equalsIgnoreCase(qName)) {
isGuid = true;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if ("description".equalsIgnoreCase(endElement.getName().getLocalPart())) {
isDescription = false;
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if (isDescription) {
description = characters.getData();
isDescription = false;
description += characters.getData();
}
if (isGuid) {
String guid = characters.getData();
Expand All @@ -73,6 +85,7 @@ private void parseXML(XMLEventReader eventReader) throws XMLStreamException {
} else {
String printerName = StringUtils.substringBeforeLast(description, "\"");
printerName = StringUtils.substringAfter(printerName, "\"");
printerName = StringEscapeUtils.unescapeXml(printerName);
tresf marked this conversation as resolved.
Show resolved Hide resolved
if (!printerName.isEmpty() && StatusMonitor.isListeningTo(printerName)) {
StatusMonitor.statusChanged(CupsUtils.getStatuses(printerName));
}
Expand Down
4 changes: 0 additions & 4 deletions src/qz/printer/status/CupsUtils.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package qz.printer.status;

import com.sun.jna.Pointer;
import org.codehaus.jettison.json.JSONArray;
import org.eclipse.jetty.util.URIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qz.printer.status.Cups.IPP;
import qz.utils.PrintingUtilities;
import qz.utils.ShellUtilities;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;

/**
* Created by kyle on 5/17/17.
Expand Down
7 changes: 6 additions & 1 deletion src/qz/printer/status/PrinterStatus.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package qz.printer.status;

import qz.printer.info.NativePrinterMap;

import java.util.Locale;

import static qz.printer.status.PrinterStatusType.*;
import static qz.utils.SystemUtilities.isMac;

/**
* Created by kyle on 7/7/17.
Expand All @@ -11,6 +14,7 @@ public class PrinterStatus {

public PrinterStatusType type;
public String issuingPrinterName;
public String issuingPrinterDescription;
public String cupsString;


Expand All @@ -21,6 +25,7 @@ public PrinterStatus(PrinterStatusType type, String issuingPrinterName) {
public PrinterStatus(PrinterStatusType type, String issuingPrinterName, String cupsString) {
this.type = type;
this.issuingPrinterName = issuingPrinterName;
this.issuingPrinterDescription = issuingPrinterName;
this.cupsString = cupsString;
}

Expand Down Expand Up @@ -54,7 +59,7 @@ public static PrinterStatus getFromCupsString(String reason, String issuingPrint
}

public String toString() {
String returnString = type.getName() + ": Level " + type.getSeverity() + ", StatusCode " + type.getCode() + ", From " + issuingPrinterName;
String returnString = type.getName() + ": Level " + type.getSeverity() + ", StatusCode " + type.getCode() + ", From " + issuingPrinterDescription;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid confusion please keep issuingPrinterName in the logs. If description is desired, add it as an additional field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this changed is because this is the status' toString, and I wanted it to reflect exactly what the status will report to the client. Should I still go ahead with this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this changed is because this is the status' toString, and I wanted it to reflect exactly what the status will report to the client. Should I still go ahead with this change?

It's just misleading that on Linux, it will use a field labeled "description", when it's clearly not.

if (!cupsString.isEmpty()) {
returnString += ", CUPS string " + cupsString;
}
Expand Down
39 changes: 30 additions & 9 deletions src/qz/printer/status/StatusMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
import org.eclipse.jetty.util.MultiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qz.printer.PrintServiceMatcher;
import qz.printer.info.NativePrinter;
import qz.printer.info.NativePrinterMap;
import qz.utils.SystemUtilities;
import qz.ws.SocketConnection;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import java.util.*;

import static qz.utils.SystemUtilities.isMac;
import static qz.utils.SystemUtilities.isWindows;

/**
Expand Down Expand Up @@ -40,7 +45,7 @@ public synchronized static boolean launchNotificationThreads() {
notificationThread.start();
}
}
//cull threads that don't have associated printers
//interrupt threads that don't have associated printers
for(Map.Entry<String,Thread> e : notificationThreadCollection.entrySet()) {
if (!printerNameList.contains(e.getKey())) {
e.getValue().interrupt();
Expand Down Expand Up @@ -80,20 +85,28 @@ public synchronized static boolean startListening(SocketConnection connection, J
clientPrinterConnections.add(ALL_PRINTERS, connection);
}
} else { //listen to specific printer(s)
for(int i = 0; i < printerNames.length(); i++) {
for (int i = 0; i < printerNames.length(); i++) {
String printerName = printerNames.getString(i);
if (SystemUtilities.isMac()) {
// Since 2.0: Mac printers use Description; Find by CUPS ID
printerName = NativePrinterMap.getInstance().getPrinterId(printerName);
// Since 2.0: Mac printers use descriptions as printer names; Find CUPS ID by Description
printerName = NativePrinterMap.getInstance().lookupPrinterId(printerName);
// Handle edge-case where printer was recently renamed/added
if (printerName == null) {
// Call PrintServiceLookup.lookupPrintServices again
PrintServiceMatcher.getNativePrinterList();

// FIXME: How do we know we got description from the command line yet?
printerName = NativePrinterMap.getInstance().lookupPrinterId(printerNames.getString(i));

}
}
if (printerName == null || "".equals(printerName)) {
throw new IllegalArgumentException();
}

if (!clientPrinterConnections.containsKey(printerNames.getString(i))) {
clientPrinterConnections.add(printerNames.getString(i), connection);
} else if (!clientPrinterConnections.getValues(printerNames.getString(i)).contains(connection)) {
clientPrinterConnections.add(printerNames.getString(i), connection);
if (!clientPrinterConnections.containsKey(printerName)) {
clientPrinterConnections.add(printerName, connection);
} else if (!clientPrinterConnections.getValues(printerName).contains(connection)) {
clientPrinterConnections.add(printerName, connection);
}
}
}
Expand Down Expand Up @@ -165,6 +178,14 @@ public synchronized static boolean isListeningTo(String PrinterName) {
public synchronized static void statusChanged(PrinterStatus[] statuses) {
HashSet<SocketConnection> connections = new HashSet<>();
for(PrinterStatus ps : statuses) {
if (isMac()) {
//On MacOS the description is used as the printer name
NativePrinter nativePrinter = PrintServiceMatcher.matchPrinter(ps.issuingPrinterName);
tresf marked this conversation as resolved.
Show resolved Hide resolved
if (nativePrinter != null) {
//If the printer description is missing from the map (usually because the printer was deleted), use the cups id instead
ps.issuingPrinterDescription = nativePrinter.getPrintService().value().getName();
}
}
if (clientPrinterConnections.containsKey(ps.issuingPrinterName)) {
connections.addAll(clientPrinterConnections.get(ps.issuingPrinterName));
}
Expand Down
2 changes: 1 addition & 1 deletion src/qz/printer/status/StatusSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void statusChanged (PrinterStatus printerStatus) {

private StreamEvent createStatusStream(PrinterStatus status) {
return new StreamEvent(StreamEvent.Stream.PRINTER, StreamEvent.Type.ACTION)
.withData("printerName", status.issuingPrinterName)
tresf marked this conversation as resolved.
Show resolved Hide resolved
.withData("printerName", status.issuingPrinterDescription)
.withData("statusCode", status.type.getCode())
.withData("statusText", status.type.getName())
.withData("severity", status.type.getSeverity())
Expand Down