001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016package org.forgerock.audit.retention;
017
018import java.io.File;
019import java.io.FilenameFilter;
020import java.nio.file.Path;
021
022import org.joda.time.format.DateTimeFormatter;
023
024/**
025 * A {@link FilenameFilter} that matches historical log files. The {@link FilenameFilter} matches a filename with a
026 * given prefix, filename and timestamp.
027 */
028public class TimestampFilenameFilter implements FilenameFilter {
029
030    private final File initialFile;
031    private final String prefix;
032    private final DateTimeFormatter suffixDateFormat;
033
034    /**
035     * Constructs a {@link TimestampFilenameFilter} given an initial file, prefix and suffix.
036     * @param initialFile The initial filename.
037     * @param prefix The audit file prefix to match.
038     * @param suffixDateFormat The audit file date suffix to match.
039     */
040    public TimestampFilenameFilter(final File initialFile, final String prefix,
041            final DateTimeFormatter suffixDateFormat) {
042        this.initialFile = initialFile;
043        this.prefix = prefix;
044        this.suffixDateFormat = suffixDateFormat;
045    }
046
047    /**
048     * Matches the name of a file to the {@link FilenameFilter}.
049     * {@inheritDoc}
050     */
051    @Override
052    public boolean accept(File dir, String name) {
053        // create prefix + filename string
054        final StringBuilder newFileNameBuilder = new StringBuilder();
055        final Path path = initialFile.toPath();
056
057        if (prefix != null) {
058            newFileNameBuilder.append(prefix);
059        }
060        newFileNameBuilder.append(path.getFileName());
061        final String newFileName = newFileNameBuilder.toString();
062
063        if (name.length() < newFileName.length()) {
064            // the filename is smaller or equal to the prefix + filename
065            return false;
066        }
067
068        final String timestamp = name.substring(newFileName.length());
069        try {
070            suffixDateFormat.parseDateTime(timestamp);
071        } catch (IllegalArgumentException | UnsupportedOperationException e) {
072            // not a valid timestamp for the given timestamp suffix
073            return false;
074        }
075
076        // if the file starts with the prefix + initial filename match it.
077        if (name.startsWith(newFileName)) {
078            return true;
079        }
080        return false;
081    }
082}