Coverage Report - org.owasp.dependencycheck.data.cpe.CpeIndexReader
 
Classes in this File Line Coverage Branch Coverage Complexity
CpeIndexReader
75%
28/37
25%
2/8
1.714
 
 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) 2013 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.data.cpe;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.util.HashMap;
 23  
 import java.util.Map;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 import org.apache.lucene.analysis.Analyzer;
 27  
 import org.apache.lucene.analysis.core.KeywordAnalyzer;
 28  
 import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
 29  
 import org.apache.lucene.document.Document;
 30  
 import org.apache.lucene.index.CorruptIndexException;
 31  
 import org.apache.lucene.index.DirectoryReader;
 32  
 import org.apache.lucene.index.IndexReader;
 33  
 import org.apache.lucene.queryparser.classic.ParseException;
 34  
 import org.apache.lucene.queryparser.classic.QueryParser;
 35  
 import org.apache.lucene.search.IndexSearcher;
 36  
 import org.apache.lucene.search.Query;
 37  
 import org.apache.lucene.search.TopDocs;
 38  
 import org.apache.lucene.util.Version;
 39  
 import org.owasp.dependencycheck.data.lucene.FieldAnalyzer;
 40  
 import org.owasp.dependencycheck.data.lucene.SearchFieldAnalyzer;
 41  
 
 42  
 /**
 43  
  *
 44  
  * @author Jeremy Long (jeremy.long@owasp.org)
 45  
  */
 46  9
 public class CpeIndexReader extends BaseIndex {
 47  
 
 48  
     /**
 49  
      * The Lucene IndexReader.
 50  
      */
 51  
     private IndexReader indexReader;
 52  
     /**
 53  
      * The Lucene IndexSearcher.
 54  
      */
 55  
     private IndexSearcher indexSearcher;
 56  
     /**
 57  
      * The Lucene Analyzer used for Searching.
 58  
      */
 59  
     private Analyzer searchingAnalyzer;
 60  
     /**
 61  
      * The Lucene QueryParser used for Searching.
 62  
      */
 63  
     private QueryParser queryParser;
 64  
     /**
 65  
      * The search field analyzer for the product field.
 66  
      */
 67  
     private SearchFieldAnalyzer productSearchFieldAnalyzer;
 68  
     /**
 69  
      * The search field analyzer for the vendor field.
 70  
      */
 71  
     private SearchFieldAnalyzer vendorSearchFieldAnalyzer;
 72  
 
 73  
     /**
 74  
      * Opens the CPE Index.
 75  
      *
 76  
      * @throws IOException is thrown if an IOException occurs opening the index.
 77  
      */
 78  
     @Override
 79  
     public void open() throws IOException {
 80  
         //TODO add spinlock (shared)
 81  9
         super.open();
 82  9
         indexReader = DirectoryReader.open(getDirectory());
 83  9
         indexSearcher = new IndexSearcher(indexReader);
 84  9
         searchingAnalyzer = createSearchingAnalyzer();
 85  9
         queryParser = new QueryParser(Version.LUCENE_43, Fields.DOCUMENT_KEY, searchingAnalyzer);
 86  9
     }
 87  
 
 88  
     /**
 89  
      * Closes the CPE Index.
 90  
      */
 91  
     @Override
 92  
     public void close() {
 93  
         //TODO remove spinlock (shared)
 94  9
         if (searchingAnalyzer != null) {
 95  9
             searchingAnalyzer.close();
 96  9
             searchingAnalyzer = null;
 97  
         }
 98  9
         if (indexReader != null) {
 99  
             try {
 100  9
                 indexReader.close();
 101  0
             } catch (IOException ex) {
 102  0
                 Logger.getLogger(CpeIndexReader.class.getName()).log(Level.FINEST, null, ex);
 103  9
             }
 104  9
             indexReader = null;
 105  
         }
 106  9
         queryParser = null;
 107  9
         indexSearcher = null;
 108  9
         super.close();
 109  9
     }
 110  
 
 111  
     /**
 112  
      * Searches the index using the given search string.
 113  
      *
 114  
      * @param searchString the query text
 115  
      * @param maxQueryResults the maximum number of documents to return
 116  
      * @return the TopDocs found by the search
 117  
      * @throws ParseException thrown when the searchString is invalid
 118  
      * @throws IOException is thrown if there is an issue with the underlying
 119  
      * Index
 120  
      */
 121  
     public TopDocs search(String searchString, int maxQueryResults) throws ParseException, IOException {
 122  41
         final Query query = queryParser.parse(searchString);
 123  41
         return indexSearcher.search(query, maxQueryResults);
 124  
     }
 125  
 
 126  
     /**
 127  
      * Searches the index using the given query.
 128  
      *
 129  
      * @param query the query used to search the index
 130  
      * @param maxQueryResults the max number of results to return
 131  
      * @return the TopDocs found be the query
 132  
      * @throws CorruptIndexException thrown if the Index is corrupt
 133  
      * @throws IOException thrown if there is an IOException
 134  
      */
 135  
     public TopDocs search(Query query, int maxQueryResults) throws CorruptIndexException, IOException {
 136  0
         resetSearchingAnalyzer();
 137  0
         return indexSearcher.search(query, maxQueryResults);
 138  
     }
 139  
 
 140  
     /**
 141  
      * Retrieves a document from the Index.
 142  
      *
 143  
      * @param documentId the id of the document to retrieve
 144  
      * @return the Document
 145  
      * @throws IOException thrown if there is an IOException
 146  
      */
 147  
     public Document getDocument(int documentId) throws IOException {
 148  417
         return indexSearcher.doc(documentId);
 149  
     }
 150  
 
 151  
     /**
 152  
      * Creates an Analyzer for searching the CPE Index.
 153  
      *
 154  
      * @return the CPE Analyzer.
 155  
      */
 156  
     @SuppressWarnings("unchecked")
 157  
     private Analyzer createSearchingAnalyzer() {
 158  9
         final Map fieldAnalyzers = new HashMap();
 159  9
         fieldAnalyzers.put(Fields.DOCUMENT_KEY, new KeywordAnalyzer());
 160  9
         productSearchFieldAnalyzer = new SearchFieldAnalyzer(Version.LUCENE_43);
 161  9
         vendorSearchFieldAnalyzer = new SearchFieldAnalyzer(Version.LUCENE_43);
 162  9
         fieldAnalyzers.put(Fields.PRODUCT, productSearchFieldAnalyzer);
 163  9
         fieldAnalyzers.put(Fields.VENDOR, vendorSearchFieldAnalyzer);
 164  
 
 165  9
         return new PerFieldAnalyzerWrapper(new FieldAnalyzer(Version.LUCENE_43), fieldAnalyzers);
 166  
     }
 167  
 
 168  
     /**
 169  
      * Resets the searching analyzers
 170  
      */
 171  
     private void resetSearchingAnalyzer() {
 172  0
         if (productSearchFieldAnalyzer != null) {
 173  0
             productSearchFieldAnalyzer.clear();
 174  
         }
 175  0
         if (vendorSearchFieldAnalyzer != null) {
 176  0
             vendorSearchFieldAnalyzer.clear();
 177  
         }
 178  0
     }
 179  
 }