3ea135bb |
1 | /**\r |
2 | * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r |
3 | *\r |
4 | * This library is free software; you can redistribute it and/or\r |
5 | * modify it under the terms of the GNU Lesser General Public\r |
6 | * License as published by the Free Software Foundation; either\r |
7 | * version 2.1 of the License, or (at your option) any later version.\r |
8 | * \r |
9 | * This library is distributed in the hope that it will be useful,\r |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r |
12 | * Lesser General Public License for more details.\r |
13 | * \r |
14 | * You should have received a copy of the GNU Lesser General Public\r |
15 | * License along with this library; if not, write to the Free Software\r |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
17 | * \r |
18 | */\r |
19 | package de.lohndirekt.print;\r |
20 | \r |
21 | import java.io.IOException;\r |
22 | import java.io.InputStream;\r |
23 | import java.util.Map;\r |
24 | import java.util.Set;\r |
25 | import java.util.logging.Level;\r |
26 | import java.util.logging.Logger;\r |
27 | \r |
28 | import javax.print.DocFlavor;\r |
29 | import javax.print.MultiDoc;\r |
30 | import javax.print.MultiDocPrintJob;\r |
31 | import javax.print.PrintException;\r |
32 | import javax.print.attribute.AttributeSet;\r |
33 | import javax.print.attribute.DocAttributeSet;\r |
34 | import javax.print.attribute.HashAttributeSet;\r |
35 | import javax.print.attribute.PrintRequestAttributeSet;\r |
36 | import javax.print.event.PrintJobEvent;\r |
37 | \r |
38 | import de.lohndirekt.print.attribute.AttributeHelper;\r |
39 | import de.lohndirekt.print.attribute.IppAttributeName;\r |
40 | import de.lohndirekt.print.attribute.IppStatus;\r |
41 | import de.lohndirekt.print.attribute.ipp.jobdesc.JobUri;\r |
42 | import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OperationsSupported;\r |
43 | /**\r |
44 | * @author bpusch\r |
45 | *\r |
46 | */\r |
47 | class MultiDocJob extends Job implements MultiDocPrintJob {\r |
48 | \r |
49 | Logger log = Logger.getLogger(this.getClass().getName());\r |
50 | \r |
51 | /**\r |
52 | * \r |
53 | */\r |
54 | protected MultiDocJob(IppMultiDocPrintService service) {\r |
55 | super(service);\r |
56 | }\r |
57 | \r |
58 | void processMultiDocEvent(MultiDocEvent event) {\r |
59 | SimpleMultiDoc doc = (SimpleMultiDoc) event.getSource();\r |
60 | sendDocument(doc);\r |
61 | }\r |
62 | \r |
63 | /**\r |
64 | *\r |
65 | */\r |
66 | public void print(MultiDoc multiDoc, PrintRequestAttributeSet attributes) throws PrintException {\r |
67 | SimpleMultiDoc multi = (SimpleMultiDoc) multiDoc;\r |
68 | multi.addMultiDocListener(new MDListener());\r |
69 | createJob(attributes);\r |
70 | sendDocument(multi);\r |
71 | }\r |
72 | \r |
73 | private void createJob(PrintRequestAttributeSet attributes) {\r |
74 | IppRequest request = null;\r |
75 | request = this.printService.request(OperationsSupported.CREATE_JOB);\r |
76 | //add the operationn attributes\r |
77 | request.addOperationAttributes(AttributeHelper.jobOperationAttributes(attributes));\r |
78 | request.setJobAttributes(AttributeHelper.jobAttributes(attributes));\r |
79 | IppResponse response = null;\r |
80 | try {\r |
81 | response = request.send();\r |
82 | } catch (IOException e) {\r |
83 | log.log(Level.SEVERE, e.getMessage(), e);\r |
84 | }\r |
85 | if (response != null) {\r |
86 | Map responseAttributes = response.getAttributes();\r |
87 | if (responseAttributes.containsKey(IppAttributeName.JOB_URI.getCategory())) {\r |
88 | Set jobUriSet = (Set) responseAttributes.get(IppAttributeName.JOB_URI.getCategory());\r |
89 | this.jobUri = (JobUri) jobUriSet.iterator().next();\r |
90 | }\r |
91 | if (response.getStatus().equals(IppStatus.SUCCESSFUL_OK)\r |
92 | || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)\r |
93 | || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)) {\r |
94 | } else {\r |
95 | notifyJobListeners(PrintJobEvent.JOB_FAILED);\r |
96 | }\r |
97 | } else {\r |
98 | notifyJobListeners(PrintJobEvent.JOB_FAILED);\r |
99 | }\r |
100 | }\r |
101 | \r |
102 | protected void sendDocument(SimpleMultiDoc multiDoc) {\r |
103 | IppRequest request = null;\r |
104 | AttributeSet operationAttributes = new HashAttributeSet();\r |
105 | request = this.request(OperationsSupported.SEND_DOCUMENT);\r |
106 | IppResponse response = null;\r |
107 | try {\r |
108 | operationAttributes.addAll(AttributeHelper.docOperationAttributes(multiDoc));\r |
109 | request.addOperationAttributes(operationAttributes);\r |
110 | if (multiDoc.getDoc().getDocFlavor() instanceof DocFlavor.INPUT_STREAM) {\r |
111 | DocAttributeSet set = multiDoc.getDoc().getAttributes();\r |
112 | request.setData((InputStream) multiDoc.getDoc().getPrintData());\r |
113 | }\r |
114 | response = request.send();\r |
115 | } catch (IOException e) {\r |
116 | log.log(Level.SEVERE, e.getMessage(), e);\r |
117 | }\r |
118 | if (response != null) {\r |
119 | Map responseAttributes = response.getAttributes();\r |
120 | if (responseAttributes.containsKey(IppAttributeName.JOB_URI.getCategory())) {\r |
121 | Set jobUriSet = (Set) responseAttributes.get(IppAttributeName.JOB_URI.getCategory());\r |
122 | this.jobUri = (JobUri) jobUriSet.iterator().next();\r |
123 | }\r |
124 | if (response.getStatus().equals(IppStatus.SUCCESSFUL_OK)\r |
125 | || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)\r |
126 | || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)) {\r |
127 | } else {\r |
128 | notifyJobListeners(PrintJobEvent.JOB_FAILED);\r |
129 | }\r |
130 | } else {\r |
131 | notifyJobListeners(PrintJobEvent.JOB_FAILED);\r |
132 | }\r |
133 | if (multiDoc.isLast()) {\r |
134 | releaseJob();\r |
135 | }\r |
136 | }\r |
137 | \r |
138 | /**\r |
139 | * \r |
140 | */\r |
141 | private void releaseJob() {\r |
142 | IppRequest request = this.request(OperationsSupported.RELEASE_JOB);\r |
143 | IppResponse response = null;\r |
144 | try {\r |
145 | response = request.send();\r |
146 | \r |
147 | } catch (IOException e) {\r |
148 | log.log(Level.SEVERE, e.getMessage(), e);\r |
149 | }\r |
150 | if (response != null) {\r |
151 | if (response.getStatus().equals(IppStatus.SUCCESSFUL_OK)\r |
152 | || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)\r |
153 | || response.getStatus().equals(IppStatus.SUCCESSFUL_OK_CONFLICTING_ATTRIBUTES)) {\r |
154 | notifyJobListeners(PrintJobEvent.JOB_COMPLETE);\r |
155 | } else {\r |
156 | notifyJobListeners(PrintJobEvent.JOB_FAILED);\r |
157 | }\r |
158 | } else {\r |
159 | notifyJobListeners(PrintJobEvent.JOB_FAILED);\r |
160 | }\r |
161 | }\r |
162 | \r |
163 | private IppRequest request(OperationsSupported operation) {\r |
164 | IppRequest request = IppRequestFactory.createIppRequest(this.jobUri.getURI(), operation, this.printService.getRequestingUserName(), this.printService.getRequestingUserPassword());\r |
165 | AttributeSet operationAttributes = new HashAttributeSet();\r |
166 | operationAttributes.add(new JobUri(this.jobUri.getURI()));\r |
167 | request.addOperationAttributes(operationAttributes);\r |
168 | return request;\r |
169 | }\r |
170 | \r |
171 | private class MDListener implements MultiDocListener {\r |
172 | public void processEvent(MultiDocEvent event) throws IOException {\r |
173 | log.log(Level.FINEST, "MultiDocevent");\r |
174 | processMultiDocEvent(event);\r |
175 | }\r |
176 | }\r |
177 | }\r |