~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to gnu/javax/print/CupsServer.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* CupsServer.java -- 
 
2
   Copyright (C) 2006 Free Software Foundation, Inc.
 
3
 
 
4
This file is part of GNU Classpath.
 
5
 
 
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)
 
9
any later version.
 
10
 
 
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.
 
15
 
 
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
 
19
02110-1301 USA.
 
20
 
 
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
 
24
combination.
 
25
 
 
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. */
 
37
 
 
38
 
 
39
package gnu.javax.print;
 
40
 
 
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;
 
47
 
 
48
import java.io.IOException;
 
49
import java.net.URI;
 
50
import java.net.URISyntaxException;
 
51
import java.util.ArrayList;
 
52
import java.util.List;
 
53
import java.util.Map;
 
54
import java.util.Set;
 
55
 
 
56
/**
 
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.
 
60
 * <p>
 
61
 * It provides methods for retrival of valid CUPS printer uris 
 
62
 * that are used to construct IppPrintService objects.
 
63
 * </p>
 
64
 * 
 
65
 * @author Wolfgang Baer (WBaer@gmx.de)
 
66
 */
 
67
public class CupsServer
 
68
{
 
69
  /** 
 
70
   * The URI of the CUPS server.
 
71
   * This is something like: http://localhost:631
 
72
   */
 
73
  private transient URI uri;
 
74
  
 
75
  /**
 
76
   * The optional username.
 
77
   */
 
78
  private transient String username;
 
79
  
 
80
  /**
 
81
   * The optional password for the user.
 
82
   */
 
83
  private transient String password;
 
84
 
 
85
  /**
 
86
   * Creates a <code>CupsServer</code> object which
 
87
   * tries to connect to the cups server on localhost.
 
88
   * 
 
89
   * @param username the username
 
90
   * @param password the password for the username.
 
91
   */
 
92
  public CupsServer(String username, String password)
 
93
  {
 
94
    try
 
95
      {
 
96
        this.uri = new URI("http://localhost:631");
 
97
      }
 
98
    catch (URISyntaxException e)
 
99
      {
 
100
        // does not happen
 
101
      }
 
102
    
 
103
    this.username = username;
 
104
    this.password = password;
 
105
  }
 
106
 
 
107
  /**
 
108
   * Creates a <code>CupsServer</code> object which
 
109
   * tries to connect to a running cups server on the
 
110
   * given URI.
 
111
   * 
 
112
   * @param uri the URI of the server.
 
113
   * @param username the username
 
114
   * @param password the password for the username.
 
115
   */
 
116
  public CupsServer(URI uri, String username, String password)
 
117
  {
 
118
    this.uri = uri;
 
119
    this.username = username;
 
120
    this.password = password;
 
121
  }
 
122
  
 
123
  /**
 
124
   * Requests the default printer from this CUPS server.
 
125
   * This is always returned as IppPrintService.
 
126
   * 
 
127
   * @return The default printer.
 
128
   * @throws IppException if problems during request/response processing occur.
 
129
   */
 
130
  public IppPrintService getDefaultPrinter() throws IppException
 
131
  {   
 
132
    IppResponse response = null;
 
133
   
 
134
    try
 
135
      {
 
136
        IppRequest request = new IppRequest(uri, username, password);    
 
137
        request.setOperationID((short)CupsIppOperation.CUPS_GET_DEFAULT);        
 
138
        request.setOperationAttributeDefaults();    
 
139
        
 
140
        RequestedAttributes requestedAttrs 
 
141
         = new RequestedAttributes("printer-uri-supported");
 
142
        request.addOperationAttribute(requestedAttrs);
 
143
        
 
144
        response = request.send();
 
145
      }   
 
146
    catch (IOException e)
 
147
      {
 
148
        throw new IppException("IOException in IPP request/response.", e);
 
149
      }    
 
150
        
 
151
    Map printerAttributes = (Map) response.getPrinterAttributes().get(0);
 
152
    Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
 
153
    PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
 
154
    
 
155
    IppPrintService service 
 
156
      = new CupsPrintService(uri.getURI(), username, password);
 
157
    
 
158
    return service;
 
159
  }
 
160
  
 
161
  /**
 
162
   * Requests all printers from this CUPS server.
 
163
   * 
 
164
   * @return The list of available printers.
 
165
   * @throws IppException if problems during request/response processing occur.
 
166
   */
 
167
  public List getAllPrinters() throws IppException
 
168
  {   
 
169
    IppResponse response = null;
 
170
   
 
171
    try
 
172
      {
 
173
        IppRequest request = new IppRequest(uri, username, password);    
 
174
        request.setOperationID((short)CupsIppOperation.CUPS_GET_PRINTERS);        
 
175
        request.setOperationAttributeDefaults();
 
176
        
 
177
        RequestedAttributes requestedAttrs 
 
178
          = new RequestedAttributes("printer-uri-supported");
 
179
        request.addOperationAttribute(requestedAttrs);
 
180
        
 
181
        response = request.send();
 
182
      }   
 
183
    catch (IOException e)
 
184
      {
 
185
        throw new IppException("IOException in IPP request/response.", e);
 
186
      }    
 
187
 
 
188
    List prAttr = response.getPrinterAttributes();
 
189
    List services = new ArrayList();
 
190
    
 
191
    for (int i=0; i < prAttr.size(); i++)
 
192
      {
 
193
        Map printerAttributes = (Map) prAttr.get(i);
 
194
        Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
 
195
        PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
 
196
        
 
197
        try
 
198
          {
 
199
            CupsPrintService cups = new CupsPrintService(uri.getURI(),
 
200
                                                         username, password);
 
201
            services.add(cups);
 
202
          }
 
203
        catch (IppException e)
 
204
          {
 
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
 
208
          }       
 
209
      }    
 
210
                     
 
211
    return services;
 
212
  }
 
213
  
 
214
  /**
 
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.
 
218
   * 
 
219
   * @return The list of available classes.
 
220
   * @throws IppException if problems during request/response processing occur.
 
221
   */
 
222
  public List getAllClasses() throws IppException
 
223
  {   
 
224
    IppResponse response = null;
 
225
   
 
226
    try
 
227
      {
 
228
        IppRequest request = new IppRequest(uri, username, password);    
 
229
        request.setOperationID((short)CupsIppOperation.CUPS_GET_CLASSES);        
 
230
        request.setOperationAttributeDefaults();
 
231
        
 
232
        RequestedAttributes requestedAttrs 
 
233
          = new RequestedAttributes("printer-uri-supported");
 
234
        request.addOperationAttribute(requestedAttrs);
 
235
        
 
236
        response = request.send();
 
237
      }   
 
238
    catch (IOException e)
 
239
      {
 
240
        throw new IppException("IOException in IPP request/response.", e);
 
241
      }    
 
242
    
 
243
    List prAttr = response.getPrinterAttributes();
 
244
    List services = new ArrayList();   
 
245
    
 
246
    for (int i=0; i < prAttr.size(); i++)
 
247
      {
 
248
        Map printerAttributes = (Map) prAttr.get(i);
 
249
        Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
 
250
        PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
 
251
        
 
252
        try
 
253
          {
 
254
            CupsPrintService cups = new CupsPrintService(uri.getURI(),
 
255
                                                         username, password);
 
256
            services.add(cups);
 
257
          }
 
258
        catch (IppException e)
 
259
          {
 
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
 
263
          }        
 
264
      }    
 
265
                     
 
266
    return services;
 
267
  }
 
268
 
 
269
}