henning     2003/03/13 07:22:17

  Modified:    src/java/org/apache/turbine/util ServletUtils.java
                        ServerData.java
  Log:
  Cleanup the translation code. Move hostUrl into the ServerData class.
  Deprecate all constants in favour of URIConstants
  
  Revision  Changes    Path
  1.4       +35 -27    
jakarta-turbine-2/src/java/org/apache/turbine/util/ServletUtils.java
  
  Index: ServletUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/util/ServletUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletUtils.java 9 Mar 2003 02:54:11 -0000       1.3
  +++ ServletUtils.java 13 Mar 2003 15:22:17 -0000      1.4
  @@ -59,36 +59,44 @@
   import javax.servlet.ServletContext;
   import javax.servlet.http.HttpServletRequest;
   
  +import org.apache.commons.lang.StringUtils;
  +
   import org.apache.turbine.Turbine;
  +import org.apache.turbine.util.uri.URIConstants;
   
   /**
    * This is where common Servlet manipulation routines should go.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Gonzalo Diethelm</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
    * @version $Id$
    */
   public class ServletUtils
   {
       /**
        * The default HTTP port number.
  +     * @deprecated use URIConstants.HTTP_PORT
        */
  -    public static final int HTTP_PORT = 80;
  +    public static final int HTTP_PORT = URIConstants.HTTP_PORT;
   
       /**
        * The default HTTPS port number.
  +     * @deprecated use URIConstants.HTTPS_PORT
        */
  -    public static final int HTTPS_PORT = 443;
  +    public static final int HTTPS_PORT = URIConstants.HTTPS_PORT;
   
       /**
        * The default FTP port number.
  +     * @deprecated use URIConstants.FTP_PORT
        */
  -    public static final int FTP_PORT = 20;
  +    public static final int FTP_PORT = URIConstants.FTP_PORT;
   
       /**
        * The part of the URI which separates the protocol indicator (i.e. the
  -     * scheme) from the rest of the URI.
  +     * scheme) from the rest of the URI. 
  +     * @deprecated use URIConstants.URI_SCHEME_SEPARATOR;
        */
  -    public static final String URI_SCHEME_SEPARATOR = "://";
  +    public static final String URI_SCHEME_SEPARATOR = 
URIConstants.URI_SCHEME_SEPARATOR;
   
       /**
        * Expands a string that points to a relative path or path list,
  @@ -103,7 +111,7 @@
       public static String expandRelative(ServletConfig config,
                                           String text)
       {
  -        if (text == null || text.length() <= 0)
  +        if (StringUtils.isEmpty(text))
           {
               return text;
           }
  @@ -122,19 +130,21 @@
               sb.append(text);
               text = sb.toString();
           }
  -
  +        
           ServletContext context = config.getServletContext();
           String base = context.getRealPath("/");
  -        if (base == null)
  -        {
  -            base = config.getInitParameter(Turbine.BASEDIR_KEY);
  -        }
  -        if (base == null)
  +        
  +        base = (StringUtils.isEmpty(base))
  +            ? config.getInitParameter(Turbine.BASEDIR_KEY) 
  +            : base;
  +            
  +        if (StringUtils.isEmpty(base))
           {
               return text;
           }
   
           String separator = System.getProperty("path.separator");
  +        
           StringTokenizer tokenizer = new StringTokenizer(text,
                   separator);
           StringBuffer buffer = new StringBuffer();
  @@ -153,6 +163,7 @@
        * Defaults to the scheme used in the supplied request.
        *
        * @see #hostURL(HttpServletRequest req, String proto)
  +     * @deprecated Use ServerData(req).getHostUrl()
        */
       public static StringBuffer hostURL(HttpServletRequest req)
       {
  @@ -168,24 +179,21 @@
        * @param scheme The protocol indicator to prefix the host name with, or
        * the protocol used to address the server with if <code>null</code>.
        * @return The desired URL fragment.
  +     * @deprecated Use ServerData(req).getHostUrl()
        */
       public static StringBuffer hostURL(HttpServletRequest req, String scheme)
       {
  -        if (scheme == null)
  -        {
  -            scheme = req.getScheme();
  -        }
  -        StringBuffer url = new StringBuffer()
  -                .append(scheme)
  -                .append(URI_SCHEME_SEPARATOR)
  -                .append(req.getServerName());
  -        int port = req.getServerPort();
  -        if (!(("http".equalsIgnoreCase(scheme) && port == HTTP_PORT) ||
  -                ("https".equalsIgnoreCase(scheme) && port == HTTPS_PORT) ||
  -                ("ftp".equalsIgnoreCase(scheme) && port == FTP_PORT)))
  +        ServerData serverData = new ServerData(req);
  +
  +        if (StringUtils.isNotEmpty(scheme))
           {
  -            url.append(':').append(port);
  +            serverData.setServerScheme(scheme);
           }
  -        return url;
  +
  +        StringBuffer sb = new StringBuffer();
  +
  +        serverData.getHostUrl(sb);
  +
  +        return sb;
       }
   }
  
  
  
  1.6       +18 -5     
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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ServerData.java   13 Mar 2003 14:02:06 -0000      1.5
  +++ ServerData.java   13 Mar 2003 15:22:17 -0000      1.6
  @@ -269,14 +269,14 @@
           this.contextPath = contextPath;
       }
   
  +
       /**
  -     * Returns this element as an URL
  +     * Appends the Host URL to the supplied StringBuffer
        *
  -     * @return The contents of this element as a String
  +     * @param url A StringBuffer object
        */
  -    public String toString()
  +    public void getHostUrl(StringBuffer url)
       {
  -        StringBuffer url = new StringBuffer();
           url.append(getServerScheme());
           url.append("://");
           url.append(getServerName());
  @@ -290,6 +290,19 @@
               url.append(":");
               url.append(getServerPort());
           }
  +    }
  +
  +
  +    /**
  +     * Returns this object as an URL
  +     *
  +     * @return The contents of this object as a String
  +     */
  +    public String toString()
  +    {
  +        StringBuffer url = new StringBuffer();
  +
  +        getHostUrl(url);
   
           url.append(getScriptName());
           url.append(getContextPath());
  
  
  

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

Reply via email to