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.lang3.StringUtils;
027import org.apache.logging.log4j.LogManager;
028import org.apache.logging.log4j.Logger;
029import 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$
041 */
042public class ServerData
043{
044    /** Cached serverName, */
045    private String serverName = null;
046
047    /** Cached serverPort. */
048    private int serverPort = 0;
049
050    /** Cached serverScheme. */
051    private String serverScheme = null;
052
053    /** Cached script name. */
054    private String  scriptName = null;
055
056    /** Cached context path. */
057    private String  contextPath = null;
058
059    /** Logging */
060    private static final Logger log = LogManager.getLogger(ServerData.class);
061
062    /**
063     * Constructor.
064     *
065     * @param serverName The server name.
066     * @param serverPort The server port.
067     * @param serverScheme The server scheme.
068     * @param scriptName The script name.
069     * @param contextPath The context Path
070     */
071    public ServerData(String serverName,
072        int serverPort,
073        String serverScheme,
074        String scriptName,
075        String contextPath)
076    {
077        if (log.isDebugEnabled())
078        {
079            log.debug("Constructor({}, {}, {}, {}, {})", serverName,
080                    Integer.valueOf(serverPort),
081                    serverScheme,
082                    scriptName,
083                    contextPath);
084        }
085
086        setServerName(serverName);
087        setServerPort(serverPort);
088        setServerScheme(serverScheme);
089        setScriptName(scriptName);
090        setContextPath(contextPath);
091    }
092
093    /**
094     * Copy-Constructor
095     *
096     * @param serverData A ServerData Object
097     */
098    public ServerData(ServerData serverData)
099    {
100        log.debug("Copy Constructor({})", serverData);
101
102        setServerName(serverData.getServerName());
103        setServerPort(serverData.getServerPort());
104        setServerScheme(serverData.getServerScheme());
105        setScriptName(serverData.getScriptName());
106        setContextPath(serverData.getContextPath());
107    }
108
109    /**
110     * A C'tor that takes a HTTP Request object and
111     * builds the server data from its contents
112     *
113     * @param req The HTTP Request
114     */
115    public ServerData(HttpServletRequest req)
116    {
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    @Override
130    public Object clone()
131    {
132        log.debug("clone()");
133        return new ServerData(this);
134    }
135
136    /**
137     * Get the name of the server.
138     *
139     * @return A String.
140     */
141    public String getServerName()
142    {
143        return StringUtils.isEmpty(serverName) ? "" : serverName;
144    }
145
146    /**
147     * Sets the cached serverName.
148     *
149     * @param serverName the server name.
150     */
151    public void setServerName(String serverName)
152    {
153        log.debug("setServerName({})", serverName);
154        this.serverName = serverName;
155    }
156
157    /**
158     * Get the server port.
159     *
160     * @return the server port.
161     */
162    public int getServerPort()
163    {
164        return this.serverPort;
165    }
166
167    /**
168     * Sets the cached serverPort.
169     *
170     * @param serverPort the server port.
171     */
172    public void setServerPort(int serverPort)
173    {
174        log.debug("setServerPort({})", Integer.valueOf(serverPort));
175        this.serverPort = serverPort;
176    }
177
178    /**
179     * Get the server scheme.
180     *
181     * @return the server scheme.
182     */
183    public String getServerScheme()
184    {
185        return StringUtils.defaultIfEmpty(serverScheme, "");
186    }
187
188    /**
189     * Sets the cached serverScheme.
190     *
191     * @param serverScheme the server scheme.
192     */
193    public void setServerScheme(String serverScheme)
194    {
195        log.debug("setServerScheme({})", serverScheme);
196        this.serverScheme = serverScheme;
197    }
198
199    /**
200     * Get the script name
201     *
202     * @return the script name.
203     */
204    public String getScriptName()
205    {
206        return StringUtils.defaultIfEmpty(scriptName, "");
207    }
208
209    /**
210     * Set the script name.
211     *
212     * @param scriptName the script name.
213     */
214    public void setScriptName(String scriptName)
215    {
216        log.debug("setScriptName({})", scriptName);
217        this.scriptName = scriptName;
218    }
219
220    /**
221     * Get the context path.
222     *
223     * @return the context path.
224     */
225    public String getContextPath()
226    {
227        return StringUtils.defaultIfEmpty(contextPath, "");
228    }
229
230    /**
231     * Set the context path.
232     *
233     * @param contextPath A String.
234     */
235    public void setContextPath(String contextPath)
236    {
237        log.debug("setContextPath({})", contextPath);
238        this.contextPath = contextPath;
239    }
240
241    /**
242     * Appends the Host URL to the supplied StringBuilder.
243     *
244     * @param url A StringBuilder object
245     */
246    public void getHostUrl(StringBuilder url)
247    {
248        url.append(getServerScheme());
249        url.append("://");
250        url.append(getServerName());
251        if ((getServerScheme().equals(URIConstants.HTTP)
252                && getServerPort() != URIConstants.HTTP_PORT)
253            ||
254            (getServerScheme().equals(URIConstants.HTTPS)
255                && getServerPort() != URIConstants.HTTPS_PORT)
256            )
257        {
258            url.append(":");
259            url.append(getServerPort());
260        }
261    }
262
263    /**
264     * Returns this object as an URL.
265     *
266     * @return The contents of this object as a String
267     */
268    @Override
269    public String toString()
270    {
271        StringBuilder url = new StringBuilder();
272
273        getHostUrl(url);
274
275        url.append(getContextPath());
276        url.append(getScriptName());
277        return url.toString();
278    }
279}