3ea135bb |
1 | /**\r |
2 | * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r |
3 | *\r |
4 | * This library is free software; you can redistribute it and/or\r |
5 | * modify it under the terms of the GNU Lesser General Public\r |
6 | * License as published by the Free Software Foundation; either\r |
7 | * version 2.1 of the License, or (at your option) any later version.\r |
8 | * \r |
9 | * This library is distributed in the hope that it will be useful,\r |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r |
12 | * Lesser General Public License for more details.\r |
13 | * \r |
14 | * You should have received a copy of the GNU Lesser General Public\r |
15 | * License along with this library; if not, write to the Free Software\r |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
17 | * \r |
18 | */\r |
19 | package de.lohndirekt.print;\r |
20 | \r |
21 | import java.net.URI;\r |
22 | import java.net.URISyntaxException;\r |
23 | import java.util.ArrayList;\r |
24 | import java.util.HashSet;\r |
25 | import java.util.Iterator;\r |
26 | import java.util.List;\r |
27 | import java.util.Locale;\r |
28 | import java.util.Set;\r |
29 | import java.util.logging.Level;\r |
30 | import java.util.logging.Logger;\r |
31 | \r |
32 | import javax.print.DocFlavor;\r |
33 | import javax.print.MultiDocPrintService;\r |
34 | import javax.print.PrintService;\r |
35 | import javax.print.PrintServiceLookup;\r |
36 | import javax.print.attribute.Attribute;\r |
37 | import javax.print.attribute.AttributeSet;\r |
38 | import javax.print.attribute.standard.RequestingUserName;\r |
39 | \r |
40 | import de.lohndirekt.print.attribute.auth.RequestingUserPassword;\r |
41 | \r |
42 | /** \r |
43 | * \r |
44 | * @author bpusch\r |
45 | * \r |
46 | * <p>\r |
47 | * This is an implementation of the Java Print Service API for IPP.<p>\r |
48 | * To use this implementation do one of the following:\r |
49 | * <p>\r |
50 | * 1. Set the system properties\r |
51 | * <ul>\r |
52 | * <li><code>de.lohndirekt.print.IppPrintService.uri</code></li>\r |
53 | * <li><code>de.lohndirekt.print.IppPrintService.username</code></li>\r |
54 | * <li><code>de.lohndirekt.print.IppPrintService.password</code></li>\r |
55 | * </ul>\r |
56 | * and use the default constructor\r |
57 | * <p>\r |
58 | * 2. use the 3 parameter constructor\r |
59 | * <p>\r |
60 | * then register the instance by invoking javax.print.PrintServiceLookup.registerServiceProvider<br>\r |
61 | * <br>\r |
62 | * @see javax.print.PrintServiceLookup#registerServiceProvider\r |
63 | */\r |
64 | public class IppPrintServiceLookup extends PrintServiceLookup {\r |
65 | public final static String URI_KEY = "de.lohndirekt.print.IppPrintService.uri";\r |
66 | public final static String USERNAME_KEY = "de.lohndirekt.print.IppPrintService.username";\r |
67 | public final static String PASSWORD_KEY = "de.lohndirekt.print.IppPrintService.password";\r |
68 | private List cupsServers = new ArrayList();\r |
69 | private Logger log = Logger.getLogger(this.getClass().getName());\r |
70 | \r |
71 | /**\r |
72 | * <p>\r |
73 | * default constructor uses the values set in\r |
74 | * <p>\r |
75 | * <code>de.lohndirekt.print.IppPrintService.uri</code><br>\r |
76 | * <code>de.lohndirekt.print.IppPrintService.username</code><br>\r |
77 | * <code>de.lohndirekt.print.IppPrintService.password</code><br>\r |
78 | */\r |
79 | public IppPrintServiceLookup() {\r |
80 | String myUri = (String) System.getProperties().get(URI_KEY);\r |
81 | String user = (String) System.getProperties().get(USERNAME_KEY);\r |
82 | String password = (String) System.getProperties().get(PASSWORD_KEY);\r |
83 | URI uri = null;\r |
84 | if (myUri == null) {\r |
85 | throw new NullPointerException("System property " + URI_KEY + " not set!");\r |
86 | } else if (user == null) {\r |
87 | throw new NullPointerException("System property " + USERNAME_KEY + " not set!");\r |
88 | } else if (password == null) {\r |
89 | throw new NullPointerException("System property " + PASSWORD_KEY + " not set!");\r |
90 | }\r |
91 | try {\r |
92 | uri = new URI(myUri);\r |
93 | } catch (URISyntaxException e) {\r |
94 | throw new NullPointerException("System property " + URI_KEY + " - " + myUri + " is not a valid Uri!");\r |
95 | }\r |
96 | cupsServers.add(new IppServer(uri, new RequestingUserName(user,Locale.getDefault()), new RequestingUserPassword(password,Locale.getDefault())));\r |
97 | }\r |
98 | \r |
99 | /**\r |
100 | * This constructor uses the given parameters to connect to an IPP service\r |
101 | * \r |
102 | * @param uri the uri of the ipp service\r |
103 | * @param username used for authentication\r |
104 | * @param password used for authentication\r |
105 | */\r |
106 | public IppPrintServiceLookup(URI uri, String username, String password) {\r |
107 | cupsServers.add(new IppServer(uri, new RequestingUserName(username,Locale.getDefault()),new RequestingUserPassword(password,Locale.getDefault())));\r |
108 | }\r |
109 | \r |
110 | /* (non-Javadoc)\r |
111 | * @see javax.print.PrintServiceLookup#getPrintServices(javax.print.DocFlavor, javax.print.attribute.AttributeSet)\r |
112 | */\r |
113 | public PrintService[] getPrintServices(DocFlavor flavor, AttributeSet attributes) {\r |
114 | PrintService[] services = getPrintServices();\r |
115 | List fittingServices = new ArrayList();\r |
116 | for (int i = 0; i < services.length; i++) {\r |
117 | IppPrintService service = (IppPrintService) services[i];\r |
118 | if (checkService(service, flavor, attributes)) {\r |
119 | fittingServices.add(service);\r |
120 | }\r |
121 | }\r |
122 | PrintService[] serviceArray = new PrintService[fittingServices.size()];\r |
123 | return (PrintService[]) fittingServices.toArray(serviceArray);\r |
124 | }\r |
125 | \r |
126 | /* (non-Javadoc)\r |
127 | * @see javax.print.PrintServiceLookup#getPrintServices()\r |
128 | */\r |
129 | public PrintService[] getPrintServices() {\r |
130 | List services = new ArrayList();\r |
131 | try {\r |
132 | for (Iterator iter = cupsServers.iterator(); iter.hasNext();) {\r |
133 | IppServer server = (IppServer) iter.next();\r |
134 | services.addAll(server.getPrintServices());\r |
135 | }\r |
136 | } catch (RuntimeException e) {\r |
137 | log.log(Level.SEVERE, e.getMessage(), e);\r |
138 | }\r |
139 | PrintService[] serviceArray = new PrintService[services.size()];\r |
140 | return (PrintService[]) services.toArray(serviceArray);\r |
141 | }\r |
142 | \r |
143 | /**\r |
144 | * @param service\r |
145 | * @param flavor\r |
146 | * @param attributes\r |
147 | * @return\r |
148 | */\r |
149 | private boolean checkService(IppPrintService service, DocFlavor flavor, AttributeSet attributes) {\r |
150 | if (flavor == null || service.isDocFlavorSupported(flavor)) {\r |
151 | if (attributes != null) {\r |
152 | Attribute[] attrArray = attributes.toArray();\r |
153 | for (int j = 0; j < attrArray.length; j++) {\r |
154 | Attribute attribute = attrArray[j];\r |
155 | if (service.isAttributeValueSupported(attribute, flavor, null)) {\r |
156 | return true;\r |
157 | }\r |
158 | }\r |
159 | } else {\r |
160 | return true;\r |
161 | }\r |
162 | }\r |
163 | return false;\r |
164 | }\r |
165 | \r |
166 | /* (non-Javadoc)\r |
167 | * @see javax.print.PrintServiceLookup#getMultiDocPrintServices(javax.print.DocFlavor[], javax.print.attribute.AttributeSet)\r |
168 | */\r |
169 | public MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavors, AttributeSet attributes) {\r |
170 | Set multiDocServices = new HashSet();\r |
171 | PrintService[] services = getPrintServices(null, attributes);\r |
172 | for (int i = 0; i < services.length; i++) {\r |
173 | PrintService service = services[i];\r |
174 | if (flavors != null) {\r |
175 | for (int j = 0; j < flavors.length; j++) {\r |
176 | DocFlavor flavor = flavors[j];\r |
177 | if (!service.isDocFlavorSupported(flavor)) {\r |
178 | break;\r |
179 | }\r |
180 | }\r |
181 | }\r |
182 | multiDocServices.add(new IppMultiDocPrintService(((IppPrintService) service).getUri()));\r |
183 | }\r |
184 | return (MultiDocPrintService[]) multiDocServices.toArray(new MultiDocPrintService[multiDocServices.size()]);\r |
185 | \r |
186 | }\r |
187 | \r |
188 | /* (non-Javadoc)\r |
189 | * @see javax.print.PrintServiceLookup#getDefaultPrintService()\r |
190 | */\r |
191 | public PrintService getDefaultPrintService() {\r |
192 | PrintService[] services = this.getPrintServices();\r |
193 | if (services.length > 0) {\r |
194 | return services[0];\r |
195 | }\r |
196 | return null;\r |
197 | }\r |
198 | \r |
199 | }\r |