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.io.BufferedInputStream;
023: import java.io.BufferedOutputStream;
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.CharArrayReader;
027: import java.io.DataOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.Reader;
031: import java.net.HttpURLConnection;
032: import java.net.URI;
033: import java.net.URL;
034: import java.net.URLConnection;
035:
036: /*
037: * IppClient is a print client based on IPP protocol.
038: * (see Internet Printing Protocol, http://www.pwg.org/ipp/index.html)
039: */
040:
041: public class IppClient {
042: protected static int verbose = 0;
043:
044: protected URI uri;
045: protected URL url;
046: protected HttpURLConnection server;
047:
048: public static int getVerbose() {
049: return verbose;
050: }
051:
052: public static void setVerbose(int newverbose) {
053: verbose = newverbose;
054: }
055:
056: public static void doVerbose(String v) {
057: System.out.println(v);
058: }
059:
060: public static void doVerbose(int level, String v) {
061: if (verbose >= level) {
062: System.out.println(v);
063: }
064: }
065:
066: public IppClient(URI clienturi) throws Exception {
067: this .uri = clienturi;
068: this .url = new URL("http", uri.getHost(),
069: (uri.getPort() == -1 ? 631 : uri.getPort()), (uri
070: .getPath() == null ? "" : uri.getPath()));
071: }
072:
073: public void request(String method, String content_type)
074: throws Exception {
075: connect();
076: server.setDoInput(true);
077: server.setDoOutput(true);
078: server.setRequestMethod(method);
079: server.setRequestProperty("Content-type", content_type);
080: server.setAllowUserInteraction(false);
081: server.connect();
082: }
083:
084: public IppResponse request(byte[] data) throws Exception {
085: connect();
086: request("POST", "application/ipp", data);
087: IppResponse response = new IppResponse(response().toByteArray());
088: disconnect();
089:
090: return response;
091: }
092:
093: public IppResponse request(IppRequest request) throws Exception {
094: connect();
095: request("POST", "application/ipp", request);
096: IppResponse response = new IppResponse(response().toByteArray());
097: disconnect();
098:
099: return response;
100: }
101:
102: public void request(String method, String content_type, byte[] data)
103: throws Exception {
104: connect();
105: server.setDoInput(true);
106: server.setDoOutput(true);
107: server.setRequestMethod(method);
108: server.setRequestProperty("Content-type", content_type);
109: server.setAllowUserInteraction(false);
110: server.connect();
111:
112: doVerbose(2,
113: "IppClient.java: request(): Write data to output stream");
114: BufferedOutputStream bw = new BufferedOutputStream(server
115: .getOutputStream());
116: bw.write(data, 0, data.length);
117: bw.flush();
118: bw.close();
119: doVerbose(2, "IppClient.java: request(): Write " + data.length
120: + " bytes OK");
121: }
122:
123: public void request(String method, String content_type,
124: IppRequest request) throws Exception {
125: connect();
126: server.setDoInput(true);
127: server.setDoOutput(true);
128: server.setRequestMethod(method);
129: server.setRequestProperty("Content-type", content_type);
130: server.setAllowUserInteraction(false);
131: server.connect();
132:
133: doVerbose(2,
134: "IppClient.java: request(): Write data to output stream");
135: DataOutputStream bw = new DataOutputStream(
136: new BufferedOutputStream(server.getOutputStream()));
137:
138: byte[] data = request.getAgroups().getBytes();
139: Object document = request.getDocument();
140:
141: bw.write(request.getVersion());
142: bw.writeShort(request.getOperationId());
143: bw.writeInt(request.getRequestId());
144: bw.write(data, 0, data.length);
145: doVerbose(2, "IppClient.java: request(): Write header OK");
146:
147: if (document != null) {
148: if (document instanceof InputStream) {
149: InputStream stream = (InputStream) document;
150: byte[] buf = new byte[1024 * 8];
151: int count = 0;
152:
153: doVerbose(
154: 2,
155: "IppClient.java: request(): Write document data to output stream from InpuStream");
156: while ((count = stream.read(buf, 0, buf.length - 10)) != -1) {
157: doVerbose(2, "IppClient.java: request(): Read "
158: + count + " bytes");
159: bw.write(buf, 0, count);
160: doVerbose(2, "IppClient.java: request(): Wrote "
161: + count + " bytes");
162: }
163: ((InputStream) document).close();
164: doVerbose(2,
165: "IppClient.java: request(): Close InputStream");
166: } else if (document instanceof URL) {
167: URLConnection urlconnection = ((URL) document)
168: .openConnection();
169:
170: doVerbose(2,
171: "IppClient.java: request(): Write document data to printer's stream from URL");
172: doVerbose(1,
173: "IppClient.java: request(): document to print: "
174: + ((URL) document).toString());
175: try {
176: BufferedInputStream stream = new BufferedInputStream(
177: urlconnection.getInputStream());
178: byte[] buf = new byte[1024 * 8];
179: int count = 0;
180: while ((count = stream.read(buf, 0, buf.length)) != -1) {
181: doVerbose(2, "IppClient.java: request(): Read "
182: + count + " bytes from "
183: + stream.toString());
184: bw.write(buf, 0, count);
185: doVerbose(2,
186: "IppClient.java: request(): Wrote "
187: + count + " bytes");
188: }
189: stream.close();
190: doVerbose(2,
191: "IppClient.java: request(): Close InputStream "
192: + stream.toString());
193: } catch (IOException e) {
194: if (urlconnection instanceof HttpURLConnection
195: && ((HttpURLConnection) urlconnection)
196: .getResponseCode() == 401) {
197: throw new IppException(
198: "HTTP/1.x 401 Unauthorized access to \n\t"
199: + ((URL) document).toString());
200: }
201: throw e;
202: }
203: } else if (document instanceof byte[]) {
204: InputStream stream = new ByteArrayInputStream(
205: (byte[]) document);
206: byte[] buf = new byte[1024 * 8];
207: int count = 0;
208:
209: while ((count = stream.read(buf, 0, buf.length)) != -1) {
210: bw.write(buf, 0, count);
211: }
212: stream.close();
213: } else if (document instanceof char[]) {
214: CharArrayReader stream = new CharArrayReader(
215: (char[]) document);
216: char[] buf = new char[1024 * 8];
217: int count = 0;
218:
219: while ((count = stream.read(buf, 0, buf.length)) != -1) {
220: bw.writeChars(new String(buf, 0, count));
221: }
222: stream.close();
223: } else if (document instanceof String) {
224: bw.writeChars((String) document);
225: } else if (document instanceof Reader) {
226: char[] buf = new char[1024 * 8];
227: int count = 0;
228:
229: while ((count = ((Reader) document).read(buf, 0,
230: buf.length)) != -1) {
231: bw.writeChars(new String(buf, 0, count));
232: }
233: ((Reader) document).close();
234: }
235: }
236:
237: bw.flush();
238: bw.close();
239: doVerbose(2, "IppClient.java: request(): Write OK");
240: }
241:
242: public ByteArrayOutputStream response() throws Exception {
243: ByteArrayOutputStream resp = new ByteArrayOutputStream(1024);
244: byte[] buf = new byte[1024 * 8];
245: int hasread = 0;
246:
247: doVerbose(2, "IppClient.java: response(): Read from server '"
248: + server.toString() + "' to ByteArrayOutputStream");
249: BufferedInputStream s = new BufferedInputStream(server
250: .getInputStream());
251: while ((hasread = s.read(buf, 0, buf.length)) > 0) {
252: doVerbose(2, "IppClient.java: response(): Read " + hasread
253: + " bytes from BufferedInputStream");
254: resp.write(buf, 0, hasread);
255: doVerbose(2, "IppClient.java: response(): Write " + hasread
256: + " bytes to ByteArrayOutputStream");
257: }
258: s.close();
259: doVerbose(2,
260: "IppClient.java: response(): Close BufferedInputStream");
261:
262: return resp;
263: }
264:
265: public HttpURLConnection connect() throws IOException {
266: if (server == null) {
267: //Authenticator.setDefault(new IppHttpAuthenticator());
268: server = (HttpURLConnection) url.openConnection();
269: }
270: return server;
271: }
272:
273: public void disconnect() {
274: server.disconnect();
275: server = null;
276: }
277:
278: }
|