[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HttpWebResponse.java,1.9,1.10 WebResponse
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-06-11 21:21:12
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv18011/src/com/meterware/httpunit
Modified Files:
HttpWebResponse.java WebResponse.java
Log Message:
Added support for binary downloads
Index: HttpWebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpWebResponse.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- HttpWebResponse.java 2001/05/31 16:44:15 1.9
+++ HttpWebResponse.java 2001/06/11 21:21:09 1.10
@@ -44,8 +44,11 @@
**/
HttpWebResponse( String target, URL url, URLConnection connection ) throws IOException {
super( target, url );
+ _connection = connection;
readHeaders( connection );
- if (_responseCode == HttpURLConnection.HTTP_OK) loadResponseText( url, connection );
+ if (_responseCode == HttpURLConnection.HTTP_OK && getContentType().startsWith( "text" )) {
+ loadResponseText( connection );
+ }
}
@@ -69,10 +72,27 @@
* Returns the text of the response (excluding headers) as a string. Use this method in preference to 'toString'
* which may be used to represent internal state of this object.
**/
- public String getText() {
+ public String getText() throws IOException {
+ if (_responseText == null) loadResponseText( _connection );
return _responseText;
}
+
+ /**
+ * Returns an input stream for reading the contents of this reply.
+ **/
+ public InputStream getInputStream() throws IOException {
+ if (_responseText != null) {
+ return new ByteArrayInputStream( _responseText.getBytes() );
+ } else {
+ return new BufferedInputStream( _connection.getInputStream() );
+ }
+ }
+
+
+ public String toString() {
+ return "[headers=" + _headers + "; ??]";
+ }
//-------------------------------------------- private members ------------------------------------------------
@@ -83,11 +103,13 @@
private int _responseCode = HttpURLConnection.HTTP_OK;
+ private URLConnection _connection;
+
private String _responseText;
private Hashtable _headers = new Hashtable();
- private void loadResponseText( URL url, URLConnection connection ) throws IOException {
+ private void loadResponseText( URLConnection connection ) throws IOException {
StringBuffer sb = new StringBuffer();
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Index: WebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- WebResponse.java 2001/06/06 13:25:04 1.34
+++ WebResponse.java 2001/06/11 21:21:09 1.35
@@ -20,6 +20,8 @@
*
*******************************************************************************************************************/
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
import java.io.IOException;
import java.io.StringReader;
@@ -148,8 +150,15 @@
* which may be used to represent internal state of this object.
**/
abstract
- public String getText();
+ public String getText() throws IOException;
+
+ /**
+ * Returns an input stream for reading the contents of this reply.
+ **/
+ abstract
+ public InputStream getInputStream() throws IOException;
+
/**
* Returns the names of the frames found in the page in the order in which they appear.
@@ -296,10 +305,8 @@
//---------------------------------------- Object methods --------------------------------------------
-
- public String toString() {
- return getText();
- }
+ abstract
+ public String toString();
//----------------------------------------- protected members -----------------------------------------------
@@ -435,8 +442,12 @@
private ReceivedPage getReceivedPage() throws SAXException {
if (_page == null) {
- if (!isHTML()) throw new NotHTMLException( getContentType() );
- _page = new ReceivedPage( _url, _target, getText(), getCharacterSet() );
+ try {
+ if (!isHTML()) throw new NotHTMLException( getContentType() );
+ _page = new ReceivedPage( _url, _target, getText(), getCharacterSet() );
+ } catch (IOException e) {
+ throw new SAXException( e );
+ }
}
return _page;
}
@@ -457,6 +468,8 @@
Method getDocumentMethod = parserClass.getMethod( "getDocument", null );
doc = (Document)getDocumentMethod.invoke( parser, null );
+ } catch (IOException e) {
+ throw new SAXException( e );
} catch (InvocationTargetException ex) {
Throwable tex = ex.getTargetException();
if (tex instanceof SAXException) {
@@ -519,6 +532,19 @@
**/
public String getText() {
return _responseText;
+ }
+
+
+ /**
+ * Returns an input stream for reading the contents of this reply.
+ **/
+ public InputStream getInputStream() {
+ return new ByteArrayInputStream( _responseText.getBytes() );
+ }
+
+
+ public String toString() {
+ return "DefaultWebResponse [" + _responseText + "]";
}
|