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 * Protected constructor
44 protected BaseJobsController () {
48 * Determines principal's name or returns null if no principal (security) is
51 * @return Principal's name or null
53 protected String determinePrincipalName () {
55 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
58 String principalName = null;
60 // Is the principal set?
61 if (userPrincipal instanceof Principal) {
62 // Get principal's name
63 principalName = userPrincipal.getName();
71 * Returns given property key or throws an exception if not found.
73 * @param parameterKey Property key
75 * @return Property value
77 * @throws NullPointerException If given key is not found
78 * @throws NumberFormatException If no number is given in context parameter
80 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
81 // Get context parameter
82 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
89 * Returns given property key or throws an exception if not found.
91 * @param parameterKey Property key
93 * @return Property value
95 * @throws NullPointerException If given key is not found
97 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
98 // Get context parameter
99 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
102 if (null == contextValue) {
104 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
112 * Checks whether debug mode is enabled for given controller
114 * @param controllerName Name of controller
116 * @return Whether debug mode is enabled
118 protected boolean isDebugModeEnabled (final String controllerName) {
119 // Parameters should be valid
120 if (null == controllerName) {
122 throw new NullPointerException("controllerName is null"); //NOI18N
123 } else if (controllerName.isEmpty()) {
125 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
128 // Try to get context parameter
129 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
131 // Is it set and true?
132 boolean isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE);
139 * Checks if given password is to weak to be used
141 * @param password Clear-text password
143 * @return Whether the entered password is to weak
145 protected boolean isWeakPassword (final String password) {
147 if (null == password) {
149 throw new NullPointerException("password is null"); //NOI18N
153 double passwordScore = UserUtils.calculatePasswordScore(password);
155 // Is the score within range?
156 boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N
163 * Shows a faces message for given causing exception. The message from the
164 * exception is being inserted into the message.
166 * @param clientId Client id to send message to
167 * @param cause Causing exception
169 protected void showFacesMessage (final String clientId, final Throwable cause) {
170 // Get context and add message
171 this.showFacesMessage(clientId, cause.getMessage());
175 * Shows a faces message with given message (i18n) key.
177 * @param clientId Client id to send message to
178 * @param i18nKey Message key
180 * @throws NullPointerException If clientId or i18nKey is null
181 * @throws IllegalArgumentException If clientId or i18nKey is empty
183 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
184 // Both parameter must be valid
185 if (null == clientId) {
187 throw new NullPointerException("clientId is null"); //NOI18N
188 } else if (clientId.isEmpty()) {
190 throw new IllegalArgumentException("clientId is null"); //NOI18N
191 } else if (null == i18nKey) {
193 throw new NullPointerException("i18nKey is null"); //NOI18N
194 } else if (i18nKey.isEmpty()) {
196 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
199 // Get current locale
200 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
203 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
205 // Default is i18nKey
206 String message = i18nKey;
211 message = bundle.getString(i18nKey);
212 } catch (final MissingResourceException ex) {
213 // Did not find it, ignored
216 // Get context and add message
217 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));