Coverage Report - org.apache.turbine.util.ServerData
 
Classes in this File Line Coverage Branch Coverage Complexity
ServerData
95%
65/68
58%
7/12
1,375
 
 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  249
     private String serverName = null;
 46  
 
 47  
     /** Cached serverPort. */
 48  249
     private int serverPort = 0;
 49  
 
 50  
     /** Cached serverScheme. */
 51  249
     private String serverScheme = null;
 52  
 
 53  
     /** Cached script name. */
 54  249
     private String  scriptName = null;
 55  
 
 56  
     /** Cached context path. */
 57  249
     private String  contextPath = null;
 58  
 
 59  
     /** Logging */
 60  42
     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  87
     {
 77  87
         if (log.isDebugEnabled())
 78  
         {
 79  174
             log.debug("Constructor({}, {}, {}, {}, {})", serverName,
 80  87
                     Integer.valueOf(serverPort),
 81  
                     serverScheme,
 82  
                     scriptName,
 83  
                     contextPath);
 84  
         }
 85  
 
 86  87
         setServerName(serverName);
 87  87
         setServerPort(serverPort);
 88  87
         setServerScheme(serverScheme);
 89  87
         setScriptName(scriptName);
 90  87
         setContextPath(contextPath);
 91  87
     }
 92  
 
 93  
     /**
 94  
      * Copy-Constructor
 95  
      *
 96  
      * @param serverData A ServerData Object
 97  
      */
 98  
     public ServerData(ServerData serverData)
 99  72
     {
 100  72
         log.debug("Copy Constructor({})", serverData);
 101  
 
 102  72
         setServerName(serverData.getServerName());
 103  72
         setServerPort(serverData.getServerPort());
 104  72
         setServerScheme(serverData.getServerScheme());
 105  72
         setScriptName(serverData.getScriptName());
 106  72
         setContextPath(serverData.getContextPath());
 107  72
     }
 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  90
     {
 117  90
         setServerName(req.getServerName());
 118  90
         setServerPort(req.getServerPort());
 119  90
         setServerScheme(req.getScheme());
 120  90
         setScriptName(req.getServletPath());
 121  90
         setContextPath(req.getContextPath());
 122  90
     }
 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  72
         log.debug("clone()");
 133  72
         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  219
         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  249
         log.debug("setServerName({})", serverName);
 154  249
         this.serverName = serverName;
 155  249
     }
 156  
 
 157  
     /**
 158  
      * Get the server port.
 159  
      *
 160  
      * @return the server port.
 161  
      */
 162  
     public int getServerPort()
 163  
     {
 164  108
         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  249
         log.debug("setServerPort({})", Integer.valueOf(serverPort));
 175  249
         this.serverPort = serverPort;
 176  249
     }
 177  
 
 178  
     /**
 179  
      * Get the server scheme.
 180  
      *
 181  
      * @return the server scheme.
 182  
      */
 183  
     public String getServerScheme()
 184  
     {
 185  504
         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  249
         log.debug("setServerScheme({})", serverScheme);
 196  249
         this.serverScheme = serverScheme;
 197  249
     }
 198  
 
 199  
     /**
 200  
      * Get the script name
 201  
      *
 202  
      * @return the script name.
 203  
      */
 204  
     public String getScriptName()
 205  
     {
 206  261
         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  249
         log.debug("setScriptName({})", scriptName);
 217  249
         this.scriptName = scriptName;
 218  249
     }
 219  
 
 220  
     /**
 221  
      * Get the context path.
 222  
      *
 223  
      * @return the context path.
 224  
      */
 225  
     public String getContextPath()
 226  
     {
 227  258
         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  249
         log.debug("setContextPath({})", contextPath);
 238  249
         this.contextPath = contextPath;
 239  249
     }
 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  144
         url.append(getServerScheme());
 249  144
         url.append("://");
 250  144
         url.append(getServerName());
 251  144
         if ((getServerScheme().equals(URIConstants.HTTP)
 252  30
                 && getServerPort() != URIConstants.HTTP_PORT)
 253  
             ||
 254  144
             (getServerScheme().equals(URIConstants.HTTPS)
 255  0
                 && getServerPort() != URIConstants.HTTPS_PORT)
 256  
             )
 257  
         {
 258  0
             url.append(":");
 259  0
             url.append(getServerPort());
 260  
         }
 261  144
     }
 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  144
         StringBuilder url = new StringBuilder();
 272  
 
 273  144
         getHostUrl(url);
 274  
 
 275  144
         url.append(getContextPath());
 276  144
         url.append(getScriptName());
 277  144
         return url.toString();
 278  
     }
 279  
 }