Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
PostgresConnectionSettings.java
Go to the documentation of this 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 
21 import java.util.Objects;
22 import java.util.regex.Pattern;
23 
29  private final static String DB_NAMES_REGEX = "[a-z][a-z0-9_]*"; // only lower case
30  private final static String DB_USER_NAMES_REGEX = "[a-zA-Z]\\w*";
31 
32  public final static String DEFAULT_HOST = ""; // NON-NLS
33  public final static int DEFAULT_PORT = 5432;
34  public final static String DEFAULT_DBNAME = "central_repository"; // NON-NLS
35  public final static String DEFAULT_USERNAME = "";
36  public final static String DEFAULT_PASSWORD = "";
37 
38  private static void validateStr(String s, String errMessage) throws CentralRepoException {
39  if (null == s || s.isEmpty())
40  throw new CentralRepoException(errMessage);
41  }
42 
43  private static void validateRegex(String s, String pattern, String errMessage) throws CentralRepoException {
44  if (!Pattern.matches(pattern, s))
45  throw new CentralRepoException(errMessage);
46  }
47 
48  private static void validateNum(int num, Integer min, Integer max, String errMessage) throws CentralRepoException {
49  if ((min != null && num < min) || (max != null && num > max))
50  throw new CentralRepoException(errMessage);
51  }
52 
53  private String host = DEFAULT_HOST;
54  private int port = DEFAULT_PORT;
55  private String dbName = DEFAULT_DBNAME;
56  private int bulkThreshold = RdbmsCentralRepo.DEFAULT_BULK_THRESHHOLD;
57  private String userName = DEFAULT_USERNAME;
58  private String password = DEFAULT_PASSWORD;
59 
64  public String getHost() {
65  return host;
66  }
67 
72  public int getPort() {
73  return port;
74  }
75 
80  public String getDbName() {
81  return dbName;
82  }
83 
88  public int getBulkThreshold() {
89  return bulkThreshold;
90  }
91 
96  public String getUserName() {
97  return userName;
98  }
99 
104  public String getPassword() {
105  return password;
106  }
107 
108 
114  public void setHost(String host) throws CentralRepoException {
115  validateStr(host, "Invalid host name. Cannot be empty.");
116  this.host = host;
117  }
118 
119 
124  public void setPort(int port) throws CentralRepoException {
125  validateNum(port, 1, 65535, "Invalid port. Must be a number greater than 0.");
126  this.port = port;
127  }
128 
129 
135  public void setDbName(String dbName) throws CentralRepoException {
136  validateStr(dbName, "Invalid database name. Cannot be empty."); // NON-NLS
137  validateRegex(dbName, DB_NAMES_REGEX,
138  "Invalid database name. Name must start with a lowercase letter and can only contain lowercase letters, numbers, and '_'."); // NON-NLS
139 
140  this.dbName = dbName.toLowerCase();
141  }
142 
143 
148  public void setBulkThreshold(int bulkThreshold) throws CentralRepoException {
149  validateNum(bulkThreshold, 1, null, "Invalid bulk threshold.");
150  this.bulkThreshold = bulkThreshold;
151  }
152 
153 
159  public void setUserName(String userName) throws CentralRepoException {
160  validateStr(userName, "Invalid user name. Cannot be empty."); // NON-NLS
161  validateRegex(userName, DB_USER_NAMES_REGEX,
162  "Invalid user name. Name must start with a letter and can only contain letters, numbers, and '_'.");
163 
164  this.userName = userName;
165  }
166 
167 
172  public void setPassword(String password) throws CentralRepoException {
173  validateStr(password, "Invalid user password. Cannot be empty.");
174  this.password = password;
175  }
176 
177  @Override
178  public int hashCode() {
179  int hash = 7;
180  hash = 43 * hash + Objects.hashCode(this.host);
181  hash = 43 * hash + this.port;
182  hash = 43 * hash + Objects.hashCode(this.dbName);
183  hash = 43 * hash + this.bulkThreshold;
184  hash = 43 * hash + Objects.hashCode(this.userName);
185  hash = 43 * hash + Objects.hashCode(this.password);
186  return hash;
187  }
188 
189 
190  @Override
191  public boolean equals(Object obj) {
192  if (this == obj) {
193  return true;
194  }
195  if (obj == null) {
196  return false;
197  }
198  if (getClass() != obj.getClass()) {
199  return false;
200  }
202  if (this.port != other.port) {
203  return false;
204  }
205  if (this.bulkThreshold != other.bulkThreshold) {
206  return false;
207  }
208  if (!Objects.equals(this.host, other.host)) {
209  return false;
210  }
211  if (!Objects.equals(this.dbName, other.dbName)) {
212  return false;
213  }
214  if (!Objects.equals(this.userName, other.userName)) {
215  return false;
216  }
217  if (!Objects.equals(this.password, other.password)) {
218  return false;
219  }
220  return true;
221  }
222 }
static void validateNum(int num, Integer min, Integer max, String errMessage)

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.