1 | |
package org.apache.turbine.util; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
import javax.servlet.http.HttpServletRequest; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
|
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
import org.apache.turbine.util.uri.URIConstants; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class ServerData |
45 | |
{ |
46 | |
|
47 | 81 | private String serverName = null; |
48 | |
|
49 | |
|
50 | 81 | private int serverPort = 0; |
51 | |
|
52 | |
|
53 | 81 | private String serverScheme = null; |
54 | |
|
55 | |
|
56 | 81 | private String scriptName = null; |
57 | |
|
58 | |
|
59 | 81 | private String contextPath = null; |
60 | |
|
61 | |
|
62 | 13 | private static Log log = LogFactory.getLog(ServerData.class); |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public ServerData(String serverName, |
74 | |
int serverPort, |
75 | |
String serverScheme, |
76 | |
String scriptName, |
77 | |
String contextPath) |
78 | 29 | { |
79 | 29 | if (log.isDebugEnabled()) |
80 | |
{ |
81 | 29 | StringBuilder sb = new StringBuilder(); |
82 | 29 | sb.append("Constructor("); |
83 | 29 | sb.append(serverName); |
84 | 29 | sb.append(", "); |
85 | 29 | sb.append(serverPort); |
86 | 29 | sb.append(", "); |
87 | 29 | sb.append(serverScheme); |
88 | 29 | sb.append(", "); |
89 | 29 | sb.append(scriptName); |
90 | 29 | sb.append(", "); |
91 | 29 | sb.append(contextPath); |
92 | 29 | sb.append(")"); |
93 | 29 | log.debug(sb.toString()); |
94 | |
} |
95 | |
|
96 | 29 | setServerName(serverName); |
97 | 29 | setServerPort(serverPort); |
98 | 29 | setServerScheme(serverScheme); |
99 | 29 | setScriptName(scriptName); |
100 | 29 | setContextPath(contextPath); |
101 | 29 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public ServerData(ServerData serverData) |
109 | 24 | { |
110 | 24 | log.debug("Copy Constructor(" + serverData + ")"); |
111 | |
|
112 | 24 | setServerName(serverData.getServerName()); |
113 | 24 | setServerPort(serverData.getServerPort()); |
114 | 24 | setServerScheme(serverData.getServerScheme()); |
115 | 24 | setScriptName(serverData.getScriptName()); |
116 | 24 | setContextPath(serverData.getContextPath()); |
117 | 24 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public ServerData(HttpServletRequest req) |
126 | 28 | { |
127 | 28 | setServerName(req.getServerName()); |
128 | 28 | setServerPort(req.getServerPort()); |
129 | 28 | setServerScheme(req.getScheme()); |
130 | 28 | setScriptName(req.getServletPath()); |
131 | 28 | setContextPath(req.getContextPath()); |
132 | 28 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public Object clone() |
140 | |
{ |
141 | 24 | log.debug("clone()"); |
142 | 24 | return new ServerData(this); |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public String getServerName() |
151 | |
{ |
152 | 73 | return StringUtils.isEmpty(serverName) ? "" : serverName; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public void setServerName(String serverName) |
161 | |
{ |
162 | 81 | log.debug("setServerName(" + serverName + ")"); |
163 | 81 | this.serverName = serverName; |
164 | 81 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public int getServerPort() |
172 | |
{ |
173 | 36 | return this.serverPort; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public void setServerPort(int serverPort) |
182 | |
{ |
183 | 81 | log.debug("setServerPort(" + serverPort + ")"); |
184 | 81 | this.serverPort = serverPort; |
185 | 81 | } |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public String getServerScheme() |
193 | |
{ |
194 | 168 | return StringUtils.isEmpty(serverScheme) ? "" : serverScheme; |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public void setServerScheme(String serverScheme) |
203 | |
{ |
204 | 81 | log.debug("setServerScheme(" + serverScheme + ")"); |
205 | 81 | this.serverScheme = serverScheme; |
206 | 81 | } |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public String getScriptName() |
214 | |
{ |
215 | 87 | return StringUtils.isEmpty(scriptName) ? "" : scriptName; |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
public void setScriptName(String scriptName) |
224 | |
{ |
225 | 81 | log.debug("setScriptName(" + scriptName + ")"); |
226 | 81 | this.scriptName = scriptName; |
227 | 81 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public String getContextPath() |
235 | |
{ |
236 | 86 | return StringUtils.isEmpty(contextPath) ? "" : contextPath; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public void setContextPath(String contextPath) |
245 | |
{ |
246 | 81 | log.debug("setContextPath(" + contextPath + ")"); |
247 | 81 | this.contextPath = contextPath; |
248 | 81 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public void getHostUrl(StringBuilder url) |
256 | |
{ |
257 | 48 | url.append(getServerScheme()); |
258 | 48 | url.append("://"); |
259 | 48 | url.append(getServerName()); |
260 | 48 | if ((getServerScheme().equals(URIConstants.HTTP) |
261 | |
&& getServerPort() != URIConstants.HTTP_PORT) |
262 | |
|| |
263 | |
(getServerScheme().equals(URIConstants.HTTPS) |
264 | |
&& getServerPort() != URIConstants.HTTPS_PORT) |
265 | |
) |
266 | |
{ |
267 | 0 | url.append(":"); |
268 | 0 | url.append(getServerPort()); |
269 | |
} |
270 | 48 | } |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
public String toString() |
278 | |
{ |
279 | 48 | StringBuilder url = new StringBuilder(); |
280 | |
|
281 | 48 | getHostUrl(url); |
282 | |
|
283 | 48 | url.append(getContextPath()); |
284 | 48 | url.append(getScriptName()); |
285 | 48 | return url.toString(); |
286 | |
} |
287 | |
} |