001package org.apache.turbine.util; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import javax.servlet.http.HttpServletRequest; 025 026import org.apache.commons.lang.StringUtils; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030 031import org.apache.turbine.util.uri.URIConstants; 032 033/** 034 * Holds basic server information under which Turbine is running. 035 * This class is accessable via the RunData object within the Turbine 036 * system. You can also use it as a placeholder for this information 037 * if you are only emulating a servlet system. 038 * 039 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a> 040 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 041 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 042 * @version $Id: ServerData.java 1709648 2015-10-20 17:08:10Z tv $ 043 */ 044public class ServerData 045{ 046 /** Cached serverName, */ 047 private String serverName = null; 048 049 /** Cached serverPort. */ 050 private int serverPort = 0; 051 052 /** Cached serverScheme. */ 053 private String serverScheme = null; 054 055 /** Cached script name. */ 056 private String scriptName = null; 057 058 /** Cached context path. */ 059 private String contextPath = null; 060 061 /** Logging */ 062 private static Log log = LogFactory.getLog(ServerData.class); 063 064 /** 065 * Constructor. 066 * 067 * @param serverName The server name. 068 * @param serverPort The server port. 069 * @param serverScheme The server scheme. 070 * @param scriptName The script name. 071 * @param contextPath The context Path 072 */ 073 public ServerData(String serverName, 074 int serverPort, 075 String serverScheme, 076 String scriptName, 077 String contextPath) 078 { 079 if (log.isDebugEnabled()) 080 { 081 StringBuilder sb = new StringBuilder(); 082 sb.append("Constructor("); 083 sb.append(serverName); 084 sb.append(", "); 085 sb.append(serverPort); 086 sb.append(", "); 087 sb.append(serverScheme); 088 sb.append(", "); 089 sb.append(scriptName); 090 sb.append(", "); 091 sb.append(contextPath); 092 sb.append(")"); 093 log.debug(sb.toString()); 094 } 095 096 setServerName(serverName); 097 setServerPort(serverPort); 098 setServerScheme(serverScheme); 099 setScriptName(scriptName); 100 setContextPath(contextPath); 101 } 102 103 /** 104 * Copy-Constructor 105 * 106 * @param serverData A ServerData Object 107 */ 108 public ServerData(ServerData serverData) 109 { 110 log.debug("Copy Constructor(" + serverData + ")"); 111 112 setServerName(serverData.getServerName()); 113 setServerPort(serverData.getServerPort()); 114 setServerScheme(serverData.getServerScheme()); 115 setScriptName(serverData.getScriptName()); 116 setContextPath(serverData.getContextPath()); 117 } 118 119 /** 120 * A C'tor that takes a HTTP Request object and 121 * builds the server data from its contents 122 * 123 * @param req The HTTP Request 124 */ 125 public ServerData(HttpServletRequest req) 126 { 127 setServerName(req.getServerName()); 128 setServerPort(req.getServerPort()); 129 setServerScheme(req.getScheme()); 130 setScriptName(req.getServletPath()); 131 setContextPath(req.getContextPath()); 132 } 133 134 /** 135 * generates a new Object with the same values as this one. 136 * 137 * @return A cloned object. 138 */ 139 public Object clone() 140 { 141 log.debug("clone()"); 142 return new ServerData(this); 143 } 144 145 /** 146 * Get the name of the server. 147 * 148 * @return A String. 149 */ 150 public String getServerName() 151 { 152 return StringUtils.isEmpty(serverName) ? "" : serverName; 153 } 154 155 /** 156 * Sets the cached serverName. 157 * 158 * @param serverName the server name. 159 */ 160 public void setServerName(String serverName) 161 { 162 log.debug("setServerName(" + serverName + ")"); 163 this.serverName = serverName; 164 } 165 166 /** 167 * Get the server port. 168 * 169 * @return the server port. 170 */ 171 public int getServerPort() 172 { 173 return this.serverPort; 174 } 175 176 /** 177 * Sets the cached serverPort. 178 * 179 * @param serverPort the server port. 180 */ 181 public void setServerPort(int serverPort) 182 { 183 log.debug("setServerPort(" + serverPort + ")"); 184 this.serverPort = serverPort; 185 } 186 187 /** 188 * Get the server scheme. 189 * 190 * @return the server scheme. 191 */ 192 public String getServerScheme() 193 { 194 return StringUtils.isEmpty(serverScheme) ? "" : serverScheme; 195 } 196 197 /** 198 * Sets the cached serverScheme. 199 * 200 * @param serverScheme the server scheme. 201 */ 202 public void setServerScheme(String serverScheme) 203 { 204 log.debug("setServerScheme(" + serverScheme + ")"); 205 this.serverScheme = serverScheme; 206 } 207 208 /** 209 * Get the script name 210 * 211 * @return the script name. 212 */ 213 public String getScriptName() 214 { 215 return StringUtils.isEmpty(scriptName) ? "" : scriptName; 216 } 217 218 /** 219 * Set the script name. 220 * 221 * @param scriptName the script name. 222 */ 223 public void setScriptName(String scriptName) 224 { 225 log.debug("setScriptName(" + scriptName + ")"); 226 this.scriptName = scriptName; 227 } 228 229 /** 230 * Get the context path. 231 * 232 * @return the context path. 233 */ 234 public String getContextPath() 235 { 236 return StringUtils.isEmpty(contextPath) ? "" : contextPath; 237 } 238 239 /** 240 * Set the context path. 241 * 242 * @param contextPath A String. 243 */ 244 public void setContextPath(String contextPath) 245 { 246 log.debug("setContextPath(" + contextPath + ")"); 247 this.contextPath = contextPath; 248 } 249 250 /** 251 * Appends the Host URL to the supplied StringBuilder. 252 * 253 * @param url A StringBuilder object 254 */ 255 public void getHostUrl(StringBuilder url) 256 { 257 url.append(getServerScheme()); 258 url.append("://"); 259 url.append(getServerName()); 260 if ((getServerScheme().equals(URIConstants.HTTP) 261 && getServerPort() != URIConstants.HTTP_PORT) 262 || 263 (getServerScheme().equals(URIConstants.HTTPS) 264 && getServerPort() != URIConstants.HTTPS_PORT) 265 ) 266 { 267 url.append(":"); 268 url.append(getServerPort()); 269 } 270 } 271 272 /** 273 * Returns this object as an URL. 274 * 275 * @return The contents of this object as a String 276 */ 277 public String toString() 278 { 279 StringBuilder url = new StringBuilder(); 280 281 getHostUrl(url); 282 283 url.append(getContextPath()); 284 url.append(getScriptName()); 285 return url.toString(); 286 } 287}