de.knightsoftnet.validators.shared.impl.IbanValidator Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package de.knightsoftnet.validators.shared.impl;
import de.knightsoftnet.validators.shared.Iban;
import de.knightsoftnet.validators.shared.data.CountryEnum;
import de.knightsoftnet.validators.shared.data.IbanLengthDefinition;
import de.knightsoftnet.validators.shared.data.IbanLengthMapSharedConstants;
import de.knightsoftnet.validators.shared.util.AbstractIbanUtil;
import de.knightsoftnet.validators.shared.util.HasSetIbanLengthMapSharedConstants;
import de.knightsoftnet.validators.shared.util.IbanUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.checkdigit.IBANCheckDigit;
import java.util.Objects;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
/**
* Check a string if it's a valid IBAN.
*
* @author Manfred Tremmel
*
*/
public class IbanValidator
implements ConstraintValidator, HasSetIbanLengthMapSharedConstants {
/**
* definition of IBAN length minimum.
*/
public static final int IBAN_LENGTH_MIN = 15;
/**
* definition of IBAN length maximum.
*/
public static final int IBAN_LENGTH_MAX = 34;
/**
* apache commons class to check/calculate IBAN check sums.
*/
private static final IBANCheckDigit CHECK_IBAN = new IBANCheckDigit();
/**
* should whitespaces be ignored (true/false).
*/
private boolean ignoreWhitspaces;
/**
* provider for map of swift countries and the length of the ibans.
*/
private IbanLengthMapSharedConstants ibanLengthMapSharedConstants;
/**
* {@inheritDoc} initialize the validator.
*
* @see jakarta.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
*/
@Override
public final void initialize(final Iban constraintAnnotation) {
ignoreWhitspaces = constraintAnnotation.ignoreWhitspaces();
final IbanUtil ibanUtil = new IbanUtil();
ibanUtil.setIbanLengthMapSharedConstantsWhenAvailable(this);
}
@Override
public void setIbanLengthMapSharedConstants(
final IbanLengthMapSharedConstants ibanLengthMapSharedConstants) {
this.ibanLengthMapSharedConstants = ibanLengthMapSharedConstants;
}
/**
* {@inheritDoc} check if given string is a valid IBAN.
*
* @see jakarta.validation.ConstraintValidator#isValid(java.lang.Object,
* jakarta.validation.ConstraintValidatorContext)
*/
@Override
public final boolean isValid(final Object value, final ConstraintValidatorContext context) {
final String valueAsString;
if (ignoreWhitspaces) {
valueAsString = AbstractIbanUtil.ibanCompress(Objects.toString(value, StringUtils.EMPTY));
} else {
valueAsString = Objects.toString(value, null);
}
if (StringUtils.isEmpty(valueAsString)) {
// empty field is ok
return true;
}
if (valueAsString.length() < IBAN_LENGTH_MIN || valueAsString.length() > IBAN_LENGTH_MAX) {
// to short or to long, but it's handled by size validator!
return true;
}
final CountryEnum countryCode = AbstractIbanUtil.getCountryOfIban(valueAsString);
final IbanLengthDefinition validIbanLength = ibanLengthMapSharedConstants == null ? null
: ibanLengthMapSharedConstants.getIbanLengthMap().get(countryCode);
if (validIbanLength == null || valueAsString.length() != validIbanLength.getLength()) {
// unknown country or wrong length for the country!
return false;
}
return CHECK_IBAN.isValid(valueAsString);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy