henning     2003/03/13 06:02:06

  Modified:    src/java/org/apache/turbine/util ServerData.java
  Log:
  - Add a C'tor which builds a ServerData object from a HttpRequest
  - Add a toString() method which returns the contents of ServerData as a
    String
  
  Revision  Changes    Path
  1.5       +47 -1     
jakarta-turbine-2/src/java/org/apache/turbine/util/ServerData.java
  
  Index: ServerData.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/util/ServerData.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ServerData.java   9 Mar 2003 02:54:11 -0000       1.4
  +++ ServerData.java   13 Mar 2003 14:02:06 -0000      1.5
  @@ -54,11 +54,15 @@
    * <http://www.apache.org/>.
    */
   
  +import javax.servlet.http.HttpServletRequest;
  +
   import org.apache.commons.lang.StringUtils;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  +import org.apache.turbine.util.uri.URIConstants;
  +
   /**
    * Holds basic server information under which Turbine is running.
    * This class is accessable via the RunData object within the Turbine
  @@ -134,6 +138,21 @@
       }
   
       /**
  +     * A C'tor that takes a HTTP Request object and
  +     * builds the server data from its contents
  +     *
  +     * @param req The HTTP Request
  +     */
  +    public ServerData(HttpServletRequest req)
  +    {
  +        setServerName(req.getServerName());
  +        setServerPort(req.getServerPort());
  +        setServerScheme(req.getScheme());
  +        setScriptName(req.getServletPath());
  +        setContextPath(req.getContextPath());
  +    }
  +
  +    /**
        * generates a new Object with the same values as this one.
        *
        * @return A cloned object.
  @@ -248,5 +267,32 @@
       {
           log.debug("setContextPath(" + contextPath + ")");
           this.contextPath = contextPath;
  +    }
  +
  +    /**
  +     * Returns this element as an URL
  +     *
  +     * @return The contents of this element as a String
  +     */
  +    public String toString()
  +    {
  +        StringBuffer url = new StringBuffer();
  +        url.append(getServerScheme());
  +        url.append("://");
  +        url.append(getServerName());
  +        if ((getServerScheme().equals(URIConstants.HTTP)
  +                && getServerPort() != URIConstants.HTTP_PORT) 
  +            ||
  +            (getServerScheme().equals(URIConstants.HTTPS)
  +                && getServerPort() != URIConstants.HTTPS_PORT)
  +            )
  +        {
  +            url.append(":");
  +            url.append(getServerPort());
  +        }
  +
  +        url.append(getScriptName());
  +        url.append(getContextPath());
  +        return url.toString();
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to