All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.data.validation.IPv4AddressCheck Maven / Gradle / Ivy

There is a newer version: 2.6.2
Show newest version
package play.data.validation;

import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;
import net.sf.oval.exception.OValException;
import org.apache.commons.lang3.StringUtils;

import static java.lang.Integer.parseInt;

public class IPv4AddressCheck extends AbstractAnnotationCheck {

    static final String mes = "validation.ipv4";

    @Override
    public void configure(IPv4Address ipv4Address) {
        setMessage(ipv4Address.message());
    }

    @Override
    public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) throws OValException {
        if (value == null || value.toString().length() == 0) {
            return true;
        }
        try {
            String[] parts = value.toString().split("[.]");
            // Check that there is no trailing separator
            if (parts.length != 4 || StringUtils.countMatches(value.toString(), ".") != 3) {
                return false;
            }

            for (int i = 0; i < parts.length; i++) {
                // Check that we don't have empty part or (+-) sign
                if (parts[i].isEmpty() || !parts[i].matches("[0-9]{1,3}")) {
                    return false;
                }
                int p = parseInt(parts[i]);
                if (p < 0 || p > 255) {
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy