|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| WebRequestImpl.java | 10% | 19.1% | 63.6% | 25% |
|
||||||||||||||
| 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;
|
|
| 23 |
import java.util.StringTokenizer;
|
|
| 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 238991 2004-05-22 11:34:50Z vmassol $
|
|
| 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 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 | 0 |
public WebRequestImpl()
|
| 66 |
{
|
|
| 67 |
} |
|
| 68 |
|
|
| 69 |
/**
|
|
| 70 |
* @param theConfiguration the Cactus configuration
|
|
| 71 |
*/
|
|
| 72 | 48 |
public WebRequestImpl(WebConfiguration theConfiguration)
|
| 73 |
{
|
|
| 74 | 48 |
super(theConfiguration);
|
| 75 |
} |
|
| 76 |
|
|
| 77 |
/**
|
|
| 78 |
* @see org.apache.cactus.WebRequest#setRedirectorName(String)
|
|
| 79 |
*/
|
|
| 80 | 24 |
public void setRedirectorName(String theRedirectorName) |
| 81 |
{
|
|
| 82 | 24 |
this.redirectorName = theRedirectorName;
|
| 83 |
} |
|
| 84 |
|
|
| 85 |
/**
|
|
| 86 |
* @see org.apache.cactus.WebRequest#getRedirectorName()
|
|
| 87 |
*/
|
|
| 88 | 72 |
public String getRedirectorName()
|
| 89 |
{
|
|
| 90 | 72 |
return this.redirectorName; |
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* @see org.apache.cactus.WebRequest#setAutomaticSession(boolean)
|
|
| 95 |
*/
|
|
| 96 | 0 |
public void setAutomaticSession(boolean isAutomaticSession) |
| 97 |
{
|
|
| 98 | 0 |
this.isAutomaticSession = isAutomaticSession;
|
| 99 |
} |
|
| 100 |
|
|
| 101 |
/**
|
|
| 102 |
* @see org.apache.cactus.WebRequest#getAutomaticSession()
|
|
| 103 |
*/
|
|
| 104 | 24 |
public boolean getAutomaticSession() |
| 105 |
{
|
|
| 106 | 24 |
return this.isAutomaticSession; |
| 107 |
} |
|
| 108 |
|
|
| 109 |
/**
|
|
| 110 |
* @see org.apache.cactus.WebRequest#setURL(String, String, String, String, String)
|
|
| 111 |
*/
|
|
| 112 | 1 |
public void setURL(String theServerName, String theContextPath, |
| 113 |
String theServletPath, String thePathInfo, String theQueryString) |
|
| 114 |
{
|
|
| 115 | 1 |
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 | 1 |
addQueryStringParameters(theQueryString); |
| 121 |
} |
|
| 122 |
|
|
| 123 |
/**
|
|
| 124 |
* @see org.apache.cactus.WebRequest#getURL()
|
|
| 125 |
*/
|
|
| 126 | 34 |
public ServletURL getURL()
|
| 127 |
{
|
|
| 128 | 34 |
return this.url; |
| 129 |
} |
|
| 130 |
|
|
| 131 |
/**
|
|
| 132 |
* @return a string representation of the request
|
|
| 133 |
*/
|
|
| 134 | 0 |
public String toString()
|
| 135 |
{
|
|
| 136 | 0 |
StringBuffer buffer = new StringBuffer();
|
| 137 |
|
|
| 138 | 0 |
buffer.append("simulation URL = [" + getURL() + "], "); |
| 139 | 0 |
buffer.append("automatic session = [" + getAutomaticSession() + "], "); |
| 140 |
|
|
| 141 | 0 |
buffer.append(super.toString());
|
| 142 |
|
|
| 143 | 0 |
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 | 1 |
private void addQueryStringParameters(String theQueryString) |
| 157 |
{
|
|
| 158 | 1 |
if (theQueryString == null) |
| 159 |
{
|
|
| 160 | 1 |
return;
|
| 161 |
} |
|
| 162 |
|
|
| 163 | 0 |
String nameValue = null;
|
| 164 | 0 |
StringTokenizer tokenizer = new StringTokenizer(theQueryString, "&"); |
| 165 | 0 |
int breakParam = -1;
|
| 166 |
|
|
| 167 | 0 |
while (tokenizer.hasMoreTokens())
|
| 168 |
{
|
|
| 169 | 0 |
nameValue = tokenizer.nextToken(); |
| 170 | 0 |
breakParam = nameValue.indexOf("=");
|
| 171 |
|
|
| 172 | 0 |
if (breakParam != -1)
|
| 173 |
{
|
|
| 174 | 0 |
addParameter(nameValue.substring(0, breakParam), |
| 175 |
nameValue.substring(breakParam + 1)); |
|
| 176 |
} |
|
| 177 |
else
|
|
| 178 |
{
|
|
| 179 | 0 |
throw new RuntimeException("Bad QueryString [" + theQueryString |
| 180 |
+ "] NameValue pair: [" + nameValue + "]"); |
|
| 181 |
} |
|
| 182 |
} |
|
| 183 |
} |
|
| 184 |
|
|
| 185 |
/**
|
|
| 186 |
* @see org.apache.cactus.WebRequest#getSessionCookie()
|
|
| 187 |
*/
|
|
| 188 | 0 |
public HttpSessionCookie getSessionCookie()
|
| 189 |
{
|
|
| 190 | 0 |
if (getConfiguration() == null) |
| 191 |
{
|
|
| 192 | 0 |
throw new ChainedRuntimeException("setConfiguration() should have " |
| 193 |
+ "been called prior to calling getSessionCookie()");
|
|
| 194 |
} |
|
| 195 |
|
|
| 196 | 0 |
HttpClientConnectionHelper helper = |
| 197 |
new HttpClientConnectionHelper(
|
|
| 198 |
((WebConfiguration) getConfiguration()).getRedirectorURL(this));
|
|
| 199 |
|
|
| 200 | 0 |
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 | 0 |
RequestDirectives directives = |
| 208 |
new RequestDirectives(obtainSessionIdRequest);
|
|
| 209 | 0 |
directives.setService(ServiceEnumeration.CREATE_SESSION_SERVICE); |
| 210 |
|
|
| 211 | 0 |
HttpURLConnection resultConnection; |
| 212 | 0 |
try
|
| 213 |
{
|
|
| 214 | 0 |
resultConnection = |
| 215 |
helper.connect(obtainSessionIdRequest, getConfiguration()); |
|
| 216 |
} |
|
| 217 |
catch (Throwable e)
|
|
| 218 |
{
|
|
| 219 | 0 |
throw new ChainedRuntimeException("Failed to connect to [" |
| 220 |
+ ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
|
|
| 221 |
+ "]", e);
|
|
| 222 |
} |
|
| 223 |
|
|
| 224 | 0 |
WebResponse response; |
| 225 | 0 |
try
|
| 226 |
{
|
|
| 227 | 0 |
response = (WebResponse) new WebResponseObjectFactory(
|
| 228 |
resultConnection).getResponseObject( |
|
| 229 |
WebResponse.class.getName(),
|
|
| 230 |
obtainSessionIdRequest); |
|
| 231 |
} |
|
| 232 |
catch (ClientException e)
|
|
| 233 |
{
|
|
| 234 | 0 |
throw new ChainedRuntimeException("Failed to connect to [" |
| 235 |
+ ((WebConfiguration) getConfiguration()).getRedirectorURL(this)
|
|
| 236 |
+ "]", e);
|
|
| 237 |
} |
|
| 238 |
|
|
| 239 | 0 |
Cookie cookie = response.getCookieIgnoreCase("jsessionid");
|
| 240 |
|
|
| 241 |
// TODO: Add a constructor to the Cookie class that takes a Cookie
|
|
| 242 |
// as parameter.
|
|
| 243 |
|
|
| 244 | 0 |
HttpSessionCookie sessionCookie = null;
|
| 245 |
|
|
| 246 | 0 |
if (cookie != null) |
| 247 |
{
|
|
| 248 | 0 |
sessionCookie = new HttpSessionCookie(cookie.getDomain(),
|
| 249 |
cookie.getName(), cookie.getValue()); |
|
| 250 | 0 |
sessionCookie.setComment(cookie.getComment()); |
| 251 | 0 |
sessionCookie.setExpiryDate(cookie.getExpiryDate()); |
| 252 | 0 |
sessionCookie.setPath(cookie.getPath()); |
| 253 | 0 |
sessionCookie.setSecure(cookie.isSecure()); |
| 254 |
} |
|
| 255 |
|
|
| 256 | 0 |
return sessionCookie;
|
| 257 |
} |
|
| 258 |
} |
|
| 259 |
|
|
||||||||||