]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/BasePizzaController.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / BasePizzaController.java
1 /*
2  * Copyright (C) 2016 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 Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.pizzaapplication.beans;
18
19 import java.io.Serializable;
20 import java.security.Principal;
21 import java.text.MessageFormat;
22 import java.util.Locale;
23 import java.util.MissingResourceException;
24 import java.util.ResourceBundle;
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
27 import org.mxchange.jusercore.model.user.UserUtils;
28
29 /**
30  * A general controller
31  * <p>
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 public abstract class BasePizzaController implements Serializable {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 50_837_597_127_567_140L;
40
41         /**
42          * Protected constructor
43          */
44         protected BasePizzaController () {
45         }
46
47         /**
48          * Determines principal's name or returns null if no principal (security) is
49          * set.
50          * <p>
51          * @return Principal's name or null
52          */
53         protected String determinePrincipalName () {
54                 // Get principal
55                 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
56
57                 // Init with null
58                 String principalName = null;
59
60                 // Is the principal set?
61                 if (userPrincipal instanceof Principal) {
62                         // Get principal's name
63                         principalName = userPrincipal.getName();
64                 }
65
66                 // Return it
67                 return principalName;
68         }
69
70         /**
71          * Returns given property key or throws an exception if not found.
72          * <p>
73          * @param parameterKey Property key
74          * <p>
75          * @return Property value
76          * <p>
77          * @throws NullPointerException If given key is not found
78          * @throws NumberFormatException If no number is given in context parameter
79          */
80         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
81                 // Get context parameter
82                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
83
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
101                 // Is it null?
102                 if (null == contextValue) {
103                         // Throw NPE
104                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
105                 }
106
107                 // Return it
108                 return contextValue;
109         }
110
111         /**
112          * Checks whether debug mode is enabled for given controller
113          * <p>
114          * @param controllerName Name of controller
115          * <p>
116          * @return Whether debug mode is enabled
117          */
118         protected boolean isDebugModeEnabled (final String controllerName) {
119                 // Parameters should be valid
120                 if (null == controllerName) {
121                         // Throw NPE
122                         throw new NullPointerException("controllerName is null"); //NOI18N
123                 } else if (controllerName.isEmpty()) {
124                         // Is empty
125                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
126                 }
127
128                 // Try to get context parameter
129                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
130
131                 // Is it set and true?
132                 boolean isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE);
133
134                 // Return it
135                 return isEnabled;
136         }
137
138         /**
139          * Checks if given password is to weak to be used
140          * <p>
141          * @param password Clear-text password
142          * <p>
143          * @return Whether the entered password is to weak
144          */
145         protected boolean isWeakPassword (final String password) {
146                 // Is parameter set?
147                 if (null == password) {
148                         // Throw NPE
149                         throw new NullPointerException("password is null"); //NOI18N
150                 }
151
152                 // Get score value
153                 double passwordScore = UserUtils.calculatePasswordScore(password);
154
155                 // Is the score within range?
156                 boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N
157
158                 // Return it
159                 return isWeak;
160         }
161
162         /**
163          * Shows a faces message for given causing exception. The message from the
164          * exception is being inserted into the message.
165          * <p>
166          * @param clientId Client id to send message to
167          * @param cause    Causing exception
168          */
169         protected void showFacesMessage (final String clientId, final Throwable cause) {
170                 // Get context and add message
171                 this.showFacesMessage(clientId, cause.getMessage());
172         }
173
174         /**
175          * Shows a faces message with given message (i18n) key.
176          * <p>
177          * @param clientId Client id to send message to
178          * @param i18nKey  Message key
179          * <p>
180          * @throws NullPointerException If clientId or i18nKey is null
181          * @throws IllegalArgumentException If clientId or i18nKey is empty
182          */
183         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
184                 // Both parameter must be valid
185                 if (null == clientId) {
186                         // Throw NPE
187                         throw new NullPointerException("clientId is null"); //NOI18N
188                 } else if (clientId.isEmpty()) {
189                         // Is empty
190                         throw new IllegalArgumentException("clientId is null"); //NOI18N
191                 } else if (null == i18nKey) {
192                         // Throw NPE
193                         throw new NullPointerException("i18nKey is null"); //NOI18N
194                 } else if (i18nKey.isEmpty()) {
195                         // Is empty
196                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
197                 }
198
199                 // Get current locale
200                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
201
202                 // Get bundle bundle
203                 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
204
205                 // Default is i18nKey
206                 String message = i18nKey;
207
208                 // Try it
209                 try {
210                         // Get message
211                         message = bundle.getString(i18nKey);
212                 } catch (final MissingResourceException ex) {
213                         // Did not find it, ignored
214                 }
215
216                 // Get context and add message
217                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
218         }
219
220 }