KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > StringWebResponse


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.io.UnsupportedEncodingException JavaDoc;
43 import java.net.MalformedURLException JavaDoc;
44 import java.net.URL JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.List JavaDoc;
47
48 /**
49  * A simple WebResponse created from a string. Content is assumed to be
50  * of type text/html.
51  *
52  * @version $Revision: 100 $
53  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54  */

55 public class StringWebResponse implements WebResponse {
56     private final String JavaDoc content_;
57     private final String JavaDoc encoding_ = "ISO-8859-1";
58     private final URL JavaDoc url_;
59
60     /**
61      * Create an instance.
62      * @param content The content to return.
63      */

64     public StringWebResponse( final String JavaDoc content ) {
65         content_ = content;
66         try {
67             url_ = new URL JavaDoc("http://HtmlUnitStringWebResponse");
68         }
69         catch( final MalformedURLException JavaDoc e ) {
70             // Theoretically impossible
71
throw new IllegalStateException JavaDoc(e.toString());
72         }
73     }
74
75     /**
76      * Create an instance associated with an originating URL
77      * @param content The content to return.
78      * @param originatingURL The url that this should be associated with
79      */

80     public StringWebResponse( final String JavaDoc content, final URL JavaDoc originatingURL ) {
81         content_ = content;
82         url_ = originatingURL;
83     }
84
85     /**
86      * Return the status code that was returned by the server
87      *
88      * @return See above.
89      */

90     public int getStatusCode() {
91         return 200;
92     }
93
94     /**
95      * Return the status message that was returned from the server
96      *
97      * @return See above
98      */

99     public String JavaDoc getStatusMessage() {
100         return "OK";
101     }
102
103
104     /**
105      * Return the content type returned from the server. Ie "text/html"
106      *
107      * @return See above
108      */

109     public String JavaDoc getContentType() {
110         return "text/html";
111     }
112
113
114     /**
115      * Return the content from the server as a string
116      *
117      * @return See above
118      */

119     public String JavaDoc getContentAsString() {
120         return content_;
121     }
122
123
124     /**
125      * Return the content from the server as an input stream
126      *
127      * @return See above
128      * @exception IOException If an IO problem occurs
129      */

130     public InputStream JavaDoc getContentAsStream() throws IOException JavaDoc {
131         return TextUtil.toInputStream( content_, encoding_ );
132     }
133
134
135     /**
136      * Return the URL that was used to load this page.
137      *
138      * @return The originating URL
139      */

140     public URL JavaDoc getUrl() {
141         return url_;
142     }
143
144
145     /**
146      * Return the response headers as a List of {@link org.org.apache.commons.httpclient.NameValuePair}s.
147      *
148      * @return an empty list.
149      */

150     public List JavaDoc getResponseHeaders() {
151         return Collections.EMPTY_LIST;
152     }
153
154
155     /**
156      * Return the value of the specified header from this response.
157      *
158      * @param headerName The name of the header
159      * @return The value of the specified header
160      */

161     public String JavaDoc getResponseHeaderValue( final String JavaDoc headerName ) {
162         return "";
163     }
164
165
166     /**
167      * Return the time it took to load this web response in milliseconds.
168      * @return The load time.
169      */

170     public long getLoadTimeInMilliSeconds() {
171         return 0;
172     }
173
174     /**
175      * Return the content charset value.
176      * @return The charset value.
177      */

178     public String JavaDoc getContentCharSet() {
179         return encoding_;
180     }
181
182     /**
183      * Return the response body as byte array.
184      * @return response body.
185      */

186     public byte[] getResponseBody() {
187         try {
188             return content_.getBytes(encoding_);
189         }
190         catch( final UnsupportedEncodingException JavaDoc e ) {
191             e.printStackTrace();
192             return new byte[0];
193         }
194     }
195 }
196
197
Popular Tags