1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/SimpleHttpConnection.java,v 1.15.2.1 2004/02/22 18:21:16 olegk Exp $
3    * $Revision: 1.15.2.1 $
4    * $Date: 2004/02/22 18:21:16 $
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.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             // write the header to a byte array
126             ByteArrayOutputStream headerOutputStream = new ByteArrayOutputStream();
127             OutputStreamWriter writer = new OutputStreamWriter( headerOutputStream );
128             writer.write((String) headers.elementAt(hits));
129             // terminate the headers
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             // combine the header and body content so they can be read from one steam
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