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