KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > WebRequestImpl


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal;
21
22 import java.net.HttpURLConnection JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.apache.cactus.Cookie;
26 import org.apache.cactus.HttpSessionCookie;
27 import org.apache.cactus.ServletURL;
28 import org.apache.cactus.WebResponse;
29 import org.apache.cactus.internal.client.ClientException;
30 import org.apache.cactus.internal.client.WebResponseObjectFactory;
31 import org.apache.cactus.internal.client.connector.http.HttpClientConnectionHelper;
32 import org.apache.cactus.internal.configuration.WebConfiguration;
33 import org.apache.cactus.util.ChainedRuntimeException;
34
35 /**
36  * Extends {@link BaseWebRequest} to add properties specific to the
37  * Cactus Web Redirectors.
38  *
39  * @version $Id: WebRequestImpl.java,v 1.1 2004/05/22 11:34:47 vmassol Exp $
40  */

41 public class WebRequestImpl extends BaseWebRequest
42 {
43     /**
44      * The URL to simulate
45      */

46     private ServletURL url;
47
48     /**
49      * Automatic session creation flag (default is true).
50      */

51     private boolean isAutomaticSession = true;
52
53     /**
54      * Redirector Name. This is to let the user the possibility to override
55      * the default Redirector Name specified in <code>cactus.properties</code>.
56      */

57     private String JavaDoc redirectorName;
58
59     /**
60      * Default constructor that requires that
61      * {@link #setConfiguration(Configuration)} be called before the methods
62      * requiring a configuration object.
63      *
64      */

65     public WebRequestImpl()
66     {
67     }
68
69     /**
70      * @param theConfiguration the Cactus configuration
71      */

72     public WebRequestImpl(WebConfiguration theConfiguration)
73     {
74         super(theConfiguration);
75     }
76
77     /**
78      * @see org.apache.cactus.WebRequest#setRedirectorName(String)
79      */

80     public void setRedirectorName(String JavaDoc theRedirectorName)
81     {
82         this.redirectorName = theRedirectorName;
83     }
84
85     /**
86      * @see org.apache.cactus.WebRequest#getRedirectorName()
87      */

88     public String JavaDoc getRedirectorName()
89     {
90         return this.redirectorName;
91     }
92
93     /**
94      * @see org.apache.cactus.WebRequest#setAutomaticSession(boolean)
95      */

96     public void setAutomaticSession(boolean isAutomaticSession)
97     {
98         this.isAutomaticSession = isAutomaticSession;
99     }
100
101     /**
102      * @see org.apache.cactus.WebRequest#getAutomaticSession()
103      */

104     public boolean getAutomaticSession()
105     {
106         return this.isAutomaticSession;
107     }
108
109     /**
110      * @see org.apache.cactus.WebRequest#setURL(String, String, String, String, String)
111      */

112     public void setURL(String JavaDoc theServerName, String JavaDoc theContextPath,
113         String JavaDoc theServletPath, String JavaDoc thePathInfo, String JavaDoc theQueryString)
114     {
115         this.url = new ServletURL(theServerName, theContextPath,
116             theServletPath, thePathInfo, theQueryString);
117
118         // Now automatically add all HTTP parameters to the list of passed
119
// parameters
120
addQueryStringParameters(theQueryString);
121     }
122
123     /**
124      * @see org.apache.cactus.WebRequest#getURL()
125      */

126     public ServletURL getURL()
127     {
128         return this.url;
129     }
130
131     /**
132      * @return a string representation of the request
133      */

134     public String JavaDoc toString()
135     {
136         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
137
138         buffer.append("simulation URL = [" + getURL() + "], ");
139         buffer.append("automatic session = [" + getAutomaticSession() + "], ");
140
141         buffer.append(super.toString());
142         
143         return buffer.toString();
144     }
145
146     /**
147      * Extract the HTTP parameters that might have been specified on the
148      * query string and add them to the list of parameters to pass to the
149      * servlet redirector.
150      *
151      * @param theQueryString the Query string in the URL to simulate, i.e. this
152      * is the string that will be returned by the
153      * <code>HttpServletResquest.getQueryString()</code>.
154      * Can be null.
155      */

156     private void addQueryStringParameters(String JavaDoc theQueryString)
157     {
158         if (theQueryString == null)
159         {
160             return;
161         }
162
163         String JavaDoc nameValue = null;
164         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(theQueryString, "&");
165         int breakParam = -1;
166
167         while (tokenizer.hasMoreTokens())
168         {
169             nameValue = tokenizer.nextToken();
170             breakParam = nameValue.indexOf("=");
171
172             if (breakParam != -1)
173             {
174                 addParameter(nameValue.substring(0, breakParam),
175                     nameValue.substring(breakParam + 1));
176             }
177             else
178             {
179                 throw new RuntimeException JavaDoc("Bad QueryString [" + theQueryString
180                     + "] NameValue pair: [" + nameValue + "]");
181             }
182         }
183     }
184     
185     /**
186      * @see org.apache.cactus.WebRequest#getSessionCookie()
187      */

188     public HttpSessionCookie getSessionCookie()
189     {
190         if (getConfiguration() == null)
191         {
192             throw new ChainedRuntimeException("setConfiguration() should have "
193                 + "been called prior to calling getSessionCookie()");
194         }
195         
196         HttpClientConnectionHelper helper =
197             new HttpClientConnectionHelper(
198                 ((WebConfiguration) getConfiguration()).getRedirectorURL(this));
199
200         WebRequestImpl obtainSessionIdRequest = new WebRequestImpl(
201             (WebConfiguration) getConfiguration());
202             
203         
204         //Not sure whether I should be adding the service parameter to
205
//this request (this) or to the obtainSessionIdRequest
206
//seems obvious that it should be the obtainSessionIdRequest
207
RequestDirectives directives =
208             new RequestDirectives(obtainSessionIdRequest);
209         directives.setService(ServiceEnumeration.CREATE_SESSION_SERVICE);
210
211         HttpURLConnection JavaDoc resultConnection;
212         try
213         {
214             resultConnection =
215                 helper.connect(obtainSessionIdRequest, getConfiguration());
216         }
217         catch (Throwable JavaDoc e)
218         {
219             throw new ChainedRuntimeException("Failed to connect to ["
220                 + ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
221                 + "]", e);
222         }
223
224         WebResponse response;
225         try
226         {
227             response = (WebResponse) new WebResponseObjectFactory(
228                 resultConnection).getResponseObject(
229                     WebResponse.class.getName(),
230                     obtainSessionIdRequest);
231         }
232         catch (ClientException e)
233         {
234             throw new ChainedRuntimeException("Failed to connect to ["
235                 + ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
236                 + "]", e);
237         }
238
239         Cookie cookie = response.getCookieIgnoreCase("jsessionid");
240
241         // TODO: Add a constructor to the Cookie class that takes a Cookie
242
// as parameter.
243

244         HttpSessionCookie sessionCookie = null;
245
246         if (cookie != null)
247         {
248             sessionCookie = new HttpSessionCookie(cookie.getDomain(),
249                 cookie.getName(), cookie.getValue());
250             sessionCookie.setComment(cookie.getComment());
251             sessionCookie.setExpiryDate(cookie.getExpiryDate());
252             sessionCookie.setPath(cookie.getPath());
253             sessionCookie.setSecure(cookie.isSecure());
254         }
255                 
256         return sessionCookie;
257     }
258 }
259
Popular Tags