DocPrintJobImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* DocPrintJobImpl.java -- Implementation of DocPrintJob.
  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.ipp;
  32. import gnu.javax.print.PrintFlavorException;
  33. import gnu.javax.print.ipp.attribute.job.JobId;
  34. import gnu.javax.print.ipp.attribute.job.JobUri;
  35. import gnu.javax.print.ipp.attribute.printer.DocumentFormat;
  36. import gnu.javax.print.ipp.attribute.supported.OperationsSupported;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import java.io.UnsupportedEncodingException;
  40. import java.net.URI;
  41. import java.net.URISyntaxException;
  42. import java.net.URL;
  43. import java.util.ArrayList;
  44. import java.util.HashSet;
  45. import java.util.Iterator;
  46. import java.util.List;
  47. import java.util.Map;
  48. import javax.print.CancelablePrintJob;
  49. import javax.print.Doc;
  50. import javax.print.DocFlavor;
  51. import javax.print.DocPrintJob;
  52. import javax.print.PrintException;
  53. import javax.print.PrintService;
  54. import javax.print.attribute.AttributeSetUtilities;
  55. import javax.print.attribute.DocAttributeSet;
  56. import javax.print.attribute.HashAttributeSet;
  57. import javax.print.attribute.HashPrintJobAttributeSet;
  58. import javax.print.attribute.PrintJobAttributeSet;
  59. import javax.print.attribute.PrintRequestAttributeSet;
  60. import javax.print.attribute.standard.JobName;
  61. import javax.print.attribute.standard.PrinterURI;
  62. import javax.print.attribute.standard.RequestingUserName;
  63. import javax.print.event.PrintJobAttributeListener;
  64. import javax.print.event.PrintJobEvent;
  65. import javax.print.event.PrintJobListener;
  66. /**
  67. * Implementation of the DocPrintJob interface. Implementation is
  68. * specific to the <code>IppPrintService</code> implementation.
  69. *
  70. * @author Wolfgang Baer (WBaer@gmx.de)
  71. */
  72. public class DocPrintJobImpl implements CancelablePrintJob
  73. {
  74. /** The print service this job is bound to. */
  75. private IppPrintService service;
  76. /** The set of print job listeners. */
  77. private HashSet printJobListener = new HashSet();
  78. /** The print job attributes listeners. */
  79. private ArrayList attributesListener = new ArrayList();
  80. /** The print job attributes listeners associated attribute set. */
  81. private ArrayList attributesListenerAttributes = new ArrayList();
  82. /** The username. */
  83. private String username;
  84. /** The password of the user. */
  85. private String password;
  86. /** Returned job uri. */
  87. private JobUri jobUri = null;
  88. /** Returned job id. */
  89. private JobId jobId = null;
  90. /** The requesting-username for later canceling */
  91. private RequestingUserName requestingUser;
  92. /** The print job sets. */
  93. private PrintJobAttributeSet oldSet = new HashPrintJobAttributeSet();
  94. private PrintJobAttributeSet currentSet = new HashPrintJobAttributeSet();
  95. /**
  96. * State variable if we already started printing.
  97. */
  98. private boolean printing = false;
  99. // TODO Implement complete PrintJobListener notification
  100. // TODO Implement PrintJobAttributeListener notification
  101. /**
  102. * Constructs a DocPrintJobImpl instance bound to the given print service.
  103. *
  104. * @param service the print service instance.
  105. * @param user the user of this print service.
  106. * @param passwd the password of the user.
  107. */
  108. public DocPrintJobImpl(IppPrintService service, String user, String passwd)
  109. {
  110. this.service = service;
  111. username = user;
  112. password = passwd;
  113. }
  114. /**
  115. * @see DocPrintJob#addPrintJobAttributeListener(PrintJobAttributeListener, PrintJobAttributeSet)
  116. */
  117. public void addPrintJobAttributeListener(PrintJobAttributeListener listener,
  118. PrintJobAttributeSet attributes)
  119. {
  120. if (listener == null)
  121. return;
  122. attributesListener.add(listener);
  123. attributesListenerAttributes.add(attributes);
  124. }
  125. /**
  126. * @see DocPrintJob#addPrintJobListener(PrintJobListener)
  127. */
  128. public void addPrintJobListener(PrintJobListener listener)
  129. {
  130. if (listener == null)
  131. return;
  132. printJobListener.add(listener);
  133. }
  134. /**
  135. * @see javax.print.DocPrintJob#getAttributes()
  136. */
  137. public PrintJobAttributeSet getAttributes()
  138. {
  139. return AttributeSetUtilities.unmodifiableView(currentSet);
  140. }
  141. /**
  142. * @see javax.print.DocPrintJob#getPrintService()
  143. */
  144. public PrintService getPrintService()
  145. {
  146. return service;
  147. }
  148. /**
  149. * @see DocPrintJob#print(Doc, PrintRequestAttributeSet)
  150. */
  151. public void print(Doc doc, PrintRequestAttributeSet attributes)
  152. throws PrintException
  153. {
  154. if (printing)
  155. throw new PrintException("already printing");
  156. printing = true;
  157. DocAttributeSet docAtts = doc.getAttributes();
  158. DocFlavor flavor = doc.getDocFlavor();
  159. if (flavor == null || (!service.isDocFlavorSupported(flavor)))
  160. {
  161. notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED));
  162. throw new PrintFlavorException("Invalid flavor", new DocFlavor[] {flavor});
  163. }
  164. // merge attributes as doc attributes take precendence
  165. // over the print request attributes
  166. HashAttributeSet mergedAtts = new HashAttributeSet();
  167. if (attributes != null)
  168. mergedAtts.addAll(attributes);
  169. if (docAtts != null)
  170. mergedAtts.addAll(docAtts);
  171. // check for requesting-user-name -add the
  172. // executing username if no other is specified
  173. // save user name so we can make a cancel operation under same user
  174. if (! mergedAtts.containsKey(RequestingUserName.class))
  175. {
  176. mergedAtts.add(IppPrintService.REQUESTING_USER_NAME);
  177. requestingUser = IppPrintService.REQUESTING_USER_NAME;
  178. }
  179. else
  180. {
  181. requestingUser = (RequestingUserName)
  182. mergedAtts.get(RequestingUserName.class);
  183. }
  184. // same for job-name
  185. if (! mergedAtts.containsKey(JobName.class))
  186. mergedAtts.add(IppPrintService.JOB_NAME);
  187. IppResponse response = null;
  188. try
  189. {
  190. PrinterURI printerUri = service.getPrinterURI();
  191. String printerUriStr = "http" + printerUri.toString().substring(3);
  192. URI uri = null;
  193. try
  194. {
  195. uri = new URI(printerUriStr);
  196. }
  197. catch (URISyntaxException e)
  198. {
  199. // does not happen
  200. }
  201. IppRequest request =
  202. new IppRequest(uri, username, password);
  203. request.setOperationID( (short) OperationsSupported.PRINT_JOB.getValue());
  204. request.setOperationAttributeDefaults();
  205. request.addOperationAttribute(printerUri);
  206. if (mergedAtts != null)
  207. {
  208. request.addAndFilterJobOperationAttributes(mergedAtts);
  209. request.addAndFilterJobTemplateAttributes(mergedAtts);
  210. }
  211. // DocFlavor getMimeType returns charset quoted
  212. DocumentFormat format = DocumentFormat.createDocumentFormat(flavor);
  213. request.addOperationAttribute(format);
  214. // Get and set the printdata based on the
  215. // representation classname
  216. String className = flavor.getRepresentationClassName();
  217. if (className.equals("[B"))
  218. {
  219. request.setData((byte[]) doc.getPrintData());
  220. response = request.send();
  221. }
  222. else if (className.equals("java.io.InputStream"))
  223. {
  224. InputStream stream = (InputStream) doc.getPrintData();
  225. request.setData(stream);
  226. response = request.send();
  227. stream.close();
  228. }
  229. else if (className.equals("[C"))
  230. {
  231. try
  232. {
  233. // CUPS only supports UTF-8 currently so we convert
  234. // We also assume that char[] is always utf-16 - correct ?
  235. String str = new String((char[]) doc.getPrintData());
  236. request.setData(str.getBytes("utf-16"));
  237. response = request.send();
  238. }
  239. catch (UnsupportedEncodingException e)
  240. {
  241. notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED));
  242. throw new PrintFlavorException("Invalid charset of flavor", e, new DocFlavor[] {flavor});
  243. }
  244. }
  245. else if (className.equals("java.io.Reader"))
  246. {
  247. try
  248. {
  249. // FIXME Implement
  250. // Convert a Reader into a InputStream properly encoded
  251. response = request.send();
  252. throw new UnsupportedEncodingException("not supported yet");
  253. }
  254. catch (UnsupportedEncodingException e)
  255. {
  256. notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED));
  257. throw new PrintFlavorException("Invalid charset of flavor", e, new DocFlavor[] {flavor});
  258. }
  259. }
  260. else if (className.equals("java.lang.String"))
  261. {
  262. try
  263. {
  264. // CUPS only supports UTF-8 currently so we convert
  265. // We also assume that String is always utf-16 - correct ?
  266. String str = (String) doc.getPrintData();
  267. request.setData(str.getBytes("utf-16"));
  268. response = request.send();
  269. }
  270. catch (UnsupportedEncodingException e)
  271. {
  272. notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED));
  273. throw new PrintFlavorException("Invalid charset of flavor", e, new DocFlavor[] {flavor});
  274. }
  275. }
  276. else if (className.equals("java.net.URL"))
  277. {
  278. URL url = (URL) doc.getPrintData();
  279. InputStream stream = url.openStream();
  280. request.setData(stream);
  281. response = request.send();
  282. stream.close();
  283. }
  284. else if (className.equals("java.awt.image.renderable.RenderableImage")
  285. || className.equals("java.awt.print.Printable")
  286. || className.equals("java.awt.print.Pageable"))
  287. {
  288. // For the future :-)
  289. throw new PrintException("Not yet supported.");
  290. }
  291. else
  292. {
  293. // should not happen - however
  294. notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED));
  295. throw new PrintFlavorException("Invalid flavor", new DocFlavor[] {flavor});
  296. }
  297. // at this point the data is transfered
  298. notifyPrintJobListeners(new PrintJobEvent(
  299. this, PrintJobEvent.DATA_TRANSFER_COMPLETE));
  300. }
  301. catch (IOException e)
  302. {
  303. throw new PrintException("IOException occured.", e);
  304. }
  305. int status = response.getStatusCode();
  306. if (! (status == IppStatusCode.SUCCESSFUL_OK
  307. || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES
  308. || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) )
  309. {
  310. notifyPrintJobListeners(new PrintJobEvent(
  311. this, PrintJobEvent.JOB_FAILED));
  312. throw new PrintException("Printing failed - received statuscode " + Integer.toHexString(status));
  313. // TODO maybe specific status codes may require to throw a specific
  314. // detailed attribute exception
  315. }
  316. else
  317. {
  318. // start print job progress monitoring thread
  319. // FIXME Implement
  320. // for now we just notify as finished
  321. notifyPrintJobListeners(
  322. new PrintJobEvent(this, PrintJobEvent.JOB_COMPLETE));
  323. }
  324. List jobAtts = response.getJobAttributes();
  325. // extract the uri and id of job for canceling and further monitoring
  326. Map jobAttributes = (Map) jobAtts.get(0);
  327. jobUri = (JobUri) ((HashSet)jobAttributes.get(JobUri.class)).toArray()[0];
  328. jobId = (JobId) ((HashSet)jobAttributes.get(JobId.class)).toArray()[0];
  329. }
  330. /**
  331. * @see DocPrintJob#removePrintJobAttributeListener(PrintJobAttributeListener)
  332. */
  333. public void removePrintJobAttributeListener(PrintJobAttributeListener listener)
  334. {
  335. if (listener == null)
  336. return;
  337. int index = attributesListener.indexOf(listener);
  338. if (index != -1)
  339. {
  340. attributesListener.remove(index);
  341. attributesListenerAttributes.remove(index);
  342. }
  343. }
  344. /**
  345. * @see DocPrintJob#removePrintJobListener(PrintJobListener)
  346. */
  347. public void removePrintJobListener(PrintJobListener listener)
  348. {
  349. if (listener == null)
  350. return;
  351. printJobListener.remove(listener);
  352. }
  353. /**
  354. * @see CancelablePrintJob#cancel()
  355. */
  356. public void cancel() throws PrintException
  357. {
  358. if (jobUri == null)
  359. {
  360. throw new PrintException("print job is not yet send");
  361. }
  362. IppResponse response = null;
  363. try
  364. {
  365. IppRequest request = new IppRequest(jobUri.getURI(), username, password);
  366. request.setOperationID( (short) OperationsSupported.CANCEL_JOB.getValue());
  367. request.setOperationAttributeDefaults();
  368. request.addOperationAttribute(jobUri);
  369. request.addOperationAttribute(requestingUser);
  370. response = request.send();
  371. }
  372. catch (IOException e)
  373. {
  374. throw new IppException("IOException occured during cancel request.", e);
  375. }
  376. int status = response.getStatusCode();
  377. if (! (status == IppStatusCode.SUCCESSFUL_OK
  378. || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES
  379. || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) )
  380. {
  381. notifyPrintJobListeners(new PrintJobEvent(
  382. this, PrintJobEvent.JOB_FAILED));
  383. throw new PrintException("Canceling failed - received statuscode " + Integer.toHexString(status));
  384. }
  385. else
  386. {
  387. notifyPrintJobListeners(new PrintJobEvent(
  388. this, PrintJobEvent.JOB_CANCELED));
  389. }
  390. }
  391. private void notifyPrintJobListeners(PrintJobEvent e)
  392. {
  393. Iterator it = printJobListener.iterator();
  394. while (it.hasNext())
  395. {
  396. PrintJobListener l = (PrintJobListener) it.next();
  397. if (e.getPrintEventType() == PrintJobEvent.DATA_TRANSFER_COMPLETE)
  398. l.printDataTransferCompleted(e);
  399. else if (e.getPrintEventType() == PrintJobEvent.JOB_CANCELED)
  400. l.printJobCanceled(e);
  401. else if (e.getPrintEventType() == PrintJobEvent.JOB_COMPLETE)
  402. l.printJobCompleted(e);
  403. else if (e.getPrintEventType() == PrintJobEvent.JOB_FAILED)
  404. l.printJobFailed(e);
  405. else if (e.getPrintEventType() == PrintJobEvent.NO_MORE_EVENTS)
  406. l.printJobNoMoreEvents(e);
  407. else
  408. l.printJobRequiresAttention(e);
  409. }
  410. }
  411. }