Permalink
Browse files

add parameter boolean printerClass to CupsPrinter for users of this

class to seperate printers from CUPS printer classes
  • Loading branch information...
harwey committed Oct 4, 2018
1 parent bf58973 commit 89f55cd475818934ee5041bdee92d89e4b5532b8
Showing with 106 additions and 61 deletions.
  1. +84 −48 src/main/java/org/cups4j/CupsPrinter.java
  2. +22 −13 src/test/java/org/cups4j/CupsClientTest.java
@@ -1,5 +1,21 @@
package org.cups4j;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cups4j.ipp.attributes.Attribute;
import org.cups4j.ipp.attributes.AttributeGroup;
import org.cups4j.operations.ipp.IppCreateJobOperation;
import org.cups4j.operations.ipp.IppGetJobAttributesOperation;
import org.cups4j.operations.ipp.IppGetJobsOperation;
import org.cups4j.operations.ipp.IppPrintJobOperation;
import org.cups4j.operations.ipp.IppSendDocumentOperation;
/**
* Copyright (C) 2009 Harald Weyhing
*
@@ -16,13 +32,6 @@
*/
import ch.ethz.vppserver.ippclient.IppResult;
import org.cups4j.ipp.attributes.Attribute;
import org.cups4j.ipp.attributes.AttributeGroup;
import org.cups4j.operations.ipp.*;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
/**
* Represents a printer on your IPP server
@@ -34,6 +43,7 @@
private String description = null;
private String location = null;
private boolean isDefault = false;
private boolean printerClass = false;
private String mediaDefault = null;
private String resolutionDefault = null;
private String colorModeDefault = null;
@@ -59,6 +69,13 @@ public CupsPrinter(URL printerURL, String printerName, boolean isDefault) {
this.printerURL = printerURL;
this.name = printerName;
this.isDefault = isDefault;
updateClassAttribute();
}
private void updateClassAttribute() {
if (printerURL != null && printerURL.toString().contains("class")) {
printerClass = true;
}
}
/**
@@ -166,13 +183,15 @@ public PrintRequestResult print(PrintJob printJob) throws Exception {
* another print job. The printer must support
* 'multiple-document-jobs-supported' which is a recommended option.
* <p>
* ATTENTION: Don't use different users for the different print jobs. You
* will get probably error 401 (forbidden) from CUPS. To avoid error 401
* you'll get now an {@link IllegalStateException}.
* ATTENTION: Don't use different users for the different print jobs. You will
* get probably error 401 (forbidden) from CUPS. To avoid error 401 you'll get
* now an {@link IllegalStateException}.
* </p>
*
* @param job1 first print job
* @param moreJobs more print jobs
* @param job1
* first print job
* @param moreJobs
* more print jobs
* @return PrintRequestResult
* @since 0.7.2
* @author oboehm
@@ -186,56 +205,61 @@ public PrintRequestResult print(PrintJob job1, PrintJob... moreJobs) {
for (int i = 0; i < printJobs.size() - 1; i++) {
print(printJobs.get(i), jobId, false);
}
return print(printJobs.get(printJobs.size()-1), jobId, true);
return print(printJobs.get(printJobs.size() - 1), jobId, true);
}
private static void verifyUser(String userName, PrintJob[] printJobs) {
for (PrintJob job : printJobs) {
String jobUserName = job.getUserName();
if (!userName.equals(jobUserName)) {
throw new IllegalStateException(
"different users (" + userName + ", " + jobUserName + ", ...) in print jobs are forbidden");
"different users (" + userName + ", " + jobUserName + ", ...) in print jobs are forbidden");
}
}
}
/**
* If you want to print serveral print jobs as one job you must first tell
* CUPS that you want to start. This is the method to create a job. The
* returned job-id must be used for the following print calls.
*
* @param jobName the name of a job
* @return the job-id
* @since 0.7.2
* @author oboehm
* @deprecated use {@link #createJob(PrintJob)} or {@link #createJob(String, String)}
*/
@Deprecated
public int createJob(String jobName) {
return createJob(jobName, CupsClient.DEFAULT_USER);
}
/**
* If you want to print serveral print jobs as one job you must first tell
* CUPS that you want to start. This is the method to create a job. The
* returned job-id must be used for the following print calls.
*
* @param jobName
* the name of a job
* @return the job-id
* @since 0.7.2
* @author oboehm
* @deprecated use {@link #createJob(PrintJob)} or
* {@link #createJob(String, String)}
*/
@Deprecated
public int createJob(String jobName) {
return createJob(jobName, CupsClient.DEFAULT_USER);
}
/**
* If you want to print serveral print jobs as one job you must first tell
* CUPS that you want to start. This is the method to create a job. The
* returned job-id must be used for the following print calls.
*
* @param jobName the name of a job
* @param userName the name of a user
* @return the job-id
* @since 0.7.4
* @author oboehm
*/
public int createJob(String jobName, String userName) {
return createJob(new PrintJob.Builder(new byte[0]).jobName(jobName).userName(userName).build());
}
/**
* If you want to print serveral print jobs as one job you must first tell
* CUPS that you want to start. This is the method to create a job. The
* returned job-id must be used for the following print calls.
*
* @param jobName
* the name of a job
* @param userName
* the name of a user
* @return the job-id
* @since 0.7.4
* @author oboehm
*/
public int createJob(String jobName, String userName) {
return createJob(new PrintJob.Builder(new byte[0]).jobName(jobName).userName(userName).build());
}
/**
/**
* If you want to print serveral print jobs as one job you must first tell
* CUPS that you want to start. This is the method to create a job. The
* returned job-id must be used for the following print calls.
*
* @param job the print-job with job-name and user-name
* @param job
* the print-job with job-name and user-name
* @return the job-id
* @since 0.7.4
* @author oboehm
@@ -254,9 +278,12 @@ public int createJob(PrintJob job) {
* Call this method if you want to print several print jobs as one print job.
* Call {@link #createJob(String)} to the get the correct job-id.
*
* @param job the job
* @param jobId the job id from {@link #createJob(String)}
* @param lastDocument set it to true if it is the last document
* @param job
* the job
* @param jobId
* the job id from {@link #createJob(String)}
* @param lastDocument
* set it to true if it is the last document
* @return the print request result
* @since 0.7.2
* @author oboehm
@@ -430,6 +457,7 @@ public String getColorModeDefault() {
public void setPrinterURL(URL printerURL) {
this.printerURL = printerURL;
updateClassAttribute();
}
public void setName(String name) {
@@ -507,4 +535,12 @@ public void setNumberUpSupported(List<String> numberUpSupported) {
this.numberUpSupported = numberUpSupported;
}
public boolean isPrinterClass() {
return printerClass;
}
public void setPrinterClass(boolean printerClass) {
this.printerClass = printerClass;
}
}
@@ -1,12 +1,15 @@
package org.cups4j;
import cups4j.TestCups;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import java.util.List;
import static org.junit.Assert.assertFalse;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cups4j.TestCups;
/**
* Unit tests for {@link CupsClient} class.
@@ -15,17 +18,23 @@
*/
public class CupsClientTest {
private static CupsClient client;
private static CupsClient client;
private static final Logger LOG = LoggerFactory.getLogger(CupsClientTest.class);
@BeforeClass
public static void setUpClient() throws Exception {
client = TestCups.getCupsClient();
}
@BeforeClass
public static void setUpClient() throws Exception {
client = TestCups.getCupsClient();
}
@Test
public void getPrinters() throws Exception {
List<CupsPrinter> printers = client.getPrinters();
@Test
public void getPrinters() throws Exception {
List<CupsPrinter> printers = client.getPrinters();
assertFalse(printers.isEmpty());
for (CupsPrinter printer : printers) {
LOG.info("printer: " + printer.getName() + "[isClass=" + printer.isPrinterClass() + "]");
}
assertFalse(printers.isEmpty());
}
}

0 comments on commit 89f55cd

Please sign in to comment.