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;
52 if (attributeType == null) {
59 String trimmedData = data.trim();
61 switch (attributeType.getId()) {
67 return normalizeEmail(trimmedData);
69 return normalizePhone(trimmedData);
89 if (defaultCorrelationType.getId() == attributeType.getId()) {
93 final String errorMessage = String.format(
94 "Validator function not found for attribute type: %s",
95 attributeType.getDisplayName());
117 if (typeOption.isPresent()) {
132 final String validMd5Regex =
"^[a-f0-9]{32}$";
133 final String dataLowered = data.toLowerCase();
134 if (dataLowered.matches(validMd5Regex)) {
146 DomainValidator validator = DomainValidator.getInstance(
true);
147 if (validator.isValid(data)) {
150 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])$";
151 if (data.matches(validIpAddressRegex)) {
169 if (isValidEmailAddress(emailAddress)) {
170 return emailAddress.toLowerCase().trim();
172 throw new CorrelationAttributeNormalizationException(String.format(
"Data was expected to be a valid email address: %s", emailAddress));
185 static String normalizePhone(String phoneNumber)
throws CorrelationAttributeNormalizationException {
186 if (isValidPhoneNumber(phoneNumber)) {
187 String normalizedNumber = phoneNumber.replaceAll(
"\\s+",
"");
188 normalizedNumber = normalizedNumber.replaceAll(
"[\\-()]",
"");
192 throw new CorrelationAttributeNormalizationException(String.format(
"Phone number string %s is too short ", phoneNumber));
194 return normalizedNumber;
197 throw new CorrelationAttributeNormalizationException(String.format(
"Data was expected to be a valid phone number: %s", phoneNumber));
223 if (data.length() <= 32) {
253 final String validIccidRegex =
"^89[f0-9]{17,22}$";
254 final String iccidWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX,
"");
255 if (iccidWithoutSeperators.matches(validIccidRegex)) {
256 return iccidWithoutSeperators;
280 final String validImsiRegex =
"^[0-9]{14,15}$";
281 final String imsiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX,
"");
282 if (imsiWithoutSeperators.matches(validImsiRegex)) {
283 return imsiWithoutSeperators;
304 final String validMacRegex =
"^([a-f0-9]{12}|[a-f0-9]{16})$";
305 final String macWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX,
"");
306 if (macWithoutSeperators.matches(validMacRegex)) {
307 return macWithoutSeperators;
333 final String validImeiRegex =
"^[0-9]{14,16}$";
334 final String imeiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX,
"");
335 if (imeiWithoutSeperators.matches(validImeiRegex)) {
336 return imeiWithoutSeperators;
360 static boolean isValidPhoneNumber(String phoneNumber) {
364 for (
int i = 0; i < phoneNumber.length(); i++) {
365 if ( !((i == 0 && phoneNumber.charAt(i) ==
'+')
366 || Character.isSpaceChar(phoneNumber.charAt(i))
367 || Character.isDigit(phoneNumber.charAt(i))
368 || PHONENUMBER_CHARS.contains(String.valueOf(phoneNumber.charAt(i))))) {
385 static boolean isValidEmailAddress(String emailAddress) {
386 if (!StringUtils.isEmpty(emailAddress)) {
387 EmailValidator validator = EmailValidator.getInstance(
true,
true);
388 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 String extractDomain(String urlString)
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