Coverage report

  %line %branch
org.apache.turbine.util.ServerData
71% 
97% 

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

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.