Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / test / IppRequestTestImpl.java
1 /**
2 * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19 package de.lohndirekt.print.test;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.logging.Logger;
31
32 import javax.print.attribute.AttributeSet;
33 import javax.print.attribute.HashAttributeSet;
34 import javax.print.attribute.HashPrintJobAttributeSet;
35 import javax.print.attribute.PrintJobAttributeSet;
36 import javax.print.attribute.standard.ColorSupported;
37 import javax.print.attribute.standard.Compression;
38 import javax.print.attribute.standard.CopiesSupported;
39 import javax.print.attribute.standard.JobImpressionsSupported;
40 import javax.print.attribute.standard.JobKOctetsSupported;
41 import javax.print.attribute.standard.JobMediaSheetsSupported;
42 import javax.print.attribute.standard.JobPrioritySupported;
43 import javax.print.attribute.standard.JobSheets;
44 import javax.print.attribute.standard.MultipleDocumentHandling;
45 import javax.print.attribute.standard.NumberUpSupported;
46 import javax.print.attribute.standard.OrientationRequested;
47 import javax.print.attribute.standard.PDLOverrideSupported;
48 import javax.print.attribute.standard.ReferenceUriSchemesSupported;
49 import javax.print.attribute.standard.Sides;
50
51 import de.lohndirekt.print.IppRequest;
52 import de.lohndirekt.print.IppResponse;
53 import de.lohndirekt.print.attribute.IppAttributeName;
54 import de.lohndirekt.print.attribute.IppStatus;
55 import de.lohndirekt.print.attribute.ipp.Charset;
56 import de.lohndirekt.print.attribute.ipp.NaturalLanguage;
57 import de.lohndirekt.print.attribute.ipp.jobtempl.LdJobHoldUntil;
58 import de.lohndirekt.print.attribute.ipp.printerdesc.PrinterDriverInstaller;
59 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.CompressionSupported;
60 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.FinishingsSupported;
61 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.JobHoldUntilSupported;
62 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.JobSheetsSupported;
63 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.MediaSupported;
64 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.MultipleDocumentHandlingSupported;
65 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OperationsSupported;
66 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OrientationRequestedSupported;
67 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.PageRangesSupported;
68 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.PrinterUriSupported;
69 import de.lohndirekt.print.attribute.ipp.printerdesc.supported.SidesSupported;
70
71 public class IppRequestTestImpl implements IppRequest {
72 private Logger log = Logger.getLogger(this.getClass().getName());
73
74 class IppResponseTestImpl implements IppResponse {
75
76 IppStatus status;
77 Map attributes;
78
79 IppResponseTestImpl() {
80
81 }
82
83 /**
84 * @return
85 */
86 public Map getAttributes() {
87 return attributes;
88 }
89
90 /**
91 * @return
92 */
93 public IppStatus getStatus() {
94 return status;
95 }
96
97 }
98
99 private IppResponseTestImpl response;
100 private Object data;
101 private OperationsSupported operation;
102 //Id wird in der Cups-API zwar übergeben, ist aber auch immer 1.
103 private PrintJobAttributeSet jobAttributes = new HashPrintJobAttributeSet();
104 private AttributeSet operationAttributes = new HashAttributeSet();
105 private AttributeSet printerAttributes = new HashAttributeSet();
106
107 /**
108 * @param printerAttributes
109 */
110 public void setPrinterAttributes(AttributeSet attrs) {
111 this.printerAttributes = attrs;
112 }
113
114 /**
115 * @param operation
116 */
117 public IppRequestTestImpl(URI path, OperationsSupported operation) {
118 this.operation = operation;
119 init();
120 }
121
122 /**
123 *
124 */
125 private void init() {
126 setStandardAttributes();
127 }
128
129 /**
130 *
131 */
132 private void setStandardAttributes() {
133 operationAttributes.add(Charset.ISO_8859_1);
134 operationAttributes.add(NaturalLanguage.EN);
135 }
136
137 /**
138 * @param attributes
139 */
140 public void addOperationAttributes(AttributeSet attributes) {
141 this.operationAttributes.addAll(attributes);
142 }
143
144 /**
145 * @param data the data as input stream
146 * @param the size of the data
147 */
148 public void setData(InputStream data) {
149 this.data = data;
150 }
151 /**
152 * @param data
153 */
154 public void setData(byte[] data) {
155 this.data = data;
156 }
157
158 /**
159 * @param attributes
160 */
161 public void setJobAttributes(PrintJobAttributeSet attributes) {
162 this.jobAttributes = attributes;
163 }
164
165 public IppResponse send() throws IOException {
166 try {
167 this.response = new IppResponseTestImpl();
168 this.send(this.operation);
169 } catch (Exception e){
170 throw new RuntimeException(e);
171 }
172 return response;
173 }
174
175 /**
176 * @param supported
177 */
178 private void send(OperationsSupported operation) throws URISyntaxException {
179 if (operation.equals(OperationsSupported.GET_PRINTER_ATTRIBUTES)) {
180 sendGetPrinterAttributes();
181 } else if (operation.equals(OperationsSupported.CUPS_GET_PRINTERS)) {
182 sendCupsGetPrinter();
183 } else{
184 log.warning("Call to request " + operation.toString() + " not implemented");
185 this.response.attributes = new HashMap();
186 }
187 response.status = IppStatus.SUCCESSFUL_OK;
188 }
189
190 /**
191 *
192 */
193 private void sendCupsGetPrinter() throws URISyntaxException {
194 Map attributes = new HashMap();
195 Set printerUris = new HashSet();
196 printerUris.add(new PrinterUriSupported(new URI("http://127.0.0.1")));
197 attributes.put(IppAttributeName.PRINTER_URI_SUPPORTED.getCategory(),printerUris);
198 this.response.attributes = attributes;
199 }
200
201 /**
202 *
203 */
204 private void sendGetPrinterAttributes() throws URISyntaxException {
205 //
206 // - Printer attributes that are Job Template attributes ("xxx-
207 // default" "xxx-supported", and "xxx-ready" in the Table in
208 // Section 4.2),
209 // - "pdl-override-supported",
210 // - "compression-supported",
211 // - "job-k-octets-supported",
212 // - "job-impressions-supported",
213 // - "job-media-sheets-supported",
214 // - "printer-driver-installer",
215 // - "color-supported", and
216 // - "reference-uri-schemes-supported"
217
218 Map attributes = new HashMap();
219 Set pdlOverride = new HashSet();
220 pdlOverride.add(PDLOverrideSupported.ATTEMPTED);
221 pdlOverride.add(PDLOverrideSupported.NOT_ATTEMPTED);
222 attributes.put(IppAttributeName.PDL_OVERRIDE_SUPPORTED.getCategory(), pdlOverride);
223 Set compression = new HashSet();
224 compression.add(new CompressionSupported(Compression.GZIP.toString(), Locale.getDefault()));
225 compression.add(new CompressionSupported(Compression.NONE.toString(), Locale.getDefault()));
226 attributes.put(IppAttributeName.COMPRESSION_SUPORTED.getCategory(), compression);
227 Set jobKOctets = new HashSet();
228 jobKOctets.add(new JobKOctetsSupported(1, 10));
229 attributes.put(IppAttributeName.JOB_K_OCTETS_SUPPORTED.getCategory(), jobKOctets);
230 Set jobImpressions = new HashSet();
231 jobImpressions.add(new JobImpressionsSupported(1, 10));
232 attributes.put(IppAttributeName.JOB_IMPRESSIONS_SUPPORTED.getCategory(), jobImpressions);
233 Set jobMediaSheets = new HashSet();
234 jobMediaSheets.add(new JobMediaSheetsSupported(1, 10));
235 attributes.put(IppAttributeName.JOB_MEDIA_SHEETS_SUPPORTED.getCategory(), jobMediaSheets);
236 Set printerDriverInst = new HashSet();
237 printerDriverInst.add(new PrinterDriverInstaller(new URI("http://127.0.0.1")));
238 attributes.put(IppAttributeName.PRINTER_DRIVER_INSTALLER.getCategory(), printerDriverInst);
239 Set color = new HashSet();
240 color.add(ColorSupported.SUPPORTED);
241 attributes.put(IppAttributeName.COLOR_SUPPORTED.getCategory(), color);
242 Set refUriScheme = new HashSet();
243 refUriScheme.add(ReferenceUriSchemesSupported.HTTP);
244 refUriScheme.add(ReferenceUriSchemesSupported.FTP);
245 attributes.put(IppAttributeName.REFERENCE_URI_SCHEMES_SUPPORTED.getCategory(), refUriScheme);
246
247 //Attributes named in 4.2 of rfc2911
248 Set jobPrio = new HashSet();
249 jobPrio.add(new JobPrioritySupported(99));
250 attributes.put(IppAttributeName.JOB_PRIORIY_SUPPORTED.getCategory(), jobPrio);
251 Set jobHoldUntil = new HashSet();
252 jobHoldUntil.add(new JobHoldUntilSupported(new LdJobHoldUntil("12:00:00",Locale.getDefault()).toString(),Locale.getDefault()));
253 jobHoldUntil.add(new JobHoldUntilSupported(LdJobHoldUntil.THIRD_SHIFT.toString(),Locale.getDefault()));
254 attributes.put(IppAttributeName.JOB_HOLD_UNTIL_SUPPORTED.getCategory(), jobHoldUntil);
255 Set jobSheets = new HashSet();
256 jobSheets.add(new JobSheetsSupported(JobSheets.NONE.toString(), Locale.getDefault()));
257 jobSheets.add(new JobSheetsSupported(JobSheets.STANDARD.toString(), Locale.getDefault()));
258 attributes.put(IppAttributeName.JOB_SHEETS_SUPORTED.getCategory(), jobSheets);
259 Set multipleDocumentHandling = new HashSet();
260 multipleDocumentHandling.add(
261 new MultipleDocumentHandlingSupported(
262 MultipleDocumentHandling.SEPARATE_DOCUMENTS_COLLATED_COPIES.toString(),
263 Locale.getDefault()));
264 multipleDocumentHandling.add(
265 new MultipleDocumentHandlingSupported(
266 MultipleDocumentHandling.SINGLE_DOCUMENT.toString(),
267 Locale.getDefault()));
268 attributes.put(
269 IppAttributeName.MULTIPLE_DOCUMENT_HANDLING_SUPPORTED.getCategory(),
270 multipleDocumentHandling);
271 Set copies = new HashSet();
272 copies.add(new CopiesSupported(1, 100));
273 attributes.put(IppAttributeName.COPIES_SUPPORTED.getCategory(), copies);
274 Set finishings = new HashSet();
275 finishings.add(new FinishingsSupported(1));
276 finishings.add(new FinishingsSupported(2));
277 attributes.put(IppAttributeName.FINISHINGS_SUPPORTED.getCategory(), finishings);
278 Set pageRanges = new HashSet();
279 pageRanges.add(PageRangesSupported.SUPPORTED);
280 attributes.put(IppAttributeName.PAGE_RANGES_SUPPORTED.getCategory(), pageRanges);
281 Set sides = new HashSet();
282 sides.add(new SidesSupported(Sides.DUPLEX.toString(), Locale.getDefault()));
283 sides.add(new SidesSupported(Sides.TWO_SIDED_SHORT_EDGE.toString(), Locale.getDefault()));
284 attributes.put(IppAttributeName.SIDES_SUPPORTED.getCategory(), sides);
285 Set numberUp = new HashSet();
286 numberUp.add(new NumberUpSupported(1, 10));
287 numberUp.add(new NumberUpSupported(100));
288 attributes.put(IppAttributeName.NUMBER_UP_SUPPORTED.getCategory(), numberUp);
289 Set orientationReq = new HashSet();
290 orientationReq.add(new OrientationRequestedSupported(OrientationRequested.LANDSCAPE.getValue()));
291 orientationReq.add(new OrientationRequestedSupported(OrientationRequested.PORTRAIT.getValue()));
292 attributes.put(IppAttributeName.ORIENTATION_REQUESTED_SUPPORTED.getCategory(), orientationReq);
293 Set media = new HashSet();
294 media.add(new MediaSupported("test", Locale.getDefault()));
295 media.add(new MediaSupported("test2", Locale.getDefault()));
296 attributes.put(IppAttributeName.MEDIA_SUPPORTED.getCategory(), media);
297 //media-ready not implemented
298 //printer-resolution-supported not implemented
299 //print-quality-supported not implemented
300 response.attributes = attributes;
301
302 }
303
304 private IppResponse getResponse() throws IOException {
305 return this.response;
306 }
307 }