Search in sources :

Example 1 with PrintException

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

the class UnixPrintJob method pageableJob.

public void pageableJob(Pageable pageable) 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);
        job.setPageable(pageable);
        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)

Example 2 with PrintException

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

the class UnixPrintJob method print.

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
    synchronized (this) {
        if (printing) {
            throw new PrintException("already printing");
        } else {
            printing = true;
        }
    }
    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);
    // set up mOptions
    if ((service instanceof IPPPrintService) && CUPSPrinter.isCupsRunning()) {
        IPPPrintService.debug_println(debugPrefix + "instanceof IPPPrintService");
        if (mediaName != null) {
            CustomMediaSizeName customMedia = ((IPPPrintService) service).findCustomMedia(mediaName);
            if (customMedia != null) {
                mOptions = " media=" + customMedia.getChoiceName();
            }
        }
        if (customTray != null && customTray instanceof CustomMediaTray) {
            String choice = customTray.getChoiceName();
            if (choice != null) {
                mOptions += " media=" + choice;
            }
        }
        if (nUp != null) {
            mOptions += " number-up=" + nUp.getValue();
        }
        if (orient != OrientationRequested.PORTRAIT && (flavor != null) && !flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE)) {
            mOptions += " orientation-requested=" + orient.getValue();
        }
        if (sides != null) {
            mOptions += " sides=" + sides;
        }
    }
    IPPPrintService.debug_println(debugPrefix + "mOptions " + mOptions);
    String repClassName = flavor.getRepresentationClassName();
    String val = flavor.getParameter("charset");
    String encoding = "us-ascii";
    if (val != null && !val.equals("")) {
        encoding = val;
    }
    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");
            }
            if (!(service instanceof IPPPrintService && ((IPPPrintService) service).isIPPSupportedImages(flavor.getMimeType()))) {
                printableJob(new ImagePrinter(instream));
                ((UnixPrintService) 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 {
            URL url = (URL) data;
            if ((service instanceof IPPPrintService) && ((IPPPrintService) service).isIPPSupportedImages(flavor.getMimeType())) {
                instream = url.openStream();
            } else {
                printableJob(new ImagePrinter(url));
                ((UnixPrintService) service).wakeNotifier();
                return;
            }
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e.toString());
        }
    } else if (flavor.equals(DocFlavor.CHAR_ARRAY.TEXT_PLAIN) || flavor.equals(DocFlavor.READER.TEXT_PLAIN) || flavor.equals(DocFlavor.STRING.TEXT_PLAIN)) {
        try {
            reader = doc.getReaderForText();
            if (reader == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No reader for data");
            }
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe.toString());
        }
    } else if (repClassName.equals("[B") || repClassName.equals("java.io.InputStream")) {
        try {
            instream = doc.getStreamForBytes();
            if (instream == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No stream for data");
            }
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe.toString());
        }
    } else if (repClassName.equals("java.net.URL")) {
        /*
             * This extracts the data from the URL and passes it the content
             * directly to the print service as a file.
             * This is appropriate for the current implementation where lp or
             * lpr is always used to spool the data. We expect to revise the
             * implementation to provide more complete IPP support (ie not just
             * CUPS) and at that time the job will be spooled via IPP
             * and the URL
             * itself should be sent to the IPP print service not the content.
             */
        URL url = (URL) data;
        try {
            instream = url.openStream();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e.toString());
        }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
        try {
            pageableJob((Pageable) doc.getPrintData());
            if (service instanceof IPPPrintService) {
                ((IPPPrintService) service).wakeNotifier();
            } else {
                ((UnixPrintService) 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());
            if (service instanceof IPPPrintService) {
                ((IPPPrintService) service).wakeNotifier();
            } else {
                ((UnixPrintService) 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 {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("unrecognized class: " + repClassName);
    }
    // now spool the print data.
    PrinterOpener po = new PrinterOpener();
    java.security.AccessController.doPrivileged(po);
    if (po.pex != null) {
        throw po.pex;
    }
    OutputStream output = po.result;
    /* There are three cases:
         * 1) Text data from a Reader, just pass through.
         * 2) Text data from an input stream which we must read using the
         *    correct encoding
         * 3) Raw byte data from an InputStream we don't interpret as text,
         *    just pass through: eg postscript.
         */
    BufferedWriter bw = null;
    if ((instream == null && reader != null)) {
        BufferedReader br = new BufferedReader(reader);
        OutputStreamWriter osw = new OutputStreamWriter(output);
        bw = new BufferedWriter(osw);
        char[] buffer = new char[1024];
        int cread;
        try {
            while ((cread = br.read(buffer, 0, buffer.length)) >= 0) {
                bw.write(buffer, 0, cread);
            }
            br.close();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e);
        }
    } else if (instream != null && flavor.getMediaType().equalsIgnoreCase("text")) {
        try {
            InputStreamReader isr = new InputStreamReader(instream, encoding);
            BufferedReader br = new BufferedReader(isr);
            OutputStreamWriter osw = new OutputStreamWriter(output);
            bw = new BufferedWriter(osw);
            char[] buffer = new char[1024];
            int cread;
            while ((cread = br.read(buffer, 0, buffer.length)) >= 0) {
                bw.write(buffer, 0, cread);
            }
            bw.flush();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e);
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
            }
        }
    } else if (instream != null) {
        BufferedInputStream bin = new BufferedInputStream(instream);
        BufferedOutputStream bout = new BufferedOutputStream(output);
        byte[] buffer = new byte[1024];
        int bread = 0;
        try {
            while ((bread = bin.read(buffer)) >= 0) {
                bout.write(buffer, 0, bread);
            }
            bin.close();
            bout.flush();
            bout.close();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e);
        }
    }
    notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
    if (mDestType == UnixPrintJob.DESTPRINTER) {
        PrinterSpooler spooler = new PrinterSpooler();
        java.security.AccessController.doPrivileged(spooler);
        if (spooler.pex != null) {
            throw spooler.pex;
        }
    }
    notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
    if (service instanceof IPPPrintService) {
        ((IPPPrintService) service).wakeNotifier();
    } else {
        ((UnixPrintService) service).wakeNotifier();
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) PrintException(javax.print.PrintException) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) java.awt.print(java.awt.print) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) DocFlavor(javax.print.DocFlavor) PrinterIsAcceptingJobs(javax.print.attribute.standard.PrinterIsAcceptingJobs)

Example 3 with PrintException

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

the class Win32PrintJob 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 sun.awt.windows.WPrinterJob();
            }
        }
        PrintService svc = getPrintService();
        job.setPrintService(svc);
        if (copies == 0) {
            Copies c = (Copies) svc.getDefaultAttributeValue(Copies.class);
            copies = c.getValue();
        }
        if (mediaName == null) {
            Object media = svc.getDefaultAttributeValue(Media.class);
            if (media instanceof MediaSizeName) {
                mediaName = (MediaSizeName) media;
                mediaSize = MediaSize.getMediaSizeForName(mediaName);
            }
        }
        if (orient == null) {
            orient = (OrientationRequested) svc.getDefaultAttributeValue(OrientationRequested.class);
        }
        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) MediaSizeName(javax.print.attribute.standard.MediaSizeName) Copies(javax.print.attribute.standard.Copies) PrintService(javax.print.PrintService)

Example 4 with PrintException

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

the class Win32PrintJob method getAttributeValues.

private void getAttributeValues(DocFlavor flavor) throws PrintException {
    if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
        fidelity = true;
    } else {
        fidelity = false;
    }
    Class category;
    Attribute[] attrs = reqAttrSet.toArray();
    for (int i = 0; i < attrs.length; i++) {
        Attribute 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 {
                    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 == 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;
                // be used to create a new PageFormat.
                if (!service.isAttributeValueSupported(attr, null, null)) {
                    mediaSize = MediaSize.getMediaSizeForName(mediaName);
                }
            }
        } else if (category == OrientationRequested.class) {
            orient = (OrientationRequested) 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) JobName(javax.print.attribute.standard.JobName) Media(javax.print.attribute.standard.Media) URI(java.net.URI) java.awt.print(java.awt.print) IOException(java.io.IOException) PrintException(javax.print.PrintException) FileNotFoundException(java.io.FileNotFoundException) PrintException(javax.print.PrintException) File(java.io.File)

Example 5 with PrintException

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

the class Win32PrintJob method pageableJob.

public void pageableJob(Pageable pageable) throws PrintException {
    try {
        synchronized (this) {
            if (job != null) {
                // shouldn't happen
                throw new PrintException("already printing");
            } else {
                job = new sun.awt.windows.WPrinterJob();
            }
        }
        PrintService svc = getPrintService();
        job.setPrintService(svc);
        if (copies == 0) {
            Copies c = (Copies) svc.getDefaultAttributeValue(Copies.class);
            copies = c.getValue();
        }
        job.setCopies(copies);
        job.setJobName(jobName);
        job.setPageable(pageable);
        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) Copies(javax.print.attribute.standard.Copies) PrintService(javax.print.PrintService)

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