2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Extends {@link BaseWebRequest} to add properties specific to the
38   * Cactus Web Redirectors.
39   *
40   * @version $Id: WebRequestImpl.java 238991 2004-05-22 11:34:50Z vmassol $
41   */
42  public class WebRequestImpl extends BaseWebRequest
43  {
44      /**
45       * The URL to simulate.
46       */
47      private ServletURL url;
48  
49      /**
50       * Automatic session creation flag (default is true).
51       */
52      private boolean isAutomaticSession = true;
53  
54      /**
55       * Redirector Name. This is to let the user the possibility to override
56       * the default Redirector Name specified in <code>cactus.properties</code>.
57       */
58      private String redirectorName;
59  
60      /**
61       * Default constructor that requires that 
62       * {@link #setConfiguration(Configuration)} be called before the methods
63       * requiring a configuration object.
64       * 
65       */
66      public WebRequestImpl()
67      {
68      }
69  
70      /**
71       * @param theConfiguration the Cactus configuration
72       */
73      public WebRequestImpl(WebConfiguration theConfiguration)
74      {
75          super(theConfiguration);
76      }
77  
78      /**
79       * {@inheritDoc}
80       * @see org.apache.cactus.WebRequest#setRedirectorName(String)
81       */
82      public void setRedirectorName(String theRedirectorName)
83      {
84          this.redirectorName = theRedirectorName;
85      }
86  
87      /**
88       * {@inheritDoc}
89       * @see org.apache.cactus.WebRequest#getRedirectorName()
90       */
91      public String getRedirectorName()
92      {
93          return this.redirectorName;
94      }
95  
96      /**
97       * {@inheritDoc}
98       * @see org.apache.cactus.WebRequest#setAutomaticSession(boolean)
99       */
100     public void setAutomaticSession(boolean isAutomaticSession)
101     {
102         this.isAutomaticSession = isAutomaticSession;
103     }
104 
105     /**
106      * {@inheritDoc}
107      * @see org.apache.cactus.WebRequest#getAutomaticSession()
108      */
109     public boolean getAutomaticSession()
110     {
111         return this.isAutomaticSession;
112     }
113 
114     /**
115      * {@inheritDoc}
116      * @see org.apache.cactus.WebRequest#setURL(String, String, String, String, String)
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         // Now automatically add all HTTP parameters to the list of passed
125         // parameters
126         addQueryStringParameters(theQueryString);
127     }
128 
129     /**
130      * {@inheritDoc}
131      * @see org.apache.cactus.WebRequest#getURL()
132      */
133     public ServletURL getURL()
134     {
135         return this.url;
136     }
137 
138     /**
139      * @return a string representation of the request
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      * Extract the HTTP parameters that might have been specified on the
155      * query string and add them to the list of parameters to pass to the
156      * servlet redirector.
157      *
158      * @param theQueryString the Query string in the URL to simulate, i.e. this
159      *                       is the string that will be returned by the
160      *                       <code>HttpServletResquest.getQueryString()</code>.
161      *                       Can be null.
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      * {@inheritDoc}
194      * @see org.apache.cactus.WebRequest#getSessionCookie()
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         //Not sure whether I should be adding the service parameter to
213         //this request (this) or to the obtainSessionIdRequest
214         //seems obvious that it should be the obtainSessionIdRequest
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         // TODO: Add a constructor to the Cookie class that takes a Cookie
250         // as parameter.
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 }