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