1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus;
|
3
|
|
import java.io.BufferedReader;
|
4
|
|
import java.io.IOException;
|
5
|
|
import java.io.InputStream;
|
6
|
|
import java.io.StringReader;
|
7
|
|
import java.net.HttpURLConnection;
|
8
|
|
import java.util.Vector;
|
9
|
|
import org.apache.commons.httpclient.Header;
|
10
|
|
import org.apache.commons.httpclient.HttpException;
|
11
|
|
import org.apache.commons.logging.Log;
|
12
|
|
import org.apache.commons.logging.LogFactory;
|
13
|
|
import org.apache.cactus.util.ChainedRuntimeException;
|
14
|
|
import org.apache.cactus.util.IoUtil;
|
15
|
|
|
16
|
|
/**
|
17
|
|
* Default web response implementation that provides a minimal
|
18
|
|
* API for asserting returned output stream from the server side. For more
|
19
|
|
* complex assertions, use an <code>com.meterware.httpunit.WebResponse</code>
|
20
|
|
* instead as parameter of your <code>endXXX()</code> methods.
|
21
|
|
*
|
22
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
23
|
|
*
|
24
|
|
* @version $Id: WebResponse.java,v 1.6 2002/07/23 22:35:36 vmassol Exp $
|
25
|
|
*/
|
26
|
|
public class WebResponse {
|
27
|
|
/**
|
28
|
|
* The logger
|
29
|
|
*/
|
30
|
|
private static Log LOGGER;
|
31
|
|
/**
|
32
|
|
* The connection object that was used to call the URL
|
33
|
|
*/
|
34
|
|
private HttpURLConnection connection;
|
35
|
|
/**
|
36
|
|
* The request data that were used to open the connection to the server.
|
37
|
|
*/
|
38
|
|
private WebRequest request;
|
39
|
|
/**
|
40
|
|
* Save the response content for repeatable reads.
|
41
|
|
*/
|
42
|
|
private String content;
|
43
|
|
/**
|
44
|
|
* @param theRequest the request data that were used to open the
|
45
|
|
* connection to the server.
|
46
|
|
* @param theConnection the original <code>HttpURLConnection</code> used
|
47
|
|
* to call the URL
|
48
|
|
*/
|
49
|
61
|
public WebResponse(WebRequest theRequest, HttpURLConnection theConnection) {
|
50
|
61
|
super();
|
51
|
61
|
this.request = theRequest;
|
52
|
61
|
this.connection = theConnection;
|
53
|
|
}
|
54
|
|
/**
|
55
|
|
* @return the original <code>HttpURLConnection</code> used to call the
|
56
|
|
* URL
|
57
|
|
*/
|
58
|
30
|
public HttpURLConnection getConnection() {
|
59
|
30
|
return this.connection;
|
60
|
|
}
|
61
|
|
|
62
|
|
/**
|
63
|
|
* @return the request data the were used to open the connection to the
|
64
|
|
* server
|
65
|
|
*/
|
66
|
12
|
public WebRequest getWebRequest() {
|
67
|
12
|
return this.request;
|
68
|
|
}
|
69
|
|
|
70
|
|
/**
|
71
|
|
* @return the text of the response (excluding headers) as a string.
|
72
|
|
*/
|
73
|
48
|
public String getText() {
|
74
|
48
|
if (this.content == null) {
|
75
|
39
|
try {
|
76
|
39
|
this.content = IoUtil.getText(this.connection.getInputStream());
|
77
|
|
} catch (IOException e) {
|
78
|
0
|
throw new ChainedRuntimeException(e);
|
79
|
|
}
|
80
|
|
}
|
81
|
48
|
return this.content;
|
82
|
|
}
|
83
|
|
|
84
|
|
/**
|
85
|
|
* @return the text of the response (excluding headers) as an array of
|
86
|
|
* strings (each string is a separate line from the output stream).
|
87
|
|
*/
|
88
|
6
|
public String[] getTextAsArray() {
|
89
|
6
|
Vector lines = new Vector();
|
90
|
6
|
try {
|
91
|
6
|
if (this.content == null) {
|
92
|
3
|
this.getText();
|
93
|
|
}
|
94
|
6
|
BufferedReader input = new BufferedReader(new StringReader(this.content));
|
95
|
6
|
String str;
|
96
|
?
|
while (null != (str = input.readLine())){
|
97
|
18
|
lines.addElement(str);
|
98
|
|
}
|
99
|
6
|
input.close();
|
100
|
|
} catch (IOException e) {
|
101
|
0
|
throw new ChainedRuntimeException(e);
|
102
|
|
}
|
103
|
6
|
String[] dummy = new java.lang.String[lines.size()];
|
104
|
6
|
return ((String[])(lines.toArray(dummy)));
|
105
|
|
}
|
106
|
|
|
107
|
|
/**
|
108
|
|
* @return a buffered input stream for reading the response data.
|
109
|
|
**/
|
110
|
0
|
public InputStream getInputStream() {
|
111
|
0
|
try {
|
112
|
0
|
return this.connection.getInputStream();
|
113
|
|
} catch (IOException e) {
|
114
|
0
|
throw new ChainedRuntimeException(e);
|
115
|
|
}
|
116
|
|
}
|
117
|
|
|
118
|
|
/**
|
119
|
|
* Return the first cookie found that has the specified name or null
|
120
|
|
* if not found.
|
121
|
|
*
|
122
|
|
* @param theName the cookie name to find
|
123
|
|
* @return the cookie or null if not found
|
124
|
|
*/
|
125
|
3
|
public Cookie getCookie(String theName) {
|
126
|
3
|
Cookie result = null;
|
127
|
3
|
Cookie[] cookies = this.getCookies();
|
128
|
3
|
for (int i = 0; i < cookies.length; i++) {
|
129
|
3
|
if (cookies[i].getName().equals(theName)) {
|
130
|
3
|
result = cookies[i];
|
131
|
3
|
break;
|
132
|
|
}
|
133
|
|
}
|
134
|
3
|
return result;
|
135
|
|
}
|
136
|
|
|
137
|
|
/**
|
138
|
|
* @return the cookies returned by the server
|
139
|
|
*/
|
140
|
3
|
public Cookie[] getCookies() {
|
141
|
3
|
Cookie[] returnCookies = null;
|
142
|
3
|
String headerName = this.connection.getHeaderFieldKey(0);
|
143
|
3
|
String headerValue = this.connection.getHeaderField(0);
|
144
|
3
|
Vector cookieVector = new Vector();
|
145
|
3
|
for (int i = 1; (headerName != null) || (headerValue != null); i++) {
|
146
|
12
|
WebResponse.LOGGER.debug("Header name = [" + headerName + "]");
|
147
|
12
|
WebResponse.LOGGER.debug("Header value = [" + headerValue + "]");
|
148
|
12
|
if ((headerName != null) && (headerName.toLowerCase().equals("set-cookie") ||
|
149
|
|
headerName.toLowerCase().equals("set-cookie2"))) {
|
150
|
4
|
org.apache.commons.httpclient.Cookie[] cookies;
|
151
|
4
|
try {
|
152
|
4
|
cookies = org.apache.commons.httpclient.Cookie.parse(Cookie.getCookieDomain(
|
153
|
|
this.getWebRequest(), this.getConnection().getURL().getHost()),
|
154
|
|
Cookie.getCookiePort(this.getWebRequest(), this.getConnection().getURL().getPort()),
|
155
|
|
Cookie.getCookiePath(this.getWebRequest(), this.getConnection().getURL().getFile()),
|
156
|
|
new Header(headerName, headerValue));
|
157
|
|
} catch (HttpException e) {
|
158
|
0
|
throw new ChainedRuntimeException("Error parsing cookies", e);
|
159
|
|
}
|
160
|
4
|
for (int j = 0; j < cookies.length; j++) {
|
161
|
7
|
Cookie cookie = new Cookie(cookies[j].getDomain(), cookies[j].getName(),
|
162
|
|
cookies[j].getValue());
|
163
|
7
|
cookie.setComment(cookies[j].getComment());
|
164
|
7
|
cookie.setExpiryDate(cookies[j].getExpiryDate());
|
165
|
7
|
cookie.setPath(cookies[j].getPath());
|
166
|
7
|
cookie.setSecure(cookies[j].getSecure());
|
167
|
7
|
cookieVector.addElement(cookie);
|
168
|
|
}
|
169
|
|
}
|
170
|
12
|
headerName = this.connection.getHeaderFieldKey(i);
|
171
|
12
|
headerValue = this.connection.getHeaderField(i);
|
172
|
|
}
|
173
|
3
|
returnCookies = new org.apache.cactus.Cookie[cookieVector.size()];
|
174
|
3
|
cookieVector.copyInto(returnCookies);
|
175
|
3
|
return returnCookies;
|
176
|
|
}
|
177
|
|
|
178
|
|
/**
|
179
|
|
* Default web response implementation that provides a minimal
|
180
|
|
* API for asserting returned output stream from the server side. For more
|
181
|
|
* complex assertions, use an <code>com.meterware.httpunit.WebResponse</code>
|
182
|
|
* instead as parameter of your <code>endXXX()</code> methods.
|
183
|
|
*
|
184
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
185
|
|
*
|
186
|
|
* @version $Id: WebResponse.java,v 1.6 2002/07/23 22:35:36 vmassol Exp $
|
187
|
|
*/
|
188
|
|
static {
|
189
|
|
/**
|
190
|
|
* The logger
|
191
|
|
*/
|
192
|
7
|
WebResponse.LOGGER = LogFactory.getLog(WebResponse.class);
|
193
|
|
}
|
194
|
|
|
195
|
|
}
|