1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 import java.io.OutputStreamWriter;
40 import java.util.Vector;
41
42 import org.apache.commons.httpclient.protocol.Protocol;
43 import org.apache.commons.httpclient.util.EncodingUtil;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47
48 /***
49 * For test-nohost testing purposes only.
50 *
51 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
52 * @author Michael Becke
53 *
54 * @deprecated
55 */
56 class SimpleHttpConnection extends HttpConnection {
57
58 static Log log = LogFactory.getLog("httpclient.test");
59
60 int hits = 0;
61
62 Vector headers = new Vector();
63 Vector bodies = new Vector();
64
65 InputStream inputStream;
66
67 ByteArrayOutputStream bodyOutputStream = null;
68
69 public void addResponse(String header) {
70 addResponse(header, "");
71 }
72
73 public void addResponse(String header, String body) {
74 headers.add(header);
75 bodies.add(body);
76 }
77
78 public SimpleHttpConnection(String header, String body) {
79 this();
80 headers.add(header);
81 bodies.add(body);
82 }
83
84 public SimpleHttpConnection() {
85 super(null, -1, "localhost", null, 80, Protocol.getProtocol("http"));
86 }
87
88 public SimpleHttpConnection(
89 String proxyHost,
90 int proxyPort,
91 String host,
92 String virtualHost,
93 int port,
94 Protocol protocol) {
95 super(proxyHost, proxyPort, host, virtualHost, port, protocol);
96 }
97
98 public SimpleHttpConnection(String host, int port){
99 super(host, port, Protocol.getProtocol("http"));
100 }
101
102 public SimpleHttpConnection(String host, int port, String schema){
103 super(host, port, Protocol.getProtocol(schema));
104 }
105
106 public void assertOpen() throws IllegalStateException {
107 if (inputStream == null) {
108 throw new IllegalStateException();
109 }
110 }
111
112 public void assertNotOpen() throws IllegalStateException{
113 if (inputStream != null) {
114 throw new IllegalStateException();
115 }
116 }
117
118 public boolean isOpen() {
119 return inputStream != null;
120 }
121
122 public void open() throws IOException {
123 if (inputStream != null) return;
124
125 try{
126 log.debug("hit: " + hits);
127
128
129 ByteArrayOutputStream headerOutputStream = new ByteArrayOutputStream();
130 OutputStreamWriter writer = new OutputStreamWriter( headerOutputStream );
131 writer.write((String) headers.elementAt(hits));
132
133 writer.write("\r\n");
134 writer.close();
135
136 byte[] headerContent = headerOutputStream.toByteArray();
137 byte[] bodyContent = EncodingUtil.getBytes((String)bodies.elementAt(hits), "ISO-8859-1");
138
139
140 byte[] content = new byte[headerContent.length + bodyContent.length];
141 System.arraycopy(headerContent, 0, content, 0, headerContent.length);
142 System.arraycopy(bodyContent, 0, content, headerContent.length, bodyContent.length);
143
144 inputStream = new ByteArrayInputStream( content );
145 bodyOutputStream = new ByteArrayOutputStream();
146 hits++;
147 } catch (ArrayIndexOutOfBoundsException aiofbe) {
148 throw new IOException("SimpleHttpConnection has been opened more times " +
149 "than it has responses. You might need to call addResponse().");
150 }
151 }
152
153 public void close() {
154 if (inputStream != null) {
155 try { inputStream.close(); } catch(IOException e) {}
156 inputStream = null;
157 }
158 if (bodyOutputStream != null) {
159 try { bodyOutputStream.close(); } catch(IOException e) {}
160 bodyOutputStream = null;
161 }
162 }
163
164 public boolean isResponseAvailable() throws IOException {
165 assertOpen();
166 return inputStream.available() > 0;
167 }
168
169 public boolean isResponseAvailable(int timeout) throws IOException {
170 return isResponseAvailable();
171 }
172
173 public void write(byte[] data)
174 throws IOException, IllegalStateException {
175 }
176
177 public void writeLine()
178 throws IOException, IllegalStateException {
179 }
180
181 public String readLine(String charset)
182 throws IOException, IllegalStateException {
183 String str = HttpParser.readLine(inputStream, charset);
184 log.debug("read: " + str);
185 return str;
186 }
187
188 /***
189 * @deprecated
190 */
191 public String readLine()
192 throws IOException, IllegalStateException {
193 String str = HttpParser.readLine(inputStream);
194 log.debug("read: " + str);
195 return str;
196 }
197
198 public InputStream getResponseInputStream() {
199 return inputStream;
200 }
201
202 public OutputStream getRequestOutputStream() {
203 return bodyOutputStream;
204 }
205
206 public void flushRequestOutputStream() throws IOException {
207 assertOpen();
208 }
209
210 public void releaseConnection() {
211
212 }
213
214 }
215