Coverage Report - org.owasp.dependencycheck.data.cpe.Index
 
Classes in this File Line Coverage Branch Coverage Complexity
Index
64%
22/34
37%
3/8
1.625
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.data.cpe;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.IOException;
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 import org.apache.lucene.analysis.Analyzer;
 26  
 import org.apache.lucene.analysis.core.KeywordAnalyzer;
 27  
 import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
 28  
 import org.apache.lucene.document.Document;
 29  
 import org.apache.lucene.document.Field;
 30  
 import org.apache.lucene.document.TextField;
 31  
 import org.apache.lucene.index.CorruptIndexException;
 32  
 import org.apache.lucene.index.Term;
 33  
 import org.apache.lucene.queryparser.classic.QueryParser;
 34  
 import org.apache.lucene.store.Directory;
 35  
 import org.apache.lucene.store.FSDirectory;
 36  
 import org.apache.lucene.util.Version;
 37  
 import org.owasp.dependencycheck.data.lucene.AbstractIndex;
 38  
 import org.owasp.dependencycheck.utils.Settings;
 39  
 import org.owasp.dependencycheck.data.lucene.FieldAnalyzer;
 40  
 import org.owasp.dependencycheck.data.lucene.SearchFieldAnalyzer;
 41  
 
 42  
 /**
 43  
  * The Index class is used to utilize and maintain the CPE Index.
 44  
  *
 45  
  * @author Jeremy Long (jeremy.long@owasp.org)
 46  
  */
 47  30
 public class Index extends AbstractIndex {
 48  
 
 49  
     /**
 50  
      * Returns the directory that holds the CPE Index.
 51  
      *
 52  
      * @return the Directory containing the CPE Index.
 53  
      * @throws IOException is thrown if an IOException occurs.
 54  
      */
 55  
     @Override
 56  
     public Directory getDirectory() throws IOException {
 57  30
         final File path = getDataDirectory();
 58  30
         return FSDirectory.open(path);
 59  
     }
 60  
 
 61  
     /**
 62  
      * Retrieves the directory that the JAR file exists in so that we can ensure
 63  
      * we always use a common data directory.
 64  
      *
 65  
      * @return the data directory for this index.
 66  
      * @throws IOException is thrown if an IOException occurs of course...
 67  
      */
 68  
     public File getDataDirectory() throws IOException {
 69  30
         final File path = Settings.getFile(Settings.KEYS.CPE_DATA_DIRECTORY);
 70  30
         if (!path.exists()) {
 71  0
             if (!path.mkdirs()) {
 72  0
                 throw new IOException("Unable to create CPE Data directory");
 73  
             }
 74  
         }
 75  30
         return path;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Creates an Analyzer for the CPE Index.
 80  
      *
 81  
      * @return the CPE Analyzer.
 82  
      */
 83  
     @SuppressWarnings("unchecked")
 84  
     @Override
 85  
     public Analyzer createIndexingAnalyzer() {
 86  27
         final Map fieldAnalyzers = new HashMap();
 87  27
         fieldAnalyzers.put(Fields.DOCUMENT_KEY, new KeywordAnalyzer());
 88  27
         return new PerFieldAnalyzerWrapper(new FieldAnalyzer(Version.LUCENE_43), fieldAnalyzers);
 89  
     }
 90  
     /**
 91  
      * The search field analyzer for the product field.
 92  
      */
 93  
     private SearchFieldAnalyzer productSearchFieldAnalyzer;
 94  
     /**
 95  
      * The search field analyzer for the vendor field.
 96  
      */
 97  
     private SearchFieldAnalyzer vendorSearchFieldAnalyzer;
 98  
 
 99  
     /**
 100  
      * Creates an Analyzer for searching the CPE Index.
 101  
      *
 102  
      * @return the CPE Analyzer.
 103  
      */
 104  
     @SuppressWarnings("unchecked")
 105  
     @Override
 106  
     public Analyzer createSearchingAnalyzer() {
 107  27
         final Map fieldAnalyzers = new HashMap();
 108  
 
 109  27
         fieldAnalyzers.put(Fields.DOCUMENT_KEY, new KeywordAnalyzer());
 110  27
         productSearchFieldAnalyzer = new SearchFieldAnalyzer(Version.LUCENE_43);
 111  27
         vendorSearchFieldAnalyzer = new SearchFieldAnalyzer(Version.LUCENE_43);
 112  27
         fieldAnalyzers.put(Fields.PRODUCT, productSearchFieldAnalyzer);
 113  27
         fieldAnalyzers.put(Fields.VENDOR, vendorSearchFieldAnalyzer);
 114  
 
 115  27
         return new PerFieldAnalyzerWrapper(new FieldAnalyzer(Version.LUCENE_43), fieldAnalyzers);
 116  
     }
 117  
 
 118  
     /**
 119  
      * Creates the Lucene QueryParser used when querying the index.
 120  
      *
 121  
      * @return a QueryParser.
 122  
      */
 123  
     @Override
 124  
     public QueryParser createQueryParser() {
 125  21
         return new QueryParser(Version.LUCENE_43, Fields.DOCUMENT_KEY, getSearchingAnalyzer());
 126  
     }
 127  
 
 128  
     /**
 129  
      * Resets the searching analyzers
 130  
      */
 131  
     @Override
 132  
     protected void resetSearchingAnalyzer() {
 133  111
         if (productSearchFieldAnalyzer != null) {
 134  111
             productSearchFieldAnalyzer.clear();
 135  
         }
 136  111
         if (vendorSearchFieldAnalyzer != null) {
 137  111
             vendorSearchFieldAnalyzer.clear();
 138  
         }
 139  111
     }
 140  
 
 141  
     /**
 142  
      * Saves a CPE IndexEntry into the Lucene index.
 143  
      *
 144  
      * @param entry a CPE entry.
 145  
      * @throws CorruptIndexException is thrown if the index is corrupt.
 146  
      * @throws IOException is thrown if an IOException occurs.
 147  
      */
 148  
     public void saveEntry(IndexEntry entry) throws CorruptIndexException, IOException {
 149  0
         final Document doc = convertEntryToDoc(entry);
 150  0
         final Term term = new Term(Fields.DOCUMENT_KEY, entry.getDocumentId());
 151  0
         getIndexWriter().updateDocument(term, doc);
 152  0
     }
 153  
 
 154  
     /**
 155  
      * Converts a CPE entry into a Lucene Document.
 156  
      *
 157  
      * @param entry a CPE IndexEntry.
 158  
      * @return a Lucene Document containing a CPE IndexEntry.
 159  
      */
 160  
     protected Document convertEntryToDoc(IndexEntry entry) {
 161  0
         final Document doc = new Document();
 162  
 
 163  0
         final Field vendor = new TextField(Fields.VENDOR, entry.getVendor(), Field.Store.YES);
 164  0
         doc.add(vendor);
 165  
 
 166  0
         final Field product = new TextField(Fields.PRODUCT, entry.getProduct(), Field.Store.YES);
 167  0
         doc.add(product);
 168  0
         return doc;
 169  
     }
 170  
 }