]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/utils/FacesUtils.java
added FacesUtils which will contain methods for dealing with JSF, such as generating...
[jcore-utils.git] / src / org / mxchange / jcoreee / utils / FacesUtils.java
1 /*
2  * Copyright (C) 2016 Cho-Time GmbH
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcoreee.utils;
18
19 import javax.faces.context.ExternalContext;
20 import javax.faces.context.FacesContext;
21
22 /**
23  * An utilities class for JSF
24  * <p>
25  * @author Roland Haeder<rhaeder@cho-time.de>
26  */
27 public class FacesUtils {
28
29         /**
30          * Generates a "base URL" for for example mail templates. For JSF
31          * pages/templates this is not needed.
32          * <p>
33          * @return Base URL
34          */
35         public static String generateBaseUrl () {
36                 // Get external context
37                 ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
38
39                 // Get request scheme and such ...
40                 String scheme = context.getRequestScheme();
41                 String hostName = context.getRequestServerName();
42                 String path = context.getRequestServletPath();
43
44                 // Is the path null?
45                 if (null == scheme) {
46                         // Throw NPE
47                         throw new NullPointerException("context.requestScheme is null"); //NOI18N
48                 } else if (null == hostName) {
49                         // And throw again ...
50                         throw new NullPointerException("context.requestServerName is null"); //NOI18N
51                 } else if (null == path) {
52                         // Set to empty string
53                         path = ""; //NOI18N
54                 }
55
56                 // Construct full URL
57                 String baseUrl = String.format("%s://%s%s", scheme, hostName, path); //NOI18N
58
59                 // Return it
60                 return baseUrl;
61         }
62
63         /**
64          * No instances from this class are required
65          */
66         private FacesUtils () {
67                 // Is this null?
68                 if (FacesContext.getCurrentInstance() == null) {
69                         // Okay, don't allow any usage
70                         throw new NullPointerException("Cannot access FacesContext, maybe you have tried to use this class from an EJB?"); //NOI18N
71                 }
72         }
73
74 }