📄 docprintjobimpl.java
字号:
// does not happen } IppRequest request = new IppRequest(uri, username, password); request.setOperationID( (short) OperationsSupported.PRINT_JOB.getValue()); request.setOperationAttributeDefaults(); request.addOperationAttribute(printerUri); if (mergedAtts != null) { request.addAndFilterJobOperationAttributes(mergedAtts); request.addAndFilterJobTemplateAttributes(mergedAtts); } // DocFlavor getMimeType returns charset quoted DocumentFormat format = DocumentFormat.createDocumentFormat(flavor); request.addOperationAttribute(format); // Get and set the printdata based on the // representation classname String className = flavor.getRepresentationClassName(); if (className.equals("[B")) { request.setData((byte[]) doc.getPrintData()); response = request.send(); } else if (className.equals("java.io.InputStream")) { InputStream stream = (InputStream) doc.getPrintData(); request.setData(stream); response = request.send(); stream.close(); } else if (className.equals("[C")) { try { // CUPS only supports UTF-8 currently so we convert // We also assume that char[] is always utf-16 - correct ? String str = new String((char[]) doc.getPrintData()); request.setData(str.getBytes("utf-16")); response = request.send(); } catch (UnsupportedEncodingException e) { notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED)); throw new PrintFlavorException("Invalid charset of flavor", e, new DocFlavor[] {flavor}); } } else if (className.equals("java.io.Reader")) { try { // FIXME Implement // Convert a Reader into a InputStream properly encoded response = request.send(); throw new UnsupportedEncodingException("not supported yet"); } catch (UnsupportedEncodingException e) { notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED)); throw new PrintFlavorException("Invalid charset of flavor", e, new DocFlavor[] {flavor}); } } else if (className.equals("java.lang.String")) { try { // CUPS only supports UTF-8 currently so we convert // We also assume that String is always utf-16 - correct ? String str = (String) doc.getPrintData(); request.setData(str.getBytes("utf-16")); response = request.send(); } catch (UnsupportedEncodingException e) { notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED)); throw new PrintFlavorException("Invalid charset of flavor", e, new DocFlavor[] {flavor}); } } else if (className.equals("java.net.URL")) { URL url = (URL) doc.getPrintData(); InputStream stream = url.openStream(); request.setData(stream); response = request.send(); stream.close(); } else if (className.equals("java.awt.image.renderable.RenderableImage") || className.equals("java.awt.print.Printable") || className.equals("java.awt.print.Pageable")) { // For the future :-) throw new PrintException("Not yet supported."); } else { // should not happen - however notifyPrintJobListeners(new PrintJobEvent(this, PrintJobEvent.JOB_FAILED)); throw new PrintFlavorException("Invalid flavor", new DocFlavor[] {flavor}); } // at this point the data is transfered notifyPrintJobListeners(new PrintJobEvent( this, PrintJobEvent.DATA_TRANSFER_COMPLETE)); } catch (IOException e) { throw new PrintException("IOException occured.", e); } int status = response.getStatusCode(); if (! (status == IppStatusCode.SUCCESSFUL_OK || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) ) { notifyPrintJobListeners(new PrintJobEvent( this, PrintJobEvent.JOB_FAILED)); throw new PrintException("Printing failed - received statuscode " + Integer.toHexString(status)); // TODO maybe specific status codes may require to throw a specific // detailed attribute exception } else { // start print job progress monitoring thread // FIXME Implement // for now we just notify as finished notifyPrintJobListeners( new PrintJobEvent(this, PrintJobEvent.JOB_COMPLETE)); } List jobAtts = response.getJobAttributes(); // extract the uri and id of job for canceling and further monitoring Map jobAttributes = (Map) jobAtts.get(0); jobUri = (JobUri) ((HashSet)jobAttributes.get(JobUri.class)).toArray()[0]; jobId = (JobId) ((HashSet)jobAttributes.get(JobId.class)).toArray()[0]; } /** * @see DocPrintJob#removePrintJobAttributeListener(PrintJobAttributeListener) */ public void removePrintJobAttributeListener(PrintJobAttributeListener listener) { if (listener == null) return; int index = attributesListener.indexOf(listener); if (index != -1) { attributesListener.remove(index); attributesListenerAttributes.remove(index); } } /** * @see DocPrintJob#removePrintJobListener(PrintJobListener) */ public void removePrintJobListener(PrintJobListener listener) { if (listener == null) return; printJobListener.remove(listener); } /** * @see CancelablePrintJob#cancel() */ public void cancel() throws PrintException { if (jobUri == null) { throw new PrintException("print job is not yet send"); } IppResponse response = null; try { IppRequest request = new IppRequest(jobUri.getURI(), username, password); request.setOperationID( (short) OperationsSupported.CANCEL_JOB.getValue()); request.setOperationAttributeDefaults(); request.addOperationAttribute(jobUri); request.addOperationAttribute(requestingUser); response = request.send(); } catch (IOException e) { throw new IppException("IOException occured during cancel request.", e); } int status = response.getStatusCode(); if (! (status == IppStatusCode.SUCCESSFUL_OK || status == IppStatusCode.SUCCESSFUL_OK_IGNORED_OR_SUBSTITUED_ATTRIBUTES || status == IppStatusCode.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES) ) { notifyPrintJobListeners(new PrintJobEvent( this, PrintJobEvent.JOB_FAILED)); throw new PrintException("Canceling failed - received statuscode " + Integer.toHexString(status)); } else { notifyPrintJobListeners(new PrintJobEvent( this, PrintJobEvent.JOB_CANCELED)); } } private void notifyPrintJobListeners(PrintJobEvent e) { Iterator it = printJobListener.iterator(); while (it.hasNext()) { PrintJobListener l = (PrintJobListener) it.next(); if (e.getPrintEventType() == PrintJobEvent.DATA_TRANSFER_COMPLETE) l.printDataTransferCompleted(e); else if (e.getPrintEventType() == PrintJobEvent.JOB_CANCELED) l.printJobCanceled(e); else if (e.getPrintEventType() == PrintJobEvent.JOB_COMPLETE) l.printJobCompleted(e); else if (e.getPrintEventType() == PrintJobEvent.JOB_FAILED) l.printJobFailed(e); else if (e.getPrintEventType() == PrintJobEvent.NO_MORE_EVENTS) l.printJobNoMoreEvents(e); else l.printJobRequiresAttention(e); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -