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.io.IOException;\r |
22 | import java.net.URI;\r |
23 | import java.util.ArrayList;\r |
24 | import java.util.Iterator;\r |
25 | import java.util.List;\r |
26 | import java.util.Set;\r |
27 | import java.util.logging.Level;\r |
28 | import java.util.logging.Logger;\r |
29 | \r |
30 | import javax.print.attribute.Attribute;\r |
31 | import javax.print.attribute.URISyntax;\r |
32 | import javax.print.attribute.standard.RequestingUserName;\r |
33 | \r |
34 | import de.lohndirekt.print.attribute.IppAttributeName;\r |
35 | import de.lohndirekt.print.attribute.auth.RequestingUserPassword;\r |
36 | import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OperationsSupported;\r |
37 | \r |
38 | /**\r |
39 | * @author bpusch\r |
40 | *\r |
41 | */\r |
42 | class IppServer {\r |
43 | \r |
44 | private final Logger log = Logger.getLogger(this.getClass().getName());\r |
45 | private URI uri;\r |
46 | private RequestingUserName user;\r |
47 | private RequestingUserPassword passwd;\r |
48 | \r |
49 | IppServer(URI uri, RequestingUserName user, RequestingUserPassword passwd) {\r |
50 | this.uri = uri;\r |
51 | this.user = user;\r |
52 | this.passwd = passwd;\r |
53 | }\r |
54 | \r |
55 | /**\r |
56 | * @return a list of all PrintServices (printers as well as classes)\r |
57 | * known to the CUPS server \r |
58 | */\r |
59 | List getPrintServices() {\r |
60 | List services = new ArrayList();\r |
61 | services.addAll(getServices(OperationsSupported.CUPS_GET_PRINTERS,user,passwd));\r |
62 | services.addAll(getServices(OperationsSupported.CUPS_GET_CLASSES,user,passwd));\r |
63 | return services;\r |
64 | }\r |
65 | \r |
66 | private List getServices(OperationsSupported operation,RequestingUserName user, RequestingUserPassword passwd){\r |
67 | if (!(operation==OperationsSupported.CUPS_GET_CLASSES || operation == OperationsSupported.CUPS_GET_PRINTERS)){\r |
68 | throw new IllegalArgumentException("Operation not applicable");\r |
69 | }\r |
70 | IppResponse response = null;\r |
71 | IppRequest request = IppRequestFactory.createIppRequest(this.uri, operation,user,passwd);\r |
72 | try {\r |
73 | response = request.send();\r |
74 | } catch (IOException e) {\r |
75 | log.log(Level.SEVERE, e.getMessage(),e);\r |
76 | }\r |
77 | List services = new ArrayList();\r |
78 | if (response != null) {\r |
79 | Set uriAttributes = (Set)response.getAttributes().get(IppAttributeName.PRINTER_URI_SUPPORTED.getCategory());\r |
80 | if (uriAttributes != null) {\r |
81 | for (Iterator iter = uriAttributes.iterator(); iter.hasNext();) {\r |
82 | Attribute attribute = (Attribute)iter.next();\r |
83 | URI printerUri = ((URISyntax)attribute).getURI();\r |
84 | IppPrintService service = new IppPrintService(printerUri);\r |
85 | service.setRequestingUserName(user);\r |
86 | service.setRequestingUserPassword(passwd);\r |
87 | services.add(service);\r |
88 | }\r |
89 | }\r |
90 | }\r |
91 | return services;\r |
92 | }\r |
93 | \r |
94 | }\r |