Coverage Report - org.apache.turbine.util.ServerData
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerData
97%
74/76
66%
12/18
1,562
 
 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.lang.StringUtils;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 
 31  
 import org.apache.turbine.util.uri.URIConstants;
 32  
 
 33  
 /**
 34  
  * Holds basic server information under which Turbine is running.
 35  
  * This class is accessable via the RunData object within the Turbine
 36  
  * system.  You can also use it as a placeholder for this information
 37  
  * if you are only emulating a servlet system.
 38  
  *
 39  
  * @author <a href="mailto:burton@apache.org">Kevin A. Burton</a>
 40  
  * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
 41  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 42  
  * @version $Id: ServerData.java 615328 2008-01-25 20:25:05Z tv $
 43  
  */
 44  
 public class ServerData
 45  
 {
 46  
     /** Cached serverName, */
 47  180
     private String serverName = null;
 48  
 
 49  
     /** Cached serverPort. */
 50  180
     private int serverPort = 0;
 51  
 
 52  
     /** Cached serverScheme. */
 53  180
     private String serverScheme = null;
 54  
 
 55  
     /** Cached script name. */
 56  180
     private String  scriptName = null;
 57  
 
 58  
     /** Cached context path. */
 59  180
     private String  contextPath = null;
 60  
 
 61  
     /** Logging */
 62  28
     private static Log log = LogFactory.getLog(ServerData.class);
 63  
 
 64  
     /**
 65  
      * Constructor.
 66  
      *
 67  
      * @param serverName The server name.
 68  
      * @param serverPort The server port.
 69  
      * @param serverScheme The server scheme.
 70  
      * @param scriptName The script name.
 71  
      * @param contextPath The context Path
 72  
      */
 73  
     public ServerData(String serverName,
 74  
         int serverPort,
 75  
         String serverScheme,
 76  
         String scriptName,
 77  
         String contextPath)
 78  70
     {
 79  70
         if (log.isDebugEnabled())
 80  
         {
 81  70
             StringBuffer sb = new StringBuffer();
 82  70
             sb.append("Constructor(");
 83  70
             sb.append(serverName);
 84  70
             sb.append(", ");
 85  70
             sb.append(serverPort);
 86  70
             sb.append(", ");
 87  70
             sb.append(serverScheme);
 88  70
             sb.append(", ");
 89  70
             sb.append(scriptName);
 90  70
             sb.append(", ");
 91  70
             sb.append(contextPath);
 92  70
             sb.append(")");
 93  70
             log.debug(sb.toString());
 94  
         }
 95  
 
 96  70
         setServerName(serverName);
 97  70
         setServerPort(serverPort);
 98  70
         setServerScheme(serverScheme);
 99  70
         setScriptName(scriptName);
 100  70
         setContextPath(contextPath);
 101  70
     }
 102  
 
 103  
     /**
 104  
      * Copy-Constructor
 105  
      *
 106  
      * @param serverData A ServerData Object
 107  
      */
 108  
     public ServerData(ServerData serverData)
 109  58
     {
 110  58
         log.debug("Copy Constructor(" + serverData + ")");
 111  
 
 112  58
         setServerName(serverData.getServerName());
 113  58
         setServerPort(serverData.getServerPort());
 114  58
         setServerScheme(serverData.getServerScheme());
 115  58
         setScriptName(serverData.getScriptName());
 116  58
         setContextPath(serverData.getContextPath());
 117  58
     }
 118  
 
 119  
     /**
 120  
      * A C'tor that takes a HTTP Request object and
 121  
      * builds the server data from its contents
 122  
      *
 123  
      * @param req The HTTP Request
 124  
      */
 125  
     public ServerData(HttpServletRequest req)
 126  52
     {
 127  52
         setServerName(req.getServerName());
 128  52
         setServerPort(req.getServerPort());
 129  52
         setServerScheme(req.getScheme());
 130  52
         setScriptName(req.getServletPath());
 131  52
         setContextPath(req.getContextPath());
 132  52
     }
 133  
 
 134  
     /**
 135  
      * generates a new Object with the same values as this one.
 136  
      *
 137  
      * @return A cloned object.
 138  
      */
 139  
     public Object clone()
 140  
     {
 141  58
         log.debug("clone()");
 142  58
         return new ServerData(this);
 143  
     }
 144  
 
 145  
     /**
 146  
      * Get the name of the server.
 147  
      *
 148  
      * @return A String.
 149  
      */
 150  
     public String getServerName()
 151  
     {
 152  190
         return StringUtils.isEmpty(serverName) ? "" : serverName;
 153  
     }
 154  
 
 155  
     /**
 156  
      * Sets the cached serverName.
 157  
      *
 158  
      * @param serverName the server name.
 159  
      */
 160  
     public void setServerName(String serverName)
 161  
     {
 162  180
         log.debug("setServerName(" + serverName + ")");
 163  180
         this.serverName = serverName;
 164  180
     }
 165  
 
 166  
     /**
 167  
      * Get the server port.
 168  
      *
 169  
      * @return the server port.
 170  
      */
 171  
     public int getServerPort()
 172  
     {
 173  124
         return this.serverPort;
 174  
     }
 175  
 
 176  
     /**
 177  
      * Sets the cached serverPort.
 178  
      *
 179  
      * @param serverPort the server port.
 180  
      */
 181  
     public void setServerPort(int serverPort)
 182  
     {
 183  180
         log.debug("setServerPort(" + serverPort + ")");
 184  180
         this.serverPort = serverPort;
 185  180
     }
 186  
 
 187  
     /**
 188  
      * Get the server scheme.
 189  
      *
 190  
      * @return the server scheme.
 191  
      */
 192  
     public String getServerScheme()
 193  
     {
 194  448
         return StringUtils.isEmpty(serverScheme) ? "" : serverScheme;
 195  
     }
 196  
 
 197  
     /**
 198  
      * Sets the cached serverScheme.
 199  
      *
 200  
      * @param serverScheme the server scheme.
 201  
      */
 202  
     public void setServerScheme(String serverScheme)
 203  
     {
 204  180
         log.debug("setServerScheme(" + serverScheme + ")");
 205  180
         this.serverScheme = serverScheme;
 206  180
     }
 207  
 
 208  
     /**
 209  
      * Get the script name
 210  
      *
 211  
      * @return the script name.
 212  
      */
 213  
     public String getScriptName()
 214  
     {
 215  218
         return StringUtils.isEmpty(scriptName) ? "" : scriptName;
 216  
     }
 217  
 
 218  
     /**
 219  
      * Set the script name.
 220  
      *
 221  
      * @param scriptName the script name.
 222  
      */
 223  
     public void setScriptName(String scriptName)
 224  
     {
 225  194
         log.debug("setScriptName(" + scriptName + ")");
 226  194
         this.scriptName = scriptName;
 227  194
     }
 228  
 
 229  
     /**
 230  
      * Get the context path.
 231  
      *
 232  
      * @return the context path.
 233  
      */
 234  
     public String getContextPath()
 235  
     {
 236  216
         return StringUtils.isEmpty(contextPath) ? "" : contextPath;
 237  
     }
 238  
 
 239  
     /**
 240  
      * Set the context path.
 241  
      *
 242  
      * @param contextPath A String.
 243  
      */
 244  
     public void setContextPath(String contextPath)
 245  
     {
 246  180
         log.debug("setContextPath(" + contextPath + ")");
 247  180
         this.contextPath = contextPath;
 248  180
     }
 249  
 
 250  
     /**
 251  
      * Appends the Host URL to the supplied StringBuffer.
 252  
      *
 253  
      * @param url A StringBuffer object
 254  
      */
 255  
     public void getHostUrl(StringBuffer url)
 256  
     {
 257  116
         url.append(getServerScheme());
 258  116
         url.append("://");
 259  116
         url.append(getServerName());
 260  116
         if ((getServerScheme().equals(URIConstants.HTTP)
 261  
                 && getServerPort() != URIConstants.HTTP_PORT)
 262  
             ||
 263  
             (getServerScheme().equals(URIConstants.HTTPS)
 264  
                 && getServerPort() != URIConstants.HTTPS_PORT)
 265  
             )
 266  
         {
 267  0
             url.append(":");
 268  0
             url.append(getServerPort());
 269  
         }
 270  116
     }
 271  
 
 272  
     /**
 273  
      * Returns this object as an URL.
 274  
      *
 275  
      * @return The contents of this object as a String
 276  
      */
 277  
     public String toString()
 278  
     {
 279  116
         StringBuffer url = new StringBuffer();
 280  
 
 281  116
         getHostUrl(url);
 282  
 
 283  116
         url.append(getContextPath());
 284  116
         url.append(getScriptName());
 285  116
         return url.toString();
 286  
     }
 287  
 }