KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > SimpleHttpConnection


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 JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.OutputStreamWriter JavaDoc;
40 import java.util.Vector JavaDoc;
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 JavaDoc headers = new Vector JavaDoc();
60     Vector JavaDoc bodies = new Vector JavaDoc();
61
62     InputStream JavaDoc inputStream;
63
64     ByteArrayOutputStream JavaDoc bodyOutputStream = null;
65
66     public void addResponse(String JavaDoc header) {
67         addResponse(header, "");
68     }
69
70     public void addResponse(String JavaDoc header, String JavaDoc body) {
71         headers.add(header);
72         bodies.add(body);
73     }
74
75     public SimpleHttpConnection(String JavaDoc header, String JavaDoc 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 JavaDoc proxyHost,
87         int proxyPort,
88         String JavaDoc host,
89         String JavaDoc virtualHost,
90         int port,
91         Protocol protocol) {
92         super(proxyHost, proxyPort, host, virtualHost, port, protocol);
93     }
94
95     public SimpleHttpConnection(String JavaDoc host, int port){
96         super(host, port, Protocol.getProtocol("http"));
97     }
98
99     public SimpleHttpConnection(String JavaDoc host, int port, String JavaDoc schema){
100         super(host, port, Protocol.getProtocol(schema));
101     }
102
103     public void assertOpen() throws IllegalStateException JavaDoc {
104         if (inputStream == null) {
105             throw new IllegalStateException JavaDoc();
106         }
107     }
108
109     public void assertNotOpen() throws IllegalStateException JavaDoc{
110         if (inputStream != null) {
111             throw new IllegalStateException JavaDoc();
112         }
113     }
114     
115     public boolean isOpen() {
116         return inputStream != null;
117     }
118     
119     public void open() throws IOException JavaDoc {
120         if (inputStream != null) return;
121
122         try{
123             log.debug("hit: " + hits);
124             
125             // write the header to a byte array
126
ByteArrayOutputStream JavaDoc headerOutputStream = new ByteArrayOutputStream JavaDoc();
127             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc( headerOutputStream );
128             writer.write((String JavaDoc) 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 JavaDoc)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 JavaDoc( content );
142             bodyOutputStream = new ByteArrayOutputStream JavaDoc();
143             hits++;
144         } catch (ArrayIndexOutOfBoundsException JavaDoc aiofbe) {
145             throw new IOException JavaDoc("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 JavaDoc e) {}
153             inputStream = null;
154         }
155         if (bodyOutputStream != null) {
156             try { bodyOutputStream.close(); } catch(IOException JavaDoc e) {}
157             bodyOutputStream = null;
158         }
159     }
160
161     public boolean isResponseAvailable() throws IOException JavaDoc {
162         assertOpen();
163         return inputStream.available() > 0;
164     }
165
166     public boolean isResponseAvailable(int timeout) throws IOException JavaDoc {
167         return isResponseAvailable();
168     }
169
170     public void write(byte[] data)
171     throws IOException JavaDoc, IllegalStateException JavaDoc, HttpRecoverableException {
172     }
173
174     public void writeLine()
175     throws IOException JavaDoc, IllegalStateException JavaDoc, HttpRecoverableException {
176     }
177
178     public String JavaDoc readLine()
179     throws IOException JavaDoc, IllegalStateException JavaDoc {
180         String JavaDoc str = HttpParser.readLine(inputStream);
181         log.debug("read: " + str);
182         return str;
183     }
184
185     public InputStream JavaDoc getResponseInputStream() {
186         return inputStream;
187     }
188
189     public OutputStream JavaDoc getRequestOutputStream() {
190         return bodyOutputStream;
191     }
192
193     public void flushRequestOutputStream() throws IOException JavaDoc {
194         assertOpen();
195     }
196 }
197
198
Popular Tags