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 if (null == password) {
143 throw new NullPointerException("password is null"); //NOI18N
147 double passwordScore = UserUtils.calculatePasswordScore(password);
149 // Is the score within range?
150 boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N
157 * Shows a faces message for given causing exception. The message from the
158 * exception is being inserted into the message.
160 * @param clientId Client id to send message to
161 * @param cause Causing exception
163 protected void showFacesMessage (final String clientId, final Throwable cause) {
164 // Get context and add message
165 this.showFacesMessage(clientId, cause.getMessage());
169 * Shows a faces message with given message (i18n) key.
171 * @param clientId Client id to send message to
172 * @param i18nKey Message key
174 * @throws NullPointerException If clientId or i18nKey is null
175 * @throws IllegalArgumentException If clientId or i18nKey is empty
177 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
178 // Both parameter must be valid
179 if (null == clientId) {
181 throw new NullPointerException("clientId is null"); //NOI18N
182 } else if (clientId.isEmpty()) {
184 throw new IllegalArgumentException("clientId is null"); //NOI18N
185 } else if (null == i18nKey) {
187 throw new NullPointerException("i18nKey is null"); //NOI18N
188 } else if (i18nKey.isEmpty()) {
190 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
193 // Get current locale
194 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
197 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
199 // Default is i18nKey
200 String message = i18nKey;
205 message = bundle.getString(i18nKey);
206 } catch (final MissingResourceException ex) {
207 // Did not find it, ignored
210 // Get context and add message
211 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));