Coverage Report

##### Coverage Report - org.melati.util.HttpUtil

Classes in this File | Line Coverage | Branch Coverage | Complexity
HttpUtil |
90% | 39/43
52% | 18/34
| 3.375;3.375

1 |  |
     /*
2 |  |
      * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.util.HttpUtil.html,v $
3 |  |
      * $Revision: 1.1 $
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 | 642 |
         String scheme = request.getScheme();
64 | 642 |
         url.append(scheme);
65 | 642 |
         url.append("://");
66 | 642 |
         url.append(request.getServerName());
67 | 642 |
         if ((scheme.equals("http") &&
68 |  |
             request.getServerPort() != 80
69 |  |
             )
70 |  |
             ||
71 |  |
             (scheme.equals("https") &&
72 |  |
             request.getServerPort() != 443)) {
73 | 532 |
           url.append(':');
74 | 532 |
           url.append(request.getServerPort());
75 |  |
         }
76 | 642 |
         appendRelativeZoneURL(url,request);
77 | 642 |
       }
78 |  |
79 |  |
       /**
80 |  |
        * Return the server URL.
81 |  |
        *
82 |  |
        * @param request the request to interrogate
83 |  |
        */
84 |  |
       public static String getServerURL(HttpServletRequest request) {
85 | 2 |
         StringBuffer url = new StringBuffer();
86 | 2 |
         String scheme = request.getScheme();
87 | 2 |
         url.append(scheme);
88 | 2 |
         url.append("://");
89 | 2 |
         url.append(request.getServerName());
90 | 2 |
         if ((scheme.equals("http") &&
91 |  |
             request.getServerPort() != 80
92 |  |
             )
93 |  |
             ||
94 |  |
             (scheme.equals("https") &&
95 |  |
             request.getServerPort() != 443)) {
96 | 0 |
           url.append(':');
97 | 0 |
           url.append(request.getServerPort());
98 |  |
         }
99 | 2 |
         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 | 720 |
         url.append(request.getContextPath());
117 | 720 |
         String servletPath = request.getServletPath();
118 | 720 |
         if (servletPath != null && !servletPath.equals("")) {
119 | 720 |
           url.append(servletPath.substring(0, servletPath.lastIndexOf('/')));
120 | 720 |
           if (servletPath.lastIndexOf('/') == -1)
121 | 0 |
             throw new MelatiBugMelatiException(
122 |  |
                 "Servlet Path does not contain a forward slash:" + servletPath);
123 |  |
         }
124 | 720 |
       }
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 | 636 |
         StringBuffer url = new StringBuffer();
133 | 636 |
         appendZoneURL(url, request);
134 | 636 |
         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 | 4 |
         StringBuffer url = new StringBuffer();
144 | 4 |
         appendZoneURL(url, request);
145 | 4 |
         String servlet = request.getServletPath();
146 | 4 |
         if (servlet != null && !servlet.equals(""))
147 | 4 |
           url.append(servlet.substring(
148 |  |
                               servlet.lastIndexOf('/'), servlet.length()));
149 | 4 |
         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 | 30 |
         StringBuffer url = new StringBuffer();
159 | 30 |
         url.append(request.getContextPath());
160 | 30 |
         if (request.getServletPath() != null) url.append(request.getServletPath());
161 | 30 |
         if (request.getPathInfo() != null) url.append(request.getPathInfo());
162 | 30 |
         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 | 6 |
         if (url.endsWith("/") && relativeUrl.startsWith("/"))
172 | 4 |
           return url.substring(0, url.lastIndexOf('/')) + relativeUrl;
173 |  |
         else
174 | 2 |
           return url + relativeUrl;
175 |  |
       }
176 |  |
177 |  |
     }