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 class SimpleHttpConnection extends HttpConnection {
55
56 static Log log = LogFactory.getLog("httpclient.test");
57
58 int hits = 0;
59
60 Vector headers = new Vector();
61 Vector bodies = new Vector();
62
63 InputStream inputStream;
64
65 ByteArrayOutputStream bodyOutputStream = null;
66
67 public void addResponse(String header) {
68 addResponse(header, "");
69 }
70
71 public void addResponse(String header, String body) {
72 headers.add(header);
73 bodies.add(body);
74 }
75
76 public SimpleHttpConnection(String header, String body) {
77 this();
78 headers.add(header);
79 bodies.add(body);
80 }
81
82 public SimpleHttpConnection() {
83 super(null, -1, "localhost", null, 80, Protocol.getProtocol("http"));
84 }
85
86 public SimpleHttpConnection(
87 String proxyHost,
88 int proxyPort,
89 String host,
90 String virtualHost,
91 int port,
92 Protocol protocol) {
93 super(proxyHost, proxyPort, host, virtualHost, port, protocol);
94 }
95
96 public SimpleHttpConnection(String host, int port){
97 super(host, port, Protocol.getProtocol("http"));
98 }
99
100 public SimpleHttpConnection(String host, int port, String schema){
101 super(host, port, Protocol.getProtocol(schema));
102 }
103
104 public void assertOpen() throws IllegalStateException {
105 if (inputStream == null) {
106 throw new IllegalStateException();
107 }
108 }
109
110 public void assertNotOpen() throws IllegalStateException{
111 if (inputStream != null) {
112 throw new IllegalStateException();
113 }
114 }
115
116 public boolean isOpen() {
117 return inputStream != null;
118 }
119
120 public void open() throws IOException {
121 if (inputStream != null) return;
122
123 try{
124 log.debug("hit: " + hits);
125
126
127 ByteArrayOutputStream headerOutputStream = new ByteArrayOutputStream();
128 OutputStreamWriter writer = new OutputStreamWriter( headerOutputStream );
129 writer.write((String) headers.elementAt(hits));
130
131 writer.write("\r\n");
132 writer.close();
133
134 byte[] headerContent = headerOutputStream.toByteArray();
135 byte[] bodyContent = EncodingUtil.getBytes((String)bodies.elementAt(hits), "ISO-8859-1");
136
137
138 byte[] content = new byte[headerContent.length + bodyContent.length];
139 System.arraycopy(headerContent, 0, content, 0, headerContent.length);
140 System.arraycopy(bodyContent, 0, content, headerContent.length, bodyContent.length);
141
142 inputStream = new ByteArrayInputStream( content );
143 bodyOutputStream = new ByteArrayOutputStream();
144 hits++;
145 } catch (ArrayIndexOutOfBoundsException aiofbe) {
146 throw new IOException("SimpleHttpConnection has been opened more times " +
147 "than it has responses. You might need to call addResponse().");
148 }
149 }
150
151 public void close() {
152 if (inputStream != null) {
153 try { inputStream.close(); } catch(IOException e) {}
154 inputStream = null;
155 }
156 if (bodyOutputStream != null) {
157 try { bodyOutputStream.close(); } catch(IOException e) {}
158 bodyOutputStream = null;
159 }
160 }
161
162 public boolean isResponseAvailable() throws IOException {
163 assertOpen();
164 return inputStream.available() > 0;
165 }
166
167 public boolean isResponseAvailable(int timeout) throws IOException {
168 return isResponseAvailable();
169 }
170
171 public void write(byte[] data)
172 throws IOException, IllegalStateException, HttpRecoverableException {
173 }
174
175 public void writeLine()
176 throws IOException, IllegalStateException, HttpRecoverableException {
177 }
178
179 public String readLine(String charset)
180 throws IOException, IllegalStateException {
181 String str = HttpParser.readLine(inputStream, charset);
182 log.debug("read: " + str);
183 return str;
184 }
185
186 /***
187 * @deprecated
188 */
189 public String readLine()
190 throws IOException, IllegalStateException {
191 String str = HttpParser.readLine(inputStream);
192 log.debug("read: " + str);
193 return str;
194 }
195
196 public InputStream getResponseInputStream() {
197 return inputStream;
198 }
199
200 public OutputStream getRequestOutputStream() {
201 return bodyOutputStream;
202 }
203
204 public void flushRequestOutputStream() throws IOException {
205 assertOpen();
206 }
207
208 public void releaseConnection() {
209
210 }
211
212 }
213