001/* 002 * Copyright 2016-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2016-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) 2016-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.transformations; 037 038 039 040import java.util.ArrayList; 041import java.util.Collection; 042import java.util.Collections; 043import java.util.HashSet; 044import java.util.Set; 045 046import com.unboundid.ldap.sdk.Attribute; 047import com.unboundid.ldap.sdk.Entry; 048import com.unboundid.ldap.sdk.Modification; 049import com.unboundid.ldap.sdk.schema.AttributeTypeDefinition; 050import com.unboundid.ldap.sdk.schema.Schema; 051import com.unboundid.ldif.LDIFAddChangeRecord; 052import com.unboundid.ldif.LDIFChangeRecord; 053import com.unboundid.ldif.LDIFModifyChangeRecord; 054import com.unboundid.util.Debug; 055import com.unboundid.util.StaticUtils; 056import com.unboundid.util.ThreadSafety; 057import com.unboundid.util.ThreadSafetyLevel; 058 059 060 061/** 062 * This class provides an implementation of an entry and LDIF change record 063 * transformation that will remove a specified set of attributes from entries 064 * or change records. Note that this transformation will not alter entry DNs, 065 * so if an attribute to exclude is included in an entry's DN, that value will 066 * still be visible in the DN even if it is removed from the set of attributes 067 * in the entry. 068 */ 069@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 070public final class ExcludeAttributeTransformation 071 implements EntryTransformation, LDIFChangeRecordTransformation 072{ 073 // The schema to use when processing. 074 private final Schema schema; 075 076 // The set of attributes to exclude from entries. 077 private final Set<String> attributes; 078 079 080 081 /** 082 * Creates a new exclude attribute transformation that will strip the 083 * specified attributes out of entries and change records. 084 * 085 * @param schema The scheme to use to identify alternate names that 086 * may be used to reference the attributes to exclude from 087 * entries. It may be {@code null} to use a default 088 * standard schema. 089 * @param attributes The names of the attributes to strip from entries and 090 * change records. It must not be {@code null} or empty. 091 */ 092 public ExcludeAttributeTransformation(final Schema schema, 093 final String... attributes) 094 { 095 this(schema, StaticUtils.toList(attributes)); 096 } 097 098 099 100 /** 101 * Creates a new exclude attribute transformation that will strip the 102 * specified attributes out of entries and change records. 103 * 104 * @param schema The scheme to use to identify alternate names that 105 * may be used to reference the attributes to exclude from 106 * entries. It may be {@code null} to use a default 107 * standard schema. 108 * @param attributes The names of the attributes to strip from entries and 109 * change records. It must not be {@code null} or empty. 110 */ 111 public ExcludeAttributeTransformation(final Schema schema, 112 final Collection<String> attributes) 113 { 114 // If a schema was provided, then use it. Otherwise, use the default 115 // standard schema. 116 Schema s = schema; 117 if (s == null) 118 { 119 try 120 { 121 s = Schema.getDefaultStandardSchema(); 122 } 123 catch (final Exception e) 124 { 125 // This should never happen. 126 Debug.debugException(e); 127 } 128 } 129 this.schema = s; 130 131 132 // Identify all of the names that may be used to reference the attributes 133 // to suppress. 134 final HashSet<String> attrNames = 135 new HashSet<>(StaticUtils.computeMapCapacity(3*attributes.size())); 136 for (final String attrName : attributes) 137 { 138 final String baseName = 139 Attribute.getBaseName(StaticUtils.toLowerCase(attrName)); 140 attrNames.add(baseName); 141 142 if (s != null) 143 { 144 final AttributeTypeDefinition at = s.getAttributeType(baseName); 145 if (at != null) 146 { 147 attrNames.add(StaticUtils.toLowerCase(at.getOID())); 148 for (final String name : at.getNames()) 149 { 150 attrNames.add(StaticUtils.toLowerCase(name)); 151 } 152 } 153 } 154 } 155 this.attributes = Collections.unmodifiableSet(attrNames); 156 } 157 158 159 160 /** 161 * {@inheritDoc} 162 */ 163 @Override() 164 public Entry transformEntry(final Entry e) 165 { 166 if (e == null) 167 { 168 return null; 169 } 170 171 172 // First, see if the entry has any of the target attributes. If not, we can 173 // just return the provided entry. 174 boolean hasAttributeToRemove = false; 175 final Collection<Attribute> originalAttributes = e.getAttributes(); 176 for (final Attribute a : originalAttributes) 177 { 178 if (attributes.contains(StaticUtils.toLowerCase(a.getBaseName()))) 179 { 180 hasAttributeToRemove = true; 181 break; 182 } 183 } 184 185 if (! hasAttributeToRemove) 186 { 187 return e; 188 } 189 190 191 // Create a copy of the entry with all appropriate attributes removed. 192 final ArrayList<Attribute> attributesToKeep = 193 new ArrayList<>(originalAttributes.size()); 194 for (final Attribute a : originalAttributes) 195 { 196 if (! attributes.contains(StaticUtils.toLowerCase(a.getBaseName()))) 197 { 198 attributesToKeep.add(a); 199 } 200 } 201 202 return new Entry(e.getDN(), schema, attributesToKeep); 203 } 204 205 206 207 /** 208 * {@inheritDoc} 209 */ 210 @Override() 211 public LDIFChangeRecord transformChangeRecord(final LDIFChangeRecord r) 212 { 213 if (r == null) 214 { 215 return null; 216 } 217 218 219 // If it's an add change record, then just use the same processing as for an 220 // entry, except we will suppress the entire change record if all of the 221 // attributes end up getting suppressed. 222 if (r instanceof LDIFAddChangeRecord) 223 { 224 final LDIFAddChangeRecord addRecord = (LDIFAddChangeRecord) r; 225 final Entry updatedEntry = transformEntry(addRecord.getEntryToAdd()); 226 if (updatedEntry.getAttributes().isEmpty()) 227 { 228 return null; 229 } 230 231 return new LDIFAddChangeRecord(updatedEntry, addRecord.getControls()); 232 } 233 234 235 // If it's a modify change record, then suppress all modifications targeting 236 // any of the appropriate attributes. If there are no more modifications 237 // left, then suppress the entire change record. 238 if (r instanceof LDIFModifyChangeRecord) 239 { 240 final LDIFModifyChangeRecord modifyRecord = (LDIFModifyChangeRecord) r; 241 242 final Modification[] originalMods = modifyRecord.getModifications(); 243 final ArrayList<Modification> modsToKeep = 244 new ArrayList<>(originalMods.length); 245 for (final Modification m : originalMods) 246 { 247 final String attrName = StaticUtils.toLowerCase( 248 Attribute.getBaseName(m.getAttributeName())); 249 if (! attributes.contains(attrName)) 250 { 251 modsToKeep.add(m); 252 } 253 } 254 255 if (modsToKeep.isEmpty()) 256 { 257 return null; 258 } 259 260 return new LDIFModifyChangeRecord(modifyRecord.getDN(), modsToKeep, 261 modifyRecord.getControls()); 262 } 263 264 265 // If it's some other type of change record (which should just be delete or 266 // modify DN), then don't do anything. 267 return r; 268 } 269 270 271 272 /** 273 * {@inheritDoc} 274 */ 275 @Override() 276 public Entry translate(final Entry original, final long firstLineNumber) 277 { 278 return transformEntry(original); 279 } 280 281 282 283 /** 284 * {@inheritDoc} 285 */ 286 @Override() 287 public LDIFChangeRecord translate(final LDIFChangeRecord original, 288 final long firstLineNumber) 289 { 290 return transformChangeRecord(original); 291 } 292 293 294 295 /** 296 * {@inheritDoc} 297 */ 298 @Override() 299 public Entry translateEntryToWrite(final Entry original) 300 { 301 return transformEntry(original); 302 } 303 304 305 306 /** 307 * {@inheritDoc} 308 */ 309 @Override() 310 public LDIFChangeRecord translateChangeRecordToWrite( 311 final LDIFChangeRecord original) 312 { 313 return transformChangeRecord(original); 314 } 315}