1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java,v 1.15 2003/05/08 17:33:53 olegk Exp $
3 * $Revision: 1.15 $
4 * $Date: 2003/05/08 17:33:53 $
5 * ====================================================================
6 *
7 * The Apache Software License, Version 1.1
8 *
9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
10 * reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 *
24 * 3. The end-user documentation included with the redistribution, if
25 * any, must include the following acknowlegement:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowlegement may appear in the software itself,
29 * if and wherever such third-party acknowlegements normally appear.
30 *
31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
32 * Foundation" must not be used to endorse or promote products derived
33 * from this software without prior written permission. For written
34 * permission, please contact apache@apache.org.
35 *
36 * 5. Products derived from this software may not be called "Apache"
37 * nor may "Apache" appear in their names without prior written
38 * permission of the Apache Group.
39 *
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
58 *
59 * [Additional notices, if required by prior licensing conditions]
60 *
61 */
62
63
64 package org.apache.commons.httpclient;
65
66 import java.io.ByteArrayInputStream;
67 import java.io.ByteArrayOutputStream;
68 import java.io.IOException;
69 import java.io.InputStream;
70 import java.io.OutputStream;
71 import java.io.OutputStreamWriter;
72 import java.util.Vector;
73
74 import org.apache.commons.httpclient.protocol.Protocol;
75 import org.apache.commons.logging.Log;
76 import org.apache.commons.logging.LogFactory;
77
78
79 /***
80 * For test-nohost testing purposes only.
81 *
82 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
83 * @author Michael Becke
84 */
85 class SimpleHttpConnection extends HttpConnection {
86
87 static Log log = LogFactory.getLog("httpclient.test");
88
89 int hits = 0;
90
91 Vector headers = new Vector();
92 Vector bodies = new Vector();
93
94 InputStream inputStream;
95
96 ByteArrayOutputStream bodyOutputStream = null;
97
98 public void addResponse(String header) {
99 addResponse(header, "");
100 }
101
102 public void addResponse(String header, String body) {
103 headers.add(header);
104 bodies.add(body);
105 }
106
107 public SimpleHttpConnection(String header, String body) {
108 this();
109 headers.add(header);
110 bodies.add(body);
111 }
112
113 public SimpleHttpConnection() {
114 super(null, -1, "localhost", null, 80, Protocol.getProtocol("http"));
115 }
116
117 public SimpleHttpConnection(
118 String proxyHost,
119 int proxyPort,
120 String host,
121 String virtualHost,
122 int port,
123 Protocol protocol) {
124 super(proxyHost, proxyPort, host, virtualHost, port, protocol);
125 }
126
127 public SimpleHttpConnection(String host, int port){
128 super(host, port, Protocol.getProtocol("http"));
129 }
130
131 public SimpleHttpConnection(String host, int port, String schema){
132 super(host, port, Protocol.getProtocol(schema));
133 }
134
135 public void assertOpen() throws IllegalStateException {
136 if (inputStream == null) {
137 throw new IllegalStateException();
138 }
139 }
140
141 public void assertNotOpen() throws IllegalStateException{
142 if (inputStream != null) {
143 throw new IllegalStateException();
144 }
145 }
146
147 public boolean isOpen() {
148 return inputStream != null;
149 }
150
151 public void open() throws IOException {
152 if (inputStream != null) return;
153
154 try{
155 log.debug("hit: " + hits);
156
157 // write the header to a byte array
158 ByteArrayOutputStream headerOutputStream = new ByteArrayOutputStream();
159 OutputStreamWriter writer = new OutputStreamWriter( headerOutputStream );
160 writer.write((String) headers.elementAt(hits));
161 // terminate the headers
162 writer.write("\r\n");
163 writer.close();
164
165 byte[] headerContent = headerOutputStream.toByteArray();
166 byte[] bodyContent = HttpConstants.getContentBytes((String)bodies.elementAt(hits));
167
168 // combine the header and body content so they can be read from one steam
169 byte[] content = new byte[headerContent.length + bodyContent.length];
170 System.arraycopy(headerContent, 0, content, 0, headerContent.length);
171 System.arraycopy(bodyContent, 0, content, headerContent.length, bodyContent.length);
172
173 inputStream = new ByteArrayInputStream( content );
174 bodyOutputStream = new ByteArrayOutputStream();
175 hits++;
176 } catch (ArrayIndexOutOfBoundsException aiofbe) {
177 throw new IOException("SimpleHttpConnection has been opened more times " +
178 "than it has responses. You might need to call addResponse().");
179 }
180 }
181
182 public void close() {
183 if (inputStream != null) {
184 try { inputStream.close(); } catch(IOException e) {}
185 inputStream = null;
186 }
187 if (bodyOutputStream != null) {
188 try { bodyOutputStream.close(); } catch(IOException e) {}
189 bodyOutputStream = null;
190 }
191 }
192
193 public boolean isResponseAvailable() throws IOException {
194 assertOpen();
195 return inputStream.available() > 0;
196 }
197
198 public boolean isResponseAvailable(int timeout) throws IOException {
199 return isResponseAvailable();
200 }
201
202 public void write(byte[] data)
203 throws IOException, IllegalStateException, HttpRecoverableException {
204 }
205
206 public void writeLine()
207 throws IOException, IllegalStateException, HttpRecoverableException {
208 }
209
210 public String readLine()
211 throws IOException, IllegalStateException {
212 String str = HttpParser.readLine(inputStream);
213 log.debug("read: " + str);
214 return str;
215 }
216
217 public InputStream getResponseInputStream() {
218 return inputStream;
219 }
220
221 public OutputStream getRequestOutputStream() {
222 return bodyOutputStream;
223 }
224
225 public void flushRequestOutputStream() throws IOException {
226 assertOpen();
227 }
228 }
229
This page was automatically generated by Maven