Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / IppServer.java
diff --git a/jspi/src/main/java/de/lohndirekt/print/IppServer.java b/jspi/src/main/java/de/lohndirekt/print/IppServer.java
new file mode 100644 (file)
index 0000000..82ea7ff
--- /dev/null
@@ -0,0 +1,94 @@
+/**\r
+ * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ * \r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *  \r
+ */\r
+package de.lohndirekt.print;\r
+\r
+import java.io.IOException;\r
+import java.net.URI;\r
+import java.util.ArrayList;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.logging.Level;\r
+import java.util.logging.Logger;\r
+\r
+import javax.print.attribute.Attribute;\r
+import javax.print.attribute.URISyntax;\r
+import javax.print.attribute.standard.RequestingUserName;\r
+\r
+import de.lohndirekt.print.attribute.IppAttributeName;\r
+import de.lohndirekt.print.attribute.auth.RequestingUserPassword;\r
+import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OperationsSupported;\r
+\r
+/**\r
+ * @author bpusch\r
+ *\r
+ */\r
+class IppServer {\r
+\r
+       private final Logger log = Logger.getLogger(this.getClass().getName());\r
+       private URI uri;\r
+       private RequestingUserName user;\r
+       private RequestingUserPassword passwd;\r
+       \r
+       IppServer(URI uri, RequestingUserName user, RequestingUserPassword passwd) {\r
+               this.uri = uri;\r
+               this.user = user;\r
+               this.passwd = passwd;\r
+       }\r
+\r
+       /**\r
+        * @return a list of all PrintServices (printers as well as classes)\r
+        *                      known to the CUPS server  \r
+        */\r
+       List getPrintServices() {\r
+               List services = new ArrayList();\r
+               services.addAll(getServices(OperationsSupported.CUPS_GET_PRINTERS,user,passwd));\r
+               services.addAll(getServices(OperationsSupported.CUPS_GET_CLASSES,user,passwd));\r
+               return services;\r
+       }\r
+       \r
+       private List getServices(OperationsSupported operation,RequestingUserName user, RequestingUserPassword passwd){\r
+               if (!(operation==OperationsSupported.CUPS_GET_CLASSES || operation == OperationsSupported.CUPS_GET_PRINTERS)){\r
+                       throw new IllegalArgumentException("Operation not applicable");\r
+               }\r
+               IppResponse response = null;\r
+               IppRequest request = IppRequestFactory.createIppRequest(this.uri, operation,user,passwd);\r
+               try {\r
+                       response = request.send();\r
+               } catch (IOException e) {\r
+                       log.log(Level.SEVERE, e.getMessage(),e);\r
+               }\r
+               List services = new ArrayList();\r
+               if (response != null) {\r
+                       Set uriAttributes = (Set)response.getAttributes().get(IppAttributeName.PRINTER_URI_SUPPORTED.getCategory());\r
+                       if (uriAttributes != null) {\r
+                               for (Iterator iter = uriAttributes.iterator(); iter.hasNext();) {\r
+                                       Attribute attribute = (Attribute)iter.next();\r
+                                       URI printerUri = ((URISyntax)attribute).getURI();\r
+                                       IppPrintService service = new IppPrintService(printerUri);\r
+                                       service.setRequestingUserName(user);\r
+                                       service.setRequestingUserPassword(passwd);\r
+                                       services.add(service);\r
+                               }\r
+                       }\r
+               }\r
+               return services;\r
+       }\r
+\r
+}\r