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
;
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
;
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
;
21 public class IppResponseIppImpl
{
22 private final Logger log
= Logger
.getLogger(this.getClass().getName());
24 private final IppStatus status
;
26 // Id wird in der Cups-API zwar Uebergeben, ist aber auch immer 1.
27 private final int id
= 1;
29 private final PrintJobAttributeSet jobAttributes
= new HashPrintJobAttributeSet();
31 private final AttributeSet operationAttributes
= new HashAttributeSet();
33 private final AttributeSet printerAttributes
= new HashAttributeSet();
35 private static final NaturalLanguage NATURAL_LANGUAGE_DEFAULT
= NaturalLanguage
.EN
;
37 private static final Charset CHARSET_DEFAULT
= Charset
.UTF_8
;
39 public IppResponseIppImpl(IppStatus status
) {
42 operationAttributes
.add(CHARSET_DEFAULT
);
43 operationAttributes
.add(NATURAL_LANGUAGE_DEFAULT
);
49 public IppStatus
getStatus() {
53 private byte[] ippFooter() {
54 byte[] footer
= new byte[1];
55 footer
[0] = (byte) IppDelimiterTag
.END_ATTRIBUTES
.getValue();
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
65 // minor version-number
68 AttributeWriter
.writeInt2(this.status
.getStatus(), out
);
70 AttributeWriter
.writeInt4(this.id
, out
);
71 return out
.toByteArray();
74 private byte[] ippAttributes() throws UnsupportedEncodingException
{
75 ByteArrayOutputStream out
= new ByteArrayOutputStream();
76 operationAttributes(out
);
77 printerAttributes(out
);
79 byte[] body
= out
.toByteArray();
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
);
103 private void printerAttributes(ByteArrayOutputStream out
)
104 throws UnsupportedEncodingException
{
105 if (!printerAttributes
.isEmpty()) {
106 out
.write((byte) IppDelimiterTag
.BEGIN_PRINTER_ATTRIBUTES
108 Attribute
[] attributes
= printerAttributes
.toArray();
109 for (int i
= 0; i
< attributes
.length
; i
++) {
110 AttributeWriter
.attributeBytes(attributes
[i
], out
);
120 private void operationAttributes(ByteArrayOutputStream out
)
121 throws UnsupportedEncodingException
{
122 if (!operationAttributes
.isEmpty()) {
123 out
.write((byte) IppDelimiterTag
.BEGIN_OPERATION_ATTRIBUTES
125 Attribute
[] attributes
= AttributeHelper
126 .getOrderedOperationAttributeArray(operationAttributes
);
127 for (int i
= 0; i
< attributes
.length
; i
++) {
128 AttributeWriter
.attributeBytes(attributes
[i
], out
);
133 public void write(OutputStream os
) throws IOException
{
134 os
.write(this.ippHeader());
135 os
.write(this.ippAttributes());
136 os
.write(this.ippFooter());
139 public PrintJobAttributeSet
getJobAttributes() {
140 return jobAttributes
;
143 public AttributeSet
getOperationAttributes() {
144 return operationAttributes
;
147 public AttributeSet
getPrinterAttributes() {
148 return printerAttributes
;