001/*
002 * Copyright 2016-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2016-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2016-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util;
037
038
039
040import java.io.BufferedReader;
041import java.io.Closeable;
042import java.io.File;
043import java.io.FileReader;
044import java.io.IOException;
045import java.util.concurrent.atomic.AtomicLong;
046
047import com.unboundid.ldap.sdk.Filter;
048import com.unboundid.ldap.sdk.LDAPException;
049import com.unboundid.ldap.sdk.ResultCode;
050
051import static com.unboundid.util.UtilityMessages.*;
052
053
054
055/**
056 * This class provides a mechanism for reading LDAP search filters from a file.
057 * The file is expected to have one filter per line.  Blank lines and lines
058 * beginning with the octothorpe (#) character will be ignored.
059 */
060@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
061public final class FilterFileReader
062       implements Closeable
063{
064  // A counter used to keep track of the line number for information read from
065  // the file.
066  private final AtomicLong lineNumberCounter;
067
068  // The reader to use to read the filters.
069  private final BufferedReader reader;
070
071  // The file from which the filters are being read.
072  private final File filterFile;
073
074
075
076  /**
077   * Creates a new filter file reader that will read from the file with the
078   * specified path.
079   *
080   * @param  path  The path to the file to be read.  It must not be {@code null}
081   *               and the file must exist.
082   *
083   * @throws  IOException  If a problem is encountered while opening the file
084   *                       for reading.
085   */
086  public FilterFileReader(final String path)
087         throws IOException
088  {
089    this(new File(path));
090  }
091
092
093
094  /**
095   * Creates a new filter file reader that will read from the specified file.
096   *
097   * @param  filterFile  The file to be read.  It must not be {@code null} and
098   *                     the file must exist.
099   *
100   * @throws  IOException  If a problem is encountered while opening the file
101   *                       for reading.
102   */
103  public FilterFileReader(final File filterFile)
104         throws IOException
105  {
106    this.filterFile = filterFile;
107
108    reader = new BufferedReader(new FileReader(filterFile));
109    lineNumberCounter = new AtomicLong(0L);
110  }
111
112
113
114  /**
115   * Reads the next filter from the file.
116   *
117   * @return  The filter read from the file, or {@code null} if there are no
118   *          more filters to be read.
119   *
120   * @throws  IOException  If a problem is encountered while trying to read from
121   *                       the file.
122   *
123   * @throws  LDAPException  If data read from the file can't be parsed as an
124   *                         LDAP search filter.
125   */
126  public Filter readFilter()
127         throws IOException, LDAPException
128  {
129    while (true)
130    {
131      final long lineNumber;
132      final String line;
133      synchronized (this)
134      {
135        line = reader.readLine();
136        lineNumber = lineNumberCounter.incrementAndGet();
137      }
138
139      if (line == null)
140      {
141        return null;
142      }
143
144      final String filterString = line.trim();
145      if (filterString.isEmpty() || filterString.startsWith("#"))
146      {
147        continue;
148      }
149
150      try
151      {
152        return Filter.create(filterString);
153      }
154      catch (final LDAPException le)
155      {
156        Debug.debugException(le);
157        throw new LDAPException(ResultCode.FILTER_ERROR,
158             ERR_FILTER_FILE_READER_CANNOT_PARSE_FILTER.get(filterString,
159                  lineNumber, filterFile.getAbsolutePath(), le.getMessage()),
160             le);
161      }
162    }
163  }
164
165
166
167  /**
168   * Closes this filter file reader.
169   *
170   * @throws  IOException  If a problem is encountered while closing the reader.
171   */
172  @Override()
173  public void close()
174         throws IOException
175  {
176    reader.close();
177  }
178}