2 * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package de
.lohndirekt
.print
;
21 import java
.io
.IOException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
27 import java
.util
.logging
.Level
;
28 import java
.util
.logging
.Logger
;
30 import javax
.print
.attribute
.Attribute
;
31 import javax
.print
.attribute
.URISyntax
;
32 import javax
.print
.attribute
.standard
.RequestingUserName
;
34 import de
.lohndirekt
.print
.attribute
.IppAttributeName
;
35 import de
.lohndirekt
.print
.attribute
.auth
.RequestingUserPassword
;
36 import de
.lohndirekt
.print
.attribute
.ipp
.printerdesc
.supported
.OperationsSupported
;
44 private final Logger log
= Logger
.getLogger(this.getClass().getName());
46 private RequestingUserName user
;
47 private RequestingUserPassword passwd
;
49 IppServer(URI uri
, RequestingUserName user
, RequestingUserPassword passwd
) {
56 * @return a list of all PrintServices (printers as well as classes)
57 * known to the CUPS server
59 List
getPrintServices() {
60 List services
= new ArrayList();
61 services
.addAll(getServices(OperationsSupported
.CUPS_GET_PRINTERS
,user
,passwd
));
62 services
.addAll(getServices(OperationsSupported
.CUPS_GET_CLASSES
,user
,passwd
));
66 private List
getServices(OperationsSupported operation
,RequestingUserName user
, RequestingUserPassword passwd
){
67 if (!(operation
==OperationsSupported
.CUPS_GET_CLASSES
|| operation
== OperationsSupported
.CUPS_GET_PRINTERS
)){
68 throw new IllegalArgumentException("Operation not applicable");
70 IppResponse response
= null;
71 IppRequest request
= IppRequestFactory
.createIppRequest(this.uri
, operation
,user
,passwd
);
73 response
= request
.send();
74 } catch (IOException e
) {
75 log
.log(Level
.SEVERE
, e
.getMessage(),e
);
77 List services
= new ArrayList();
78 if (response
!= null) {
79 Set uriAttributes
= (Set
)response
.getAttributes().get(IppAttributeName
.PRINTER_URI_SUPPORTED
.getCategory());
80 if (uriAttributes
!= null) {
81 for (Iterator iter
= uriAttributes
.iterator(); iter
.hasNext();) {
82 Attribute attribute
= (Attribute
)iter
.next();
83 URI printerUri
= ((URISyntax
)attribute
).getURI();
84 IppPrintService service
= new IppPrintService(printerUri
);
85 service
.setRequestingUserName(user
);
86 service
.setRequestingUserPassword(passwd
);
87 services
.add(service
);