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 package fr.paris.lutece.portal.service.util;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.text.MessageFormat;
40 import java.util.Enumeration;
41 import java.util.StringTokenizer;
42
43 import javax.servlet.ServletContext;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpSession;
46
47 import org.apache.commons.lang3.StringUtils;
48
49 import fr.paris.lutece.portal.service.datastore.DatastoreService;
50 import fr.paris.lutece.portal.service.message.SiteMessageService;
51 import fr.paris.lutece.portal.service.security.SecurityTokenService;
52 import fr.paris.lutece.portal.web.LocalVariables;
53 import fr.paris.lutece.portal.web.constants.Parameters;
54 import fr.paris.lutece.util.ReferenceList;
55 import fr.paris.lutece.util.stream.StreamUtil;
56 import fr.paris.lutece.util.string.StringUtil;
57 import fr.paris.lutece.util.url.UrlItem;
58
59
60
61
62 public final class AppPathService
63 {
64 public static final String SESSION_BASE_URL = "base_url";
65 private static final String MSG_LOG_PROPERTY_NOT_FOUND = "Property {0} not found in the properties file ";
66 private static final int PORT_NUMBER_HTTP = 80;
67 private static final String PROPERTY_BASE_URL = "lutece.base.url";
68 private static final String PROPERTY_PORTAL_URL = "lutece.portal.path";
69 private static final String PROPERTY_SITE_MESSAGE_URL = "lutece.siteMessage.path";
70 private static final String PROPERTY_ADMIN_URL = "lutece.admin.path";
71 private static final String PROPERTY_ADMIN_MENU_URL = "lutece.admin.menu.url";
72 private static final String PROPERTY_PORTAL_REDIRECT_URL = "lutece.portal.redirect.url";
73 private static final String PROPERTY_VIRTUAL_HOST_KEYS = "virtualHostKeys";
74 private static final String PROPERTY_VIRTUAL_HOST_KEY_PARAMETER = "virtualHostKey.parameterName";
75 private static final String PROPERTY_VIRTUAL_HOST = "virtualHost.";
76 private static final String PROPERTY_PREFIX_URL = "url.";
77 private static final String PROPERTY_PROD_BASE_URL = "lutece.prod.url";
78 private static final String PROPERTY_INSTANCE = "lutece.webapp.instance";
79 private static final String INSTANCE_DEFAULT = "default";
80 private static final String SUFFIX_BASE_URL = ".baseUrl";
81 private static final String SUFFIX_DESCRIPTION = ".description";
82 private static final String SLASH = "/";
83 private static final String DOUBLE_POINTS = ":";
84
85
86 private static final String KEY_ADMIN_HOME_URL = "portal.site.site_property.admin_home_url";
87 private static final String KEY_PORTAL_HOME_URL = "portal.site.site_property.home_url";
88 private static String _strWebAppPath;
89
90
91
92
93 private AppPathService( )
94 {
95 }
96
97
98
99
100
101
102
103 public static void init( ServletContext context )
104 {
105 String strRealPath = context.getRealPath( "/" );
106 _strWebAppPath = normalizeWebappPath( strRealPath );
107 }
108
109
110
111
112
113
114
115 public static void init( String strWebAppPath )
116 {
117 _strWebAppPath = normalizeWebappPath( strWebAppPath );
118 }
119
120
121
122
123
124
125
126
127
128 public static String getPath( String strKey )
129 {
130
131 String strDirectory = AppPropertiesService.getProperty( strKey );
132
133 if ( strDirectory == null )
134 {
135 Object [ ] propertyMissing = {
136 strKey
137 };
138 String strMsg = MessageFormat.format( MSG_LOG_PROPERTY_NOT_FOUND, propertyMissing );
139 throw new AppException( strMsg );
140 }
141
142 return getWebAppPath( ) + strDirectory;
143 }
144
145
146
147
148
149
150
151 public static String getWebAppPath( )
152 {
153 return _strWebAppPath;
154 }
155
156
157
158
159
160
161
162
163
164
165 public static String getPath( String strKey, String strFilename )
166 {
167 return getPath( strKey ) + SLASH + strFilename;
168 }
169
170
171
172
173
174
175
176
177
178
179 public static FileInputStream getResourceAsStream( String strPath, String strFilename )
180 {
181 String strFilePath = getWebAppPath( ) + strPath + strFilename;
182
183 FileInputStream fis = null;
184 try
185 {
186 File file = new File( strFilePath );
187 fis = new FileInputStream( file );
188
189 return fis;
190 }
191 catch( IOException e )
192 {
193 StreamUtil.safeClose( fis );
194 throw new AppException( "Unable to get file : " + strFilePath );
195 }
196 }
197
198
199
200
201
202
203
204
205
206 public static String getAbsolutePathFromRelativePath( String strDirectory )
207 {
208 return _strWebAppPath + strDirectory;
209 }
210
211
212
213
214
215
216
217
218 public static String getBaseUrl( HttpServletRequest request )
219 {
220 if ( request == null )
221 {
222 return getBaseUrl( );
223 }
224
225 String strBase;
226
227
228 strBase = getVirtualHostBaseUrl( request );
229
230
231 if ( ( strBase == null ) || strBase.equals( StringUtils.EMPTY ) )
232 {
233 HttpSession session = request.getSession( false );
234
235 if ( session != null )
236 {
237 Object oBase = session.getAttribute( SESSION_BASE_URL );
238
239 if ( oBase != null )
240 {
241 strBase = (String) oBase;
242 }
243 }
244 }
245
246
247 if ( ( strBase == null ) || ( strBase.equals( StringUtils.EMPTY ) ) )
248 {
249 strBase = AppPropertiesService.getProperty( PROPERTY_BASE_URL );
250 }
251
252 if ( ( strBase == null ) || ( strBase.equals( StringUtils.EMPTY ) ) )
253 {
254
255 strBase = request.getScheme( ) + DOUBLE_POINTS + SLASH + SLASH + request.getServerName( );
256
257 int nPort = request.getServerPort( );
258
259 if ( nPort != PORT_NUMBER_HTTP )
260 {
261 strBase += ( DOUBLE_POINTS + nPort );
262 }
263
264 strBase += request.getContextPath( );
265 }
266
267 if ( !strBase.endsWith( SLASH ) )
268 {
269 strBase += SLASH;
270 }
271
272 return strBase;
273 }
274
275
276
277
278
279
280
281
282 @Deprecated
283 public static String getBaseUrl( )
284 {
285 HttpServletRequest request = LocalVariables.getRequest( );
286
287 if ( request != null )
288 {
289 return getBaseUrl( request );
290 }
291
292
293 String strBaseUrl = AppPropertiesService.getProperty( PROPERTY_BASE_URL );
294
295 if ( strBaseUrl == null )
296 {
297 strBaseUrl = StringUtils.EMPTY;
298 }
299 else
300 {
301 if ( !strBaseUrl.endsWith( SLASH ) )
302 {
303 strBaseUrl += SLASH;
304 }
305 }
306
307 return strBaseUrl;
308 }
309
310
311
312
313
314
315
316
317 public static String getProdUrl( HttpServletRequest request )
318 {
319 String strBaseUrl = AppPropertiesService.getProperty( PROPERTY_PROD_BASE_URL );
320
321 if ( StringUtils.isBlank( strBaseUrl ) )
322 {
323 strBaseUrl = getBaseUrl( request );
324 }
325
326 if ( !strBaseUrl.endsWith( SLASH ) )
327 {
328 strBaseUrl += SLASH;
329 }
330
331 return strBaseUrl;
332 }
333
334
335
336
337
338
339
340
341 public static String getProdUrl( String strBaseUrl )
342 {
343 String strProdUrl = AppPropertiesService.getProperty( PROPERTY_PROD_BASE_URL );
344
345 if ( StringUtils.isBlank( strProdUrl ) )
346 {
347 strProdUrl = strBaseUrl;
348 }
349
350 if ( ( strProdUrl != null ) && !strProdUrl.endsWith( SLASH ) )
351 {
352 strProdUrl += SLASH;
353 }
354
355 return strProdUrl;
356 }
357
358
359
360
361
362
363
364
365 public static String getSiteMessageUrl( HttpServletRequest request )
366 {
367
368 return SiteMessageService.setSiteMessageUrl( getBaseUrl( request ) + getSiteMessageUrl( ) );
369 }
370
371
372
373
374
375
376 public static String getPortalUrl( )
377 {
378 return AppPropertiesService.getProperty( PROPERTY_PORTAL_URL );
379 }
380
381
382
383
384
385
386 public static String getRootForwardUrl( )
387 {
388 return DatastoreService.getDataValue( KEY_PORTAL_HOME_URL, AppPropertiesService.getProperty( PROPERTY_PORTAL_REDIRECT_URL ) );
389 }
390
391
392
393
394
395
396 public static String getSiteMessageUrl( )
397 {
398 return AppPropertiesService.getProperty( PROPERTY_SITE_MESSAGE_URL );
399 }
400
401
402
403
404
405
406 public static String getAdminPortalUrl( )
407 {
408 return AppPropertiesService.getProperty( PROPERTY_ADMIN_URL );
409 }
410
411
412
413
414
415
416 public static String getAdminMenuUrl( )
417 {
418 return DatastoreService.getDataValue( KEY_ADMIN_HOME_URL, AppPropertiesService.getProperty( PROPERTY_ADMIN_MENU_URL ) );
419 }
420
421
422
423
424
425
426
427
428 private static String normalizeWebappPath( String strPath )
429 {
430 String strNormalized = strPath;
431
432
433 if ( ( strNormalized.length( ) > 3 ) && ( strNormalized.indexOf( ':' ) == 2 ) )
434 {
435 strNormalized = strNormalized.substring( 1 );
436 }
437
438
439 strNormalized = StringUtil.substitute( strNormalized, "/", "\\" );
440
441
442 if ( strNormalized.endsWith( "/" ) )
443 {
444 strNormalized = strNormalized.substring( 0, strNormalized.length( ) - 1 );
445 }
446
447 return strNormalized;
448 }
449
450
451
452
453
454
455
456
457
458 public static ReferenceList getAvailableVirtualHosts( )
459 {
460 ReferenceList list = null;
461
462
463 String strKeysList = AppPropertiesService.getProperty( PROPERTY_VIRTUAL_HOST_KEYS );
464
465 if ( strKeysList != null )
466 {
467 list = new ReferenceList( );
468
469
470 StringTokenizer strTokens = new StringTokenizer( strKeysList, "," );
471
472 while ( strTokens.hasMoreTokens( ) )
473 {
474 String strHostKey = strTokens.nextToken( );
475 String strHostKeyDescription = AppPropertiesService.getProperty( PROPERTY_VIRTUAL_HOST + strHostKey + SUFFIX_DESCRIPTION );
476 list.addItem( strHostKey, strHostKeyDescription );
477 }
478 }
479
480 return list;
481 }
482
483
484
485
486
487
488
489
490 public static String getVirtualHostKey( HttpServletRequest request )
491 {
492 String strVirtalHostKey = null;
493
494
495 String strVirtualHostKeyParameter = AppPropertiesService.getProperty( PROPERTY_VIRTUAL_HOST_KEY_PARAMETER );
496
497 if ( ( request != null ) && ( strVirtualHostKeyParameter != null ) && ( !strVirtualHostKeyParameter.equals( "" ) ) )
498 {
499
500 strVirtalHostKey = request.getParameter( strVirtualHostKeyParameter );
501 }
502
503 return strVirtalHostKey;
504 }
505
506
507
508
509
510
511
512
513 private static String getVirtualHostBaseUrl( HttpServletRequest request )
514 {
515 String strBaseUrl = null;
516 String strVirtalHostKey = getVirtualHostKey( request );
517
518 if ( ( strVirtalHostKey != null ) && ( !strVirtalHostKey.equals( "" ) ) )
519 {
520
521 strBaseUrl = AppPropertiesService.getProperty( PROPERTY_VIRTUAL_HOST + strVirtalHostKey + SUFFIX_BASE_URL );
522 }
523
524 return strBaseUrl;
525 }
526
527
528
529
530
531
532
533
534
535
536 public static UrlItem buildRedirectUrlItem( String strRootUrl, String strUrlPropertySuffixKey )
537 {
538 String strUrl = AppPropertiesService.getProperty( PROPERTY_PREFIX_URL + strUrlPropertySuffixKey );
539 UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( strRootUrl + strUrl );
540 url.addParameter( Parameters.REDIRECT_URL, strUrlPropertySuffixKey );
541
542 return url;
543 }
544
545
546
547
548
549
550
551
552
553
554
555 public static UrlItem resolveRedirectUrl( HttpServletRequest request, String strDefaultRedirectUrl )
556 {
557 String strUrl = strDefaultRedirectUrl;
558
559 String strUrlKey = request.getParameter( Parameters.REDIRECT_URL );
560 String strRedirectUrl = null;
561
562 if ( strUrlKey != null )
563 {
564 strRedirectUrl = AppPropertiesService.getProperty( PROPERTY_PREFIX_URL + strUrlKey );
565 }
566
567 if ( strRedirectUrl != null )
568 {
569 strUrl = strRedirectUrl;
570 }
571
572 Enumeration enumParams = request.getParameterNames( );
573 UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getBaseUrl( request ) + strUrl );
574
575 String strParamName;
576
577 while ( enumParams.hasMoreElements( ) )
578 {
579 strParamName = (String) enumParams.nextElement( );
580
581 if ( !strParamName.equals( Parameters.REDIRECT_URL ) && !strParamName.equals( Parameters.ACCESS_CODE )
582 && !strParamName.equals( Parameters.PASSWORD ) && !strParamName.equals( SecurityTokenService.PARAMETER_TOKEN ) )
583 {
584 url.addParameter( strParamName, request.getParameter( strParamName ) );
585 }
586 }
587
588 return url;
589 }
590
591
592
593
594
595
596
597
598
599
600
601
602
603 public static String getAbsoluteUrl( HttpServletRequest request, String strUrl )
604 {
605 if ( ( strUrl != null ) && !strUrl.startsWith( "http://" ) && !strUrl.startsWith( "https://" ) )
606 {
607 return AppPathService.getBaseUrl( request ) + strUrl;
608 }
609
610 return strUrl;
611 }
612
613
614
615
616
617
618
619 public static String getWebappInstance( )
620 {
621 String strInstance = AppPropertiesService.getProperty( PROPERTY_INSTANCE );
622
623 if ( ( strInstance != null ) && ( !strInstance.equals( StringUtils.EMPTY ) ) )
624 {
625 return strInstance;
626 }
627
628 return INSTANCE_DEFAULT;
629 }
630
631
632
633
634
635
636 public static boolean isDefaultWebappInstance( )
637 {
638 return INSTANCE_DEFAULT.equals( getWebappInstance( ) );
639 }
640 }