Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / com / google / code / jspi / IppResponseIppImpl.java
1 package com.google.code.jspi;
2 import java.io.ByteArrayOutputStream;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.UnsupportedEncodingException;
6 import java.util.logging.Logger;
7
8 import javax.print.attribute.Attribute;
9 import javax.print.attribute.AttributeSet;
10 import javax.print.attribute.HashAttributeSet;
11 import javax.print.attribute.HashPrintJobAttributeSet;
12 import javax.print.attribute.PrintJobAttributeSet;
13
14 import de.lohndirekt.print.attribute.AttributeHelper;
15 import de.lohndirekt.print.attribute.AttributeWriter;
16 import de.lohndirekt.print.attribute.IppDelimiterTag;
17 import de.lohndirekt.print.attribute.IppStatus;
18 import de.lohndirekt.print.attribute.ipp.Charset;
19 import de.lohndirekt.print.attribute.ipp.NaturalLanguage;
20
21 public class IppResponseIppImpl {
22 private final Logger log = Logger.getLogger(this.getClass().getName());
23
24 private final IppStatus status;
25
26 // Id wird in der Cups-API zwar Uebergeben, ist aber auch immer 1.
27 private final int id = 1;
28
29 private final PrintJobAttributeSet jobAttributes = new HashPrintJobAttributeSet();
30
31 private final AttributeSet operationAttributes = new HashAttributeSet();
32
33 private final AttributeSet printerAttributes = new HashAttributeSet();
34
35 private static final NaturalLanguage NATURAL_LANGUAGE_DEFAULT = NaturalLanguage.EN;
36
37 private static final Charset CHARSET_DEFAULT = Charset.UTF_8;
38
39 public IppResponseIppImpl(IppStatus status) {
40 super();
41 this.status = status;
42 operationAttributes.add(CHARSET_DEFAULT);
43 operationAttributes.add(NATURAL_LANGUAGE_DEFAULT);
44 }
45
46 /**
47 * @return
48 */
49 public IppStatus getStatus() {
50 return status;
51 }
52
53 private byte[] ippFooter() {
54 byte[] footer = new byte[1];
55 footer[0] = (byte) IppDelimiterTag.END_ATTRIBUTES.getValue();
56 return footer;
57 }
58
59 private byte[] ippHeader() {
60 // Ipp header data according to http://www.ietf.org/rfc/rfc2910.txt
61 ByteArrayOutputStream out = new ByteArrayOutputStream(8);
62 // The first 2 bytes represent the IPP version number (1.1)
63 // major version-number
64 out.write((byte) 1);
65 // minor version-number
66 out.write((byte) 1);
67 // 2 byte status id
68 AttributeWriter.writeInt2(this.status.getStatus(), out);
69 // 4 byte request id
70 AttributeWriter.writeInt4(this.id, out);
71 return out.toByteArray();
72 }
73
74 private byte[] ippAttributes() throws UnsupportedEncodingException {
75 ByteArrayOutputStream out = new ByteArrayOutputStream();
76 operationAttributes(out);
77 printerAttributes(out);
78 jobAttributes(out);
79 byte[] body = out.toByteArray();
80 return body;
81 }
82
83 /**
84 * @param out
85 * @return
86 */
87 private void jobAttributes(ByteArrayOutputStream out)
88 throws UnsupportedEncodingException {
89 if (!jobAttributes.isEmpty()) {
90 out.write((byte) IppDelimiterTag.BEGIN_JOB_ATTRIBUTES.getValue());
91 for (int i = 0; i < jobAttributes.toArray().length; i++) {
92 Attribute attribute = jobAttributes.toArray()[i];
93 AttributeWriter.attributeBytes(attribute, out);
94 }
95 }
96 }
97
98 /**
99 *
100 * @param out
101 * @return
102 */
103 private void printerAttributes(ByteArrayOutputStream out)
104 throws UnsupportedEncodingException {
105 if (!printerAttributes.isEmpty()) {
106 out.write((byte) IppDelimiterTag.BEGIN_PRINTER_ATTRIBUTES
107 .getValue());
108 Attribute[] attributes = printerAttributes.toArray();
109 for (int i = 0; i < attributes.length; i++) {
110 AttributeWriter.attributeBytes(attributes[i], out);
111 }
112 }
113 }
114
115 /**
116 *
117 * @param out
118 * @return
119 */
120 private void operationAttributes(ByteArrayOutputStream out)
121 throws UnsupportedEncodingException {
122 if (!operationAttributes.isEmpty()) {
123 out.write((byte) IppDelimiterTag.BEGIN_OPERATION_ATTRIBUTES
124 .getValue());
125 Attribute[] attributes = AttributeHelper
126 .getOrderedOperationAttributeArray(operationAttributes);
127 for (int i = 0; i < attributes.length; i++) {
128 AttributeWriter.attributeBytes(attributes[i], out);
129 }
130 }
131 }
132
133 public void write(OutputStream os) throws IOException {
134 os.write(this.ippHeader());
135 os.write(this.ippAttributes());
136 os.write(this.ippFooter());
137 }
138
139 public PrintJobAttributeSet getJobAttributes() {
140 return jobAttributes;
141 }
142
143 public AttributeSet getOperationAttributes() {
144 return operationAttributes;
145 }
146
147 public AttributeSet getPrinterAttributes() {
148 return printerAttributes;
149 }
150
151 }