Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / IppPrintServiceLookup.java
diff --git a/jspi/src/main/java/de/lohndirekt/print/IppPrintServiceLookup.java b/jspi/src/main/java/de/lohndirekt/print/IppPrintServiceLookup.java
new file mode 100644 (file)
index 0000000..c9117d4
--- /dev/null
@@ -0,0 +1,199 @@
+/**\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.net.URI;\r
+import java.net.URISyntaxException;\r
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Locale;\r
+import java.util.Set;\r
+import java.util.logging.Level;\r
+import java.util.logging.Logger;\r
+\r
+import javax.print.DocFlavor;\r
+import javax.print.MultiDocPrintService;\r
+import javax.print.PrintService;\r
+import javax.print.PrintServiceLookup;\r
+import javax.print.attribute.Attribute;\r
+import javax.print.attribute.AttributeSet;\r
+import javax.print.attribute.standard.RequestingUserName;\r
+\r
+import de.lohndirekt.print.attribute.auth.RequestingUserPassword;\r
+\r
+/** \r
+ * \r
+ * @author bpusch\r
+ * \r
+ * <p>\r
+ * This is an implementation of the Java Print Service API for IPP.<p>\r
+ * To use this implementation do one of the following:\r
+ * <p>\r
+ *     1. Set the system properties\r
+ * <ul>\r
+ * <li><code>de.lohndirekt.print.IppPrintService.uri</code></li>\r
+ * <li><code>de.lohndirekt.print.IppPrintService.username</code></li>\r
+ * <li><code>de.lohndirekt.print.IppPrintService.password</code></li>\r
+ * </ul>\r
+ * and use the default constructor\r
+ * <p>\r
+ *  2. use the 3 parameter constructor\r
+ * <p>\r
+ *  then register the instance by invoking javax.print.PrintServiceLookup.registerServiceProvider<br>\r
+ * <br>\r
+ *  @see javax.print.PrintServiceLookup#registerServiceProvider\r
+ */\r
+public class IppPrintServiceLookup extends PrintServiceLookup {\r
+    public final static String URI_KEY = "de.lohndirekt.print.IppPrintService.uri";\r
+    public final static String USERNAME_KEY = "de.lohndirekt.print.IppPrintService.username";\r
+    public final static String PASSWORD_KEY = "de.lohndirekt.print.IppPrintService.password";\r
+    private List cupsServers = new ArrayList();\r
+    private Logger log = Logger.getLogger(this.getClass().getName());\r
+\r
+    /**\r
+     * <p>\r
+     * default constructor uses the values set in\r
+     * <p>\r
+     * <code>de.lohndirekt.print.IppPrintService.uri</code><br>\r
+     * <code>de.lohndirekt.print.IppPrintService.username</code><br>\r
+     * <code>de.lohndirekt.print.IppPrintService.password</code><br>\r
+     */\r
+    public IppPrintServiceLookup() {\r
+        String myUri = (String) System.getProperties().get(URI_KEY);\r
+        String user = (String) System.getProperties().get(USERNAME_KEY);\r
+        String password = (String) System.getProperties().get(PASSWORD_KEY);\r
+        URI uri = null;\r
+        if (myUri == null) {\r
+            throw new NullPointerException("System property " + URI_KEY + " not set!");\r
+        } else if (user == null) {\r
+            throw new NullPointerException("System property " + USERNAME_KEY + " not set!");\r
+        } else if (password == null) {\r
+            throw new NullPointerException("System property " + PASSWORD_KEY + " not set!");\r
+        }\r
+        try {\r
+            uri = new URI(myUri);\r
+        } catch (URISyntaxException e) {\r
+            throw new NullPointerException("System property " + URI_KEY + " - " + myUri + " is not a valid Uri!");\r
+        }\r
+        cupsServers.add(new IppServer(uri, new RequestingUserName(user,Locale.getDefault()), new RequestingUserPassword(password,Locale.getDefault())));\r
+    }\r
+\r
+    /**\r
+     * This constructor uses the given parameters to connect to an IPP service\r
+     * \r
+     * @param uri the uri of the ipp service\r
+     * @param username used for authentication\r
+     * @param password used for authentication\r
+     */\r
+    public IppPrintServiceLookup(URI uri, String username, String password) {\r
+        cupsServers.add(new IppServer(uri, new RequestingUserName(username,Locale.getDefault()),new RequestingUserPassword(password,Locale.getDefault())));\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see javax.print.PrintServiceLookup#getPrintServices(javax.print.DocFlavor, javax.print.attribute.AttributeSet)\r
+     */\r
+    public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) {\r
+        PrintService[] services = getPrintServices();\r
+        List fittingServices = new ArrayList();\r
+        for (int i = 0; i < services.length; i++) {\r
+            IppPrintService service = (IppPrintService) services[i];\r
+            if (checkService(service, flavor, attributes)) {\r
+                fittingServices.add(service);\r
+            }\r
+        }\r
+        PrintService[] serviceArray = new PrintService[fittingServices.size()];\r
+        return (PrintService[]) fittingServices.toArray(serviceArray);\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see javax.print.PrintServiceLookup#getPrintServices()\r
+     */\r
+    public PrintService[] getPrintServices() {\r
+        List services = new ArrayList();\r
+        try {\r
+            for (Iterator iter = cupsServers.iterator(); iter.hasNext();) {\r
+                IppServer server = (IppServer) iter.next();\r
+                services.addAll(server.getPrintServices());\r
+            }\r
+        } catch (RuntimeException e) {\r
+            log.log(Level.SEVERE, e.getMessage(), e);\r
+        }\r
+        PrintService[] serviceArray = new PrintService[services.size()];\r
+        return (PrintService[]) services.toArray(serviceArray);\r
+    }\r
+\r
+    /**\r
+     * @param service\r
+     * @param flavor\r
+     * @param attributes\r
+     * @return\r
+     */\r
+    private boolean checkService(IppPrintService service, DocFlavor flavor, AttributeSet attributes) {\r
+        if (flavor == null || service.isDocFlavorSupported(flavor)) {\r
+            if (attributes != null) {\r
+                Attribute[] attrArray = attributes.toArray();\r
+                for (int j = 0; j < attrArray.length; j++) {\r
+                    Attribute attribute = attrArray[j];\r
+                    if (service.isAttributeValueSupported(attribute, flavor, null)) {\r
+                        return true;\r
+                    }\r
+                }\r
+            } else {\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see javax.print.PrintServiceLookup#getMultiDocPrintServices(javax.print.DocFlavor[], javax.print.attribute.AttributeSet)\r
+     */\r
+    public MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavors, AttributeSet attributes) {\r
+        Set multiDocServices = new HashSet();\r
+        PrintService[] services = getPrintServices(null, attributes);\r
+        for (int i = 0; i < services.length; i++) {\r
+            PrintService service = services[i];\r
+            if (flavors != null) {\r
+                for (int j = 0; j < flavors.length; j++) {\r
+                    DocFlavor flavor = flavors[j];\r
+                    if (!service.isDocFlavorSupported(flavor)) {\r
+                        break;\r
+                    }\r
+                }\r
+            }\r
+            multiDocServices.add(new IppMultiDocPrintService(((IppPrintService) service).getUri()));\r
+        }\r
+        return (MultiDocPrintService[]) multiDocServices.toArray(new MultiDocPrintService[multiDocServices.size()]);\r
+\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see javax.print.PrintServiceLookup#getDefaultPrintService()\r
+     */\r
+    public PrintService getDefaultPrintService() {\r
+        PrintService[] services = this.getPrintServices();\r
+        if (services.length > 0) {\r
+            return services[0];\r
+        }\r
+        return null;\r
+    }\r
+\r
+}\r