Coverage Report - org.melati.util.HttpUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpUtil
88%
44/50
52%
18/34
3.375
 
 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  
 
 45  
 import javax.servlet.http.HttpServletRequest;
 46  
 
 47  
 /**
 48  
  * An assortment of useful things to do with <code>Http</code>.
 49  
  */
 50  
 public final class HttpUtil {
 51  
 
 52  
   
 53  0
   private HttpUtil() {}
 54  
 
 55  
   /**
 56  
    * Add a Zone URL to buffer.
 57  
    *  
 58  
    * @param url an empty StringBuffer to append to 
 59  
    * @param request the request to interrogate
 60  
    */
 61  
   public static void appendZoneURL(StringBuffer url, 
 62  
                                    HttpServletRequest request) {
 63  321
     String scheme = request.getScheme();
 64  321
     url.append(scheme);
 65  321
     url.append("://");
 66  321
     url.append(request.getServerName());
 67  321
     if ((scheme.equals("http") && 
 68  321
         request.getServerPort() != 80
 69  
         )
 70  
         ||
 71  55
         (scheme.equals("https") && 
 72  0
         request.getServerPort() != 443)) {
 73  266
       url.append(':');
 74  266
       url.append(request.getServerPort());
 75  
     }
 76  321
     appendRelativeZoneURL(url,request);
 77  321
   }
 78  
 
 79  
   /**
 80  
    * Return the server URL.
 81  
    *  
 82  
    * @param request the request to interrogate
 83  
    */
 84  
   public static String getServerURL(HttpServletRequest request) {
 85  1
     StringBuffer url = new StringBuffer();
 86  1
     String scheme = request.getScheme();
 87  1
     url.append(scheme);
 88  1
     url.append("://");
 89  1
     url.append(request.getServerName());
 90  1
     if ((scheme.equals("http") && 
 91  1
         request.getServerPort() != 80
 92  
         )
 93  
         ||
 94  1
         (scheme.equals("https") && 
 95  0
         request.getServerPort() != 443)) {
 96  0
       url.append(':');
 97  0
       url.append(request.getServerPort());
 98  
     }
 99  1
     return url.toString();
 100  
   }
 101  
 
 102  
   /**
 103  
    * Append relative servlet zone url.
 104  
    * 
 105  
    * Note that this function should return 
 106  
    * /zone/servlet from a request of form 
 107  
    * http://host/zone/servlet/pathinfo?querystring
 108  
    * on all servlet API versions greater than 2.0.
 109  
    * In 2.0 the zone was returned in the ServletPath 
 110  
    * it is now in the ContextPath.
 111  
    * @param url StringBuffer to append to 
 112  
    * @param request the request to interrogate
 113  
    */
 114  
   public static void appendRelativeZoneURL (
 115  
       StringBuffer url, HttpServletRequest request) {
 116  361
     url.append(request.getContextPath());
 117  361
     String servletPath = request.getServletPath();
 118  361
     if (servletPath != null && !servletPath.equals("")) {
 119  361
       url.append(servletPath.substring(0, servletPath.lastIndexOf('/')));
 120  361
       if (servletPath.lastIndexOf('/') == -1) 
 121  0
         throw new MelatiBugMelatiException(
 122  
             "Servlet Path does not contain a forward slash:" + servletPath);
 123  
     }
 124  361
   }
 125  
 
 126  
   /**
 127  
    * Retrieve a Zone url.
 128  
    * @param request the request to interrogate
 129  
    * @return an Url up to the zone specification as a String 
 130  
    */
 131  
   public static String zoneURL(HttpServletRequest request) {
 132  318
     StringBuffer url = new StringBuffer();
 133  318
     appendZoneURL(url, request);
 134  318
     return url.toString();
 135  
   }
 136  
 
 137  
   /**
 138  
    * Retrieve a Servlet url from a request.
 139  
    * @param request the request to interrogate
 140  
    * @return an Url up to the servlet specification as a String 
 141  
    */
 142  
   public static String servletURL(HttpServletRequest request) {
 143  2
     StringBuffer url = new StringBuffer();
 144  2
     appendZoneURL(url, request);
 145  2
     String servlet = request.getServletPath();
 146  2
     if (servlet != null && !servlet.equals(""))
 147  4
       url.append(servlet.substring(
 148  2
                           servlet.lastIndexOf('/'), servlet.length()));
 149  2
     return url.toString();
 150  
   }
 151  
 
 152  
   /**
 153  
    * Retrieve a relative url from a request.
 154  
    * @param request the request to interrogate
 155  
    * @return a relative Url  
 156  
    */
 157  
   public static String getRelativeRequestURL(HttpServletRequest request) {
 158  15
     StringBuffer url = new StringBuffer();
 159  15
     url.append(request.getContextPath());
 160  15
     if (request.getServletPath() != null) url.append(request.getServletPath());
 161  15
     if (request.getPathInfo() != null) url.append(request.getPathInfo());
 162  15
     return url.toString();
 163  
   }
 164  
 
 165  
   /**
 166  
    * @param url An url or relative url which may end in a slash
 167  
    * @param relativeUrl A relative url which may start with a slash
 168  
    * @return an url without a duplicated slash at the join
 169  
    */
 170  
   public static String concatenateUrls(String url, String relativeUrl) {
 171  3
     if (url.endsWith("/") && relativeUrl.startsWith("/"))
 172  2
       return url.substring(0, url.lastIndexOf('/')) + relativeUrl;
 173  
     else 
 174  1
       return url + relativeUrl;
 175  
   }
 176  
 
 177  
 }