1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus.client;
|
3
|
|
import org.apache.cactus.WebRequest;
|
4
|
|
import org.apache.cactus.Cookie;
|
5
|
|
import org.apache.commons.httpclient.Header;
|
6
|
|
import java.net.MalformedURLException;
|
7
|
|
import java.net.URL;
|
8
|
|
import java.net.URLEncoder;
|
9
|
|
import java.util.Enumeration;
|
10
|
|
import java.util.Vector;
|
11
|
|
|
12
|
|
/**
|
13
|
|
* Common helper methods for implementing <code>ConnectionHelper</code>. These
|
14
|
|
* methods are common to any implementation.
|
15
|
|
*
|
16
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
17
|
|
*
|
18
|
|
* @version $Id: AbstractConnectionHelper.java,v 1.1 2002/07/24 20:46:48 vmassol Exp $
|
19
|
|
*/
|
20
|
|
public abstract class AbstractConnectionHelper implements ConnectionHelper {
|
21
|
|
/**
|
22
|
|
* Add the HTTP parameters that need to be passed in the query string of
|
23
|
|
* the URL.
|
24
|
|
*
|
25
|
|
* @param theRequest the request containing all data to pass to the server
|
26
|
|
* redirector.
|
27
|
|
* @param theURL the URL used to connect to the server redirector.
|
28
|
|
* @return the new URL
|
29
|
|
* @exception MalformedURLException if the URL is malformed
|
30
|
|
*/
|
31
|
354
|
protected URL addParametersGet(WebRequest theRequest, URL theURL) throws MalformedURLException {
|
32
|
354
|
if (!theRequest.getParameterNamesGet().hasMoreElements()) {
|
33
|
0
|
return theURL;
|
34
|
|
}
|
35
|
354
|
StringBuffer queryString = new StringBuffer();
|
36
|
354
|
Enumeration keys = theRequest.getParameterNamesGet();
|
37
|
354
|
if (keys.hasMoreElements()) {
|
38
|
354
|
String key = (String)keys.nextElement();
|
39
|
354
|
String[] values = theRequest.getParameterValuesGet(key);
|
40
|
354
|
queryString.append(key);
|
41
|
354
|
queryString.append('=');
|
42
|
354
|
queryString.append(URLEncoder.encode(values[0]));
|
43
|
354
|
for (int i = 1; i < values.length; i++) {
|
44
|
0
|
queryString.append('&');
|
45
|
0
|
queryString.append(key);
|
46
|
0
|
queryString.append('=');
|
47
|
0
|
queryString.append(URLEncoder.encode(values[i]));
|
48
|
|
}
|
49
|
|
}
|
50
|
354
|
while (keys.hasMoreElements()){
|
51
|
666
|
String key = (String)keys.nextElement();
|
52
|
666
|
String[] values = theRequest.getParameterValuesGet(key);
|
53
|
666
|
for (int i = 0; i < values.length; i++) {
|
54
|
669
|
queryString.append('&');
|
55
|
669
|
queryString.append(key);
|
56
|
669
|
queryString.append('=');
|
57
|
669
|
queryString.append(URLEncoder.encode(values[i]));
|
58
|
|
}
|
59
|
|
}
|
60
|
354
|
String file = theURL.getFile();
|
61
|
354
|
if (file.endsWith("/")) {
|
62
|
0
|
file = file.substring(0, file.length() - 1);
|
63
|
|
}
|
64
|
354
|
if (theURL.toString().indexOf("?") > 0) {
|
65
|
0
|
file = file + "&" + queryString.toString();
|
66
|
|
} else {
|
67
|
354
|
file = file + "?" + queryString.toString();
|
68
|
|
}
|
69
|
354
|
return new URL(theURL.getProtocol(), theURL.getHost(), theURL.getPort(), file);
|
70
|
|
}
|
71
|
|
|
72
|
|
/**
|
73
|
|
* @return the cookie string which will be added as a HTTP "Cookie" header
|
74
|
|
* or null if no cookie has been set
|
75
|
|
* @param theRequest the request containing all data to pass to the server
|
76
|
|
* redirector.
|
77
|
|
* @param theUrl the URL to connect to
|
78
|
|
*/
|
79
|
354
|
public String getCookieString(WebRequest theRequest, URL theUrl) {
|
80
|
354
|
Vector cookies = theRequest.getCookies();
|
81
|
354
|
if (!cookies.isEmpty()) {
|
82
|
9
|
org.apache.commons.httpclient.Cookie[] httpclientCookies = new
|
83
|
|
org.apache.commons.httpclient.Cookie[cookies.size()];
|
84
|
9
|
for (int i = 0; i < cookies.size(); i++) {
|
85
|
12
|
Cookie cactusCookie = (Cookie)cookies.elementAt(i);
|
86
|
12
|
String domain;
|
87
|
12
|
if (cactusCookie.getDomain() == null) {
|
88
|
0
|
domain = Cookie.getCookieDomain(theRequest, theUrl.getHost());
|
89
|
|
} else {
|
90
|
12
|
domain = cactusCookie.getDomain();
|
91
|
|
}
|
92
|
12
|
String path;
|
93
|
12
|
if (cactusCookie.getPath() == null) {
|
94
|
12
|
path = Cookie.getCookiePath(theRequest, theUrl.getFile());
|
95
|
|
} else {
|
96
|
0
|
path = cactusCookie.getPath();
|
97
|
|
}
|
98
|
12
|
httpclientCookies[i] = new org.apache.commons.httpclient.Cookie(domain,
|
99
|
|
cactusCookie.getName(), cactusCookie.getValue());
|
100
|
12
|
httpclientCookies[i].setComment(cactusCookie.getComment());
|
101
|
12
|
httpclientCookies[i].setExpiryDate(cactusCookie.getExpiryDate());
|
102
|
12
|
httpclientCookies[i].setPath(path);
|
103
|
12
|
httpclientCookies[i].setSecure(cactusCookie.isSecure());
|
104
|
|
}
|
105
|
9
|
Header cookieHeader = org.apache.commons.httpclient.Cookie.createCookieHeader(
|
106
|
|
Cookie.getCookieDomain(theRequest, theUrl.getHost()), Cookie.getCookiePath(theRequest,
|
107
|
|
theUrl.getFile()), httpclientCookies);
|
108
|
9
|
return cookieHeader.getValue();
|
109
|
|
}
|
110
|
345
|
return ((String)(null));
|
111
|
|
}
|
112
|
|
|
113
|
|
/**
|
114
|
|
* Common helper methods for implementing <code>ConnectionHelper</code>. These
|
115
|
|
* methods are common to any implementation.
|
116
|
|
*
|
117
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
118
|
|
*
|
119
|
|
* @version $Id: AbstractConnectionHelper.java,v 1.1 2002/07/24 20:46:48 vmassol Exp $
|
120
|
|
*/
|
121
|
354
|
public AbstractConnectionHelper() {
|
122
|
354
|
super();
|
123
|
|
}
|
124
|
|
/**
|
125
|
|
* Connects to the Cactus Redirector using HTTP.
|
126
|
|
*
|
127
|
|
* @param theRequest the request containing all data to pass to the
|
128
|
|
* server redirector.
|
129
|
|
* @return the HTTP Connection used to connect to the redirector.
|
130
|
|
* @exception Throwable if an unexpected error occured
|
131
|
|
*/
|
132
|
|
public abstract java.net.HttpURLConnection connect(WebRequest theRequest) throws Throwable;
|
133
|
|
|
134
|
|
}
|