001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal;
021:
022: import java.net.HttpURLConnection;
023: import java.util.StringTokenizer;
024:
025: import org.apache.cactus.Cookie;
026: import org.apache.cactus.HttpSessionCookie;
027: import org.apache.cactus.ServletURL;
028: import org.apache.cactus.WebResponse;
029: import org.apache.cactus.internal.client.ClientException;
030: import org.apache.cactus.internal.client.WebResponseObjectFactory;
031: import org.apache.cactus.internal.client.connector.http.HttpClientConnectionHelper;
032: import org.apache.cactus.internal.configuration.WebConfiguration;
033: import org.apache.cactus.util.ChainedRuntimeException;
034:
035: /**
036: * Extends {@link BaseWebRequest} to add properties specific to the
037: * Cactus Web Redirectors.
038: *
039: * @version $Id: WebRequestImpl.java 238991 2004-05-22 11:34:50Z vmassol $
040: */
041: public class WebRequestImpl extends BaseWebRequest {
042: /**
043: * The URL to simulate
044: */
045: private ServletURL url;
046:
047: /**
048: * Automatic session creation flag (default is true).
049: */
050: private boolean isAutomaticSession = true;
051:
052: /**
053: * Redirector Name. This is to let the user the possibility to override
054: * the default Redirector Name specified in <code>cactus.properties</code>.
055: */
056: private String redirectorName;
057:
058: /**
059: * Default constructor that requires that
060: * {@link #setConfiguration(Configuration)} be called before the methods
061: * requiring a configuration object.
062: *
063: */
064: public WebRequestImpl() {
065: }
066:
067: /**
068: * @param theConfiguration the Cactus configuration
069: */
070: public WebRequestImpl(WebConfiguration theConfiguration) {
071: super (theConfiguration);
072: }
073:
074: /**
075: * @see org.apache.cactus.WebRequest#setRedirectorName(String)
076: */
077: public void setRedirectorName(String theRedirectorName) {
078: this .redirectorName = theRedirectorName;
079: }
080:
081: /**
082: * @see org.apache.cactus.WebRequest#getRedirectorName()
083: */
084: public String getRedirectorName() {
085: return this .redirectorName;
086: }
087:
088: /**
089: * @see org.apache.cactus.WebRequest#setAutomaticSession(boolean)
090: */
091: public void setAutomaticSession(boolean isAutomaticSession) {
092: this .isAutomaticSession = isAutomaticSession;
093: }
094:
095: /**
096: * @see org.apache.cactus.WebRequest#getAutomaticSession()
097: */
098: public boolean getAutomaticSession() {
099: return this .isAutomaticSession;
100: }
101:
102: /**
103: * @see org.apache.cactus.WebRequest#setURL(String, String, String, String, String)
104: */
105: public void setURL(String theServerName, String theContextPath,
106: String theServletPath, String thePathInfo,
107: String theQueryString) {
108: this .url = new ServletURL(theServerName, theContextPath,
109: theServletPath, thePathInfo, theQueryString);
110:
111: // Now automatically add all HTTP parameters to the list of passed
112: // parameters
113: addQueryStringParameters(theQueryString);
114: }
115:
116: /**
117: * @see org.apache.cactus.WebRequest#getURL()
118: */
119: public ServletURL getURL() {
120: return this .url;
121: }
122:
123: /**
124: * @return a string representation of the request
125: */
126: public String toString() {
127: StringBuffer buffer = new StringBuffer();
128:
129: buffer.append("simulation URL = [" + getURL() + "], ");
130: buffer.append("automatic session = [" + getAutomaticSession()
131: + "], ");
132:
133: buffer.append(super .toString());
134:
135: return buffer.toString();
136: }
137:
138: /**
139: * Extract the HTTP parameters that might have been specified on the
140: * query string and add them to the list of parameters to pass to the
141: * servlet redirector.
142: *
143: * @param theQueryString the Query string in the URL to simulate, i.e. this
144: * is the string that will be returned by the
145: * <code>HttpServletResquest.getQueryString()</code>.
146: * Can be null.
147: */
148: private void addQueryStringParameters(String theQueryString) {
149: if (theQueryString == null) {
150: return;
151: }
152:
153: String nameValue = null;
154: StringTokenizer tokenizer = new StringTokenizer(theQueryString,
155: "&");
156: int breakParam = -1;
157:
158: while (tokenizer.hasMoreTokens()) {
159: nameValue = tokenizer.nextToken();
160: breakParam = nameValue.indexOf("=");
161:
162: if (breakParam != -1) {
163: addParameter(nameValue.substring(0, breakParam),
164: nameValue.substring(breakParam + 1));
165: } else {
166: throw new RuntimeException("Bad QueryString ["
167: + theQueryString + "] NameValue pair: ["
168: + nameValue + "]");
169: }
170: }
171: }
172:
173: /**
174: * @see org.apache.cactus.WebRequest#getSessionCookie()
175: */
176: public HttpSessionCookie getSessionCookie() {
177: if (getConfiguration() == null) {
178: throw new ChainedRuntimeException(
179: "setConfiguration() should have "
180: + "been called prior to calling getSessionCookie()");
181: }
182:
183: HttpClientConnectionHelper helper = new HttpClientConnectionHelper(
184: ((WebConfiguration) getConfiguration())
185: .getRedirectorURL(this ));
186:
187: WebRequestImpl obtainSessionIdRequest = new WebRequestImpl(
188: (WebConfiguration) getConfiguration());
189:
190: //Not sure whether I should be adding the service parameter to
191: //this request (this) or to the obtainSessionIdRequest
192: //seems obvious that it should be the obtainSessionIdRequest
193: RequestDirectives directives = new RequestDirectives(
194: obtainSessionIdRequest);
195: directives
196: .setService(ServiceEnumeration.CREATE_SESSION_SERVICE);
197:
198: HttpURLConnection resultConnection;
199: try {
200: resultConnection = helper.connect(obtainSessionIdRequest,
201: getConfiguration());
202: } catch (Throwable e) {
203: throw new ChainedRuntimeException("Failed to connect to ["
204: + ((WebConfiguration) getConfiguration())
205: .getRedirectorURL(this ) + "]", e);
206: }
207:
208: WebResponse response;
209: try {
210: response = (WebResponse) new WebResponseObjectFactory(
211: resultConnection)
212: .getResponseObject(WebResponse.class.getName(),
213: obtainSessionIdRequest);
214: } catch (ClientException e) {
215: throw new ChainedRuntimeException("Failed to connect to ["
216: + ((WebConfiguration) getConfiguration())
217: .getRedirectorURL(this ) + "]", e);
218: }
219:
220: Cookie cookie = response.getCookieIgnoreCase("jsessionid");
221:
222: // TODO: Add a constructor to the Cookie class that takes a Cookie
223: // as parameter.
224:
225: HttpSessionCookie sessionCookie = null;
226:
227: if (cookie != null) {
228: sessionCookie = new HttpSessionCookie(cookie.getDomain(),
229: cookie.getName(), cookie.getValue());
230: sessionCookie.setComment(cookie.getComment());
231: sessionCookie.setExpiryDate(cookie.getExpiryDate());
232: sessionCookie.setPath(cookie.getPath());
233: sessionCookie.setSecure(cookie.isSecure());
234: }
235:
236: return sessionCookie;
237: }
238: }
|