1 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 41 public class WebRequestImpl extends BaseWebRequest 42 { 43 46 private ServletURL url; 47 48 51 private boolean isAutomaticSession = true; 52 53 57 private String redirectorName; 58 59 65 public WebRequestImpl() 66 { 67 } 68 69 72 public WebRequestImpl(WebConfiguration theConfiguration) 73 { 74 super(theConfiguration); 75 } 76 77 80 public void setRedirectorName(String theRedirectorName) 81 { 82 this.redirectorName = theRedirectorName; 83 } 84 85 88 public String getRedirectorName() 89 { 90 return this.redirectorName; 91 } 92 93 96 public void setAutomaticSession(boolean isAutomaticSession) 97 { 98 this.isAutomaticSession = isAutomaticSession; 99 } 100 101 104 public boolean getAutomaticSession() 105 { 106 return this.isAutomaticSession; 107 } 108 109 112 public void setURL(String theServerName, String theContextPath, 113 String theServletPath, String thePathInfo, String theQueryString) 114 { 115 this.url = new ServletURL(theServerName, theContextPath, 116 theServletPath, thePathInfo, theQueryString); 117 118 addQueryStringParameters(theQueryString); 121 } 122 123 126 public ServletURL getURL() 127 { 128 return this.url; 129 } 130 131 134 public String toString() 135 { 136 StringBuffer buffer = new StringBuffer (); 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 156 private void addQueryStringParameters(String theQueryString) 157 { 158 if (theQueryString == null) 159 { 160 return; 161 } 162 163 String nameValue = null; 164 StringTokenizer tokenizer = new StringTokenizer (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 ("Bad QueryString [" + theQueryString 180 + "] NameValue pair: [" + nameValue + "]"); 181 } 182 } 183 } 184 185 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 RequestDirectives directives = 208 new RequestDirectives(obtainSessionIdRequest); 209 directives.setService(ServiceEnumeration.CREATE_SESSION_SERVICE); 210 211 HttpURLConnection resultConnection; 212 try 213 { 214 resultConnection = 215 helper.connect(obtainSessionIdRequest, getConfiguration()); 216 } 217 catch (Throwable 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 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 |