001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.printer;
018    
019    import java.io.File;
020    import java.io.FileOutputStream;
021    import java.io.InputStream;
022    import java.util.UUID;
023    import javax.print.Doc;
024    import javax.print.DocFlavor;
025    import javax.print.DocPrintJob;
026    import javax.print.PrintException;
027    import javax.print.PrintService;
028    import javax.print.PrintServiceLookup;
029    import javax.print.attribute.HashPrintRequestAttributeSet;
030    import javax.print.attribute.PrintRequestAttributeSet;
031    import javax.print.attribute.standard.Copies;
032    import javax.print.attribute.standard.MediaSizeName;
033    import javax.print.attribute.standard.Sides;
034    
035    import org.apache.camel.util.IOHelper;
036    import org.apache.commons.logging.Log;
037    import org.apache.commons.logging.LogFactory;
038    
039    public class PrinterOperations implements PrinterOperationsInterface {
040        private static final transient Log LOG = LogFactory.getLog(PrinterOperations.class);
041        private PrintService printService;
042        private DocPrintJob job;
043        private DocFlavor flavor;
044        private PrintRequestAttributeSet printRequestAttributeSet;
045        private Doc doc;
046    
047        public PrinterOperations() throws PrintException {        
048            printService = PrintServiceLookup.lookupDefaultPrintService();
049            if (printService == null) {
050                throw new PrintException("Printer lookup failure. No default printer set up for this host");
051            }
052            job = printService.createPrintJob(); 
053            flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
054            printRequestAttributeSet = new HashPrintRequestAttributeSet();
055            printRequestAttributeSet.add(new Copies(1));
056            printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
057            printRequestAttributeSet.add(Sides.ONE_SIDED);
058        }
059    
060        public PrinterOperations(PrintService printService, DocPrintJob job, DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
061            this();
062            this.setPrintService(printService);
063            this.setJob(job);
064            this.setFlavor(flavor);
065            this.setPrintRequestAttributeSet(printRequestAttributeSet);
066        }
067    
068        public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType) throws PrintException {
069            LOG.trace("Print Service: " + this.printService.getName());
070            LOG.trace("About to print " + copies + " copy(s)");
071            
072            for (int i = 0; i < copies; i++) {
073                if (!sendToPrinter) {
074                    LOG.debug("Print flag is set to false. This job will not be printed until this setting remains in effect."
075                            + " Please set the flag to true or remove the setting.");
076    
077                    File file;
078                    if (mimeType.equalsIgnoreCase("GIF") || mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
079                        file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".gif");
080                    } else if (mimeType.equalsIgnoreCase("JPEG")) {
081                        file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".jpeg");
082                    } else if (mimeType.equalsIgnoreCase("PDF")) {
083                        file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".pdf");
084                    } else {
085                        file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".txt");
086                    }
087    
088                    LOG.debug("Writing print job to file: " + file.getAbsolutePath());
089                    try {
090                        InputStream in = doc.getStreamForBytes();
091                        FileOutputStream fos = new FileOutputStream(file);
092                        IOHelper.copyAndCloseInput(in, fos);
093                        IOHelper.close(fos);
094                    } catch (Exception e) {
095                        throw new PrintException("Error writing Document to the target file " + file.getAbsolutePath());
096                    }    
097                } else {
098                    if (LOG.isDebugEnabled()) {
099                        LOG.debug("Issuing Job " + i + " to Printer: " + this.printService.getName());
100                    }
101                    print(doc);
102                }
103            }
104        }
105            
106        public void print(Doc doc) throws PrintException {
107            job.print(doc, printRequestAttributeSet);
108        }
109    
110        public PrintService getPrintService() {
111            return printService;
112        }
113    
114        public void setPrintService(PrintService printService) {
115            this.printService = printService;
116        }
117    
118        public DocPrintJob getJob() {
119            return job;
120        }
121    
122        public void setJob(DocPrintJob job) {
123            this.job = job;
124        }
125    
126        public DocFlavor getFlavor() {
127            return flavor;
128        }
129    
130        public void setFlavor(DocFlavor flavor) {
131            this.flavor = flavor;
132        }
133    
134        public PrintRequestAttributeSet getPrintRequestAttributeSet() {
135            return printRequestAttributeSet;
136        }
137    
138        public void setPrintRequestAttributeSet(PrintRequestAttributeSet printRequestAttributeSet) {
139            this.printRequestAttributeSet = printRequestAttributeSet;
140        }
141    
142        public Doc getDoc() {
143            return doc;
144        }
145    
146        public void setDoc(Doc doc) {
147            this.doc = doc;
148        }
149    
150    }