diff --git a/bindings/java/ivy.xml b/bindings/java/ivy.xml index 14073535ff52e0c2de4bbcfe0a2cfa2707639c4e..af9f85d09f8d67a0bc96cab2f3647f211098b736 100644 --- a/bindings/java/ivy.xml +++ b/bindings/java/ivy.xml @@ -4,6 +4,7 @@ <dependency org="joda-time" name="joda-time" rev="2.4" /> <dependency org="com.google.guava" name="guava" rev="19.0"/> <dependency org="org.apache.commons" name="commons-lang3" rev="3.0"/> + <dependency org="commons-validator" name="commons-validator" rev="1.6"/> <dependency org="com.google.code.gson" name="gson" rev="2.8.5"/> diff --git a/bindings/java/nbproject/project.xml b/bindings/java/nbproject/project.xml index ccbcee3fdd497578ef58a0350a783bc6615147c8..58ab53b9b262c0a5a2414f1bbbefc6a354627323 100755 --- a/bindings/java/nbproject/project.xml +++ b/bindings/java/nbproject/project.xml @@ -114,7 +114,7 @@ <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/4"> <compilation-unit> <package-root>src</package-root> - <classpath mode="compile">lib;lib/diffutils-1.2.1.jar;lib/junit-4.8.2.jar;lib/postgresql-9.4-1201.jdbc41.jar;lib/c3p0-0.9.5.jar;lib/mchange-commons-java-0.2.9.jar;lib/c3p0-0.9.5-sources.jar;lib/c3p0-0.9.5-javadoc.jar;lib/joda-time-2.4.jar;lib/commons-lang3-3.0.jar;lib/guava-19.0.jar;lib/SparseBitSet-1.1.jar;lib/gson-2.8.5.jar</classpath> + <classpath mode="compile">lib;lib/diffutils-1.2.1.jar;lib/junit-4.8.2.jar;lib/postgresql-9.4-1201.jdbc41.jar;lib/c3p0-0.9.5.jar;lib/mchange-commons-java-0.2.9.jar;lib/c3p0-0.9.5-sources.jar;lib/c3p0-0.9.5-javadoc.jar;lib/joda-time-2.4.jar;lib/commons-lang3-3.0.jar;lib/guava-19.0.jar;lib/SparseBitSet-1.1.jar;lib/gson-2.8.5.jar;lib/commons-validator-1.6.jar</classpath> <built-to>build</built-to> <source-level>1.8</source-level> </compilation-unit> diff --git a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java index e4bdea274df1d5b1d1ce04ec707a93c08d278523..8790b4bcaf3a3f29625489ba69ab318a2b3c1b6c 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java +++ b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsManager.java @@ -1315,52 +1315,19 @@ int getAccountTypeId(Account.Type accountType) { * * @return The normalized account id. */ - private String normalizeAccountID(Account.Type accountType, String accountUniqueID) { + private String normalizeAccountID(Account.Type accountType, String accountUniqueID) throws TskCoreException { String normailzeAccountID = accountUniqueID; if (accountType.equals(Account.Type.PHONE)) { - normailzeAccountID = normalizePhoneNum(accountUniqueID); + normailzeAccountID = CommunicationsUtils.normalizePhoneNum(accountUniqueID); } else if (accountType.equals(Account.Type.EMAIL)) { - normailzeAccountID = normalizeEmailAddress(accountUniqueID); + normailzeAccountID = CommunicationsUtils.normalizeEmailAddress(accountUniqueID); } return normailzeAccountID; } - /** - * Normalize the phone number by removing all non numeric characters, except - * for leading +. - * - * @param phoneNum The phone number to normalize - * - * @return The normalized phone number. - */ - private String normalizePhoneNum(String phoneNum) { - String normailzedPhoneNum = phoneNum.replaceAll("\\D", ""); - - if (phoneNum.startsWith("+")) { - normailzedPhoneNum = "+" + normailzedPhoneNum; - } - - if (normailzedPhoneNum.isEmpty()) { - normailzedPhoneNum = phoneNum; - } - - return normailzedPhoneNum; - } - - /** - * Normalize the given email address by converting it to lowercase. - * - * @param emailAddress The email address tot normalize - * - * @return The normalized email address. - */ - private String normalizeEmailAddress(String emailAddress) { - String normailzedEmailAddr = emailAddress.toLowerCase(); - - return normailzedEmailAddr; - } + /** * Builds the SQL for the given CommunicationsFilter. diff --git a/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..c25eb2cf92e9bdc4af0c683728bd24ebfd4e5fe6 --- /dev/null +++ b/bindings/java/src/org/sleuthkit/datamodel/CommunicationsUtils.java @@ -0,0 +1,69 @@ +/* + * Sleuth Kit Data Model + * + * Copyright 2020 Basis Technology Corp. + * Contact: carrier <at> sleuthkit <dot> org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.datamodel; + +import org.apache.commons.validator.routines.EmailValidator; + +/** + * Provides general utility methods related to communications artifacts. + * + */ +public final class CommunicationsUtils { + + + /** + * Checks if the given string may be a phone number. + * Normalize the phone number by removing all non numeric characters, except + * for leading +. + * + * @param phoneNum The string to check and normalize. + * + * @return The normalized phone number. + * + * @throws TskCoreException If the given string is not a valid phone number. + * + */ + public static String normalizePhoneNum(String phoneNum) throws TskCoreException { + if (phoneNum.matches("\\+?[0-9()\\-\\s]+")) { + return phoneNum.replaceAll("[^0-9\\+]", ""); + } else { + throw new TskCoreException(String.format("Input string is not a valid phone number: %s", phoneNum)); + } + } + + /** + * Checks if the given string is a valid email address. + * Normalizes the given email address by converting it to lowercase. + * + * @param emailAddress The string to be checked and normalized. + * + * @return The normalized email address. + * @throws TskCoreException If the given string is not a valid email address. + */ + public static String normalizeEmailAddress(String emailAddress) throws TskCoreException { + + EmailValidator validator = EmailValidator.getInstance(true, true); + if (validator.isValid(emailAddress)) { + return emailAddress.toLowerCase(); + } else { + throw new TskCoreException(String.format("Input string is not a valid email address: %s", emailAddress)); + } + } + +}