2
Copyright (C) 2006 Free Software Foundation, Inc.
4
This file is part of GNU Classpath.
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING. If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library. Thus, the terms and
23
conditions of the GNU General Public License cover the whole
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module. An independent module is a module which is not derived from
33
or based on this library. If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so. If you do not wish to do so, delete this
36
exception statement from your version. */
39
package gnu.javax.print;
41
import gnu.javax.print.ipp.IppException;
42
import gnu.javax.print.ipp.IppPrintService;
43
import gnu.javax.print.ipp.IppRequest;
44
import gnu.javax.print.ipp.IppResponse;
45
import gnu.javax.print.ipp.attribute.RequestedAttributes;
46
import gnu.javax.print.ipp.attribute.supported.PrinterUriSupported;
48
import java.io.IOException;
50
import java.net.URISyntaxException;
51
import java.util.ArrayList;
52
import java.util.List;
57
* <code>CupsServer</code> represents a host running a cups
58
* compatible server. It mainly consists of its URI and optional
59
* user and password combination if access is restricted.
61
* It provides methods for retrival of valid CUPS printer uris
62
* that are used to construct IppPrintService objects.
65
* @author Wolfgang Baer (WBaer@gmx.de)
67
public class CupsServer
70
* The URI of the CUPS server.
71
* This is something like: http://localhost:631
73
private transient URI uri;
76
* The optional username.
78
private transient String username;
81
* The optional password for the user.
83
private transient String password;
86
* Creates a <code>CupsServer</code> object which
87
* tries to connect to the cups server on localhost.
89
* @param username the username
90
* @param password the password for the username.
92
public CupsServer(String username, String password)
96
this.uri = new URI("http://localhost:631");
98
catch (URISyntaxException e)
103
this.username = username;
104
this.password = password;
108
* Creates a <code>CupsServer</code> object which
109
* tries to connect to a running cups server on the
112
* @param uri the URI of the server.
113
* @param username the username
114
* @param password the password for the username.
116
public CupsServer(URI uri, String username, String password)
119
this.username = username;
120
this.password = password;
124
* Requests the default printer from this CUPS server.
125
* This is always returned as IppPrintService.
127
* @return The default printer.
128
* @throws IppException if problems during request/response processing occur.
130
public IppPrintService getDefaultPrinter() throws IppException
132
IppResponse response = null;
136
IppRequest request = new IppRequest(uri, username, password);
137
request.setOperationID((short)CupsIppOperation.CUPS_GET_DEFAULT);
138
request.setOperationAttributeDefaults();
140
RequestedAttributes requestedAttrs
141
= new RequestedAttributes("printer-uri-supported");
142
request.addOperationAttribute(requestedAttrs);
144
response = request.send();
146
catch (IOException e)
148
throw new IppException("IOException in IPP request/response.", e);
151
Map printerAttributes = (Map) response.getPrinterAttributes().get(0);
152
Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
153
PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
155
IppPrintService service
156
= new CupsPrintService(uri.getURI(), username, password);
162
* Requests all printers from this CUPS server.
164
* @return The list of available printers.
165
* @throws IppException if problems during request/response processing occur.
167
public List getAllPrinters() throws IppException
169
IppResponse response = null;
173
IppRequest request = new IppRequest(uri, username, password);
174
request.setOperationID((short)CupsIppOperation.CUPS_GET_PRINTERS);
175
request.setOperationAttributeDefaults();
177
RequestedAttributes requestedAttrs
178
= new RequestedAttributes("printer-uri-supported");
179
request.addOperationAttribute(requestedAttrs);
181
response = request.send();
183
catch (IOException e)
185
throw new IppException("IOException in IPP request/response.", e);
188
List prAttr = response.getPrinterAttributes();
189
List services = new ArrayList();
191
for (int i=0; i < prAttr.size(); i++)
193
Map printerAttributes = (Map) prAttr.get(i);
194
Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
195
PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
199
CupsPrintService cups = new CupsPrintService(uri.getURI(),
203
catch (IppException e)
205
// do nothing, we only catch the IppException which could be
206
// thrown during instantiation as single printers may be discovered
207
// correctly but not usable due to other security restrictions
215
* Requests all classes from this CUPS server. Classes in cups are
216
* collections of printers. This means jobs directed to a class
217
* are forwarded to the first available printer of the collection.
219
* @return The list of available classes.
220
* @throws IppException if problems during request/response processing occur.
222
public List getAllClasses() throws IppException
224
IppResponse response = null;
228
IppRequest request = new IppRequest(uri, username, password);
229
request.setOperationID((short)CupsIppOperation.CUPS_GET_CLASSES);
230
request.setOperationAttributeDefaults();
232
RequestedAttributes requestedAttrs
233
= new RequestedAttributes("printer-uri-supported");
234
request.addOperationAttribute(requestedAttrs);
236
response = request.send();
238
catch (IOException e)
240
throw new IppException("IOException in IPP request/response.", e);
243
List prAttr = response.getPrinterAttributes();
244
List services = new ArrayList();
246
for (int i=0; i < prAttr.size(); i++)
248
Map printerAttributes = (Map) prAttr.get(i);
249
Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
250
PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
254
CupsPrintService cups = new CupsPrintService(uri.getURI(),
258
catch (IppException e)
260
// do nothing, we only catch the IppException which could be
261
// thrown during instantiation as single printers may be discovered
262
// correctly but not usable due to other security restrictions