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.filter; 017 018import java.util.Arrays; 019import java.util.LinkedHashMap; 020import java.util.LinkedList; 021import java.util.List; 022import java.util.Map; 023 024import org.forgerock.audit.AuditException; 025import org.forgerock.json.JsonPointer; 026import org.forgerock.util.Reject; 027import org.forgerock.util.Utils; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031public class Filters { 032 private static final Logger logger = LoggerFactory.getLogger(Filters.class); 033 034 protected enum FilterNames { 035 VALUE, 036 FIELD 037 } 038 039 public static ValueOrFieldFilter newValueOrFieldFilter(final List<String> auditTopics, final FilterPolicy policy) { 040 return new ValueOrFieldFilter(exclusionListPerTopic(auditTopics, policy)); 041 } 042 043 public static ValueOrFieldFilter newFilter(final String name, final List<String> auditTopics, 044 final FilterPolicy policy) throws AuditException { 045 Reject.ifNull(name); 046 FilterNames filterName = Utils.asEnum(name, FilterNames.class); 047 switch (filterName) { 048 case VALUE: 049 case FIELD: 050 return newValueOrFieldFilter(auditTopics, policy); 051 default: 052 final String error = String.format("Unknown filter policy name: %s", name); 053 logger.error(error); 054 throw new AuditException(error); 055 } 056 } 057 058 private static Map<String, List<JsonPointer>> exclusionListPerTopic(final List<String> auditTopicsList, 059 final FilterPolicy policy) { 060 final Map<String, List<JsonPointer>> topicMap = initializeTopicMap(auditTopicsList); 061 for (final String value : policy.getExcludeIf()) { 062 addToTopicMap(value, topicMap); 063 } 064 return topicMap; 065 } 066 067 private static void addToTopicMap(final String value, final Map<String, List<JsonPointer>> topicMap) { 068 final JsonPointer pointer = new JsonPointer(value); 069 final String topic = pointer.get(0); 070 final JsonPointer exclusionPointer = 071 new JsonPointer(Arrays.copyOfRange(pointer.toArray(), 1, pointer.size())); 072 final List<JsonPointer> topicPolicies = topicMap.get(topic); 073 if (topicPolicies != null) { 074 topicPolicies.add(exclusionPointer); 075 } else { 076 logger.error(String.format("Attempting to create a policy for an audit topic not registered: %s", value)); 077 } 078 } 079 080 private static Map<String, List<JsonPointer>> initializeTopicMap(final List<String> auditTopicsList) { 081 Map<String, List<JsonPointer>> topicMap = new LinkedHashMap<>(auditTopicsList.size()); 082 for (final String auditTopic : auditTopicsList) { 083 topicMap.put(auditTopic, new LinkedList<JsonPointer>()); 084 } 085 return topicMap; 086 } 087 088}