01: package org.objectweb.celtix.bus.transports.http.protocol.pipe;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.OutputStream;
06: import java.io.PipedInputStream;
07: import java.io.PipedOutputStream;
08: import java.net.URLConnection;
09: import java.util.List;
10: import java.util.Map;
11:
12: public class PipeResponse {
13: Map<String, List<String>> reqHeaders;
14: InputStream reqIn;
15: URLConnection connection;
16:
17: Map<String, List<String>> headers;
18: InputStream in;
19: OutputStream pout;
20:
21: public PipeResponse(URLConnection con, InputStream in2,
22: Map<String, List<String>> requestHeaders) {
23: connection = con;
24: reqIn = in2;
25: reqHeaders = requestHeaders;
26: }
27:
28: public URLConnection getURLConnection() {
29: return connection;
30: }
31:
32: public Map<String, List<String>> getRequestHeaders() {
33: return reqHeaders;
34: }
35:
36: public InputStream getRequestInputStream() {
37: return reqIn;
38: }
39:
40: public OutputStream setResponse(Map<String, List<String>> h)
41: throws IOException {
42: synchronized (this ) {
43: PipedInputStream pin = new BiggerPipeInputStream();
44: pout = new PipedOutputStream(pin);
45: headers = h;
46: in = pin;
47: notifyAll();
48: return pout;
49: }
50: }
51:
52: public OutputStream getOutputStream() {
53: return pout;
54: }
55:
56: public Map<String, List<String>> getResponseHeaders(int timeout)
57: throws IOException {
58: synchronized (this ) {
59: if (headers == null) {
60: try {
61: wait(timeout);
62: } catch (InterruptedException e) {
63: //ignore
64: }
65: if (headers == null) {
66: throw new IOException("Timeout getting response");
67: }
68: }
69: return headers;
70: }
71: }
72:
73: public InputStream getInputStream(int timeout) throws IOException {
74: getResponseHeaders(timeout);
75: return in;
76: }
77:
78: private static class BiggerPipeInputStream extends PipedInputStream {
79: BiggerPipeInputStream() {
80: super ();
81: buffer = new byte[PIPE_SIZE * 4];
82: }
83: }
84: }
|