]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/BaseJobsController.java
cellphone is history, now there is mobile ... ;-)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / BaseJobsController.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.jjobs.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 BaseJobsController implements Serializable {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 50_837_597_127_567_140L;
40
41         /**
42          * Determines principal's name or returns null if no principal (security) is
43          * set.
44          * <p>
45          * @return Principal's name or null
46          */
47         protected String determinePrincipalName () {
48                 // Get principal
49                 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
50
51                 // Init with null
52                 String principalName = null;
53
54                 // Is the principal set?
55                 if (userPrincipal instanceof Principal) {
56                         // Get principal's name
57                         principalName = userPrincipal.getName();
58                 }
59
60                 // Return it
61                 return principalName;
62         }
63
64         /**
65          * Returns given property key or throws an exception if not found.
66          * <p>
67          * @param parameterKey Property key
68          * <p>
69          * @return Property value
70          * <p>
71          * @throws NullPointerException If given key is not found
72          * @throws NumberFormatException If no number is given in context parameter
73          */
74         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
75                 // Get context parameter
76                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
77
78                 // Return it
79                 return contextValue;
80         }
81
82         /**
83          * Returns given property key or throws an exception if not found.
84          * <p>
85          * @param parameterKey Property key
86          * <p>
87          * @return Property value
88          * <p>
89          * @throws NullPointerException If given key is not found
90          */
91         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
92                 // Get context parameter
93                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
94
95                 // Is it null?
96                 if (null == contextValue) {
97                         // Throw NPE
98                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
99                 }
100
101                 // Return it
102                 return contextValue;
103         }
104
105         /**
106          * Checks whether debug mode is enabled for given controller
107          * <p>
108          * @param controllerName Name of controller
109          * <p>
110          * @return Whether debug mode is enabled
111          */
112         protected boolean isDebugModeEnabled (final String controllerName) {
113                 // Parameters should be valid
114                 if (null == controllerName) {
115                         // Throw NPE
116                         throw new NullPointerException("controllerName is null"); //NOI18N
117                 } else if (controllerName.isEmpty()) {
118                         // Is empty
119                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
120                 }
121
122                 // Try to get context parameter
123                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
124
125                 // Is it set and true?
126                 boolean isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE);
127
128                 // Return it
129                 return isEnabled;
130         }
131
132         /**
133          * Checks if given password is to weak to be used
134          * <p>
135          * @param password Clear-text password
136          * <p>
137          * @return Whether the entered password is to weak
138          */
139         protected boolean isWeakPassword (final String password) {
140                 // Log message
141                 System.out.println(this.getClass().getSimpleName() + ":isWeakPassword: password=" + password + " - CALLED!");
142
143                 // Is parameter set?
144                 if (null == password) {
145                         // Throw NPE
146                         throw new NullPointerException("password is null"); //NOI18N
147                 }
148
149                 // Get score value
150                 double passwordScore = UserUtils.calculatePasswordScore(password);
151
152                 // Log message
153                 System.out.println(this.getClass().getSimpleName() + ".isWeakPassword: passwordScore=" + passwordScore);
154
155                 // Is the score within range?
156                 boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N
157
158                 // Log message
159                 System.out.println(this.getClass().getSimpleName() + ".isWeakPassword: isWeak=" + isWeak + " - EXIT!");
160
161                 // Return it
162                 return isWeak;
163         }
164
165         /**
166          * Shows a faces message for given causing exception. The message from the
167          * exception is being inserted into the message.
168          * <p>
169          * @param clientId Client id to send message to
170          * @param cause Causing exception
171          */
172         protected void showFacesMessage (final String clientId, final Throwable cause) {
173                 // Trace message
174                 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
175
176                 // Get context and add message
177                 this.showFacesMessage(clientId, cause.getMessage());
178         }
179
180         /**
181          * Shows a faces message with given message (i18n) key.
182          * <p>
183          * @param clientId Client id to send message to
184          * @param i18nKey Message key
185          * <p>
186          * @throws NullPointerException If clientId or i18nKey is null
187          * @throws IllegalArgumentException If clientId or i18nKey is empty
188          */
189         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
190                 // Both parameter must be valid
191                 if (null == clientId) {
192                         // Throw NPE
193                         throw new NullPointerException("clientId is null"); //NOI18N
194                 } else if (clientId.isEmpty()) {
195                         // Is empty
196                         throw new IllegalArgumentException("clientId is null"); //NOI18N
197                 } else if (null == i18nKey) {
198                         // Throw NPE
199                         throw new NullPointerException("i18nKey is null"); //NOI18N
200                 } else if (i18nKey.isEmpty()) {
201                         // Is empty
202                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
203                 }
204
205                 // Get current locale
206                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
207
208                 // Get bundle bundle
209                 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
210
211                 // Default is i18nKey
212                 String message = i18nKey;
213
214                 // Try it
215                 try {
216                         // Get message
217                         message = bundle.getString(i18nKey);
218                 } catch (final MissingResourceException ex) {
219                         // Did not find it, ignored
220                 }
221
222                 // Get context and add message
223                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
224         }
225
226 }