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;
 24 
 28 final class Index {
 29 
 30  private final String indexPath;
 31  private final String schemaVersion;
 32  private final String solrVersion;
 33  private final String indexName;
 34  private static final String DEFAULT_CORE_NAME = "text_index"; //NON-NLS
 35  private final UNCPathUtilities uncPathUtilities = new UNCPathUtilities();
 36 
 48  Index(String indexPath, String solrVersion, String schemaVersion, String coreName, String caseName) {
 49  this.indexPath = uncPathUtilities.convertPathToUNC(indexPath);
 50  this.solrVersion = solrVersion;
 51  this.schemaVersion = schemaVersion;
 52  if (coreName == null || coreName.isEmpty()) {
 53  // come up with a new core name
 54  coreName = createCoreName(caseName);
 55  }
 56  this.indexName = coreName;
 57  }
 58 
 66  private String createCoreName(String caseName) {
 67  String coreName = sanitizeCoreName(caseName);
 68  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
 69  Date date = new Date();
 70  return coreName + "_" + dateFormat.format(date);
 71  }
 72 
 86  static private String sanitizeCoreName(String coreName) {
 87 
 88  String result;
 89  
 90  // Allow these characters: '-', '.', '0'-'9', 'A'-'Z', '_', and 'a'-'z'.
 91  // Replace all else with '_'.
 92  result = coreName.replaceAll("[^-.0-9A-Z_a-z]", "_"); // NON-NLS
 93 
 94  // Make it all lowercase
 95  result = result.toLowerCase();
 96 
 97  // Must not start with hyphen
 98  if (result.length() > 0 && (result.codePointAt(0) == '-')) {
 99  result = "_" + result;
 100  }
 101 
 102  if (result.isEmpty()) {
 103  result = DEFAULT_CORE_NAME;
 104  }
 105 
 106  return result;
 107  }
 108 
 112  String getIndexPath() {
 113  return indexPath;
 114  }
 115 
 119  String getSchemaVersion() {
 120  return schemaVersion;
 121  }
 122 
 126  String getSolrVersion() {
 127  return solrVersion;
 128  }
 129 
 133  String getIndexName() {
 134  return indexName;
 135  }
 136 }