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

org.kiwiproject.validation.Ipv4AndPortValidator Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.validation;

import static java.util.Objects.isNull;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * Validates that a string matches a regular expression representing an IPv4 address and port separated by a colon.
 * 

* Does not perform any range checks on the IP address segments or port. */ public class Ipv4AndPortValidator implements ConstraintValidator { private static final String IPV4_AND_PORT_REGEX = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}):\\d+$"; private static final Pattern IPV4_AND_PORT_PATTERN = Pattern.compile(IPV4_AND_PORT_REGEX); private Ipv4AndPort ipv4AndPort; @Override public void initialize(Ipv4AndPort constraintAnnotation) { this.ipv4AndPort = constraintAnnotation; } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (isNull(value)) { return ipv4AndPort.allowNull(); } return IPV4_AND_PORT_PATTERN.matcher(value).matches(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy