1 package org.apache.turbine.util.uri;
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.HttpServletResponse;
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.Turbine;
30 import org.apache.turbine.TurbineConstants;
31 import org.apache.turbine.util.RunData;
32 import org.apache.turbine.util.ServerData;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class BaseURI
47 implements URI,
48 URIConstants
49 {
50
51 private static final Logger log = LogManager.getLogger(BaseURI.class);
52
53
54 private ServerData serverData =
55 new ServerData(null, HTTP_PORT, HTTP, null, null);
56
57
58 private boolean redirect = false;
59
60
61 private HttpServletResponse response = null;
62
63
64 private String reference = null;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public BaseURI()
80 {
81 init(Turbine.getDefaultServerData());
82 setResponse(null);
83 }
84
85
86
87
88
89
90 public BaseURI(RunData runData)
91 {
92 init(runData.getServerData());
93 setResponse(runData.getResponse());
94 }
95
96
97
98
99
100
101
102 public BaseURI(RunData runData, boolean redirect)
103 {
104 init(runData.getServerData());
105 setResponse(runData.getResponse());
106 setRedirect(redirect);
107 }
108
109
110
111
112
113
114 public BaseURI(ServerData serverData)
115 {
116 init(serverData);
117 setResponse(null);
118 }
119
120
121
122
123
124
125
126 public BaseURI(ServerData serverData, boolean redirect)
127 {
128 init(serverData);
129 setResponse(null);
130 setRedirect(redirect);
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 private void init(ServerData serverData)
149 {
150 log.debug("init({})", serverData);
151
152 if (serverData != null)
153 {
154
155
156
157
158 this.serverData = (ServerData) serverData.clone();
159 }
160 else
161 {
162 log.error("Passed null ServerData object!");
163 }
164 reference = null;
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 public void setRedirect(boolean redirect)
182 {
183 this.redirect = redirect;
184 }
185
186
187
188
189
190
191
192 public boolean isRedirect()
193 {
194 return redirect;
195 }
196
197
198
199
200
201
202 @Override
203 public String getScriptName()
204 {
205 return serverData.getScriptName();
206 }
207
208
209
210
211
212
213 public void setScriptName(String scriptName)
214 {
215 serverData.setScriptName(scriptName);
216 }
217
218
219
220
221
222
223 @Override
224 public String getContextPath()
225 {
226 return serverData.getContextPath();
227 }
228
229
230
231
232
233
234 public void setContextPath(String contextPath)
235 {
236 serverData.setContextPath(contextPath);
237 }
238
239
240
241
242
243
244 @Override
245 public String getServerName()
246 {
247 return serverData.getServerName();
248 }
249
250
251
252
253
254
255 public void setServerName(String serverName)
256 {
257 serverData.setServerName(serverName);
258 }
259
260
261
262
263
264
265 @Override
266 public int getServerPort()
267 {
268 int serverPort = serverData.getServerPort();
269
270 if (serverPort == 0)
271 {
272 if(getServerScheme().equals(HTTPS))
273 {
274 serverPort = HTTPS_PORT;
275 }
276 else
277 {
278 serverPort = HTTP_PORT;
279 }
280 }
281 return serverPort;
282 }
283
284
285
286
287
288
289 public void setServerPort(int serverPort)
290 {
291 serverData.setServerPort(serverPort);
292 }
293
294
295
296
297
298 public void setSecure()
299 {
300 setSecure(HTTPS_PORT);
301 }
302
303
304
305
306
307
308
309
310
311 public void setSecure(int port)
312 {
313 boolean useSSL =
314 Turbine.getConfiguration()
315 .getBoolean(TurbineConstants.USE_SSL_KEY,
316 TurbineConstants.USE_SSL_DEFAULT);
317
318 setServerScheme(useSSL ? HTTPS : HTTP);
319 setServerPort(port);
320 }
321
322
323
324
325
326
327 public void setServerScheme(String serverScheme)
328 {
329 serverData.setServerScheme(StringUtils.isNotEmpty(serverScheme)
330 ? serverScheme : "");
331 }
332
333
334
335
336
337
338
339 @Override
340 public String getServerScheme()
341 {
342 String serverScheme = serverData.getServerScheme();
343
344 return StringUtils.isNotEmpty(serverScheme) ? serverScheme : HTTP;
345 }
346
347
348
349
350
351
352 public void setReference(String reference)
353 {
354 this.reference = reference;
355 }
356
357
358
359
360
361
362 @Override
363 public String getReference()
364 {
365 return hasReference() ? reference : "";
366 }
367
368
369
370
371
372
373 public boolean hasReference()
374 {
375 return StringUtils.isNotEmpty(reference);
376 }
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 protected void setResponse(HttpServletResponse response)
394 {
395 this.response = response;
396 }
397
398
399
400
401
402
403
404 protected HttpServletResponse getResponse()
405 {
406 return response;
407 }
408
409
410
411
412
413
414
415
416
417
418
419
420
421 protected void getContextAndScript(StringBuilder sb)
422 {
423 String context = getContextPath();
424
425 if(StringUtils.isNotEmpty(context))
426 {
427 if(context.charAt(0) != '/')
428 {
429 sb.append('/');
430 }
431 sb.append (context);
432 }
433
434
435 String script = getScriptName();
436
437 if(StringUtils.isNotEmpty(script))
438 {
439 if(script.charAt(0) != '/')
440 {
441 sb.append('/');
442 }
443 sb.append (script);
444 }
445 }
446
447
448
449
450
451
452
453
454
455
456
457
458
459 protected void getSchemeAndPort(StringBuilder sb)
460 {
461
462 sb.append(getServerScheme());
463 sb.append(URIConstants.URI_SCHEME_SEPARATOR);
464 sb.append(getServerName());
465
466
467 if ((getServerScheme().equals(HTTP)
468 && getServerPort() != HTTP_PORT)
469 || (getServerScheme().equals(HTTPS)
470 && getServerPort() != HTTPS_PORT))
471 {
472 sb.append(':');
473 sb.append(getServerPort());
474 }
475 }
476
477
478
479
480
481
482
483
484
485
486 protected String encodeResponse(String uri)
487 {
488 String res = uri;
489
490 HttpServletResponse response = getResponse();
491
492 if(response == null)
493 {
494 log.debug("No Response Object!");
495 }
496 else
497 {
498 try
499 {
500 if(isRedirect())
501 {
502 log.debug("Should Redirect");
503 res = response.encodeRedirectURL(uri);
504 }
505 else
506 {
507 res = response.encodeURL(uri);
508 }
509 }
510 catch(Exception e)
511 {
512 log.error("response" + response + ", uri: " + uri);
513 log.error("While trying to encode the URI: ", e);
514 }
515 }
516
517 log.debug("encodeResponse(): " + res);
518 return res;
519 }
520 }