1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus.client;
|
3
|
|
import java.net.URL;
|
4
|
|
import java.net.HttpURLConnection;
|
5
|
|
import java.util.Enumeration;
|
6
|
|
import java.io.InputStream;
|
7
|
|
import java.io.IOException;
|
8
|
|
import java.io.ByteArrayOutputStream;
|
9
|
|
import org.apache.commons.httpclient.HttpClient;
|
10
|
|
import org.apache.commons.httpclient.methods.PostMethod;
|
11
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
12
|
|
import org.apache.cactus.WebRequest;
|
13
|
|
import org.apache.cactus.util.UrlUtil;
|
14
|
|
import org.apache.cactus.client.authentication.AbstractAuthentication;
|
15
|
|
|
16
|
|
/**
|
17
|
|
* Implementation of <code>ConnectionHelper</code> using Jakarta Commons
|
18
|
|
* HttpClient.
|
19
|
|
*
|
20
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
21
|
|
*
|
22
|
|
* @version $Id: HttpClientConnectionHelper.java,v 1.1.2.1 2002/08/08 21:50:05 vmassol Exp $
|
23
|
|
*/
|
24
|
|
public class HttpClientConnectionHelper extends AbstractConnectionHelper {
|
25
|
|
/**
|
26
|
|
* The <code>HttpMethod</code> used to connect to the HTTP server. It is
|
27
|
|
* either a <code>GetMethod</code> or a <code>PostMethod</code>.
|
28
|
|
*/
|
29
|
|
private GetMethod method;
|
30
|
|
/**
|
31
|
|
* The URL that will be used for the HTTP connection.
|
32
|
|
*/
|
33
|
|
private String url;
|
34
|
|
/**
|
35
|
|
* @param theURL the URL that will be used for the HTTP connection.
|
36
|
|
*/
|
37
|
128
|
public HttpClientConnectionHelper(String theURL) {
|
38
|
128
|
super();
|
39
|
128
|
this.url = theURL;
|
40
|
|
}
|
41
|
|
/**
|
42
|
|
* @see ConnectionHelper#connect(WebRequest)
|
43
|
|
*/
|
44
|
128
|
public HttpURLConnection connect(WebRequest theRequest) throws Throwable {
|
45
|
128
|
URL url = new URL(this.url);
|
46
|
128
|
AbstractAuthentication authentication = theRequest.getAuthentication();
|
47
|
128
|
if (authentication != null) {
|
48
|
2
|
authentication.configure(theRequest);
|
49
|
|
}
|
50
|
128
|
url = this.addParametersGet(theRequest, url);
|
51
|
128
|
if (theRequest.getParameterNamesPost().hasMoreElements() || (theRequest.getUserData() !=
|
52
|
|
null)) {
|
53
|
3
|
this.method = new PostMethod();
|
54
|
|
} else {
|
55
|
125
|
this.method = new GetMethod();
|
56
|
|
}
|
57
|
128
|
this.method.setUseDisk(false);
|
58
|
128
|
this.method.setFollowRedirects(false);
|
59
|
128
|
this.method.setPath(UrlUtil.getPath(url));
|
60
|
128
|
this.method.setQueryString(UrlUtil.getQuery(url));
|
61
|
128
|
this.method.setRequestHeader("Content-type", theRequest.getContentType());
|
62
|
128
|
this.addHeaders(theRequest);
|
63
|
128
|
String cookieString = this.getCookieString(theRequest, url);
|
64
|
128
|
if (cookieString != null) {
|
65
|
3
|
this.method.addRequestHeader("Cookie", cookieString);
|
66
|
|
}
|
67
|
128
|
if (theRequest.getUserData() != null) {
|
68
|
1
|
this.addUserData(theRequest);
|
69
|
|
} else {
|
70
|
127
|
this.addParametersPost(theRequest);
|
71
|
|
}
|
72
|
128
|
HttpClient client = new HttpClient();
|
73
|
128
|
client.startSession(url.getHost(), url.getPort());
|
74
|
128
|
client.executeMethod(this.method);
|
75
|
128
|
return new org.apache.cactus.util.HttpURLConnection(this.method, url);
|
76
|
|
}
|
77
|
|
|
78
|
|
/**
|
79
|
|
* Add the HTTP parameters that need to be passed in the request body.
|
80
|
|
*
|
81
|
|
* @param theRequest the request containing all data to pass to the server
|
82
|
|
* redirector.
|
83
|
|
*/
|
84
|
127
|
private void addParametersPost(WebRequest theRequest) {
|
85
|
127
|
if (!theRequest.getParameterNamesPost().hasMoreElements()) {
|
86
|
125
|
return;
|
87
|
|
}
|
88
|
2
|
Enumeration keys = theRequest.getParameterNamesPost();
|
89
|
2
|
if (keys.hasMoreElements()) {
|
90
|
2
|
String key = (String)keys.nextElement();
|
91
|
2
|
String[] values = theRequest.getParameterValuesPost(key);
|
92
|
2
|
for (int i = 0; i < values.length; i++) {
|
93
|
2
|
((PostMethod)this.method).addParameter(key, values[i]);
|
94
|
|
}
|
95
|
|
}
|
96
|
|
}
|
97
|
|
|
98
|
|
/**
|
99
|
|
* Add the Headers to the request.
|
100
|
|
*
|
101
|
|
* @param theRequest the request containing all data to pass to the server
|
102
|
|
* redirector.
|
103
|
|
*/
|
104
|
128
|
private void addHeaders(WebRequest theRequest) {
|
105
|
128
|
Enumeration keys = theRequest.getHeaderNames();
|
106
|
128
|
while (keys.hasMoreElements()){
|
107
|
4
|
String key = (String)keys.nextElement();
|
108
|
4
|
String[] values = theRequest.getHeaderValues(key);
|
109
|
4
|
StringBuffer fullHeaderValue = new StringBuffer(values[0]);
|
110
|
4
|
for (int i = 1; i < values.length; i++) {
|
111
|
1
|
fullHeaderValue.append("," + values[i]);
|
112
|
|
}
|
113
|
4
|
this.method.addRequestHeader(key, fullHeaderValue.toString());
|
114
|
|
}
|
115
|
|
}
|
116
|
|
|
117
|
|
/**
|
118
|
|
* Add user data in the request body.
|
119
|
|
*
|
120
|
|
* @param theRequest the request containing all data to pass to the server
|
121
|
|
* redirector.
|
122
|
|
* @exception IOException if we fail to read the user data
|
123
|
|
*/
|
124
|
1
|
private void addUserData(WebRequest theRequest) throws IOException {
|
125
|
1
|
if (theRequest.getUserData() == null) {
|
126
|
0
|
return;
|
127
|
|
}
|
128
|
1
|
InputStream stream = theRequest.getUserData();
|
129
|
1
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
130
|
1
|
byte[] buffer = new byte[2048];
|
131
|
1
|
int length;
|
132
|
?
|
while ((length = stream.read(buffer)) != -1){
|
133
|
1
|
baos.write(buffer, 0, length);
|
134
|
|
}
|
135
|
1
|
((PostMethod)this.method).setRequestBody(baos.toString());
|
136
|
|
}
|
137
|
|
|
138
|
|
}
|