]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java
The security principal is a faces-only (web container) feature and not
[jcoreee.git] / src / org / mxchange / jcoreee / bean / faces / BaseFacesBean.java
1 /*
2  * Copyright (C) 2017 Roland Häder
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.bean.faces;
18
19 import java.security.Principal;
20 import java.text.MessageFormat;
21 import java.util.Locale;
22 import java.util.MissingResourceException;
23 import java.util.ResourceBundle;
24 import javax.faces.application.FacesMessage;
25 import javax.faces.context.FacesContext;
26 import org.mxchange.jcoreee.bean.BaseBean;
27
28 /**
29  * An abstract bean for faces (web) projects.
30  * <p>
31  * @author Roland Häder<roland@mxchange.org>
32  */
33 public abstract class BaseFacesBean extends BaseBean {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 18_605_498_672_261L;
39
40         /**
41          * Protected constructor
42          */
43         protected BaseFacesBean () {
44                 // Call super constructor
45                 super();
46         }
47
48         /**
49          * Determines principal's name or returns null if no principal (security) is
50          * set.
51          * <p>
52          * @return Principal's name or null
53          */
54         protected String determinePrincipalName () {
55                 // Get principal
56                 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
57
58                 // Init with null
59                 String principalName = null;
60
61                 // Is the principal set?
62                 if (userPrincipal instanceof Principal) {
63                         // Get principal's name
64                         principalName = userPrincipal.getName();
65                 }
66
67                 // Return it
68                 return principalName;
69         }
70
71         /**
72          * Returns given property key or throws an exception if not found.
73          * <p>
74          * @param parameterKey Property key
75          * <p>
76          * @return Property value
77          * <p>
78          * @throws NullPointerException If given key is not found
79          * @throws NumberFormatException If no number is given in context parameter
80          */
81         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
82                 // Get context parameter
83                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
84                 // Return it
85                 return contextValue;
86         }
87
88         /**
89          * Returns given property key or throws an exception if not found.
90          * <p>
91          * @param parameterKey Property key
92          * <p>
93          * @return Property value
94          * <p>
95          * @throws NullPointerException If given key is not found
96          */
97         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
98                 // Get context parameter
99                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
100                 // Is it null?
101                 if (null == contextValue) {
102                         // Throw NPE
103                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
104                 }
105                 // Return it
106                 return contextValue;
107         }
108
109         /**
110          * Checks whether debug mode is enabled for given controller
111          * <p>
112          * @param controllerName Name of controller
113          * <p>
114          * @return Whether debug mode is enabled
115          */
116         protected boolean isDebugModeEnabled (final String controllerName) {
117                 // Parameters should be valid
118                 if (null == controllerName) {
119                         // Throw NPE
120                         throw new NullPointerException("controllerName is null"); //NOI18N
121                 } else if (controllerName.isEmpty()) {
122                         // Is empty
123                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
124                 }
125                 // Try to get context parameter
126                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
127                 // Is it set and true?
128                 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
129                 // Return it
130                 return isEnabled;
131         }
132
133         /**
134          * Loads resource bundle for given locale. This must be implemented per
135          * project so all projects can still customize their methods. Calling
136          * ResourceBundleloadBundle() in this class means that also the bundle files
137          * must be present here.
138          * <p>
139          * @param locale Locale from e.g. FacesContext
140          * <p>
141          * @return Initialized and loaded resource bundle
142          */
143         protected abstract ResourceBundle loadResourceBundle (final Locale locale);
144
145         /**
146          * Shows a faces message for given causing exception. The message from the
147          * exception is being inserted into the message.
148          * <p>
149          * @param clientId Client id to send message to
150          * @param cause    Causing exception
151          */
152         protected void showFacesMessage (final String clientId, final Throwable cause) {
153                 // Get context and add message
154                 this.showFacesMessage(clientId, cause.getMessage());
155         }
156
157         /**
158          * Shows a faces message with given message (i18n) key.
159          * <p>
160          * @param clientId Client id to send message to
161          * @param i18nKey  Message key
162          * <p>
163          * @throws NullPointerException If clientId or i18nKey is null
164          * @throws IllegalArgumentException If clientId or i18nKey is empty
165          */
166         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
167                 // Both parameter must be valid
168                 if (null == clientId) {
169                         // Throw NPE
170                         throw new NullPointerException("clientId is null"); //NOI18N
171                 } else if (clientId.isEmpty()) {
172                         // Is empty
173                         throw new IllegalArgumentException("clientId is null"); //NOI18N
174                 } else if (null == i18nKey) {
175                         // Throw NPE
176                         throw new NullPointerException("i18nKey is null"); //NOI18N
177                 } else if (i18nKey.isEmpty()) {
178                         // Is empty
179                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
180                 }
181                 // Get current locale
182                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
183                 // Get bundle bundle
184                 ResourceBundle bundle = this.loadResourceBundle(locale);
185                 // Default is i18nKey
186                 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
187                 // Try it
188                 try {
189                         // Get message
190                         message = bundle.getString(i18nKey);
191                 } catch (final MissingResourceException ex) {
192                         // Did not find it, ignored
193                 }
194                 // Get context and add message
195                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
196         }
197
198 }