001/* 002 * Copyright 2016-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2016-2024 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-2024 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.experimental; 037 038 039 040import java.util.ArrayList; 041import java.util.Collections; 042import java.util.LinkedHashMap; 043import java.util.List; 044 045import com.unboundid.ldap.sdk.Attribute; 046import com.unboundid.ldap.sdk.DeleteRequest; 047import com.unboundid.ldap.sdk.Entry; 048import com.unboundid.ldap.sdk.LDAPException; 049import com.unboundid.ldap.sdk.OperationType; 050import com.unboundid.ldap.sdk.ResultCode; 051import com.unboundid.util.NotMutable; 052import com.unboundid.util.NotNull; 053import com.unboundid.util.StaticUtils; 054import com.unboundid.util.ThreadSafety; 055import com.unboundid.util.ThreadSafetyLevel; 056 057import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*; 058 059 060 061/** 062 * This class represents an entry that holds information about a delete 063 * operation processed by an LDAP server, as per the specification described in 064 * draft-chu-ldap-logschema-00. 065 */ 066@NotMutable() 067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 068public final class DraftChuLDAPLogSchema00DeleteEntry 069 extends DraftChuLDAPLogSchema00Entry 070{ 071 /** 072 * The name of the attribute used to hold information about attributes 073 * contained in the entry that was deleted. 074 */ 075 @NotNull public static final String ATTR_DELETED_ATTRIBUTE = "reqOld"; 076 077 078 079 /** 080 * The serial version UID for this serializable class. 081 */ 082 private static final long serialVersionUID = -4326357861964770357L; 083 084 085 086 // The list of deleted attributes, if available. 087 @NotNull private final List<Attribute> deletedAttributes; 088 089 090 091 /** 092 * Creates a new instance of this delete access log entry from the provided 093 * entry. 094 * 095 * @param entry The entry used to create this delete access log entry. 096 * 097 * @throws LDAPException If the provided entry cannot be decoded as a valid 098 * delete access log entry as per the specification 099 * contained in draft-chu-ldap-logschema-00. 100 */ 101 public DraftChuLDAPLogSchema00DeleteEntry(@NotNull final Entry entry) 102 throws LDAPException 103 { 104 super(entry, OperationType.DELETE); 105 106 final byte[][] deletedAttrBytes = 107 entry.getAttributeValueByteArrays(ATTR_DELETED_ATTRIBUTE); 108 if ((deletedAttrBytes == null) || (deletedAttrBytes.length == 0)) 109 { 110 deletedAttributes = Collections.emptyList(); 111 return; 112 } 113 114 final LinkedHashMap<String,List<Attribute>> attrMap = new LinkedHashMap<>( 115 StaticUtils.computeMapCapacity(deletedAttrBytes.length)); 116 for (final byte[] attrBytes : deletedAttrBytes) 117 { 118 int colonPos = -1; 119 for (int i=0; i < attrBytes.length; i++) 120 { 121 if (attrBytes[i] == ':') 122 { 123 colonPos = i; 124 break; 125 } 126 } 127 128 if (colonPos < 0) 129 { 130 throw new LDAPException(ResultCode.DECODING_ERROR, 131 ERR_LOGSCHEMA_DECODE_DELETE_OLD_ATTR_MISSING_COLON.get( 132 entry.getDN(), ATTR_DELETED_ATTRIBUTE, 133 StaticUtils.toUTF8String(attrBytes))); 134 } 135 else if (colonPos == 0) 136 { 137 throw new LDAPException(ResultCode.DECODING_ERROR, 138 ERR_LOGSCHEMA_DECODE_DELETE_OLD_ATTR_MISSING_ATTR.get( 139 entry.getDN(), ATTR_DELETED_ATTRIBUTE, 140 StaticUtils.toUTF8String(attrBytes))); 141 } 142 143 if ((colonPos == (attrBytes.length - 1)) || 144 (attrBytes[colonPos+1] != ' ')) 145 { 146 throw new LDAPException(ResultCode.DECODING_ERROR, 147 ERR_LOGSCHEMA_DECODE_DELETE_OLD_ATTR_MISSING_SPACE.get( 148 entry.getDN(), ATTR_DELETED_ATTRIBUTE, 149 StaticUtils.toUTF8String(attrBytes))); 150 } 151 152 final String attrName = 153 StaticUtils.toUTF8String(attrBytes, 0, colonPos); 154 final String lowerName = StaticUtils.toLowerCase(attrName); 155 156 List<Attribute> attrList = attrMap.get(lowerName); 157 if (attrList == null) 158 { 159 attrList = new ArrayList<>(10); 160 attrMap.put(lowerName, attrList); 161 } 162 163 final byte[] attrValue = new byte[attrBytes.length - colonPos - 2]; 164 if (attrValue.length > 0) 165 { 166 System.arraycopy(attrBytes, colonPos + 2, attrValue, 0, 167 attrValue.length); 168 } 169 170 attrList.add(new Attribute(attrName, attrValue)); 171 } 172 173 final ArrayList<Attribute> oldAttributes = new ArrayList<>(attrMap.size()); 174 for (final List<Attribute> attrList : attrMap.values()) 175 { 176 if (attrList.size() == 1) 177 { 178 oldAttributes.addAll(attrList); 179 } 180 else 181 { 182 final byte[][] valueArray = new byte[attrList.size()][]; 183 for (int i=0; i < attrList.size(); i++) 184 { 185 valueArray[i] = attrList.get(i).getValueByteArray(); 186 } 187 oldAttributes.add(new Attribute(attrList.get(0).getName(), valueArray)); 188 } 189 } 190 191 deletedAttributes = Collections.unmodifiableList(oldAttributes); 192 } 193 194 195 196 /** 197 * Retrieves a list of the attributes from the entry that was deleted, if 198 * available. 199 * 200 * @return A list of the attributes from the entry that was deleted, or an 201 * empty list if no deleted attribute information was included in the 202 * access log entry. 203 */ 204 @NotNull() 205 public List<Attribute> getDeletedAttributes() 206 { 207 return deletedAttributes; 208 } 209 210 211 212 /** 213 * Retrieves an {@code DeleteRequest} created from this delete access log 214 * entry. 215 * 216 * @return The {@code DeleteRequest} created from this delete access log 217 * entry. 218 */ 219 @NotNull() 220 public DeleteRequest toDeleteRequest() 221 { 222 return new DeleteRequest(getTargetEntryDN(), getRequestControlArray()); 223 } 224}