HttpUtil.java

  1. /*
  2.  * $Source$
  3.  * $Revision$
  4.  *
  5.  * Copyright (C) 2001 Myles Chippendale
  6.  *
  7.  * Part of Melati (http://melati.org), a framework for the rapid
  8.  * development of clean, maintainable web applications.
  9.  *
  10.  * Melati is free software; Permission is granted to copy, distribute
  11.  * and/or modify this software under the terms either:
  12.  *
  13.  * a) the GNU General Public License as published by the Free Software
  14.  *    Foundation; either version 2 of the License, or (at your option)
  15.  *    any later version,
  16.  *
  17.  *    or
  18.  *
  19.  * b) any version of the Melati Software License, as published
  20.  *    at http://melati.org
  21.  *
  22.  * You should have received a copy of the GNU General Public License and
  23.  * the Melati Software License along with this program;
  24.  * if not, write to the Free Software Foundation, Inc.,
  25.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
  26.  * GNU General Public License and visit http://melati.org to obtain the
  27.  * Melati Software License.
  28.  *
  29.  * Feel free to contact the Developers of Melati (http://melati.org),
  30.  * if you would like to work out a different arrangement than the options
  31.  * outlined here.  It is our intention to allow Melati to be used by as
  32.  * wide an audience as possible.
  33.  *
  34.  * This program is distributed in the hope that it will be useful,
  35.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37.  * GNU General Public License for more details.
  38.  *
  39.  * Contact details for copyright holder:
  40.  *
  41.  *     Myles Chippendale <mylesc At paneris.org>
  42.  */
  43. package org.melati.util;

  44. import javax.servlet.http.HttpServletRequest;

  45. /**
  46.  * An assortment of useful things to do with <code>Http</code>.
  47.  */
  48. public final class HttpUtil {

  49.  
  50.   private HttpUtil() {}

  51.   /**
  52.    * Add a Zone URL to buffer.
  53.    *  
  54.    * @param url an empty StringBuffer to append to
  55.    * @param request the request to interrogate
  56.    */
  57.   public static void appendZoneURL(StringBuffer url,
  58.                                    HttpServletRequest request) {
  59.     String scheme = request.getScheme();
  60.     url.append(scheme);
  61.     url.append("://");
  62.     url.append(request.getServerName());
  63.     if ((scheme.equals("http") &&
  64.         request.getServerPort() != 80
  65.         )
  66.         ||
  67.         (scheme.equals("https") &&
  68.         request.getServerPort() != 443)) {
  69.       url.append(':');
  70.       url.append(request.getServerPort());
  71.     }
  72.     appendRelativeZoneURL(url,request);
  73.   }

  74.   /**
  75.    * Return the server URL.
  76.    *  
  77.    * @param request the request to interrogate
  78.    */
  79.   public static String getServerURL(HttpServletRequest request) {
  80.     StringBuffer url = new StringBuffer();
  81.     String scheme = request.getScheme();
  82.     url.append(scheme);
  83.     url.append("://");
  84.     url.append(request.getServerName());
  85.     if ((scheme.equals("http") &&
  86.         request.getServerPort() != 80
  87.         )
  88.         ||
  89.         (scheme.equals("https") &&
  90.         request.getServerPort() != 443)) {
  91.       url.append(':');
  92.       url.append(request.getServerPort());
  93.     }
  94.     return url.toString();
  95.   }

  96.   /**
  97.    * Append relative servlet zone url.
  98.    *
  99.    * Note that this function should return
  100.    * /zone/servlet from a request of form
  101.    * http://host/zone/servlet/pathinfo?querystring
  102.    * on all servlet API versions greater than 2.0.
  103.    * In 2.0 the zone was returned in the ServletPath
  104.    * it is now in the ContextPath.
  105.    * @param url StringBuffer to append to
  106.    * @param request the request to interrogate
  107.    */
  108.   public static void appendRelativeZoneURL (
  109.       StringBuffer url, HttpServletRequest request) {
  110.     url.append(request.getContextPath());
  111.     String servletPath = request.getServletPath();
  112.     if (servletPath != null && !servletPath.equals("")) {
  113.       url.append(servletPath.substring(0, servletPath.lastIndexOf('/')));
  114.       if (servletPath.lastIndexOf('/') == -1)
  115.         throw new MelatiBugMelatiException(
  116.             "Servlet Path does not contain a forward slash:" + servletPath);
  117.     }
  118.   }

  119.   /**
  120.    * Retrieve a Zone url.
  121.    * @param request the request to interrogate
  122.    * @return an Url up to the zone specification as a String
  123.    */
  124.   public static String zoneURL(HttpServletRequest request) {
  125.     StringBuffer url = new StringBuffer();
  126.     appendZoneURL(url, request);
  127.     return url.toString();
  128.   }

  129.   /**
  130.    * Retrieve a Servlet url from a request.
  131.    * @param request the request to interrogate
  132.    * @return an Url up to the servlet specification as a String
  133.    */
  134.   public static String servletURL(HttpServletRequest request) {
  135.     StringBuffer url = new StringBuffer();
  136.     appendZoneURL(url, request);
  137.     String servlet = request.getServletPath();
  138.     if (servlet != null && !servlet.equals(""))
  139.       url.append(servlet.substring(
  140.                           servlet.lastIndexOf('/'), servlet.length()));
  141.     return url.toString();
  142.   }

  143.   /**
  144.    * Retrieve a relative url from a request.
  145.    * @param request the request to interrogate
  146.    * @return a relative Url  
  147.    */
  148.   public static String getRelativeRequestURL(HttpServletRequest request) {
  149.     StringBuffer url = new StringBuffer();
  150.     url.append(request.getContextPath());
  151.     if (request.getServletPath() != null) url.append(request.getServletPath());
  152.     if (request.getPathInfo() != null) url.append(request.getPathInfo());
  153.     return url.toString();
  154.   }

  155.   /**
  156.    * @param url An url or relative url which may end in a slash
  157.    * @param relativeUrl A relative url which may start with a slash
  158.    * @return an url without a duplicated slash at the join
  159.    */
  160.   public static String concatenateUrls(String url, String relativeUrl) {
  161.     if (url.endsWith("/") && relativeUrl.startsWith("/"))
  162.       return url.substring(0, url.lastIndexOf('/')) + relativeUrl;
  163.     else
  164.       return url + relativeUrl;
  165.   }

  166. }