1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus.client;
|
3
|
|
import java.io.IOException;
|
4
|
|
import java.io.InputStream;
|
5
|
|
import java.io.OutputStream;
|
6
|
|
import java.io.PrintWriter;
|
7
|
|
import java.net.HttpURLConnection;
|
8
|
|
import java.net.URL;
|
9
|
|
import java.net.URLConnection;
|
10
|
|
import java.net.URLEncoder;
|
11
|
|
import java.util.Enumeration;
|
12
|
|
import org.apache.commons.logging.LogFactory;
|
13
|
|
import org.apache.commons.logging.Log;
|
14
|
|
import org.apache.cactus.WebRequest;
|
15
|
|
import org.apache.cactus.client.authentication.AbstractAuthentication;
|
16
|
|
import org.apache.cactus.util.ChainedRuntimeException;
|
17
|
|
|
18
|
|
/**
|
19
|
|
* Implementation of <code>ConnectionHelper</code> using the JDK
|
20
|
|
* <code>HttpURLConnection</code> class.
|
21
|
|
*
|
22
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
23
|
|
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
|
24
|
|
*
|
25
|
|
* @version $Id: JdkConnectionHelper.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $
|
26
|
|
*/
|
27
|
|
public class JdkConnectionHelper extends AbstractConnectionHelper {
|
28
|
|
/**
|
29
|
|
* The logger
|
30
|
|
*/
|
31
|
|
private static Log LOGGER;
|
32
|
|
/**
|
33
|
|
* The URL that will be used for the HTTP connection.
|
34
|
|
*/
|
35
|
|
private String url;
|
36
|
|
/**
|
37
|
|
* @param theURL the URL that will be used for the HTTP connection.
|
38
|
|
*/
|
39
|
0
|
public JdkConnectionHelper(String theURL) {
|
40
|
0
|
super();
|
41
|
0
|
this.url = theURL;
|
42
|
|
}
|
43
|
|
/**
|
44
|
|
* @see ConnectionHelper#connect(WebRequest)
|
45
|
|
*/
|
46
|
0
|
public HttpURLConnection connect(WebRequest theRequest) throws Throwable {
|
47
|
0
|
URL url = new URL(this.url);
|
48
|
0
|
AbstractAuthentication authentication = theRequest.getAuthentication();
|
49
|
0
|
if (authentication != null) {
|
50
|
0
|
authentication.configure(theRequest);
|
51
|
|
}
|
52
|
0
|
url = this.addParametersGet(theRequest, url);
|
53
|
0
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
54
|
0
|
connection.setDoInput(true);
|
55
|
0
|
if (theRequest.getParameterNamesPost().hasMoreElements() || (theRequest.getUserData() !=
|
56
|
|
null)) {
|
57
|
0
|
connection.setDoOutput(true);
|
58
|
|
} else {
|
59
|
0
|
connection.setDoOutput(false);
|
60
|
|
}
|
61
|
0
|
connection.setUseCaches(false);
|
62
|
0
|
connection.setRequestProperty("Content-type", theRequest.getContentType());
|
63
|
0
|
this.addHeaders(theRequest, connection);
|
64
|
0
|
String cookieString = this.getCookieString(theRequest, url);
|
65
|
0
|
if (cookieString != null) {
|
66
|
0
|
connection.setRequestProperty("Cookie", cookieString);
|
67
|
|
}
|
68
|
0
|
if (theRequest.getUserData() != null) {
|
69
|
0
|
this.addUserData(theRequest, connection);
|
70
|
|
} else {
|
71
|
0
|
this.addParametersPost(theRequest, connection);
|
72
|
|
}
|
73
|
0
|
JdkConnectionHelper.LOGGER.debug("ContentLength = [" + connection.getContentLength() + "]");
|
74
|
0
|
connection.connect();
|
75
|
0
|
return connection;
|
76
|
|
}
|
77
|
|
|
78
|
|
/**
|
79
|
|
* Add user data in the request body.
|
80
|
|
*
|
81
|
|
* @param theRequest the request containing all data to pass to the server
|
82
|
|
* redirector.
|
83
|
|
* @param theConnection the HTTP connection
|
84
|
|
* @exception IOException if we fail to read the user data
|
85
|
|
*/
|
86
|
0
|
private void addUserData(WebRequest theRequest, URLConnection theConnection) throws IOException {
|
87
|
0
|
if (theRequest.getUserData() == null) {
|
88
|
0
|
return;
|
89
|
|
}
|
90
|
0
|
OutputStream out = this.getConnectionStream(theConnection);
|
91
|
0
|
InputStream stream = theRequest.getUserData();
|
92
|
0
|
byte[] buffer = new byte[2048];
|
93
|
0
|
int length;
|
94
|
0
|
while ((length = stream.read(buffer)) != -1){
|
95
|
0
|
out.write(buffer, 0, length);
|
96
|
|
}
|
97
|
0
|
out.close();
|
98
|
|
}
|
99
|
|
|
100
|
|
/**
|
101
|
|
* Add the HTTP parameters that need to be passed in the request body.
|
102
|
|
*
|
103
|
|
* @param theRequest the request containing all data to pass to the server
|
104
|
|
* redirector.
|
105
|
|
* @param theConnection the HTTP connection
|
106
|
|
*/
|
107
|
0
|
private void addParametersPost(WebRequest theRequest, URLConnection theConnection) {
|
108
|
0
|
if (!theRequest.getParameterNamesPost().hasMoreElements()) {
|
109
|
0
|
return;
|
110
|
|
}
|
111
|
0
|
PrintWriter out = new PrintWriter(this.getConnectionStream(theConnection));
|
112
|
0
|
StringBuffer queryString = new StringBuffer();
|
113
|
0
|
Enumeration keys = theRequest.getParameterNamesPost();
|
114
|
0
|
if (keys.hasMoreElements()) {
|
115
|
0
|
String key = (String)keys.nextElement();
|
116
|
0
|
String[] values = theRequest.getParameterValuesPost(key);
|
117
|
0
|
queryString.append(key);
|
118
|
0
|
queryString.append('=');
|
119
|
0
|
queryString.append(URLEncoder.encode(values[0]));
|
120
|
0
|
for (int i = 1; i < values.length; i++) {
|
121
|
0
|
queryString.append('&');
|
122
|
0
|
queryString.append(key);
|
123
|
0
|
queryString.append('=');
|
124
|
0
|
queryString.append(URLEncoder.encode(values[i]));
|
125
|
|
}
|
126
|
|
}
|
127
|
0
|
while (keys.hasMoreElements()){
|
128
|
0
|
String key = (String)keys.nextElement();
|
129
|
0
|
String[] values = theRequest.getParameterValuesPost(key);
|
130
|
0
|
for (int i = 0; i < values.length; i++) {
|
131
|
0
|
queryString.append('&');
|
132
|
0
|
queryString.append(key);
|
133
|
0
|
queryString.append('=');
|
134
|
0
|
queryString.append(URLEncoder.encode(values[i]));
|
135
|
|
}
|
136
|
|
}
|
137
|
0
|
out.print(queryString.toString());
|
138
|
0
|
out.close();
|
139
|
|
}
|
140
|
|
|
141
|
|
/**
|
142
|
|
* @param theConnection the HTTP connection
|
143
|
|
* @return an output stream to write in the request body
|
144
|
|
*/
|
145
|
0
|
private OutputStream getConnectionStream(URLConnection theConnection) {
|
146
|
0
|
OutputStream out;
|
147
|
0
|
try {
|
148
|
0
|
out = theConnection.getOutputStream();
|
149
|
|
} catch (IOException e) {
|
150
|
0
|
String reason = "Cannot connect to URL [" + theConnection.getURL() + "]. Reason : [" +
|
151
|
|
e.getMessage() + "]\r\n";
|
152
|
0
|
reason += "Possible reasons :\r\n";
|
153
|
0
|
reason += "\t- The server is not running,\r\n";
|
154
|
0
|
reason += "\t- The server redirector is not correctly mapped in web.xml,\r\n";
|
155
|
0
|
reason += "\t- Something else ... !";
|
156
|
0
|
throw new ChainedRuntimeException(reason);
|
157
|
|
}
|
158
|
0
|
return out;
|
159
|
|
}
|
160
|
|
|
161
|
|
/**
|
162
|
|
* Add the Headers to the request.
|
163
|
|
*
|
164
|
|
* @param theRequest the request containing all data to pass to the server
|
165
|
|
* redirector.
|
166
|
|
* @param theConnection the HTTP connection
|
167
|
|
*/
|
168
|
0
|
private void addHeaders(WebRequest theRequest, URLConnection theConnection) {
|
169
|
0
|
Enumeration keys = theRequest.getHeaderNames();
|
170
|
0
|
while (keys.hasMoreElements()){
|
171
|
0
|
String key = (String)keys.nextElement();
|
172
|
0
|
String[] values = theRequest.getHeaderValues(key);
|
173
|
0
|
StringBuffer fullHeaderValue = new StringBuffer(values[0]);
|
174
|
0
|
for (int i = 1; i < values.length; i++) {
|
175
|
0
|
fullHeaderValue.append("," + values[i]);
|
176
|
|
}
|
177
|
0
|
theConnection.setRequestProperty(key, fullHeaderValue.toString());
|
178
|
|
}
|
179
|
|
}
|
180
|
|
|
181
|
|
/**
|
182
|
|
* Implementation of <code>ConnectionHelper</code> using the JDK
|
183
|
|
* <code>HttpURLConnection</code> class.
|
184
|
|
*
|
185
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
186
|
|
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
|
187
|
|
*
|
188
|
|
* @version $Id: JdkConnectionHelper.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $
|
189
|
|
*/
|
190
|
|
static {
|
191
|
|
/**
|
192
|
|
* The logger
|
193
|
|
*/
|
194
|
0
|
JdkConnectionHelper.LOGGER = LogFactory.getLog(JdkConnectionHelper.class);
|
195
|
0
|
HttpURLConnection.setFollowRedirects(false);
|
196
|
|
}
|
197
|
|
|
198
|
|
}
|