Sleuth Kit Java Bindings (JNI)  4.12.1
Java bindings for using The Sleuth Kit
CommunicationsUtils.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.datamodel;
20 
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.Set;
24 import org.apache.commons.lang3.StringUtils;
25 
30 public final class CommunicationsUtils {
31 
32  // These symbols are allowed in dialed or written forms of phone numbers.
33  // A '+' is allowed only as a leading digit and hence not inlcuded here.
34  private static final Set<String> TELEPHONY_CHARS = new HashSet<>(Arrays.asList(
35  "-", "(", ")", "#", "*", ","
36  ));
37 
38  private static final int MIN_PHONENUMBER_LEN = 3;
39 
43  private CommunicationsUtils() {
44  }
45 
61  public static String normalizePhoneNum(String phoneNumber) throws InvalidAccountIDException {
62 
63  if (StringUtils.isEmpty(phoneNumber)) {
64  throw new InvalidAccountIDException(String.format("Input phone number is empty or null."));
65  }
66 
67  if (isValidPhoneNumber(phoneNumber) == false) {
68  throw new InvalidAccountIDException(String.format("Input string is not a valid phone number: %s", phoneNumber));
69  }
70 
71  String normalizedNumber = phoneNumber.trim();
72  normalizedNumber = normalizedNumber.replaceAll("\\s+", ""); // remove spaces.
73  normalizedNumber = normalizedNumber.replaceAll("[\\-()]", ""); // remove parens & dashes.
74 
75  // ensure a min length
76  if (normalizedNumber.length() < MIN_PHONENUMBER_LEN) {
77  throw new InvalidAccountIDException("Invalid phone number string " + phoneNumber);
78 
79  }
80  return normalizedNumber;
81  }
82 
93  public static String normalizeEmailAddress(String emailAddress) throws InvalidAccountIDException {
94 
95  if (StringUtils.isEmpty(emailAddress)) {
96  throw new InvalidAccountIDException(String.format("Input email address is empty or null."));
97  }
98 
99  if (isValidEmailAddress(emailAddress) == false) {
100  throw new InvalidAccountIDException(String.format("Input string is not a valid email address: %s", emailAddress));
101  }
102 
103  return emailAddress.toLowerCase().replace(";", "").trim();
104  }
105 
116  public static boolean isValidAccountId(Account.Type accountType, String accountUniqueID) {
117  if (accountType == Account.Type.PHONE) {
118  return isValidPhoneNumber(accountUniqueID);
119  }
120  if (accountType == Account.Type.EMAIL) {
121  return isValidEmailAddress(accountUniqueID);
122  }
123 
124  return !StringUtils.isEmpty(accountUniqueID);
125  }
126 
138  public static boolean isValidPhoneNumber(String phoneNum) {
139  if (StringUtils.isEmpty(phoneNum)) {
140  return false;
141  }
142 
143  String trimmedPhoneNum = phoneNum.trim();
144 
145  // A phone number may have a leading '+', special telephony chars, or digits.
146  // Anything else implies an invalid phone number.
147  for (int i = 0; i < trimmedPhoneNum.length(); i++) {
148  if (!((trimmedPhoneNum.charAt(i) == '+' && i == 0) // a '+' is allowed only at the beginning
149  || isValidPhoneChar(trimmedPhoneNum.charAt(i)))) {
150  return false;
151  }
152  }
153 
154  return true;
155  }
156 
164  private static boolean isValidPhoneChar(char ch) {
165  return Character.isSpaceChar(ch)
166  || Character.isDigit(ch)
167  || TELEPHONY_CHARS.contains(String.valueOf(ch));
168  }
169 
187  public static boolean isValidEmailAddress(String emailAddress) {
188  if (StringUtils.isEmpty(emailAddress)) {
189  return false;
190  }
191 
192  if (emailAddress.contains("@") == false
193  || emailAddress.contains(".") == false ) {
194  return false;
195  }
196 
197  // emsure there's a username and domain
198  String[] tokens = emailAddress.split("@");
199  if (tokens.length < 2
200  || StringUtils.isEmpty(tokens[0])
201  || StringUtils.isEmpty(tokens[1])) {
202  return false;
203  }
204 
205  // ensure domain has name and suffix
206  String[] tokens2 = tokens[1].split("\\.");
207  return !(tokens2.length < 2
208  || StringUtils.isEmpty(tokens2[0])
209  || StringUtils.isEmpty(tokens2[1]));
210  }
211 }
static String normalizeEmailAddress(String emailAddress)
static boolean isValidPhoneNumber(String phoneNum)
static String normalizePhoneNum(String phoneNumber)
static final Account.Type PHONE
Definition: Account.java:49
static boolean isValidEmailAddress(String emailAddress)
static boolean isValidAccountId(Account.Type accountType, String accountUniqueID)
static final Account.Type EMAIL
Definition: Account.java:50

Copyright © 2011-2021 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.