20 package org.sleuthkit.autopsy.centralrepository.datamodel;
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Optional;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.commons.validator.routines.DomainValidator;
29 import org.apache.commons.validator.routines.EmailValidator;
51 if (attributeType == null) {
58 String trimmedData = data.trim();
60 switch (attributeType.getId()) {
66 return normalizeEmail(trimmedData);
68 return normalizePhone(trimmedData);
88 if (defaultCorrelationType.getId() == attributeType.getId()) {
92 final String errorMessage = String.format(
93 "Validator function not found for attribute type: %s",
94 attributeType.getDisplayName());
116 if (typeOption.isPresent()) {
131 final String validMd5Regex =
"^[a-f0-9]{32}$";
132 final String dataLowered = data.toLowerCase();
133 if (dataLowered.matches(validMd5Regex)) {
145 DomainValidator validator = DomainValidator.getInstance(
true);
146 if (validator.isValid(data)) {
147 return data.toLowerCase();
149 final String validIpAddressRegex =
"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
150 if (data.matches(validIpAddressRegex)) {
168 if (isValidEmailAddress(emailAddress)) {
169 return emailAddress.toLowerCase().trim();
171 throw new CorrelationAttributeNormalizationException(String.format(
"Data was expected to be a valid email address: %s", emailAddress));
184 static String normalizePhone(String phoneNumber)
throws CorrelationAttributeNormalizationException {
185 if (isValidPhoneNumber(phoneNumber)) {
186 String normalizedNumber = phoneNumber.replaceAll(
"\\s+",
"");
187 normalizedNumber = normalizedNumber.replaceAll(
"[\\-()]",
"");
191 throw new CorrelationAttributeNormalizationException(String.format(
"Phone number string %s is too short ", phoneNumber));
193 return normalizedNumber;
196 throw new CorrelationAttributeNormalizationException(String.format(
"Data was expected to be a valid phone number: %s", phoneNumber));
222 if (data.length() <= 32) {
252 final String validIccidRegex =
"^89[f0-9]{17,22}$";
253 final String iccidWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX,
"");
254 if (iccidWithoutSeperators.matches(validIccidRegex)) {
255 return iccidWithoutSeperators;
279 final String validImsiRegex =
"^[0-9]{14,15}$";
280 final String imsiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX,
"");
281 if (imsiWithoutSeperators.matches(validImsiRegex)) {
282 return imsiWithoutSeperators;
303 final String validMacRegex =
"^([a-f0-9]{12}|[a-f0-9]{16})$";
304 final String macWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX,
"");
305 if (macWithoutSeperators.matches(validMacRegex)) {
306 return macWithoutSeperators;
332 final String validImeiRegex =
"^[0-9]{14,16}$";
333 final String imeiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX,
"");
334 if (imeiWithoutSeperators.matches(validImeiRegex)) {
335 return imeiWithoutSeperators;
359 static boolean isValidPhoneNumber(String phoneNumber) {
363 for (
int i = 0; i < phoneNumber.length(); i++) {
364 if ( !((i == 0 && phoneNumber.charAt(i) ==
'+')
365 || Character.isSpaceChar(phoneNumber.charAt(i))
366 || Character.isDigit(phoneNumber.charAt(i))
367 || PHONENUMBER_CHARS.contains(String.valueOf(phoneNumber.charAt(i))))) {
384 static boolean isValidEmailAddress(String emailAddress) {
385 if (!StringUtils.isEmpty(emailAddress)) {
386 EmailValidator validator = EmailValidator.getInstance(
true,
true);
387 return validator.isValid(emailAddress);
static final String SEPERATORS_REGEX
static final int EMAIL_TYPE_ID
static final int USBID_TYPE_ID
static String normalizeImsi(String data)
static String normalizeImei(String data)
static final int ICCID_TYPE_ID
static String normalize(int attributeTypeId, String data)
static List< CorrelationAttributeInstance.Type > getDefaultCorrelationTypes()
static String normalizeMd5(String data)
static final int IMEI_TYPE_ID
static final Set< String > PHONENUMBER_CHARS
static final int MIN_PHONENUMBER_LEN
static String normalizeMac(String data)
static String normalize(CorrelationAttributeInstance.Type attributeType, String data)
static String normalizeIccid(String data)
static final int DOMAIN_TYPE_ID
static String normalizeDomain(String data)
static final int PHONE_TYPE_ID
static final int MAC_TYPE_ID
static final int IMSI_TYPE_ID
static String verifySsid(String data)
CorrelationAttributeNormalizer()
static final int SSID_TYPE_ID
static String normalizeUsbId(String data)
static final int FILES_TYPE_ID