1 /*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2011-2017 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.keywordsearch;
20
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import org.apache.commons.lang.math.NumberUtils;
25
29 final class Index {
30
31 private final String indexPath;
32 private final String schemaVersion;
33 private final String solrVersion;
34 private final String indexName;
35 private static final String DEFAULT_CORE_NAME = "text_index"; //NON-NLS
36 private final UNCPathUtilities uncPathUtilities = new UNCPathUtilities();
37
49 Index(String indexPath, String solrVersion, String schemaVersion, String coreName, String caseName) {
50 this.indexPath = uncPathUtilities.convertPathToUNC(indexPath);
51 this.solrVersion = solrVersion;
52 this.schemaVersion = schemaVersion;
53 if (coreName == null || coreName.isEmpty()) {
54 // come up with a new core name
55 coreName = createCoreName(caseName);
56 }
57 this.indexName = coreName;
58 }
59
67 private String createCoreName(String caseName) {
68 String coreName = sanitizeCoreName(caseName);
69 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
70 Date date = new Date();
71 return coreName + "_" + dateFormat.format(date);
72 }
73
87 static private String sanitizeCoreName(String coreName) {
88
89 String result;
90
91 // Allow these characters: '-', '.', '0'-'9', 'A'-'Z', '_', and 'a'-'z'.
92 // Replace all else with '_'.
93 result = coreName.replaceAll("[^-.0-9A-Z_a-z]", "_"); // NON-NLS
94
95 // Make it all lowercase
96 result = result.toLowerCase();
97
98 // Must not start with hyphen
99 if (result.length() > 0 && (result.codePointAt(0) == '-')) {
100 result = "_" + result;
101 }
102
103 if (result.isEmpty()) {
104 result = DEFAULT_CORE_NAME;
105 }
106
107 return result;
108 }
109
113 String getIndexPath() {
114 return indexPath;
115 }
116
120 String getSchemaVersion() {
121 return schemaVersion;
122 }
123
127 String getSolrVersion() {
128 return solrVersion;
129 }
130
134 String getIndexName() {
135 return indexName;
136 }
137
146 boolean isCompatible(String version) {
147 // Versions are compatible if they have the same major version no
148 int currentMajorVersion = NumberUtils.toInt(schemaVersion.substring(0, schemaVersion.indexOf('.')));
149 int givenMajorVersion = NumberUtils.toInt(version.substring(0, version.indexOf('.')));
150
151 return currentMajorVersion == givenMajorVersion;
152 }
153 }