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: /**
018: * @author Igor A. Pyankov
019: * @version $Revision: 1.2 $
020: */package org.apache.harmony.x.print.ipp;
021:
022: import java.net.URI;
023: import java.security.AccessController;
024: import java.security.PrivilegedAction;
025: import java.util.Vector;
026:
027: public class IppPrinter {
028: protected static int verbose = 0;
029: protected URI printeruri;
030: protected IppAttributeGroupSet agroups; /* all attributes as in IppRequest */
031:
032: public static int getVerbose() {
033: return verbose;
034: }
035:
036: public static void setVerbose(int newverbose) {
037: verbose = newverbose;
038: IppClient.setVerbose(verbose);
039: }
040:
041: public static void doVerbose(String v) {
042: System.out.println(v);
043: }
044:
045: public static void doVerbose(int level, String v) {
046: if (verbose >= level) {
047: System.out.println(v);
048: }
049: }
050:
051: public IppPrinter(URI uri) throws Exception {
052: this .printeruri = uri;
053:
054: IppClient c = new IppClient(this .printeruri);
055: IppRequest request;
056: IppResponse response;
057: IppAttributeGroup agroup;
058: Vector va = new Vector();
059:
060: request = new IppRequest();
061: request.setVersion(1, 1);
062: request.setOperationId(IppOperation.GET_PRINTER_ATTRIBUTES);
063: agroup = request
064: .newGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
065: agroup.add(new IppAttribute(IppAttribute.TAG_CHARSET,
066: "attributes-charset", "utf-8"));
067: agroup.add(new IppAttribute(IppAttribute.TAG_NATURAL_LANGUAGE,
068: "attributes-natural-language", "en-us"));
069: agroup.add(new IppAttribute(IppAttribute.TAG_URI,
070: "printer-uri", uri.toString()));
071: va.add("printer-uri-supported".getBytes());
072: va.add("ipp-versions-supported".getBytes());
073: agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
074: "requested-attributes", va));
075:
076: response = c.request(request.getBytes());
077:
078: if (response.operationid >= 0x0000
079: && response.operationid <= 0x00FF) {
080: if (response.ippversion[0] != 1
081: || response.ippversion[1] != 1) {
082: throw new IppException(
083: "Can't use ipp protocol version "
084: + response.ippversion[0] + "."
085: + response.ippversion[1]
086: + " of printer <" + uri.toString()
087: + ">");
088: }
089: } else {
090: throw new IppException("Can't use ipp printer <"
091: + uri.toString() + ">" + "\n" + response.toString());
092: }
093: this .agroups = new IppAttributeGroupSet();
094: }
095:
096: public URI getURI() {
097: return printeruri;
098: }
099:
100: public IppResponse requestPrinterAttributes() throws Exception {
101: return requestPrinterAttributes("all", null);
102: }
103:
104: public IppResponse requestJobTemplateAttributes() throws Exception {
105: return requestPrinterAttributes("job-template", null);
106: }
107:
108: public IppResponse requestPrinterDescriptionAttributes()
109: throws Exception {
110: //return requestPrinterAttributes("printer-description", null);
111: return requestPrinterAttributes("all", null);
112: }
113:
114: public IppResponse requestPrinterAttributes(String what,
115: String mimetype) throws Exception {
116: IppClient c = new IppClient(printeruri);
117: IppRequest request;
118: IppResponse response;
119: IppAttributeGroup agroup;
120: Vector va = new Vector();
121:
122: request = new IppRequest();
123: request.setVersion(1, 1);
124: request.setOperationId(IppOperation.GET_PRINTER_ATTRIBUTES);
125: agroup = request
126: .newGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
127: agroup.add(new IppAttribute(IppAttribute.TAG_CHARSET,
128: "attributes-charset", "utf-8"));
129: agroup.add(new IppAttribute(IppAttribute.TAG_NATURAL_LANGUAGE,
130: "attributes-natural-language", "en-us"));
131: agroup.add(new IppAttribute(IppAttribute.TAG_URI,
132: "printer-uri", printeruri.toString()));
133: va = new Vector();
134: va.add(what.getBytes());
135: agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
136: "requested-attributes", va));
137: agroup.add(new IppAttribute(IppAttribute.TAG_BOOLEAN,
138: "ipp-attribute-fidelity", 0x01));
139: if (mimetype != null && mimetype.length() > 0) {
140: agroup.add(new IppAttribute(IppAttribute.TAG_MIMEMEDIATYPE,
141: "document-format", mimetype));
142: }
143:
144: response = c.request(request.getBytes());
145:
146: if (response.operationid >= 0x0000
147: && response.operationid <= 0x00FF) {
148: return response;
149: }
150: return null;
151: }
152:
153: public boolean setAttribute(String aname, Object avalue) {
154: return agroups.setAttribute(aname, avalue);
155: }
156:
157: public IppResponse requestValidateJob(String jobname,
158: IppDocument document, IppAttributeGroupSet agset)
159: throws Exception {
160: IppClient c = new IppClient(printeruri);
161: IppRequest request;
162: IppResponse response;
163: IppAttributeGroup opa, joba;
164:
165: request = new IppRequest();
166: request.setVersion(1, 1);
167: request.setOperationId(IppOperation.VALIDATE_JOB);
168:
169: opa = request
170: .newGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
171: opa.add(new IppAttribute(IppAttribute.TAG_CHARSET,
172: "attributes-charset", "utf-8"));
173: opa.add(new IppAttribute(IppAttribute.TAG_NATURAL_LANGUAGE,
174: "attributes-natural-language", "en-us"));
175: opa.add(new IppAttribute(IppAttribute.TAG_URI, "printer-uri",
176: printeruri.toString()));
177: opa.add(new IppAttribute(IppAttribute.TAG_MIMEMEDIATYPE,
178: "document-format", document.getFormat()));
179: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
180: "requesting-user-name", (String) AccessController
181: .doPrivileged(new PrivilegedAction() {
182: public Object run() {
183: return System.getProperty("user.name");
184: }
185: })));
186: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
187: "job-name", jobname));
188: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
189: "document-name", document.getName()));
190: opa.add(new IppAttribute(IppAttribute.TAG_BOOLEAN,
191: "ipp-attribute-fidelity", 0x01));
192: opa
193: .set((IppAttributeGroup) document
194: .getAgroups()
195: .get(
196: new Integer(
197: IppAttributeGroup.TAG_OPERATION_ATTRIBUTES)));
198: if (agset != null) {
199: opa.set((IppAttributeGroup) agset.get(new Integer(
200: IppAttributeGroup.TAG_OPERATION_ATTRIBUTES)));
201: }
202:
203: joba = request
204: .newGroup(IppAttributeGroup.TAG_JOB_TEMPLATE_ATTRIBUTES);
205: joba
206: .set((IppAttributeGroup) document
207: .getAgroups()
208: .get(
209: new Integer(
210: IppAttributeGroup.TAG_JOB_TEMPLATE_ATTRIBUTES)));
211: if (agset != null) {
212: joba.set((IppAttributeGroup) agset.get(new Integer(
213: IppAttributeGroup.TAG_JOB_TEMPLATE_ATTRIBUTES)));
214: }
215:
216: response = c.request(request);
217:
218: return response;
219: }
220:
221: /*
222: * The method validate mimetypes.
223: * Returned array is the same as parameter,
224: * just with null in position of unsupported mimetypes.
225: * This method created for performance issue
226: */
227: public String[] requestGetSupportedMimeTypes(String[] mimetypes)
228: throws Exception {
229: IppClient c = new IppClient(printeruri);
230: IppRequest request;
231: IppResponse response;
232: IppAttributeGroup opa;
233:
234: request = new IppRequest();
235: request.setVersion(1, 1);
236: request.setOperationId(IppOperation.VALIDATE_JOB);
237:
238: opa = request
239: .newGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
240: opa.add(new IppAttribute(IppAttribute.TAG_CHARSET,
241: "attributes-charset", "utf-8"));
242: opa.add(new IppAttribute(IppAttribute.TAG_NATURAL_LANGUAGE,
243: "attributes-natural-language", "en-us"));
244: opa.add(new IppAttribute(IppAttribute.TAG_URI, "printer-uri",
245: printeruri.toString()));
246: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
247: "requesting-user-name", (String) AccessController
248: .doPrivileged(new PrivilegedAction() {
249: public Object run() {
250: return System.getProperty("user.name");
251: }
252: })));
253: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
254: "job-name", "validatemimetypes"));
255: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
256: "document-name", "validatemimetypes"));
257: opa.add(new IppAttribute(IppAttribute.TAG_BOOLEAN,
258: "ipp-attribute-fidelity", 0x01));
259: opa.add(new IppAttribute(IppAttribute.TAG_MIMEMEDIATYPE,
260: "document-format", ""));
261:
262: String[] validmimes = new String[mimetypes.length];
263: int mimeattrindex = opa.findAttribute("document-format");
264: for (int i = 0, ii = mimetypes.length; i < ii; i++) {
265: opa.set(mimeattrindex, new IppAttribute(
266: IppAttribute.TAG_MIMEMEDIATYPE, "document-format",
267: mimetypes[i]));
268:
269: response = c.request(request);
270:
271: if (response.getStatusCode() == 0) {
272: validmimes[i] = mimetypes[i];
273: } else {
274: validmimes[i] = null;
275: }
276: }
277:
278: return validmimes;
279: }
280:
281: public IppResponse requestPrintJob(String jobname,
282: IppDocument document, IppAttributeGroupSet agset)
283: throws Exception {
284: IppClient c = new IppClient(printeruri);
285: IppRequest request;
286: IppResponse response;
287: IppAttributeGroup opa, joba;
288:
289: request = new IppRequest();
290: request.setVersion(1, 1);
291: request.setOperationId(IppOperation.PRINT_JOB);
292:
293: opa = request
294: .newGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
295: opa.add(new IppAttribute(IppAttribute.TAG_CHARSET,
296: "attributes-charset", "utf-8"));
297: opa.add(new IppAttribute(IppAttribute.TAG_NATURAL_LANGUAGE,
298: "attributes-natural-language", "en-us"));
299: opa.add(new IppAttribute(IppAttribute.TAG_URI, "printer-uri",
300: printeruri.toString()));
301: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
302: "requesting-user-name", (String) AccessController
303: .doPrivileged(new PrivilegedAction() {
304: public Object run() {
305: return System.getProperty("user.name");
306: }
307: })));
308: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
309: "job-name", jobname));
310: opa.add(new IppAttribute(IppAttribute.TAG_NAMEWITHOUTLANGUAGE,
311: "document-name", document.getName()));
312: opa.add(new IppAttribute(IppAttribute.TAG_MIMEMEDIATYPE,
313: "document-format", document.getFormat()));
314: opa.add(new IppAttribute(IppAttribute.TAG_BOOLEAN,
315: "ipp-attribute-fidelity", 0x01));
316: opa
317: .set((IppAttributeGroup) document
318: .getAgroups()
319: .get(
320: new Integer(
321: IppAttributeGroup.TAG_OPERATION_ATTRIBUTES)));
322:
323: joba = request
324: .newGroup(IppAttributeGroup.TAG_JOB_TEMPLATE_ATTRIBUTES);
325: joba
326: .set((IppAttributeGroup) document
327: .getAgroups()
328: .get(
329: new Integer(
330: IppAttributeGroup.TAG_JOB_TEMPLATE_ATTRIBUTES)));
331: if (agset != null) {
332: joba.set((IppAttributeGroup) agset.get(new Integer(
333: IppAttributeGroup.TAG_JOB_TEMPLATE_ATTRIBUTES)));
334: }
335:
336: request.setDocument(document.getDocument());
337:
338: DoIppRequest job = new DoIppRequest(c, request);
339: response = job.getResponse();
340: if (job.exceptionOccured()) {
341: throw new IppException(job.getException().getMessage());
342: }
343:
344: return response;
345: }
346:
347: private class DoIppRequest extends Thread {
348: Object lock;
349: IppClient client;
350: IppRequest request;
351: IppResponse response;
352: Exception exception;
353: boolean exceptionisnotnull;
354: boolean requestisdone = false;
355:
356: public DoIppRequest(IppClient ippclient, IppRequest ipprequest) {
357: this .client = ippclient;
358: this .request = ipprequest;
359: this .response = null;
360: this .exception = null;
361: this .exceptionisnotnull = false;
362: this .requestisdone = false;
363: lock = new Object();
364:
365: start();
366: }
367:
368: public void run() {
369: synchronized (lock) {
370: try {
371: response = client.request(request);
372: } catch (Exception e) {
373: if (verbose > 1) {
374: doVerbose(2,
375: "IppPrinter.java: run(): Exception thrown: "
376: + e.toString());
377: e.printStackTrace();
378: }
379: response = null;
380: exception = e;
381: exceptionisnotnull = true;
382: }
383:
384: requestisdone = true;
385: lock.notifyAll();
386: }
387: }
388:
389: /**
390: * Returns the deserialized object. This method will block until the
391: * object is actually available.
392: */
393: IppResponse getResponse() {
394: try {
395: synchronized (lock) {
396: if (requestisdone) {
397: return response;
398: }
399: lock.wait();
400: }
401: } catch (InterruptedException ie) {
402: if (verbose > 1) {
403: doVerbose(2,
404: "IppPrinter.java: getResponse(): Exception thrown: "
405: + ie.toString());
406: ie.printStackTrace();
407: }
408: return null;
409: }
410: return response;
411: }
412:
413: boolean exceptionOccured() {
414: return exceptionisnotnull;
415: }
416:
417: Exception getException() {
418: return exception;
419: }
420: }
421: }
|