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.commons.logging.Log;
034 import org.apache.commons.logging.LogFactory;
035
036 public class PrinterProducer extends DefaultProducer {
037 private static final transient Log LOG = LogFactory.getLog(PrinterProducer.class);
038 Endpoint endpoint;
039 PrinterConfiguration config;
040 PrinterOperations printerOperations;
041 PrintService printService;
042 String printer;
043
044 public PrinterProducer(Endpoint endpoint, PrinterConfiguration config) throws Exception {
045 super(endpoint);
046 this.endpoint = endpoint;
047 this.config = config;
048 this.printService = assignPrintService();
049 this.printerOperations = new PrinterOperations(printService, assignPrintJob(printService), assignDocFlavor(), assignPrintAttributes());
050 }
051
052 @Override
053 protected void doStart() throws Exception {
054 LOG.info("In PrinterProducer.start()");
055 super.doStart();
056 }
057
058 @Override
059 protected void doStop() throws Exception {
060 LOG.info("In PrinterProducer.stop()");
061 super.doStop();
062 }
063
064 public void process(Exchange exchange) throws Exception {
065 LOG.info("In printerProducer.print()");
066 Object body = exchange.getIn().getBody();
067 InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
068 print(is);
069 }
070
071 private void print(InputStream body) throws PrintException {
072 if (printerOperations.getPrintService().isDocFlavorSupported(printerOperations.getFlavor())) {
073 PrintDocument printDoc = new PrintDocument(body, printerOperations.getFlavor());
074 printerOperations.print(printDoc, config.getCopies(), config.isSendToPrinter(), config.getMimeType());
075 }
076 }
077
078 private DocFlavor assignDocFlavor() throws Exception {
079 return config.getDocFlavor();
080 }
081
082 private PrintRequestAttributeSet assignPrintAttributes() throws PrintException {
083 PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
084 if (config.getCopies() >= 1) {
085 printRequestAttributeSet.add(new Copies(config.getCopies()));
086 } else {
087 throw new PrintException("Number of print copies should be greater than zero");
088 }
089 printRequestAttributeSet.add(config.getMediaSizeName());
090 printRequestAttributeSet.add(config.getInternalSides());
091
092 return printRequestAttributeSet;
093 }
094
095 private DocPrintJob assignPrintJob(PrintService printService) {
096 return printService.createPrintJob();
097 }
098
099 private PrintService assignPrintService() throws PrintException {
100 PrintService printService;
101
102 if ((config.getHostname().equalsIgnoreCase("localhost"))
103 && (config.getPrintername().equalsIgnoreCase("/default"))) {
104 printService = PrintServiceLookup.lookupDefaultPrintService();
105 } else {
106 PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
107 setPrinter("\\\\" + config.getHostname() + "\\" + config.getPrintername());
108 int position = findPrinter(services, printer);
109 if (position < 0) {
110 LOG.error("PrintServiceLookup failed. No printer found with the Printer Name: " + printer);
111 throw new PrintException("Printer Lookup Failure. Please verify that the host and printer are registered and reachable from this machine");
112 }
113 printService = services[position];
114 }
115 // LOG.info("PrintServiceLookup succeeded. PrintService located at " + printService.getName());
116 return printService;
117 }
118
119 private int findPrinter(PrintService[] services, String printer) {
120 boolean found = false;
121 int position = -1;
122 for (int i = 0; (i < services.length) && (!found); i++) {
123 if (printer.equalsIgnoreCase(services[i].getName())) {
124 found = true;
125 position = i;
126 }
127 }
128
129 return position;
130 }
131
132 public Endpoint getEndpoint() {
133 return endpoint;
134 }
135
136 public void setEndpoint(Endpoint endpoint) {
137 this.endpoint = endpoint;
138 }
139
140 public PrinterConfiguration getConfig() {
141 return config;
142 }
143
144 public void setConfig(PrinterConfiguration config) {
145 this.config = config;
146 }
147
148 public PrinterOperations getPrinterOperations() {
149 return printerOperations;
150 }
151
152 public void setPrinterOperations(PrinterOperations printerOperations) {
153 this.printerOperations = printerOperations;
154 }
155
156 public PrintService getPrintService() {
157 return printService;
158 }
159
160 public void setPrintService(PrintService printService) {
161 this.printService = printService;
162 }
163
164 public String getPrinter() {
165 return printer;
166 }
167
168 public void setPrinter(String printer) {
169 this.printer = printer;
170 }
171
172
173 }