View Javadoc

1   package org.apache.turbine.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
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 534527 2007-05-02 16:10:59Z 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 Log log = LogFactory.getLog(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              StringBuffer sb = new StringBuffer();
80              sb.append("Constructor(");
81              sb.append(serverName);
82              sb.append(", ");
83              sb.append(serverPort);
84              sb.append(", ");
85              sb.append(serverScheme);
86              sb.append(", ");
87              sb.append(scriptName);
88              sb.append(", ");
89              sb.append(contextPath);
90              sb.append(")");
91              log.debug(sb.toString());
92          }
93  
94          setServerName(serverName);
95          setServerPort(serverPort);
96          setServerScheme(serverScheme);
97          setScriptName(scriptName);
98          setContextPath(contextPath);
99      }
100 
101     /***
102      * Copy-Constructor
103      *
104      * @param serverData A ServerData Object
105      */
106     public ServerData(ServerData serverData)
107     {
108         log.debug("Copy Constructor(" + serverData + ")");
109 
110         setServerName(serverData.getServerName());
111         setServerPort(serverData.getServerPort());
112         setServerScheme(serverData.getServerScheme());
113         setScriptName(serverData.getScriptName());
114         setContextPath(serverData.getContextPath());
115     }
116 
117     /***
118      * A C'tor that takes a HTTP Request object and
119      * builds the server data from its contents
120      *
121      * @param req The HTTP Request
122      */
123     public ServerData(HttpServletRequest req)
124     {
125         setServerName(req.getServerName());
126         setServerPort(req.getServerPort());
127         setServerScheme(req.getScheme());
128         setScriptName(req.getServletPath());
129         setContextPath(req.getContextPath());
130     }
131 
132     /***
133      * generates a new Object with the same values as this one.
134      *
135      * @return A cloned object.
136      */
137     public Object clone()
138     {
139         log.debug("clone()");
140         return new ServerData(this);
141     }
142 
143     /***
144      * Get the name of the server.
145      *
146      * @return A String.
147      */
148     public String getServerName()
149     {
150         return StringUtils.isEmpty(serverName) ? "" : serverName;
151     }
152 
153     /***
154      * Sets the cached serverName.
155      *
156      * @param serverName the server name.
157      */
158     public void setServerName(String serverName)
159     {
160         log.debug("setServerName(" + serverName + ")");
161         this.serverName = serverName;
162     }
163 
164     /***
165      * Get the server port.
166      *
167      * @return the server port.
168      */
169     public int getServerPort()
170     {
171         return this.serverPort;
172     }
173 
174     /***
175      * Sets the cached serverPort.
176      *
177      * @param serverPort the server port.
178      */
179     public void setServerPort(int serverPort)
180     {
181         log.debug("setServerPort(" + serverPort + ")");
182         this.serverPort = serverPort;
183     }
184 
185     /***
186      * Get the server scheme.
187      *
188      * @return the server scheme.
189      */
190     public String getServerScheme()
191     {
192         return StringUtils.isEmpty(serverScheme) ? "" : serverScheme;
193     }
194 
195     /***
196      * Sets the cached serverScheme.
197      *
198      * @param serverScheme the server scheme.
199      */
200     public void setServerScheme(String serverScheme)
201     {
202         log.debug("setServerScheme(" + serverScheme + ")");
203         this.serverScheme = serverScheme;
204     }
205 
206     /***
207      * Get the script name
208      *
209      * @return the script name.
210      */
211     public String getScriptName()
212     {
213         return StringUtils.isEmpty(scriptName) ? "" : scriptName;
214     }
215 
216     /***
217      * Set the script name.
218      *
219      * @param scriptName the script name.
220      */
221     public void setScriptName(String scriptName)
222     {
223         log.debug("setScriptName(" + scriptName + ")");
224         this.scriptName = scriptName;
225     }
226 
227     /***
228      * Get the context path.
229      *
230      * @return the context path.
231      */
232     public String getContextPath()
233     {
234         return StringUtils.isEmpty(contextPath) ? "" : contextPath;
235     }
236 
237     /***
238      * Set the context path.
239      *
240      * @param contextPath A String.
241      */
242     public void setContextPath(String contextPath)
243     {
244         log.debug("setContextPath(" + contextPath + ")");
245         this.contextPath = contextPath;
246     }
247 
248     /***
249      * Appends the Host URL to the supplied StringBuffer.
250      *
251      * @param url A StringBuffer object
252      */
253     public void getHostUrl(StringBuffer url)
254     {
255         url.append(getServerScheme());
256         url.append("://");
257         url.append(getServerName());
258         if ((getServerScheme().equals(URIConstants.HTTP)
259                 && getServerPort() != URIConstants.HTTP_PORT)
260             ||
261             (getServerScheme().equals(URIConstants.HTTPS)
262                 && getServerPort() != URIConstants.HTTPS_PORT)
263             )
264         {
265             url.append(":");
266             url.append(getServerPort());
267         }
268     }
269 
270     /***
271      * Returns this object as an URL.
272      *
273      * @return The contents of this object as a String
274      */
275     public String toString()
276     {
277         StringBuffer url = new StringBuffer();
278 
279         getHostUrl(url);
280 
281         url.append(getContextPath());
282         url.append(getScriptName());
283         return url.toString();
284     }
285 }