1 package org.apache.turbine.util; 2 3 18 19 import javax.servlet.http.HttpServletRequest ; 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.util.uri.URIConstants; 27 28 39 public class ServerData 40 { 41 42 private String serverName = null; 43 44 45 private int serverPort = 0; 46 47 48 private String serverScheme = null; 49 50 51 private String scriptName = null; 52 53 54 private String contextPath = null; 55 56 57 private static Log log = LogFactory.getLog(ServerData.class); 58 59 68 public ServerData(String serverName, 69 int serverPort, 70 String serverScheme, 71 String scriptName, 72 String contextPath) 73 { 74 if (log.isDebugEnabled()) 75 { 76 StringBuffer sb = new StringBuffer (); 77 sb.append("Constructor("); 78 sb.append(serverName); 79 sb.append(", "); 80 sb.append(serverPort); 81 sb.append(", "); 82 sb.append(serverScheme); 83 sb.append(", "); 84 sb.append(scriptName); 85 sb.append(", "); 86 sb.append(contextPath); 87 sb.append(")"); 88 log.debug(sb.toString()); 89 } 90 91 setServerName(serverName); 92 setServerPort(serverPort); 93 setServerScheme(serverScheme); 94 setScriptName(scriptName); 95 setContextPath(contextPath); 96 } 97 98 103 public ServerData(ServerData serverData) 104 { 105 log.debug("Copy Constructor(" + serverData + ")"); 106 107 setServerName(serverData.getServerName()); 108 setServerPort(serverData.getServerPort()); 109 setServerScheme(serverData.getServerScheme()); 110 setScriptName(serverData.getScriptName()); 111 setContextPath(serverData.getContextPath()); 112 } 113 114 120 public ServerData(HttpServletRequest req) 121 { 122 setServerName(req.getServerName()); 123 setServerPort(req.getServerPort()); 124 setServerScheme(req.getScheme()); 125 setScriptName(req.getServletPath()); 126 setContextPath(req.getContextPath()); 127 } 128 129 134 public Object clone() 135 { 136 log.debug("clone()"); 137 return new ServerData(this); 138 } 139 140 145 public String getServerName() 146 { 147 return StringUtils.isEmpty(serverName) ? "" : serverName; 148 } 149 150 155 public void setServerName(String serverName) 156 { 157 log.debug("setServerName(" + serverName + ")"); 158 this.serverName = serverName; 159 } 160 161 166 public int getServerPort() 167 { 168 return this.serverPort; 169 } 170 171 176 public void setServerPort(int serverPort) 177 { 178 log.debug("setServerPort(" + serverPort + ")"); 179 this.serverPort = serverPort; 180 } 181 182 187 public String getServerScheme() 188 { 189 return StringUtils.isEmpty(serverScheme) ? "" : serverScheme; 190 } 191 192 197 public void setServerScheme(String serverScheme) 198 { 199 log.debug("setServerScheme(" + serverScheme + ")"); 200 this.serverScheme = serverScheme; 201 } 202 203 208 public String getScriptName() 209 { 210 return StringUtils.isEmpty(scriptName) ? "" : scriptName; 211 } 212 213 218 public void setScriptName(String scriptName) 219 { 220 log.debug("setScriptName(" + scriptName + ")"); 221 this.scriptName = scriptName; 222 } 223 224 229 public String getContextPath() 230 { 231 return StringUtils.isEmpty(contextPath) ? "" : contextPath; 232 } 233 234 239 public void setContextPath(String contextPath) 240 { 241 log.debug("setContextPath(" + contextPath + ")"); 242 this.contextPath = contextPath; 243 } 244 245 250 public void getHostUrl(StringBuffer url) 251 { 252 url.append(getServerScheme()); 253 url.append("://"); 254 url.append(getServerName()); 255 if ((getServerScheme().equals(URIConstants.HTTP) 256 && getServerPort() != URIConstants.HTTP_PORT) 257 || 258 (getServerScheme().equals(URIConstants.HTTPS) 259 && getServerPort() != URIConstants.HTTPS_PORT) 260 ) 261 { 262 url.append(":"); 263 url.append(getServerPort()); 264 } 265 } 266 267 272 public String toString() 273 { 274 StringBuffer url = new StringBuffer (); 275 276 getHostUrl(url); 277 278 url.append(getContextPath()); 279 url.append(getScriptName()); 280 return url.toString(); 281 } 282 } 283 | Popular Tags |