Search in sources :

Example 11 with PrintException

use of javax.print.PrintException in project camel by apache.

the class PrinterProducer method assignPrintAttributes.

private PrintRequestAttributeSet assignPrintAttributes() throws PrintException {
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    if (config.getCopies() >= 1) {
        printRequestAttributeSet.add(new Copies(config.getCopies()));
    } else {
        throw new PrintException("Number of print copies should be greater than zero");
    }
    printRequestAttributeSet.add(config.getMediaSizeName());
    printRequestAttributeSet.add(config.getInternalSides());
    printRequestAttributeSet.add(config.getInternalOrientation());
    if (config.getMediaTray() != null) {
        MediaTray mediaTray = resolveMediaTray(config.getMediaTray());
        if (mediaTray == null) {
            throw new PrintException("mediatray not found " + config.getMediaTray());
        }
        printRequestAttributeSet.add(mediaTray);
    }
    return printRequestAttributeSet;
}
Also used : PrintException(javax.print.PrintException) Copies(javax.print.attribute.standard.Copies) MediaTray(javax.print.attribute.standard.MediaTray) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 12 with PrintException

use of javax.print.PrintException in project jdk8u_jdk by JetBrains.

the class RasterPrinterJob method spoolToService.

/*
     * Services we don't recognize as built-in services can't be
     * implemented as subclasses of PrinterJob, therefore we create
     * a DocPrintJob from their service and pass a Doc representing
     * the application's printjob
     */
// MacOSX - made protected so subclasses can reference it.
protected void spoolToService(PrintService psvc, PrintRequestAttributeSet attributes) throws PrinterException {
    if (psvc == null) {
        throw new PrinterException("No print service found.");
    }
    DocPrintJob job = psvc.createPrintJob();
    Doc doc = new PageableDoc(getPageable());
    if (attributes == null) {
        attributes = new HashPrintRequestAttributeSet();
    }
    try {
        job.print(doc, attributes);
    } catch (PrintException e) {
        throw new PrinterException(e.toString());
    }
}
Also used : PrintException(javax.print.PrintException) Doc(javax.print.Doc) PageableDoc(sun.print.PageableDoc) PrinterException(java.awt.print.PrinterException) DocPrintJob(javax.print.DocPrintJob) PageableDoc(sun.print.PageableDoc) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 13 with PrintException

use of javax.print.PrintException in project jdk8u_jdk by JetBrains.

the class Win32PrintJob method print.

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
    synchronized (this) {
        if (printing) {
            throw new PrintException("already printing");
        } else {
            printing = true;
        }
    }
    PrinterState prnState = (PrinterState) service.getAttribute(PrinterState.class);
    if (prnState == PrinterState.STOPPED) {
        PrinterStateReasons prnStateReasons = (PrinterStateReasons) service.getAttribute(PrinterStateReasons.class);
        if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) {
            throw new PrintException("PrintService is no longer available.");
        }
    }
    if ((PrinterIsAcceptingJobs) (service.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
        throw new PrintException("Printer is not accepting job.");
    }
    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;
    try {
        data = doc.getPrintData();
    } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("can't get print data: " + e.toString());
    }
    if (data == null) {
        throw new PrintException("Null print data.");
    }
    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintJobFlavorException("invalid flavor", flavor);
    }
    initializeAttributeSets(doc, attributes);
    getAttributeValues(flavor);
    String repClassName = flavor.getRepresentationClassName();
    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
        try {
            instream = doc.getStreamForBytes();
            if (instream == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No stream for data");
            }
            printableJob(new ImagePrinter(instream));
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
        try {
            printableJob(new ImagePrinter((URL) data));
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
        try {
            pageableJob((Pageable) doc.getPrintData());
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("java.awt.print.Printable")) {
        try {
            printableJob((Printable) doc.getPrintData());
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("[B") || repClassName.equals("java.io.InputStream") || repClassName.equals("java.net.URL")) {
        if (repClassName.equals("java.net.URL")) {
            URL url = (URL) data;
            try {
                instream = url.openStream();
            } catch (IOException e) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(e.toString());
            }
        } else {
            try {
                instream = doc.getStreamForBytes();
            } catch (IOException ioe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe.toString());
            }
        }
        if (instream == null) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("No stream for data");
        }
        if (mDestination != null) {
            // if destination attribute is set
            try {
                FileOutputStream fos = new FileOutputStream(mDestination);
                byte[] buffer = new byte[1024];
                int cread;
                while ((cread = instream.read(buffer, 0, buffer.length)) >= 0) {
                    fos.write(buffer, 0, cread);
                }
                fos.flush();
                fos.close();
            } catch (FileNotFoundException fnfe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(fnfe.toString());
            } catch (IOException ioe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe.toString());
            }
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
            notifyEvent(PrintJobEvent.JOB_COMPLETE);
            service.wakeNotifier();
            return;
        }
        if (!startPrintRawData(service.getName(), jobName)) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("Print job failed to start.");
        }
        BufferedInputStream bin = new BufferedInputStream(instream);
        int bread = 0;
        try {
            byte[] buffer = new byte[PRINTBUFFERLEN];
            while ((bread = bin.read(buffer, 0, PRINTBUFFERLEN)) >= 0) {
                if (!printRawData(buffer, bread)) {
                    bin.close();
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException("Problem while spooling data");
                }
            }
            bin.close();
            if (!endPrintRawData()) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("Print job failed to close properly.");
            }
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e.toString());
        } finally {
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
        }
    } else {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("unrecognized class: " + repClassName);
    }
    service.wakeNotifier();
}
Also used : PrinterState(javax.print.attribute.standard.PrinterState) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URL(java.net.URL) java.awt.print(java.awt.print) PrintException(javax.print.PrintException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) PrinterStateReasons(javax.print.attribute.standard.PrinterStateReasons) DocFlavor(javax.print.DocFlavor) PrinterIsAcceptingJobs(javax.print.attribute.standard.PrinterIsAcceptingJobs)

Example 14 with PrintException

use of javax.print.PrintException in project jdk8u_jdk by JetBrains.

the class UnixPrintJob method getAttributeValues.

private void getAttributeValues(DocFlavor flavor) throws PrintException {
    Attribute attr;
    Class category;
    if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
        fidelity = true;
    } else {
        fidelity = false;
    }
    Attribute[] attrs = reqAttrSet.toArray();
    for (int i = 0; i < attrs.length; i++) {
        attr = attrs[i];
        category = attr.getCategory();
        if (fidelity == true) {
            if (!service.isAttributeCategorySupported(category)) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintJobAttributeException("unsupported category: " + category, category, null);
            } else if (!service.isAttributeValueSupported(attr, flavor, null)) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintJobAttributeException("unsupported attribute: " + attr, null, attr);
            }
        }
        if (category == Destination.class) {
            URI uri = ((Destination) attr).getURI();
            if (!"file".equals(uri.getScheme())) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("Not a file: URI");
            } else {
                try {
                    mDestType = DESTFILE;
                    mDestination = (new File(uri)).getPath();
                } catch (Exception e) {
                    throw new PrintException(e);
                }
                // check write access
                SecurityManager security = System.getSecurityManager();
                if (security != null) {
                    try {
                        security.checkWrite(mDestination);
                    } catch (SecurityException se) {
                        notifyEvent(PrintJobEvent.JOB_FAILED);
                        throw new PrintException(se);
                    }
                }
            }
        } else if (category == JobSheets.class) {
            if ((JobSheets) attr == JobSheets.NONE) {
                mNoJobSheet = true;
            }
        } else if (category == JobName.class) {
            jobName = ((JobName) attr).getValue();
        } else if (category == Copies.class) {
            copies = ((Copies) attr).getValue();
        } else if (category == Media.class) {
            if (attr instanceof MediaSizeName) {
                mediaName = (MediaSizeName) attr;
                IPPPrintService.debug_println(debugPrefix + "mediaName " + mediaName);
                if (!service.isAttributeValueSupported(attr, null, null)) {
                    mediaSize = MediaSize.getMediaSizeForName(mediaName);
                }
            } else if (attr instanceof CustomMediaTray) {
                customTray = (CustomMediaTray) attr;
            }
        } else if (category == OrientationRequested.class) {
            orient = (OrientationRequested) attr;
        } else if (category == NumberUp.class) {
            nUp = (NumberUp) attr;
        } else if (category == Sides.class) {
            sides = (Sides) attr;
        }
    }
}
Also used : Fidelity(javax.print.attribute.standard.Fidelity) Destination(javax.print.attribute.standard.Destination) PrintJobAttribute(javax.print.attribute.PrintJobAttribute) PrintRequestAttribute(javax.print.attribute.PrintRequestAttribute) Attribute(javax.print.attribute.Attribute) MediaSizeName(javax.print.attribute.standard.MediaSizeName) JobSheets(javax.print.attribute.standard.JobSheets) URI(java.net.URI) java.awt.print(java.awt.print) PrintException(javax.print.PrintException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) OrientationRequested(javax.print.attribute.standard.OrientationRequested) PrintException(javax.print.PrintException) Copies(javax.print.attribute.standard.Copies) File(java.io.File) Sides(javax.print.attribute.standard.Sides)

Example 15 with PrintException

use of javax.print.PrintException in project jdk8u_jdk by JetBrains.

the class UnixPrintJob method printableJob.

public void printableJob(Printable printable) throws PrintException {
    try {
        synchronized (this) {
            if (job != null) {
                // shouldn't happen
                throw new PrintException("already printing");
            } else {
                job = new PSPrinterJob();
            }
        }
        job.setPrintService(getPrintService());
        job.setCopies(copies);
        job.setJobName(jobName);
        PageFormat pf = new PageFormat();
        if (mediaSize != null) {
            Paper p = new Paper();
            p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0);
            p.setImageableArea(72.0, 72.0, p.getWidth() - 144.0, p.getHeight() - 144.0);
            pf.setPaper(p);
        }
        if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
            pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
        } else if (orient == OrientationRequested.LANDSCAPE) {
            pf.setOrientation(PageFormat.LANDSCAPE);
        }
        job.setPrintable(printable, pf);
        job.print(reqAttrSet);
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        return;
    } catch (PrinterException pe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(pe);
    } finally {
        printReturned = true;
        notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
    }
}
Also used : PrintException(javax.print.PrintException)

Aggregations

PrintException (javax.print.PrintException)19 IOException (java.io.IOException)7 PrintService (javax.print.PrintService)6 java.awt.print (java.awt.print)4 DocFlavor (javax.print.DocFlavor)4 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)4 Copies (javax.print.attribute.standard.Copies)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 DocPrintJob (javax.print.DocPrintJob)3 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 URI (java.net.URI)2 Doc (javax.print.Doc)2 SimpleDoc (javax.print.SimpleDoc)2 Attribute (javax.print.attribute.Attribute)2