]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/BaseJobsController.java
Please cherry-pick:
[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                 // Is parameter set?
141                 if (null == password) {
142                         // Throw NPE
143                         throw new NullPointerException("password is null"); //NOI18N
144                 }
145
146                 // Get score value
147                 double passwordScore = UserUtils.calculatePasswordScore(password);
148
149                 // Is the score within range?
150                 boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N
151
152                 // Return it
153                 return isWeak;
154         }
155
156         /**
157          * Shows a faces message for given causing exception. The message from the
158          * exception is being inserted into the message.
159          * <p>
160          * @param clientId Client id to send message to
161          * @param cause Causing exception
162          */
163         protected void showFacesMessage (final String clientId, final Throwable cause) {
164                 // Get context and add message
165                 this.showFacesMessage(clientId, cause.getMessage());
166         }
167
168         /**
169          * Shows a faces message with given message (i18n) key.
170          * <p>
171          * @param clientId Client id to send message to
172          * @param i18nKey Message key
173          * <p>
174          * @throws NullPointerException If clientId or i18nKey is null
175          * @throws IllegalArgumentException If clientId or i18nKey is empty
176          */
177         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
178                 // Both parameter must be valid
179                 if (null == clientId) {
180                         // Throw NPE
181                         throw new NullPointerException("clientId is null"); //NOI18N
182                 } else if (clientId.isEmpty()) {
183                         // Is empty
184                         throw new IllegalArgumentException("clientId is null"); //NOI18N
185                 } else if (null == i18nKey) {
186                         // Throw NPE
187                         throw new NullPointerException("i18nKey is null"); //NOI18N
188                 } else if (i18nKey.isEmpty()) {
189                         // Is empty
190                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
191                 }
192
193                 // Get current locale
194                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
195
196                 // Get bundle bundle
197                 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
198
199                 // Default is i18nKey
200                 String message = i18nKey;
201
202                 // Try it
203                 try {
204                         // Get message
205                         message = bundle.getString(i18nKey);
206                 } catch (final MissingResourceException ex) {
207                         // Did not find it, ignored
208                 }
209
210                 // Get context and add message
211                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
212         }
213
214 }