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.InputStream;
020
021 import javax.print.DocFlavor;
022 import javax.print.DocPrintJob;
023 import javax.print.PrintException;
024 import javax.print.PrintService;
025 import javax.print.PrintServiceLookup;
026 import javax.print.attribute.HashPrintRequestAttributeSet;
027 import javax.print.attribute.PrintRequestAttributeSet;
028 import javax.print.attribute.standard.Copies;
029
030 import org.apache.camel.Endpoint;
031 import org.apache.camel.Exchange;
032 import org.apache.camel.impl.DefaultProducer;
033 import org.apache.camel.util.ObjectHelper;
034 import org.slf4j.Logger;
035 import org.slf4j.LoggerFactory;
036
037 public class PrinterProducer extends DefaultProducer {
038 private final PrinterConfiguration config;
039 private PrinterOperations printerOperations;
040 private PrintService printService;
041 private String printer;
042
043 public PrinterProducer(Endpoint endpoint, PrinterConfiguration config) throws Exception {
044 super(endpoint);
045 this.config = config;
046 }
047
048 public void process(Exchange exchange) throws Exception {
049 Object body = exchange.getIn().getBody();
050 InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
051 String jobName = exchange.getIn().getHeader(PrinterEndpoint.JOB_NAME, "Camel: lpr", String.class);
052 print(is, jobName);
053 }
054
055 private void print(InputStream body, String jobName) throws PrintException {
056 if (printerOperations.getPrintService().isDocFlavorSupported(printerOperations.getFlavor())) {
057 PrintDocument printDoc = new PrintDocument(body, printerOperations.getFlavor());
058 printerOperations.print(printDoc, config.getCopies(), config.isSendToPrinter(), config.getMimeType(), jobName);
059 }
060 }
061
062 private DocFlavor assignDocFlavor() throws Exception {
063 return config.getDocFlavor();
064 }
065
066 private PrintRequestAttributeSet assignPrintAttributes() throws PrintException {
067 PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
068 if (config.getCopies() >= 1) {
069 printRequestAttributeSet.add(new Copies(config.getCopies()));
070 } else {
071 throw new PrintException("Number of print copies should be greater than zero");
072 }
073 printRequestAttributeSet.add(config.getMediaSizeName());
074 printRequestAttributeSet.add(config.getInternalSides());
075
076 return printRequestAttributeSet;
077 }
078
079 private DocPrintJob assignPrintJob(PrintService printService) {
080 return printService.createPrintJob();
081 }
082
083 private PrintService assignPrintService() throws PrintException {
084 PrintService printService;
085
086 if ((config.getHostname().equalsIgnoreCase("localhost"))
087 && (config.getPrintername().equalsIgnoreCase("/default"))) {
088 printService = PrintServiceLookup.lookupDefaultPrintService();
089 } else {
090 PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
091 setPrinter("\\\\" + config.getHostname() + "\\" + config.getPrintername());
092 int position = findPrinter(services, printer);
093 if (position < 0) {
094 throw new PrintException("No printer found with name: " + printer + ". Please verify that the host and printer are registered and reachable from this machine.");
095 }
096 printService = services[position];
097 }
098 return printService;
099 }
100
101 private int findPrinter(PrintService[] services, String printer) {
102 int position = -1;
103 for (int i = 0; i < services.length; i++) {
104 if (printer.equalsIgnoreCase(services[i].getName())) {
105 position = i;
106 break;
107 }
108 }
109 return position;
110 }
111
112 public PrinterConfiguration getConfig() {
113 return config;
114 }
115
116 public PrinterOperations getPrinterOperations() {
117 return printerOperations;
118 }
119
120 public void setPrinterOperations(PrinterOperations printerOperations) {
121 this.printerOperations = printerOperations;
122 }
123
124 public PrintService getPrintService() {
125 return printService;
126 }
127
128 public void setPrintService(PrintService printService) {
129 this.printService = printService;
130 }
131
132 public String getPrinter() {
133 return printer;
134 }
135
136 public void setPrinter(String printer) {
137 this.printer = printer;
138 }
139
140 @Override
141 protected void doStart() throws Exception {
142 if (printService == null) {
143 printService = assignPrintService();
144 }
145 ObjectHelper.notNull(printService, "PrintService", this);
146
147 if (printerOperations == null) {
148 printerOperations = new PrinterOperations(printService, assignDocFlavor(), assignPrintAttributes());
149 }
150 super.doStart();
151 }
152
153 @Override
154 protected void doStop() throws Exception {
155 super.doStop();
156 }
157
158 }