1 package org.apache.turbine.util.uri; 2 3 18 19 import javax.servlet.http.HttpServletResponse ; 20 21 import org.apache.commons.lang.StringUtils; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.apache.turbine.Turbine; 27 import org.apache.turbine.TurbineConstants; 28 29 import org.apache.turbine.util.RunData; 30 import org.apache.turbine.util.ServerData; 31 32 43 44 public abstract class BaseURI 45 implements URI, 46 URIConstants 47 { 48 49 private static Log log = LogFactory.getLog(BaseURI.class); 50 51 52 private ServerData serverData = 53 new ServerData(null, HTTP_PORT, HTTP, null, null); 54 55 56 private boolean redirect = false; 57 58 59 private HttpServletResponse response = null; 60 61 62 private String reference = null; 63 64 72 73 77 public BaseURI() 78 { 79 init(Turbine.getDefaultServerData()); 80 setResponse(null); 81 } 82 83 88 public BaseURI(RunData runData) 89 { 90 init(runData.getServerData()); 91 setResponse(runData.getResponse()); 92 } 93 94 100 public BaseURI(RunData runData, boolean redirect) 101 { 102 init(runData.getServerData()); 103 setResponse(runData.getResponse()); 104 setRedirect(redirect); 105 } 106 107 112 public BaseURI(ServerData serverData) 113 { 114 init(serverData); 115 setResponse(null); 116 } 117 118 124 public BaseURI(ServerData serverData, boolean redirect) 125 { 126 init(serverData); 127 setResponse(null); 128 setRedirect(redirect); 129 } 130 131 139 140 146 private void init(ServerData serverData) 147 { 148 log.debug("init(" + serverData + ")"); 149 150 if(serverData != null) 151 { 152 this.serverData = (ServerData) serverData.clone(); 157 } 158 else 159 { 160 log.error("Passed null ServerData object!"); 161 } 162 reference = null; 163 } 164 165 173 174 179 public void setRedirect(boolean redirect) 180 { 181 this.redirect = redirect; 182 } 183 184 190 public boolean isRedirect() 191 { 192 return redirect; 193 } 194 195 200 public String getScriptName() 201 { 202 return serverData.getScriptName(); 203 } 204 205 210 public void setScriptName(String scriptName) 211 { 212 serverData.setScriptName(scriptName); 213 } 214 215 220 public String getContextPath() 221 { 222 return serverData.getContextPath(); 223 } 224 225 230 public void setContextPath(String contextPath) 231 { 232 serverData.setContextPath(contextPath); 233 } 234 235 240 public String getServerName() 241 { 242 return serverData.getServerName(); 243 } 244 245 250 public void setServerName(String serverName) 251 { 252 serverData.setServerName(serverName); 253 } 254 255 260 public int getServerPort() 261 { 262 int serverPort = serverData.getServerPort(); 263 264 if (serverPort == 0) 265 { 266 if(getServerScheme().equals(HTTPS)) 267 { 268 serverPort = HTTPS_PORT; 269 } 270 else 271 { 272 serverPort = HTTP_PORT; 273 } 274 } 275 return serverPort; 276 } 277 278 283 public void setServerPort(int serverPort) 284 { 285 serverData.setServerPort(serverPort); 286 } 287 288 292 public void setSecure() 293 { 294 setSecure(HTTPS_PORT); 295 } 296 297 305 public void setSecure(int port) 306 { 307 boolean useSSL = 308 Turbine.getConfiguration() 309 .getBoolean(TurbineConstants.USE_SSL_KEY, 310 TurbineConstants.USE_SSL_DEFAULT); 311 312 setServerScheme(useSSL ? HTTPS : HTTP); 313 setServerPort(port); 314 } 315 316 321 public void setServerScheme(String serverScheme) 322 { 323 serverData.setServerScheme(StringUtils.isNotEmpty(serverScheme) 324 ? serverScheme : ""); 325 } 326 327 333 public String getServerScheme() 334 { 335 String serverScheme = serverData.getServerScheme(); 336 337 return StringUtils.isNotEmpty(serverScheme) ? serverScheme : HTTP; 338 } 339 340 345 public void setReference(String reference) 346 { 347 this.reference = reference; 348 } 349 350 355 public String getReference() 356 { 357 return hasReference() ? reference : ""; 358 } 359 360 365 public boolean hasReference() 366 { 367 return StringUtils.isNotEmpty(reference); 368 } 369 370 378 379 384 protected void setResponse(HttpServletResponse response) 385 { 386 this.response = response; 387 } 388 389 395 protected HttpServletResponse getResponse() 396 { 397 return response; 398 } 399 400 411 protected void getContextAndScript(StringBuffer sb) 412 { 413 String context = getContextPath(); 414 415 if(StringUtils.isNotEmpty(context)) 416 { 417 if(context.charAt(0) != '/') 418 { 419 sb.append('/'); 420 } 421 sb.append (context); 422 } 423 424 String script = getScriptName(); 426 427 if(StringUtils.isNotEmpty(script)) 428 { 429 if(script.charAt(0) != '/') 430 { 431 sb.append('/'); 432 } 433 sb.append (script); 434 } 435 } 436 437 448 protected void getSchemeAndPort(StringBuffer sb) 449 { 450 sb.append(getServerScheme()); 452 sb.append(URIConstants.URI_SCHEME_SEPARATOR); 453 sb.append(getServerName()); 454 455 if ((getServerScheme().equals(HTTP) 457 && getServerPort() != HTTP_PORT) 458 || (getServerScheme().equals(HTTPS) 459 && getServerPort() != HTTPS_PORT)) 460 { 461 sb.append(':'); 462 sb.append(getServerPort()); 463 } 464 } 465 466 475 protected String encodeResponse(String uri) 476 { 477 String res = uri; 478 479 HttpServletResponse response = getResponse(); 480 481 if(response == null) 482 { 483 log.debug("No Response Object!"); 484 } 485 else 486 { 487 try 488 { 489 if(isRedirect()) 490 { 491 log.debug("Should Redirect"); 492 res = response.encodeRedirectURL(uri); 493 } 494 else 495 { 496 res = response.encodeURL(uri); 497 } 498 } 499 catch(Exception e) 500 { 501 log.error("response" + response + ", uri: " + uri); 502 log.error("While trying to encode the URI: ", e); 503 } 504 } 505 506 log.debug("encodeResponse(): " + res); 507 return res; 508 } 509 } 510 | Popular Tags |