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