|
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.2.1 2002/08/31 18:09:16 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
|
141
|
public WebResponse(WebRequest theRequest, HttpURLConnection theConnection) {
|
|
50
|
141
|
super();
|
|
51
|
141
|
this.request = theRequest;
|
|
52
|
141
|
this.connection = theConnection;
|
|
53
|
|
}
|
|
54
|
|
/**
|
|
55
|
|
* @return the original <code>HttpURLConnection</code> used to call the
|
|
56
|
|
* URL
|
|
57
|
|
*/
|
|
58
|
63
|
public HttpURLConnection getConnection() {
|
|
59
|
63
|
return this.connection;
|
|
60
|
|
}
|
|
61
|
|
|
|
62
|
|
/**
|
|
63
|
|
* @return the request data the were used to open the connection to the
|
|
64
|
|
* server
|
|
65
|
|
*/
|
|
66
|
21
|
public WebRequest getWebRequest() {
|
|
67
|
21
|
return this.request;
|
|
68
|
|
}
|
|
69
|
|
|
|
70
|
|
/**
|
|
71
|
|
* @return the text of the response (excluding headers) as a string.
|
|
72
|
|
*/
|
|
73
|
112
|
public String getText() {
|
|
74
|
112
|
if (this.content == null) {
|
|
75
|
91
|
try {
|
|
76
|
91
|
this.content = IoUtil.getText(this.connection.getInputStream());
|
|
77
|
|
} catch (IOException e) {
|
|
78
|
0
|
throw new ChainedRuntimeException(e);
|
|
79
|
|
}
|
|
80
|
|
}
|
|
81
|
112
|
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
|
14
|
public String[] getTextAsArray() {
|
|
89
|
14
|
Vector lines = new Vector();
|
|
90
|
14
|
try {
|
|
91
|
14
|
if (this.content == null) {
|
|
92
|
7
|
this.getText();
|
|
93
|
|
}
|
|
94
|
14
|
BufferedReader input = new BufferedReader(new StringReader(this.content));
|
|
95
|
14
|
String str;
|
|
96
|
?
|
while (null != (str = input.readLine())){
|
|
97
|
42
|
lines.addElement(str);
|
|
98
|
|
}
|
|
99
|
14
|
input.close();
|
|
100
|
|
} catch (IOException e) {
|
|
101
|
0
|
throw new ChainedRuntimeException(e);
|
|
102
|
|
}
|
|
103
|
14
|
String[] dummy = new java.lang.String[lines.size()];
|
|
104
|
14
|
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
|
7
|
public Cookie getCookie(String theName) {
|
|
126
|
7
|
Cookie result = null;
|
|
127
|
7
|
Cookie[] cookies = this.getCookies();
|
|
128
|
8
|
for (int i = 0; i < cookies.length; i++) {
|
|
129
|
8
|
if (cookies[i].getName().equals(theName)) {
|
|
130
|
7
|
result = cookies[i];
|
|
131
|
7
|
break;
|
|
132
|
|
}
|
|
133
|
|
}
|
|
134
|
7
|
return result;
|
|
135
|
|
}
|
|
136
|
|
|
|
137
|
|
/**
|
|
138
|
|
* @return the cookies returned by the server
|
|
139
|
|
*/
|
|
140
|
7
|
public Cookie[] getCookies() {
|
|
141
|
7
|
Cookie[] returnCookies = null;
|
|
142
|
7
|
String headerName = this.connection.getHeaderFieldKey(0);
|
|
143
|
7
|
String headerValue = this.connection.getHeaderField(0);
|
|
144
|
7
|
WebResponse.LOGGER.debug("Header name [0] = [" + headerName + "]");
|
|
145
|
7
|
WebResponse.LOGGER.debug("Header value [0] = [" + headerValue + "]");
|
|
146
|
7
|
Vector cookieVector = new Vector();
|
|
147
|
7
|
for (int i = 1; (headerName != null) || (headerValue != null); i++) {
|
|
148
|
35
|
WebResponse.LOGGER.debug("Header name = [" + headerName + "]");
|
|
149
|
35
|
WebResponse.LOGGER.debug("Header value = [" + headerValue + "]");
|
|
150
|
35
|
if ((headerName != null) && (headerName.toLowerCase().equals("set-cookie") ||
|
|
151
|
|
headerName.toLowerCase().equals("set-cookie2"))) {
|
|
152
|
7
|
org.apache.commons.httpclient.Cookie[] cookies;
|
|
153
|
7
|
try {
|
|
154
|
7
|
cookies = org.apache.commons.httpclient.Cookie.parse(Cookie.getCookieDomain(
|
|
155
|
|
this.getWebRequest(), this.getConnection().getURL().getHost()),
|
|
156
|
|
Cookie.getCookiePort(this.getWebRequest(), this.getConnection().getURL().getPort()),
|
|
157
|
|
Cookie.getCookiePath(this.getWebRequest(), this.getConnection().getURL().getFile()),
|
|
158
|
|
new Header(headerName, headerValue));
|
|
159
|
|
} catch (HttpException e) {
|
|
160
|
0
|
throw new ChainedRuntimeException("Error parsing cookies", e);
|
|
161
|
|
}
|
|
162
|
7
|
for (int j = 0; j < cookies.length; j++) {
|
|
163
|
14
|
Cookie cookie = new Cookie(cookies[j].getDomain(), cookies[j].getName(),
|
|
164
|
|
cookies[j].getValue());
|
|
165
|
14
|
cookie.setComment(cookies[j].getComment());
|
|
166
|
14
|
cookie.setExpiryDate(cookies[j].getExpiryDate());
|
|
167
|
14
|
cookie.setPath(cookies[j].getPath());
|
|
168
|
14
|
cookie.setSecure(cookies[j].getSecure());
|
|
169
|
14
|
cookieVector.addElement(cookie);
|
|
170
|
|
}
|
|
171
|
|
}
|
|
172
|
35
|
headerName = this.connection.getHeaderFieldKey(i);
|
|
173
|
35
|
headerValue = this.connection.getHeaderField(i);
|
|
174
|
|
}
|
|
175
|
7
|
returnCookies = new org.apache.cactus.Cookie[cookieVector.size()];
|
|
176
|
7
|
cookieVector.copyInto(returnCookies);
|
|
177
|
7
|
return returnCookies;
|
|
178
|
|
}
|
|
179
|
|
|
|
180
|
|
/**
|
|
181
|
|
* Default web response implementation that provides a minimal
|
|
182
|
|
* API for asserting returned output stream from the server side. For more
|
|
183
|
|
* complex assertions, use an <code>com.meterware.httpunit.WebResponse</code>
|
|
184
|
|
* instead as parameter of your <code>endXXX()</code> methods.
|
|
185
|
|
*
|
|
186
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
187
|
|
*
|
|
188
|
|
* @version $Id: WebResponse.java,v 1.6.2.1 2002/08/31 18:09:16 vmassol Exp $
|
|
189
|
|
*/
|
|
190
|
|
static {
|
|
191
|
|
/**
|
|
192
|
|
* The logger
|
|
193
|
|
*/
|
|
194
|
15
|
WebResponse.LOGGER = LogFactory.getLog(WebResponse.class);
|
|
195
|
|
}
|
|
196
|
|
|
|
197
|
|
}
|