2011/08/05 - Jakarta Cactus has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.cactus.internal;
22
23 import java.net.HttpURLConnection;
24 import java.util.StringTokenizer;
25
26 import org.apache.cactus.Cookie;
27 import org.apache.cactus.HttpSessionCookie;
28 import org.apache.cactus.ServletURL;
29 import org.apache.cactus.WebResponse;
30 import org.apache.cactus.internal.client.ClientException;
31 import org.apache.cactus.internal.client.WebResponseObjectFactory;
32 import org.apache.cactus.internal.client.connector.http.HttpClientConnectionHelper;
33 import org.apache.cactus.internal.configuration.WebConfiguration;
34 import org.apache.cactus.util.ChainedRuntimeException;
35
36
37
38
39
40
41
42 public class WebRequestImpl extends BaseWebRequest
43 {
44
45
46
47 private ServletURL url;
48
49
50
51
52 private boolean isAutomaticSession = true;
53
54
55
56
57
58 private String redirectorName;
59
60
61
62
63
64
65
66 public WebRequestImpl()
67 {
68 }
69
70
71
72
73 public WebRequestImpl(WebConfiguration theConfiguration)
74 {
75 super(theConfiguration);
76 }
77
78
79
80
81
82 public void setRedirectorName(String theRedirectorName)
83 {
84 this.redirectorName = theRedirectorName;
85 }
86
87
88
89
90
91 public String getRedirectorName()
92 {
93 return this.redirectorName;
94 }
95
96
97
98
99
100 public void setAutomaticSession(boolean isAutomaticSession)
101 {
102 this.isAutomaticSession = isAutomaticSession;
103 }
104
105
106
107
108
109 public boolean getAutomaticSession()
110 {
111 return this.isAutomaticSession;
112 }
113
114
115
116
117
118 public void setURL(String theServerName, String theContextPath,
119 String theServletPath, String thePathInfo, String theQueryString)
120 {
121 this.url = new ServletURL(theServerName, theContextPath,
122 theServletPath, thePathInfo, theQueryString);
123
124
125
126 addQueryStringParameters(theQueryString);
127 }
128
129
130
131
132
133 public ServletURL getURL()
134 {
135 return this.url;
136 }
137
138
139
140
141 public String toString()
142 {
143 StringBuffer buffer = new StringBuffer();
144
145 buffer.append("simulation URL = [" + getURL() + "], ");
146 buffer.append("automatic session = [" + getAutomaticSession() + "], ");
147
148 buffer.append(super.toString());
149
150 return buffer.toString();
151 }
152
153
154
155
156
157
158
159
160
161
162
163 private void addQueryStringParameters(String theQueryString)
164 {
165 if (theQueryString == null)
166 {
167 return;
168 }
169
170 String nameValue = null;
171 StringTokenizer tokenizer = new StringTokenizer(theQueryString, "&");
172 int breakParam = -1;
173
174 while (tokenizer.hasMoreTokens())
175 {
176 nameValue = tokenizer.nextToken();
177 breakParam = nameValue.indexOf("=");
178
179 if (breakParam != -1)
180 {
181 addParameter(nameValue.substring(0, breakParam),
182 nameValue.substring(breakParam + 1));
183 }
184 else
185 {
186 throw new RuntimeException("Bad QueryString [" + theQueryString
187 + "] NameValue pair: [" + nameValue + "]");
188 }
189 }
190 }
191
192
193
194
195
196 public HttpSessionCookie getSessionCookie()
197 {
198 if (getConfiguration() == null)
199 {
200 throw new ChainedRuntimeException("setConfiguration() should have "
201 + "been called prior to calling getSessionCookie()");
202 }
203
204 HttpClientConnectionHelper helper =
205 new HttpClientConnectionHelper(
206 ((WebConfiguration) getConfiguration()).getRedirectorURL(this));
207
208 WebRequestImpl obtainSessionIdRequest = new WebRequestImpl(
209 (WebConfiguration) getConfiguration());
210
211
212
213
214
215 RequestDirectives directives =
216 new RequestDirectives(obtainSessionIdRequest);
217 directives.setService(ServiceEnumeration.CREATE_SESSION_SERVICE);
218
219 HttpURLConnection resultConnection;
220 try
221 {
222 resultConnection =
223 helper.connect(obtainSessionIdRequest, getConfiguration());
224 }
225 catch (Throwable e)
226 {
227 throw new ChainedRuntimeException("Failed to connect to ["
228 + ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
229 + "]", e);
230 }
231
232 WebResponse response;
233 try
234 {
235 response = (WebResponse) new WebResponseObjectFactory(
236 resultConnection).getResponseObject(
237 WebResponse.class.getName(),
238 obtainSessionIdRequest);
239 }
240 catch (ClientException e)
241 {
242 throw new ChainedRuntimeException("Failed to connect to ["
243 + ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
244 + "]", e);
245 }
246
247 Cookie cookie = response.getCookieIgnoreCase("jsessionid");
248
249
250
251
252 HttpSessionCookie sessionCookie = null;
253
254 if (cookie != null)
255 {
256 sessionCookie = new HttpSessionCookie(cookie.getDomain(),
257 cookie.getName(), cookie.getValue());
258 sessionCookie.setComment(cookie.getComment());
259 sessionCookie.setExpiryDate(cookie.getExpiryDate());
260 sessionCookie.setPath(cookie.getPath());
261 sessionCookie.setSecure(cookie.isSecure());
262 }
263
264 return sessionCookie;
265 }
266 }