1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 /***
29 * Holds basic server information under which Turbine is running.
30 * This class is accessable via the RunData object within the Turbine
31 * system. You can also use it as a placeholder for this information
32 * if you are only emulating a servlet system.
33 *
34 * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
35 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
36 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37 * @version $Id: ServerData.java 264148 2005-08-29 14:21:04Z henning $
38 */
39 public class ServerData
40 {
41 /*** Cached serverName, */
42 private String serverName = null;
43
44 /*** Cached serverPort. */
45 private int serverPort = 0;
46
47 /*** Cached serverScheme. */
48 private String serverScheme = null;
49
50 /*** Cached script name. */
51 private String scriptName = null;
52
53 /*** Cached context path. */
54 private String contextPath = null;
55
56 /*** Logging */
57 private static Log log = LogFactory.getLog(ServerData.class);
58
59 /***
60 * Constructor.
61 *
62 * @param serverName The server name.
63 * @param serverPort The server port.
64 * @param serverScheme The server scheme.
65 * @param scriptName The script name.
66 * @param contextPath The context Path
67 */
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 /***
99 * Copy-Constructor
100 *
101 * @param serverData A ServerData Object
102 */
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 /***
115 * A C'tor that takes a HTTP Request object and
116 * builds the server data from its contents
117 *
118 * @param req The HTTP Request
119 */
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 /***
130 * generates a new Object with the same values as this one.
131 *
132 * @return A cloned object.
133 */
134 public Object clone()
135 {
136 log.debug("clone()");
137 return new ServerData(this);
138 }
139
140 /***
141 * Get the name of the server.
142 *
143 * @return A String.
144 */
145 public String getServerName()
146 {
147 return StringUtils.isEmpty(serverName) ? "" : serverName;
148 }
149
150 /***
151 * Sets the cached serverName.
152 *
153 * @param serverName the server name.
154 */
155 public void setServerName(String serverName)
156 {
157 log.debug("setServerName(" + serverName + ")");
158 this.serverName = serverName;
159 }
160
161 /***
162 * Get the server port.
163 *
164 * @return the server port.
165 */
166 public int getServerPort()
167 {
168 return this.serverPort;
169 }
170
171 /***
172 * Sets the cached serverPort.
173 *
174 * @param serverPort the server port.
175 */
176 public void setServerPort(int serverPort)
177 {
178 log.debug("setServerPort(" + serverPort + ")");
179 this.serverPort = serverPort;
180 }
181
182 /***
183 * Get the server scheme.
184 *
185 * @return the server scheme.
186 */
187 public String getServerScheme()
188 {
189 return StringUtils.isEmpty(serverScheme) ? "" : serverScheme;
190 }
191
192 /***
193 * Sets the cached serverScheme.
194 *
195 * @param serverScheme the server scheme.
196 */
197 public void setServerScheme(String serverScheme)
198 {
199 log.debug("setServerScheme(" + serverScheme + ")");
200 this.serverScheme = serverScheme;
201 }
202
203 /***
204 * Get the script name
205 *
206 * @return the script name.
207 */
208 public String getScriptName()
209 {
210 return StringUtils.isEmpty(scriptName) ? "" : scriptName;
211 }
212
213 /***
214 * Set the script name.
215 *
216 * @param scriptName the script name.
217 */
218 public void setScriptName(String scriptName)
219 {
220 log.debug("setScriptName(" + scriptName + ")");
221 this.scriptName = scriptName;
222 }
223
224 /***
225 * Get the context path.
226 *
227 * @return the context path.
228 */
229 public String getContextPath()
230 {
231 return StringUtils.isEmpty(contextPath) ? "" : contextPath;
232 }
233
234 /***
235 * Set the context path.
236 *
237 * @param contextPath A String.
238 */
239 public void setContextPath(String contextPath)
240 {
241 log.debug("setContextPath(" + contextPath + ")");
242 this.contextPath = contextPath;
243 }
244
245 /***
246 * Appends the Host URL to the supplied StringBuffer.
247 *
248 * @param url A StringBuffer object
249 */
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 /***
268 * Returns this object as an URL.
269 *
270 * @return The contents of this object as a String
271 */
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 }