2 * Copyright (C) 2016 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans;
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;
30 * A general controller
32 * @author Roland Häder<roland@mxchange.org>
34 public abstract class BaseJobsController implements Serializable {
39 private static final long serialVersionUID = 50_837_597_127_567_140L;
42 * Determines principal's name or returns null if no principal (security) is
45 * @return Principal's name or null
47 protected String determinePrincipalName () {
49 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
52 String principalName = null;
54 // Is the principal set?
55 if (userPrincipal instanceof Principal) {
56 // Get principal's name
57 principalName = userPrincipal.getName();
65 * Returns given property key or throws an exception if not found.
67 * @param parameterKey Property key
69 * @return Property value
71 * @throws NullPointerException If given key is not found
72 * @throws NumberFormatException If no number is given in context parameter
74 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
75 // Get context parameter
76 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
83 * Returns given property key or throws an exception if not found.
85 * @param parameterKey Property key
87 * @return Property value
89 * @throws NullPointerException If given key is not found
91 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
92 // Get context parameter
93 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
96 if (null == contextValue) {
98 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
106 * Checks whether debug mode is enabled for given controller
108 * @param controllerName Name of controller
110 * @return Whether debug mode is enabled
112 protected boolean isDebugModeEnabled (final String controllerName) {
113 // Parameters should be valid
114 if (null == controllerName) {
116 throw new NullPointerException("controllerName is null"); //NOI18N
117 } else if (controllerName.isEmpty()) {
119 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
122 // Try to get context parameter
123 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
125 // Is it set and true?
126 boolean isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE);
133 * Checks if given password is to weak to be used
135 * @param password Clear-text password
137 * @return Whether the entered password is to weak
139 protected boolean isWeakPassword (final String password) {
141 System.out.println(this.getClass().getSimpleName() + ":isWeakPassword: password=" + password + " - CALLED!");
144 if (null == password) {
146 throw new NullPointerException("password is null"); //NOI18N
150 double passwordScore = UserUtils.calculatePasswordScore(password);
153 System.out.println(this.getClass().getSimpleName() + ".isWeakPassword: passwordScore=" + passwordScore);
155 // Is the score within range?
156 boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N
159 System.out.println(this.getClass().getSimpleName() + ".isWeakPassword: isWeak=" + isWeak + " - EXIT!");
166 * Shows a faces message for given causing exception. The message from the
167 * exception is being inserted into the message.
169 * @param clientId Client id to send message to
170 * @param cause Causing exception
172 protected void showFacesMessage (final String clientId, final Throwable cause) {
174 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
176 // Get context and add message
177 this.showFacesMessage(clientId, cause.getMessage());
181 * Shows a faces message with given message (i18n) key.
183 * @param clientId Client id to send message to
184 * @param i18nKey Message key
186 * @throws NullPointerException If clientId or i18nKey is null
187 * @throws IllegalArgumentException If clientId or i18nKey is empty
189 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
190 // Both parameter must be valid
191 if (null == clientId) {
193 throw new NullPointerException("clientId is null"); //NOI18N
194 } else if (clientId.isEmpty()) {
196 throw new IllegalArgumentException("clientId is null"); //NOI18N
197 } else if (null == i18nKey) {
199 throw new NullPointerException("i18nKey is null"); //NOI18N
200 } else if (i18nKey.isEmpty()) {
202 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
205 // Get current locale
206 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
209 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
211 // Default is i18nKey
212 String message = i18nKey;
217 message = bundle.getString(i18nKey);
218 } catch (final MissingResourceException ex) {
219 // Did not find it, ignored
222 // Get context and add message
223 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));