001/*
002 * Copyright 2016-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2016-2024 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-2024 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  @NotNull private final AtomicLong lineNumberCounter;
067
068  // The reader to use to read the filters.
069  @NotNull private final BufferedReader reader;
070
071  // The file from which the filters are being read.
072  @NotNull 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(@NotNull 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(@NotNull 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  @Nullable()
127  public Filter readFilter()
128         throws IOException, LDAPException
129  {
130    while (true)
131    {
132      final long lineNumber;
133      final String line;
134      synchronized (this)
135      {
136        line = reader.readLine();
137        lineNumber = lineNumberCounter.incrementAndGet();
138      }
139
140      if (line == null)
141      {
142        return null;
143      }
144
145      final String filterString = line.trim();
146      if (filterString.isEmpty() || filterString.startsWith("#"))
147      {
148        continue;
149      }
150
151      try
152      {
153        return Filter.create(filterString);
154      }
155      catch (final LDAPException le)
156      {
157        Debug.debugException(le);
158        throw new LDAPException(ResultCode.FILTER_ERROR,
159             ERR_FILTER_FILE_READER_CANNOT_PARSE_FILTER.get(filterString,
160                  lineNumber, filterFile.getAbsolutePath(), le.getMessage()),
161             le);
162      }
163    }
164  }
165
166
167
168  /**
169   * Closes this filter file reader.
170   *
171   * @throws  IOException  If a problem is encountered while closing the reader.
172   */
173  @Override()
174  public void close()
175         throws IOException
176  {
177    reader.close();
178  }
179}