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.events; 017 018import org.forgerock.json.JsonValue; 019import org.slf4j.Logger; 020import org.slf4j.LoggerFactory; 021 022import java.util.HashMap; 023import java.util.Map; 024import java.util.Set; 025 026/** 027 * Encapsulates meta-data for event topics. 028 */ 029public final class EventTopicsMetaData { 030 031 private static final Logger logger = LoggerFactory.getLogger(EventTopicsMetaData.class); 032 private final Map<String, JsonValue> eventTopicsMetaData; 033 034 /** 035 * Create a new EventTopicsMetaData. 036 * 037 * @param eventTopicsMetaData 038 * Event topic schemas mapped by event topic name. 039 */ 040 public EventTopicsMetaData(Map<String, JsonValue> eventTopicsMetaData) { 041 this.eventTopicsMetaData = eventTopicsMetaData; 042 } 043 044 /** 045 * Returns <tt>true</tt> if this object has meta-data for the specified topic. 046 * 047 * @param topic 048 * The name of the topic to check. 049 * @return <tt>true</tt> if this object has meta-data for the specified topic; <tt>false</tt> otherwise. 050 */ 051 public boolean containsTopic(String topic) { 052 return eventTopicsMetaData.containsKey(topic); 053 } 054 055 /** 056 * Returns the JSON schema for the requested topic if this object has meta-data for that topic. 057 * Otherwise, null is returned. 058 * 059 * @param topic 060 * The name of the topic to check. 061 * @return JSON schema if this object has meta-data for the specified topic; <tt>null</tt> otherwise. 062 */ 063 public JsonValue getSchema(String topic) { 064 return eventTopicsMetaData.get(topic); 065 } 066 067 /** 068 * Returns the names of the set of topics for which this object has meta-data. 069 * 070 * @return set of topic names. 071 */ 072 public Set<String> getTopics() { 073 return eventTopicsMetaData.keySet(); 074 } 075 076 /** 077 * Returns a new instance of <tt>EventTopicsMetaData</tt> containing only the meta-data for topics 078 * held by this object that are named within provided <tt>topics</tt> parameter. 079 * <p/> 080 * Any entries within <tt>topics</tt> that are not known to this object will not be included in the resulting 081 * <tt>EventTopicsMetaData</tt> object. 082 * 083 * @param topics 084 * The names of topics whose meta-data should be included. 085 * @return a new instance of <tt>EventTopicsMetaData</tt>. 086 */ 087 public EventTopicsMetaData filter(Set<String> topics) { 088 Map<String, JsonValue> filteredTopicSchemas = new HashMap<>(); 089 for (String topic : topics) { 090 if (!containsTopic(topic)) { 091 logger.error("unknown audit event topic : {}", topic); 092 continue; 093 } 094 filteredTopicSchemas.put(topic, getSchema(topic)); 095 } 096 return new EventTopicsMetaData(filteredTopicSchemas); 097 } 098}