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.commons.logging.Log;
036 import org.apache.commons.logging.LogFactory;
037
038 public class PrinterOperations implements PrinterOperationsInterface {
039 private static final transient Log LOG = LogFactory.getLog(PrinterOperations.class);
040 private PrintService printService;
041 private DocPrintJob job;
042 private DocFlavor flavor;
043 private PrintRequestAttributeSet printRequestAttributeSet;
044 private Doc doc;
045
046 public PrinterOperations() throws PrintException {
047 printService = PrintServiceLookup.lookupDefaultPrintService();
048 if (printService == null) {
049 throw new PrintException("Printer Lookup Failure. No Default printer set up for this host");
050 }
051 job = printService.createPrintJob();
052 flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
053 printRequestAttributeSet = new HashPrintRequestAttributeSet();
054 printRequestAttributeSet.add(new Copies(1));
055 printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
056 printRequestAttributeSet.add(Sides.ONE_SIDED);
057 }
058
059 public PrinterOperations(PrintService printService, DocPrintJob job, DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
060 this();
061 this.setPrintService(printService);
062 this.setJob(job);
063 this.setFlavor(flavor);
064 this.setPrintRequestAttributeSet(printRequestAttributeSet);
065 }
066
067 public PrinterOperations(DocPrintJob job, DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
068 this();
069 this.setJob(job);
070 this.setFlavor(flavor);
071 this.setPrintRequestAttributeSet(printRequestAttributeSet);
072 }
073
074 public PrinterOperations(DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
075 this();
076 this.setFlavor(flavor);
077 this.setPrintRequestAttributeSet(printRequestAttributeSet);
078 }
079
080 public PrinterOperations(PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
081 this();
082 this.setPrintRequestAttributeSet(printRequestAttributeSet);
083 }
084
085 public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType) throws PrintException {
086 byte[] buffer = null;
087 File file;
088
089 LOG.trace("In printerOperations.print()");
090 LOG.trace("Print Service = " + this.printService.getName());
091 LOG.trace("About to print " + copies + " copy(s)");
092
093 for (int i = 0; i < copies; i++) {
094 if (!sendToPrinter) {
095 LOG.debug("\tPrint Flag is set to false. This job(s) will not be printed until this setting remains in effect. Please set the flag to true or remove the setting");
096 if (mimeType.equalsIgnoreCase("GIF") || mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
097 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".gif");
098 } else if (mimeType.equalsIgnoreCase("JPEG")) {
099 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".jpeg");
100 } else if (mimeType.equalsIgnoreCase("PDF")) {
101 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".pdf");
102 } else {
103 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".txt");
104 }
105 LOG.debug("\tWriting Print Job to File: " + file.getAbsolutePath());
106 try {
107 if (buffer == null) {
108 InputStream stream = doc.getStreamForBytes();
109 buffer = new byte[stream.available()];
110 int n = stream.available();
111 for (int j = 0; j < n; j++) {
112 buffer[j] = (byte)stream.read();
113 }
114 }
115 FileOutputStream fileOutputStream = new FileOutputStream(file);
116 fileOutputStream.write(buffer);
117 fileOutputStream.flush();
118 fileOutputStream.close();
119 } catch (Exception e) {
120 throw new PrintException("Error writing Document to the target file " + file.getAbsolutePath());
121 }
122 } else {
123 LOG.debug("\tIssuing Job " + i + " to Printer: " + this.printService.getName());
124 print(doc);
125 }
126 }
127 }
128
129 public void print(Doc doc) throws PrintException {
130 job.print(doc, printRequestAttributeSet);
131 }
132
133 public PrintService getPrintService() {
134 return printService;
135 }
136
137 public void setPrintService(PrintService printService) {
138 this.printService = printService;
139 }
140
141 public DocPrintJob getJob() {
142 return job;
143 }
144
145 public void setJob(DocPrintJob job) {
146 this.job = job;
147 }
148
149 public DocFlavor getFlavor() {
150 return flavor;
151 }
152
153 public void setFlavor(DocFlavor flavor) {
154 this.flavor = flavor;
155 }
156
157 public PrintRequestAttributeSet getPrintRequestAttributeSet() {
158 return printRequestAttributeSet;
159 }
160
161 public void setPrintRequestAttributeSet(PrintRequestAttributeSet printRequestAttributeSet) {
162 this.printRequestAttributeSet = printRequestAttributeSet;
163 }
164
165 public Doc getDoc() {
166 return doc;
167 }
168
169 public void setDoc(Doc doc) {
170 this.doc = doc;
171 }
172
173 }