use of javax.faces.application.FacesMessage in project oxTrust by GluuFederation.
the class UpdatePersonAction method validateConfirmPassword.
public void validateConfirmPassword(FacesContext context, UIComponent comp, Object value) {
Pattern pattern = null;
String attributeValue = (String) value;
if (StringHelper.isEmpty(attributeValue)) {
FacesMessage message = new FacesMessage("Value is required");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
AttributeValidation validation = attributeService.getAttributeByName("userPassword").getAttributeValidation();
boolean canValidate = validation != null && validation.getRegexp() != null && !validation.getRegexp().isEmpty();
if (comp.getClientId().endsWith("custpasswordId")) {
this.password = (String) value;
} else if (comp.getClientId().endsWith("custconfirmpasswordId")) {
this.confirmPassword = (String) value;
}
if (canValidate) {
pattern = Pattern.compile(validation.getRegexp());
}
if (!StringHelper.equalsIgnoreCase(password, confirmPassword) && this.confirmPassword != null) {
((UIInput) comp).setValid(false);
FacesMessage message = new FacesMessage("Both passwords should be the same!");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
if (canValidate && (!pattern.matcher(this.password).matches() || !pattern.matcher(this.confirmPassword).matches())) {
((UIInput) comp).setValid(false);
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, facesMessages.evalResourceAsString("#{msgs['password.validation.invalid']}"), facesMessages.evalResourceAsString("#{msgs['password.validation.invalid']}"));
context.addMessage(comp.getClientId(context), message);
}
}
use of javax.faces.application.FacesMessage in project oxTrust by GluuFederation.
the class OxTrustAttributeValidator method validate.
@Override
public void validate(FacesContext context, UIComponent comp, Object value) {
GluuAttribute attribute = (GluuAttribute) comp.getAttributes().get("attribute");
if (attribute == null) {
((UIInput) comp).setValid(true);
return;
}
AttributeValidation attributeValidation = attribute.getAttributeValidation();
Integer minvalue = attributeValidation != null ? attributeValidation.getMinLength() : null;
Integer maxValue = attributeValidation != null ? attributeValidation.getMaxLength() : null;
String regexpValue = attributeValidation != null ? attributeValidation.getRegexp() : null;
String attributeValue = (String) value;
// Minimum length validation
if (minvalue != null) {
int min = attributeValidation.getMinLength();
if ((attributeValue != null) && (attributeValue.length() < min)) {
((UIInput) comp).setValid(false);
FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should be at least " + min + " symbols. ");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(comp.getClientId(context), message);
}
}
// default maxlength
int max = 400;
if (maxValue != null) {
max = attributeValidation.getMaxLength();
}
// Maximum Length validation
if ((attributeValue != null) && (attributeValue.length() > max)) {
((UIInput) comp).setValid(false);
FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should not exceed " + max + " symbols. ");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(comp.getClientId(context), message);
}
if ((attribute.getName().equalsIgnoreCase("mail") && ((regexpValue == null) || (StringHelper.isEmpty(regexpValue))))) {
regexpValue = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" + "[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
}
if ((regexpValue != null) && StringHelper.isNotEmpty(regexpValue)) {
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regexpValue);
if ((attributeValue != null) && !(attributeValue.trim().equals(""))) {
java.util.regex.Matcher matcher = pattern.matcher(attributeValue);
boolean flag = matcher.matches();
if (!flag) {
((UIInput) comp).setValid(false);
FacesMessage message = new FacesMessage(attribute.getDisplayName() + " Format is invalid. ");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(comp.getClientId(context), message);
}
}
}
}
use of javax.faces.application.FacesMessage in project oxTrust by GluuFederation.
the class GluuAttributeValidator method validate.
@Override
public void validate(FacesContext context, UIComponent comp, Object value) {
String attributeValue;
if (value instanceof AttributeDataType) {
attributeValue = ((AttributeDataType) value).getValue();
} else {
attributeValue = (String) value;
}
if (StringHelper.isEmpty(attributeValue)) {
FacesMessage message = new FacesMessage("Value is required");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
use of javax.faces.application.FacesMessage in project quickstart by wildfly.
the class BatchController method generate.
public void generate() throws IOException {
File tempFile = new File(System.getProperty("java.io.tmpdir"), fileName);
try (BufferedWriter bos = new BufferedWriter(new FileWriter(tempFile, false))) {
log.info("Starting to generate " + numRecords + " records in file " + tempFile);
String previousName = null;
final Random random = new Random();
for (int x = 0; x < numRecords; x++) {
// generate random name
String name = random.ints('a', 'z' + 1).limit(10).collect(StringBuilder::new, (sb, i) -> sb.append((char) i), StringBuilder::append).toString();
// generate random phone number
String phone = random.ints('0', '9' + 1).limit(9).collect(StringBuilder::new, (sb, i) -> sb.append((char) i), StringBuilder::append).toString();
// Generate a duplicate name;
if (generateWithError && x == (numRecords / 2)) {
name = previousName;
}
String record = (x + 1) + "|" + name + "|" + phone;
bos.write(record + "\n");
previousName = name;
}
log.info("File generated at " + tempFile);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "File generated with " + numRecords + " records to be imported. File name: " + getFileName(), null));
if (generateWithError) {
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Attention: This file contains duplicate records for test purpose.", null));
}
}
}
use of javax.faces.application.FacesMessage in project quickstart by wildfly.
the class AuthController method createUser.
private User createUser(String username) {
try {
User user = new User(username);
userDao.createUser(user);
facesContext.addMessage(null, new FacesMessage("User successfully created"));
return user;
} catch (Exception e) {
facesContext.addMessage(null, new FacesMessage("Failed to create user '" + username + "'", e.getMessage()));
return null;
}
}
Aggregations