CupsServer.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* CupsServer.java --
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.print;
  32. import gnu.javax.print.ipp.IppException;
  33. import gnu.javax.print.ipp.IppPrintService;
  34. import gnu.javax.print.ipp.IppRequest;
  35. import gnu.javax.print.ipp.IppResponse;
  36. import gnu.javax.print.ipp.attribute.RequestedAttributes;
  37. import gnu.javax.print.ipp.attribute.supported.PrinterUriSupported;
  38. import java.io.IOException;
  39. import java.net.URI;
  40. import java.net.URISyntaxException;
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import java.util.Map;
  44. import java.util.Set;
  45. /**
  46. * <code>CupsServer</code> represents a host running a cups
  47. * compatible server. It mainly consists of its URI and optional
  48. * user and password combination if access is restricted.
  49. * <p>
  50. * It provides methods for retrival of valid CUPS printer uris
  51. * that are used to construct IppPrintService objects.
  52. * </p>
  53. *
  54. * @author Wolfgang Baer (WBaer@gmx.de)
  55. */
  56. public class CupsServer
  57. {
  58. /**
  59. * The URI of the CUPS server.
  60. * This is something like: http://localhost:631
  61. */
  62. private transient URI uri;
  63. /**
  64. * The optional username.
  65. */
  66. private transient String username;
  67. /**
  68. * The optional password for the user.
  69. */
  70. private transient String password;
  71. /**
  72. * Creates a <code>CupsServer</code> object which
  73. * tries to connect to a cups server.
  74. *
  75. * If <code>gnu.javax.print.server</code> is explicitly set, then
  76. * that hostname will be used. Otherwise it will default to localhost.
  77. *
  78. * @param username the username
  79. * @param password the password for the username.
  80. */
  81. public CupsServer(String username, String password)
  82. {
  83. this.username = username;
  84. this.password = password;
  85. this.uri = null;
  86. try
  87. {
  88. String serv = System.getProperty("gnu.javax.print.server");
  89. if( serv != null )
  90. this.uri = new URI("http://"+serv+":631");
  91. }
  92. catch(URISyntaxException use)
  93. {
  94. throw new RuntimeException("gnu.javax.print.CupsServer value is not a valid hostname.");
  95. }
  96. catch(SecurityException se)
  97. {
  98. }
  99. try
  100. {
  101. if( this.uri == null )
  102. this.uri = new URI("http://localhost:631");
  103. }
  104. catch (URISyntaxException e)
  105. {
  106. // does not happen
  107. }
  108. }
  109. /**
  110. * Creates a <code>CupsServer</code> object which
  111. * tries to connect to a running cups server on the
  112. * given URI.
  113. *
  114. * @param uri the URI of the server.
  115. * @param username the username
  116. * @param password the password for the username.
  117. */
  118. public CupsServer(URI uri, String username, String password)
  119. {
  120. this.uri = uri;
  121. this.username = username;
  122. this.password = password;
  123. }
  124. /**
  125. * Requests the default printer from this CUPS server.
  126. * This is always returned as IppPrintService.
  127. *
  128. * @return The default printer.
  129. * @throws IppException if problems during request/response processing occur.
  130. */
  131. public IppPrintService getDefaultPrinter() throws IppException
  132. {
  133. IppResponse response = null;
  134. try
  135. {
  136. IppRequest request = new IppRequest(uri, username, password);
  137. request.setOperationID((short)CupsIppOperation.CUPS_GET_DEFAULT);
  138. request.setOperationAttributeDefaults();
  139. RequestedAttributes requestedAttrs
  140. = new RequestedAttributes("printer-uri-supported");
  141. request.addOperationAttribute(requestedAttrs);
  142. response = request.send();
  143. }
  144. catch (IOException e)
  145. {
  146. throw new IppException("IOException in IPP request/response.", e);
  147. }
  148. Map printerAttributes = (Map) response.getPrinterAttributes().get(0);
  149. Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
  150. PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
  151. IppPrintService service
  152. = new CupsPrintService(uri.getURI(), username, password);
  153. return service;
  154. }
  155. /**
  156. * Requests all printers from this CUPS server.
  157. *
  158. * @return The list of available printers.
  159. * @throws IppException if problems during request/response processing occur.
  160. */
  161. public List getAllPrinters() throws IppException
  162. {
  163. IppResponse response = null;
  164. try
  165. {
  166. IppRequest request = new IppRequest(uri, username, password);
  167. request.setOperationID((short)CupsIppOperation.CUPS_GET_PRINTERS);
  168. request.setOperationAttributeDefaults();
  169. RequestedAttributes requestedAttrs
  170. = new RequestedAttributes("printer-uri-supported");
  171. request.addOperationAttribute(requestedAttrs);
  172. response = request.send();
  173. }
  174. catch (IOException e)
  175. {
  176. throw new IppException("IOException in IPP request/response.", e);
  177. }
  178. List prAttr = response.getPrinterAttributes();
  179. List services = new ArrayList();
  180. for (int i=0; i < prAttr.size(); i++)
  181. {
  182. Map printerAttributes = (Map) prAttr.get(i);
  183. Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
  184. PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
  185. try
  186. {
  187. CupsPrintService cups = new CupsPrintService(uri.getURI(),
  188. username, password);
  189. services.add(cups);
  190. }
  191. catch (IppException e)
  192. {
  193. // do nothing, we only catch the IppException which could be
  194. // thrown during instantiation as single printers may be discovered
  195. // correctly but not usable due to other security restrictions
  196. }
  197. }
  198. return services;
  199. }
  200. /**
  201. * Requests all classes from this CUPS server. Classes in cups are
  202. * collections of printers. This means jobs directed to a class
  203. * are forwarded to the first available printer of the collection.
  204. *
  205. * @return The list of available classes.
  206. * @throws IppException if problems during request/response processing occur.
  207. */
  208. public List getAllClasses() throws IppException
  209. {
  210. IppResponse response = null;
  211. try
  212. {
  213. IppRequest request = new IppRequest(uri, username, password);
  214. request.setOperationID((short)CupsIppOperation.CUPS_GET_CLASSES);
  215. request.setOperationAttributeDefaults();
  216. RequestedAttributes requestedAttrs
  217. = new RequestedAttributes("printer-uri-supported");
  218. request.addOperationAttribute(requestedAttrs);
  219. response = request.send();
  220. }
  221. catch (IOException e)
  222. {
  223. throw new IppException("IOException in IPP request/response.", e);
  224. }
  225. List prAttr = response.getPrinterAttributes();
  226. List services = new ArrayList();
  227. for (int i=0; i < prAttr.size(); i++)
  228. {
  229. Map printerAttributes = (Map) prAttr.get(i);
  230. Set uris = (Set) printerAttributes.get(PrinterUriSupported.class);
  231. PrinterUriSupported uri = (PrinterUriSupported) uris.toArray()[0];
  232. try
  233. {
  234. CupsPrintService cups = new CupsPrintService(uri.getURI(),
  235. username, password);
  236. services.add(cups);
  237. }
  238. catch (IppException e)
  239. {
  240. // do nothing, we only catch the IppException which could be
  241. // thrown during instantiation as single printers may be discovered
  242. // correctly but not usable due to other security restrictions
  243. }
  244. }
  245. return services;
  246. }
  247. }