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.Locale;
023 import java.util.UUID;
024 import javax.print.Doc;
025 import javax.print.DocFlavor;
026 import javax.print.DocPrintJob;
027 import javax.print.PrintException;
028 import javax.print.PrintService;
029 import javax.print.PrintServiceLookup;
030 import javax.print.attribute.HashPrintRequestAttributeSet;
031 import javax.print.attribute.PrintRequestAttributeSet;
032 import javax.print.attribute.standard.Copies;
033 import javax.print.attribute.standard.JobName;
034 import javax.print.attribute.standard.MediaSizeName;
035 import javax.print.attribute.standard.Sides;
036
037 import org.apache.camel.util.IOHelper;
038 import org.slf4j.Logger;
039 import org.slf4j.LoggerFactory;
040
041 public class PrinterOperations implements PrinterOperationsInterface {
042 private static final transient Logger LOG = LoggerFactory.getLogger(PrinterOperations.class);
043 private PrintService printService;
044 private DocFlavor flavor;
045 private PrintRequestAttributeSet printRequestAttributeSet;
046 private Doc doc;
047
048 public PrinterOperations() throws PrintException {
049 printService = PrintServiceLookup.lookupDefaultPrintService();
050 if (printService == null) {
051 throw new PrintException("Printer lookup failure. No default printer set up for this host");
052 }
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, DocFlavor flavor, PrintRequestAttributeSet printRequestAttributeSet) throws PrintException {
061 this.setPrintService(printService);
062 this.setFlavor(flavor);
063 this.setPrintRequestAttributeSet(printRequestAttributeSet);
064 }
065
066 public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType, String jobName) throws PrintException {
067 LOG.trace("Print Service: " + this.printService.getName());
068 LOG.trace("About to print " + copies + " copy(s)");
069
070 for (int i = 0; i < copies; i++) {
071 if (!sendToPrinter) {
072 LOG.debug("Print flag is set to false. This job will not be printed until this setting remains in effect."
073 + " Please set the flag to true or remove the setting.");
074
075 File file;
076 if (mimeType.equalsIgnoreCase("GIF") || mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
077 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".gif");
078 } else if (mimeType.equalsIgnoreCase("JPEG")) {
079 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".jpeg");
080 } else if (mimeType.equalsIgnoreCase("PDF")) {
081 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".pdf");
082 } else {
083 file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".txt");
084 }
085
086 LOG.debug("Writing print job to file: " + file.getAbsolutePath());
087 try {
088 InputStream in = doc.getStreamForBytes();
089 FileOutputStream fos = new FileOutputStream(file);
090 IOHelper.copyAndCloseInput(in, fos);
091 IOHelper.close(fos);
092 } catch (Exception e) {
093 throw new PrintException("Error writing Document to the target file " + file.getAbsolutePath());
094 }
095 } else {
096 if (LOG.isDebugEnabled()) {
097 LOG.debug("Issuing Job {} to Printer: {}", i, this.printService.getName());
098 }
099 print(doc, jobName);
100 }
101 }
102 }
103
104 public void print(Doc doc, String jobName) throws PrintException {
105 // we need create a new job for each print
106 DocPrintJob job = getPrintService().createPrintJob();
107 PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(printRequestAttributeSet);
108 attrs.add(new JobName(jobName, Locale.getDefault()));
109 job.print(doc, attrs);
110 }
111
112 public PrintService getPrintService() {
113 return printService;
114 }
115
116 public void setPrintService(PrintService printService) {
117 this.printService = printService;
118 }
119
120 public DocFlavor getFlavor() {
121 return flavor;
122 }
123
124 public void setFlavor(DocFlavor flavor) {
125 this.flavor = flavor;
126 }
127
128 public PrintRequestAttributeSet getPrintRequestAttributeSet() {
129 return printRequestAttributeSet;
130 }
131
132 public void setPrintRequestAttributeSet(PrintRequestAttributeSet printRequestAttributeSet) {
133 this.printRequestAttributeSet = printRequestAttributeSet;
134 }
135
136 public Doc getDoc() {
137 return doc;
138 }
139
140 public void setDoc(Doc doc) {
141 this.doc = doc;
142 }
143
144 }