001: package org.objectweb.celtix.bus.transports.http.protocol.pipe;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006: import java.io.PipedInputStream;
007: import java.io.PipedOutputStream;
008: import java.net.URL;
009: import java.net.URLConnection;
010: import java.util.Arrays;
011: import java.util.Collections;
012: import java.util.HashMap;
013: import java.util.List;
014: import java.util.Map;
015: import java.util.Set;
016:
017: public class PipeURLConnection extends URLConnection {
018:
019: Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
020: PipedOutputStream pout;
021: PipeResponse response;
022:
023: public PipeURLConnection(URL url) {
024: super (url);
025: }
026:
027: public void connect() throws IOException {
028: PipedInputStream pin = new BiggerPipeInputStream();
029: pout = new PipedOutputStream(pin);
030: response = PipeServer.startDispatch(url, this , pin,
031: requestHeaders);
032: connected = true;
033: }
034:
035: public InputStream getInputStream() throws IOException {
036: return response.getInputStream(getReadTimeout());
037: }
038:
039: public OutputStream getOutputStream() throws IOException {
040: if (!connected) {
041: connect();
042: }
043: return pout;
044: }
045:
046: private void setupReceive() throws IOException {
047: if (pout != null) {
048: pout.flush();
049: pout.close();
050: pout = null;
051: }
052: }
053:
054: //request properties support
055: public void setRequestProperty(String key, String value) {
056: super .setRequestProperty(key, value);
057: requestHeaders.put(key, Arrays.asList(new String[] { value }));
058: }
059:
060: public void addRequestProperty(String key, String value) {
061: super .addRequestProperty(key, value);
062: List<String> list = requestHeaders.get(key);
063: if (null == list) {
064: setRequestProperty(key, value);
065: } else {
066: list.add(value);
067: }
068: }
069:
070: public String getRequestProperty(String key) {
071: super .getRequestProperty(key);
072: List<String> list = requestHeaders.get(key);
073: if (list != null && !list.isEmpty()) {
074: if (list.size() == 1) {
075: return list.get(0);
076: } else {
077: StringBuffer buf = new StringBuffer(list.get(0));
078: for (int x = 1; x < list.size(); x++) {
079: buf.append(";");
080: buf.append(list.get(x));
081: }
082: return buf.toString();
083: }
084: }
085: return null;
086: }
087:
088: public Map<String, List<String>> getRequestProperties() {
089: return Collections.unmodifiableMap(requestHeaders);
090: }
091:
092: //response fields
093: public Map<String, List<String>> getHeaderFields() {
094: try {
095: setupReceive();
096: return Collections.unmodifiableMap(response
097: .getResponseHeaders(getReadTimeout()));
098: } catch (IOException e) {
099: return null;
100: }
101: }
102:
103: public String getHeaderField(String name) {
104: try {
105: setupReceive();
106: List<String> list = response.getResponseHeaders(
107: getReadTimeout()).get(name);
108: if (list != null && !list.isEmpty()) {
109: if (list.size() == 1) {
110: return list.get(0);
111: } else {
112: StringBuffer buf = new StringBuffer(list.get(0));
113: for (int x = 1; x < list.size(); x++) {
114: buf.append(";");
115: buf.append(list.get(x));
116: }
117: return buf.toString();
118: }
119: }
120: } catch (IOException e) {
121: //ignore
122: }
123: return null;
124: }
125:
126: public String getHeaderFieldKey(int n) {
127: int x = 0;
128: Set<String> keys;
129: try {
130: setupReceive();
131: keys = response.getResponseHeaders(getReadTimeout())
132: .keySet();
133: } catch (IOException e) {
134: return null;
135: }
136: for (String name : keys) {
137: if (x == n) {
138: return name;
139: }
140: ++x;
141: }
142: return null;
143: }
144:
145: public String getHeaderField(int n) {
146: int x = 0;
147: Set<String> keys;
148: try {
149: setupReceive();
150: keys = response.getResponseHeaders(getReadTimeout())
151: .keySet();
152: } catch (IOException e) {
153: return null;
154: }
155: for (String name : keys) {
156: if (x == n) {
157: return getHeaderField(name);
158: }
159: ++x;
160: }
161: return null;
162: }
163:
164: private static class BiggerPipeInputStream extends PipedInputStream {
165: BiggerPipeInputStream() {
166: super ();
167: buffer = new byte[PIPE_SIZE * 4];
168: }
169: }
170: }
|