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