1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.melati.util;
44
45 import javax.servlet.http.HttpServletRequest;
46
47
48
49
50 public final class HttpUtil {
51
52
53 private HttpUtil() {}
54
55
56
57
58
59
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
81
82
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
104
105
106
107
108
109
110
111
112
113
114 public static void appendRelativeZoneURL (
115 StringBuffer url, HttpServletRequest request) {
116 url.append(request.getContextPath());
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
128
129
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
139
140
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
154
155
156
157 public static String getRelativeRequestURL(HttpServletRequest request) {
158 StringBuffer url = new StringBuffer();
159 url.append(request.getContextPath());
160 if (request.getServletPath() != null) url.append(request.getServletPath());
161 if (request.getPathInfo() != null) url.append(request.getPathInfo());
162 return url.toString();
163 }
164
165
166
167
168
169
170 public static String concatenateUrls(String url, String relativeUrl) {
171 if (url.endsWith("/") && relativeUrl.startsWith("/"))
172 return url.substring(0, url.lastIndexOf('/')) + relativeUrl;
173 else
174 return url + relativeUrl;
175 }
176
177 }