001/* 002 * Copyright 2018-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2018-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) 2018-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.ldap.sdk.unboundidds.logs; 037 038 039 040import java.io.BufferedReader; 041import java.io.Closeable; 042import java.io.File; 043import java.io.FileReader; 044import java.io.InputStream; 045import java.io.IOException; 046import java.io.InputStreamReader; 047import java.io.Reader; 048import java.util.ArrayList; 049import java.util.List; 050 051import com.unboundid.ldif.LDIFAddChangeRecord; 052import com.unboundid.ldif.LDIFChangeRecord; 053import com.unboundid.ldif.LDIFDeleteChangeRecord; 054import com.unboundid.ldif.LDIFModifyChangeRecord; 055import com.unboundid.ldif.LDIFModifyDNChangeRecord; 056import com.unboundid.ldif.LDIFReader; 057import com.unboundid.util.Debug; 058import com.unboundid.util.NotMutable; 059import com.unboundid.util.StaticUtils; 060import com.unboundid.util.ThreadSafety; 061import com.unboundid.util.ThreadSafetyLevel; 062 063import static com.unboundid.ldap.sdk.unboundidds.logs.LogMessages.*; 064 065 066 067/** 068 * This class provides a mechanism for reading messages from a Directory Server 069 * audit log. 070 * <BR> 071 * <BLOCKQUOTE> 072 * <B>NOTE:</B> This class, and other classes within the 073 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 074 * supported for use against Ping Identity, UnboundID, and 075 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 076 * for proprietary functionality or for external specifications that are not 077 * considered stable or mature enough to be guaranteed to work in an 078 * interoperable way with other types of LDAP servers. 079 * </BLOCKQUOTE> 080 */ 081@NotMutable() 082@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 083public final class AuditLogReader 084 implements Closeable 085{ 086 // The reader used to read the contents of the log file. 087 private final BufferedReader reader; 088 089 090 091 /** 092 * Creates a new audit log reader that will read messages from the specified 093 * log file. 094 * 095 * @param path The path of the log file to read. 096 * 097 * @throws IOException If a problem occurs while opening the file for 098 * reading. 099 */ 100 public AuditLogReader(final String path) 101 throws IOException 102 { 103 reader = new BufferedReader(new FileReader(path)); 104 } 105 106 107 108 /** 109 * Creates a new audit log reader that will read messages from the specified 110 * log file. 111 * 112 * @param file The log file to read. 113 * 114 * @throws IOException If a problem occurs while opening the file for 115 * reading. 116 */ 117 public AuditLogReader(final File file) 118 throws IOException 119 { 120 reader = new BufferedReader(new FileReader(file)); 121 } 122 123 124 125 /** 126 * Creates a new audit log reader that will read messages using the provided 127 * {@code Reader} object. 128 * 129 * @param reader The reader to use to read log messages. 130 */ 131 public AuditLogReader(final Reader reader) 132 { 133 if (reader instanceof BufferedReader) 134 { 135 this.reader = (BufferedReader) reader; 136 } 137 else 138 { 139 this.reader = new BufferedReader(reader); 140 } 141 } 142 143 144 145 /** 146 * Creates a new audit log reader that will read messages from the provided 147 * input stream. 148 * 149 * @param inputStream The input stream from which to read log messages. 150 */ 151 public AuditLogReader(final InputStream inputStream) 152 { 153 reader = new BufferedReader(new InputStreamReader(inputStream)); 154 } 155 156 157 158 /** 159 * Reads the next audit log message from the log file. 160 * 161 * @return The audit log message read from the log file, or {@code null} if 162 * there are no more messages to be read. 163 * 164 * @throws IOException If an error occurs while trying to read from the 165 * file. 166 * 167 * @throws AuditLogException If an error occurs while trying to parse the 168 * log message. 169 */ 170 public AuditLogMessage read() 171 throws IOException, AuditLogException 172 { 173 // Read a list of lines until we find the end of the file or a blank line 174 // after a series of non-blank lines. 175 final List<String> fullMessageLines = new ArrayList<>(20); 176 final List<String> nonCommentLines = new ArrayList<>(20); 177 while (true) 178 { 179 final String line = reader.readLine(); 180 if (line == null) 181 { 182 // We hit the end of the audit log file. We obviously can't read any 183 // more. 184 break; 185 } 186 187 if (line.isEmpty()) 188 { 189 if (nonCommentLines.isEmpty()) 190 { 191 // This means that we encountered consecutive blank lines, or blank 192 // lines with only comments between them. This is okay. We'll just 193 // clear the list of full message lines and keep reading. 194 fullMessageLines.clear(); 195 continue; 196 } 197 else 198 { 199 // We found a blank line after some non-blank lines that included at 200 // least one non-comment line. Break out of the loop and process what 201 // we read as an audit log message. 202 break; 203 } 204 } 205 else 206 { 207 // We read a non-empty line. Add it to the list of full message lines, 208 // and if it's not a comment, then add it to the list of non-comment 209 // lines. 210 fullMessageLines.add(line); 211 if (! line.startsWith("#")) 212 { 213 nonCommentLines.add(line); 214 } 215 } 216 } 217 218 219 // If we've gotten here and the list of non-comment lines is empty, then 220 // that must mean that we hit the end of the audit log without finding any 221 // more messages. In that case, return null to indicate that we've hit the 222 // end of the file. 223 if (nonCommentLines.isEmpty()) 224 { 225 return null; 226 } 227 228 229 // Try to parse the set of non-comment lines as an LDIF change record. If 230 // that fails, then throw a log exception. 231 final LDIFChangeRecord changeRecord; 232 try 233 { 234 final String[] ldifLines = 235 StaticUtils.toArray(nonCommentLines, String.class); 236 changeRecord = LDIFReader.decodeChangeRecord(ldifLines); 237 } 238 catch (final Exception e) 239 { 240 Debug.debugException(e); 241 242 final String concatenatedLogLines = StaticUtils.concatenateStrings( 243 "[ ", "\"", ", ", "\"", " ]", fullMessageLines); 244 throw new AuditLogException(fullMessageLines, 245 ERR_AUDIT_LOG_READER_CANNOT_PARSE_CHANGE_RECORD.get( 246 concatenatedLogLines, StaticUtils.getExceptionMessage(e)), 247 e); 248 } 249 250 251 // Create the appropriate type of audit log message based on the change 252 // record. 253 if (changeRecord instanceof LDIFAddChangeRecord) 254 { 255 return new AddAuditLogMessage(fullMessageLines, 256 (LDIFAddChangeRecord) changeRecord); 257 } 258 else if (changeRecord instanceof LDIFDeleteChangeRecord) 259 { 260 return new DeleteAuditLogMessage(fullMessageLines, 261 (LDIFDeleteChangeRecord) changeRecord); 262 } 263 else if (changeRecord instanceof LDIFModifyChangeRecord) 264 { 265 return new ModifyAuditLogMessage(fullMessageLines, 266 (LDIFModifyChangeRecord) changeRecord); 267 } 268 else if (changeRecord instanceof LDIFModifyDNChangeRecord) 269 { 270 return new ModifyDNAuditLogMessage(fullMessageLines, 271 (LDIFModifyDNChangeRecord) changeRecord); 272 } 273 else 274 { 275 // This should never happen. 276 final String concatenatedLogLines = StaticUtils.concatenateStrings( 277 "[ ", "\"", ", ", "\"", " ]", fullMessageLines); 278 throw new AuditLogException(fullMessageLines, 279 ERR_AUDIT_LOG_READER_UNSUPPORTED_CHANGE_RECORD.get( 280 concatenatedLogLines, changeRecord.getChangeType().getName())); 281 } 282 } 283 284 285 286 /** 287 * Closes this error log reader. 288 * 289 * @throws IOException If a problem occurs while closing the reader. 290 */ 291 @Override() 292 public void close() 293 throws IOException 294 { 295 reader.close(); 296 } 297}