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