View Javadoc
1   package org.apache.turbine.util;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.Logger;
29  import org.apache.turbine.util.uri.URIConstants;
30  
31  /**
32   * Holds basic server information under which Turbine is running.
33   * This class is accessable via the RunData object within the Turbine
34   * system.  You can also use it as a placeholder for this information
35   * if you are only emulating a servlet system.
36   *
37   * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
38   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
39   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40   * @version $Id: ServerData.java 1854688 2019-03-03 10:36:42Z tv $
41   */
42  public class ServerData
43  {
44      /** Cached serverName, */
45      private String serverName = null;
46  
47      /** Cached serverPort. */
48      private int serverPort = 0;
49  
50      /** Cached serverScheme. */
51      private String serverScheme = null;
52  
53      /** Cached script name. */
54      private String  scriptName = null;
55  
56      /** Cached context path. */
57      private String  contextPath = null;
58  
59      /** Logging */
60      private static final Logger log = LogManager.getLogger(ServerData.class);
61  
62      /**
63       * Constructor.
64       *
65       * @param serverName The server name.
66       * @param serverPort The server port.
67       * @param serverScheme The server scheme.
68       * @param scriptName The script name.
69       * @param contextPath The context Path
70       */
71      public ServerData(String serverName,
72          int serverPort,
73          String serverScheme,
74          String scriptName,
75          String contextPath)
76      {
77          if (log.isDebugEnabled())
78          {
79              log.debug("Constructor({}, {}, {}, {}, {})", serverName,
80                      Integer.valueOf(serverPort),
81                      serverScheme,
82                      scriptName,
83                      contextPath);
84          }
85  
86          setServerName(serverName);
87          setServerPort(serverPort);
88          setServerScheme(serverScheme);
89          setScriptName(scriptName);
90          setContextPath(contextPath);
91      }
92  
93      /**
94       * Copy-Constructor
95       *
96       * @param serverData A ServerData Object
97       */
98      public ServerDatahref="../../../../org/apache/turbine/util/ServerData.html#ServerData">ServerData(ServerData serverData)
99      {
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 }