dlr         01/05/14 19:17:41

  Modified:    src/java/org/apache/turbine/util ServletUtils.java
  Log:
  A bunch of useful constants and the functionality for extracting the
  host part of a URL as a buffer for use in URL generation.
  
  Revision  Changes    Path
  1.9       +64 -1     
jakarta-turbine/src/java/org/apache/turbine/util/ServletUtils.java
  
  Index: ServletUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/util/ServletUtils.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ServletUtils.java 2001/05/05 03:19:18     1.8
  +++ ServletUtils.java 2001/05/15 02:17:39     1.9
  @@ -57,17 +57,40 @@
   import java.util.StringTokenizer;
   import javax.servlet.ServletConfig;
   import javax.servlet.ServletContext;
  +import javax.servlet.http.HttpServletRequest;
  +
   import org.apache.turbine.Turbine;
   
   /**
    * This is where common Servlet manipulation routines should go.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Gonzalo Diethelm</a>
  - * @version $Id: ServletUtils.java,v 1.8 2001/05/05 03:19:18 jvanzyl Exp $
  + * @version $Id: ServletUtils.java,v 1.9 2001/05/15 02:17:39 dlr Exp $
    */
   public class ServletUtils
   {
       /**
  +     * The default HTTP port number.
  +     */
  +    public static final int HTTP_PORT = 80;
  +
  +    /**
  +     * The default HTTPS port number.
  +     */
  +    public static final int HTTPS_PORT = 443;
  +
  +    /**
  +     * The default FTP port number.
  +     */
  +    public static final int FTP_PORT = 20;
  +
  +    /**
  +     * The part of the URI which separates the protocal indicator (i.e. the
  +     * scheme) from the rest of the URI.
  +     */
  +    public static final String URI_SCHEME_SEPARATOR = "://";
  +
  +    /**
        * Expands a string that points to a relative path or path list,
        * leaving it as an absolute path based on the servlet context.
        * It will return null if the text is empty or the config object
  @@ -124,5 +147,45 @@
               }
           }
           return buffer.toString();
  +    }
  +
  +    /**
  +     * Defaults to the scheme used in the supplied request.
  +     *
  +     * @see #hostURL(HttpServletRequest req, String proto)
  +     */
  +    public static StringBuffer hostURL(HttpServletRequest req)
  +    {
  +        return hostURL(req, null);
  +    }
  +
  +    /**
  +     * Returns a URL fragment derived from the provided HTTP request,
  +     * including the protocol used to address the server (if non-standard
  +     * for HTTP/HTTPS).  Returns the fragment as a buffer 
  +     *
  +     * @param req The request to extract information from.
  +     * @param scheme The protocal indicator to prefix the host name with, or
  +     * the protocal used to address the server with if <code>null</code>.
  +     * @return The desired URL fragment.
  +     */
  +    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)) )
  +        {
  +            url.append(':').append(port);
  +        }
  +        return url;
       }
   }
  
  
  

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

Reply via email to