1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java,v 1.18 2004/02/22 18:08:49 olegk Exp $
3    * $Revision: 1.18 $
4    * $Date: 2004/02/22 18:08:49 $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
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             // write the header to a byte array
127             ByteArrayOutputStream headerOutputStream = new ByteArrayOutputStream();
128             OutputStreamWriter writer = new OutputStreamWriter( headerOutputStream );
129             writer.write((String) headers.elementAt(hits));
130             // terminate the headers
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             // combine the header and body content so they can be read from one steam
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         //do nothing, as there's nothing to release
210     }
211 
212 }
213