/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/PostgresConnectionSettings.java

https://github.com/sleuthkit/autopsy · Java · 222 lines · 119 code · 30 blank · 73 comment · 24 complexity · 213aa2637e8e8766a61dd61e6b6ea6ce MD5 · raw file

  1. /*
  2. * Central Repository
  3. *
  4. * Copyright 2015-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.autopsy.centralrepository.datamodel;
  20. import java.util.Objects;
  21. import java.util.regex.Pattern;
  22. /**
  23. *
  24. * This class is a POJO for postgres settings to be used with central repository.
  25. */
  26. public class PostgresConnectionSettings {
  27. private final static String DB_NAMES_REGEX = "[a-z][a-z0-9_]*"; // only lower case
  28. private final static String DB_USER_NAMES_REGEX = "[a-zA-Z]\\w*";
  29. public final static String DEFAULT_HOST = ""; // NON-NLS
  30. public final static int DEFAULT_PORT = 5432;
  31. public final static String DEFAULT_DBNAME = "central_repository"; // NON-NLS
  32. public final static String DEFAULT_USERNAME = "";
  33. public final static String DEFAULT_PASSWORD = "";
  34. private static void validateStr(String s, String errMessage) throws CentralRepoException {
  35. if (null == s || s.isEmpty())
  36. throw new CentralRepoException(errMessage);
  37. }
  38. private static void validateRegex(String s, String pattern, String errMessage) throws CentralRepoException {
  39. if (!Pattern.matches(pattern, s))
  40. throw new CentralRepoException(errMessage);
  41. }
  42. private static void validateNum(int num, Integer min, Integer max, String errMessage) throws CentralRepoException {
  43. if ((min != null && num < min) || (max != null && num > max))
  44. throw new CentralRepoException(errMessage);
  45. }
  46. private String host = DEFAULT_HOST;
  47. private int port = DEFAULT_PORT;
  48. private String dbName = DEFAULT_DBNAME;
  49. private int bulkThreshold = RdbmsCentralRepo.DEFAULT_BULK_THRESHHOLD;
  50. private String userName = DEFAULT_USERNAME;
  51. private String password = DEFAULT_PASSWORD;
  52. /**
  53. * This method retrieves the postgres host.
  54. * @return The host for these settings.
  55. */
  56. public String getHost() {
  57. return host;
  58. }
  59. /**
  60. * This method returns the port number for these settings.
  61. * @return The port number for these settings.
  62. */
  63. public int getPort() {
  64. return port;
  65. }
  66. /**
  67. * This method returns the database name for these settings.
  68. * @return The database name for these settings.
  69. */
  70. public String getDbName() {
  71. return dbName;
  72. }
  73. /**
  74. * This method returns the bulk threshold.
  75. * @return The bulk threshold.
  76. */
  77. public int getBulkThreshold() {
  78. return bulkThreshold;
  79. }
  80. /**
  81. * This method returns the username to use for this connection.
  82. * @return The username to use.
  83. */
  84. public String getUserName() {
  85. return userName;
  86. }
  87. /**
  88. * This method returns the password to use for this connection.
  89. * @return The password to use for this connection.
  90. */
  91. public String getPassword() {
  92. return password;
  93. }
  94. /**
  95. * This method sets the host for this connection.
  96. * NOTE: must be non-empty string.
  97. * @param host the host to set
  98. */
  99. public void setHost(String host) throws CentralRepoException {
  100. validateStr(host, "Invalid host name. Cannot be empty.");
  101. this.host = host;
  102. }
  103. /**
  104. * This method sets the port for this connection.
  105. * @param port The port to set (must be [1,65535]).
  106. */
  107. public void setPort(int port) throws CentralRepoException {
  108. validateNum(port, 1, 65535, "Invalid port. Must be a number greater than 0.");
  109. this.port = port;
  110. }
  111. /**
  112. * This methods sets the name of the database.
  113. * NOTE: this name needs to be a valid postgres database name.
  114. * @param dbName The database name.
  115. */
  116. public void setDbName(String dbName) throws CentralRepoException {
  117. validateStr(dbName, "Invalid database name. Cannot be empty."); // NON-NLS
  118. validateRegex(dbName, DB_NAMES_REGEX,
  119. "Invalid database name. Name must start with a lowercase letter and can only contain lowercase letters, numbers, and '_'."); // NON-NLS
  120. this.dbName = dbName.toLowerCase();
  121. }
  122. /**
  123. * This method sets the bulk threshold of this connection.
  124. * @param bulkThreshold The bulk threshold to set (must be greater than 0).
  125. */
  126. public void setBulkThreshold(int bulkThreshold) throws CentralRepoException {
  127. validateNum(bulkThreshold, 1, null, "Invalid bulk threshold.");
  128. this.bulkThreshold = bulkThreshold;
  129. }
  130. /**
  131. * This method sets the username for this connection.
  132. * NOTE: must be a valid postgres username.
  133. * @param userName The user name to set.
  134. */
  135. public void setUserName(String userName) throws CentralRepoException {
  136. validateStr(userName, "Invalid user name. Cannot be empty."); // NON-NLS
  137. validateRegex(userName, DB_USER_NAMES_REGEX,
  138. "Invalid user name. Name must start with a letter and can only contain letters, numbers, and '_'.");
  139. this.userName = userName;
  140. }
  141. /**
  142. * This method sets the password for this connection.
  143. * @param password The password to set.
  144. */
  145. public void setPassword(String password) throws CentralRepoException {
  146. validateStr(password, "Invalid user password. Cannot be empty.");
  147. this.password = password;
  148. }
  149. @Override
  150. public int hashCode() {
  151. int hash = 7;
  152. hash = 43 * hash + Objects.hashCode(this.host);
  153. hash = 43 * hash + this.port;
  154. hash = 43 * hash + Objects.hashCode(this.dbName);
  155. hash = 43 * hash + this.bulkThreshold;
  156. hash = 43 * hash + Objects.hashCode(this.userName);
  157. hash = 43 * hash + Objects.hashCode(this.password);
  158. return hash;
  159. }
  160. @Override
  161. public boolean equals(Object obj) {
  162. if (this == obj) {
  163. return true;
  164. }
  165. if (obj == null) {
  166. return false;
  167. }
  168. if (getClass() != obj.getClass()) {
  169. return false;
  170. }
  171. final PostgresConnectionSettings other = (PostgresConnectionSettings) obj;
  172. if (this.port != other.port) {
  173. return false;
  174. }
  175. if (this.bulkThreshold != other.bulkThreshold) {
  176. return false;
  177. }
  178. if (!Objects.equals(this.host, other.host)) {
  179. return false;
  180. }
  181. if (!Objects.equals(this.dbName, other.dbName)) {
  182. return false;
  183. }
  184. if (!Objects.equals(this.userName, other.userName)) {
  185. return false;
  186. }
  187. if (!Objects.equals(this.password, other.password)) {
  188. return false;
  189. }
  190. return true;
  191. }
  192. }