refs #200: Create accouting-lib library
https://support.d4science.org/issues/200 Creating more validators git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115164 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
84181f9655
commit
8f4642f0ae
|
@ -1,18 +1,51 @@
|
|||
package org.gcube.accounting.datamodel.validators;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.gcube.accounting.datamodel.UsageRecord;
|
||||
import org.gcube.common.validator.annotations.FieldValidator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ValidIPValidator implements FieldValidator<ValidTime>{
|
||||
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ValidIPValidator.class);
|
||||
|
||||
public Class<ValidTime> annotation() {
|
||||
return ValidTime.class;
|
||||
}
|
||||
|
||||
private static Pattern IPV4_PATTERN = null;
|
||||
private static Pattern IPV6_PATTERN = null;
|
||||
|
||||
private static final String ipv4Regex = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
|
||||
private static final String ipv6Regex = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";
|
||||
|
||||
static {
|
||||
try {
|
||||
IPV4_PATTERN = Pattern.compile(ipv4Regex,
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
IPV6_PATTERN = Pattern.compile(ipv6Regex,
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to compile pattern", e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
protected static boolean isIpAddress(String ipAddress) {
|
||||
Matcher ipV4Matcher = IPV4_PATTERN.matcher(ipAddress);
|
||||
if (ipV4Matcher.matches()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Matcher ipV6Matcher = IPV6_PATTERN.matcher(ipAddress);
|
||||
return ipV6Matcher.matches();
|
||||
}
|
||||
|
||||
public boolean isValid(Object toValidate) {
|
||||
if(toValidate instanceof String){
|
||||
// TODO Validate it
|
||||
return true;
|
||||
return isIpAddress((String) toValidate);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue