001/* 002 * Copyright 2016-2017 UnboundID Corp. 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2016-2017 UnboundID Corp. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.ldap.sdk.experimental; 022 023 024 025import java.util.ArrayList; 026import java.util.Collections; 027import java.util.LinkedHashMap; 028import java.util.List; 029 030import com.unboundid.ldap.sdk.Attribute; 031import com.unboundid.ldap.sdk.DeleteRequest; 032import com.unboundid.ldap.sdk.Entry; 033import com.unboundid.ldap.sdk.LDAPException; 034import com.unboundid.ldap.sdk.OperationType; 035import com.unboundid.ldap.sdk.ResultCode; 036import com.unboundid.util.NotMutable; 037import com.unboundid.util.StaticUtils; 038import com.unboundid.util.ThreadSafety; 039import com.unboundid.util.ThreadSafetyLevel; 040 041import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*; 042 043 044 045/** 046 * This class represents an entry that holds information about a delete 047 * operation processed by an LDAP server, as per the specification described in 048 * draft-chu-ldap-logschema-00. 049 */ 050@NotMutable() 051@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 052public final class DraftChuLDAPLogSchema00DeleteEntry 053 extends DraftChuLDAPLogSchema00Entry 054{ 055 /** 056 * The name of the attribute used to hold information about attributes 057 * contained in the entry that was deleted. 058 */ 059 public static final String ATTR_DELETED_ATTRIBUTE = "reqOld"; 060 061 062 063 /** 064 * The serial version UID for this serializable class. 065 */ 066 private static final long serialVersionUID = -4326357861964770357L; 067 068 069 070 // The list of deleted attributes, if available. 071 private final List<Attribute> deletedAttributes; 072 073 074 075 /** 076 * Creates a new instance of this delete access log entry from the provided 077 * entry. 078 * 079 * @param entry The entry used to create this delete access log entry. 080 * 081 * @throws LDAPException If the provided entry cannot be decoded as a valid 082 * delete access log entry as per the specification 083 * contained in draft-chu-ldap-logschema-00. 084 */ 085 public DraftChuLDAPLogSchema00DeleteEntry(final Entry entry) 086 throws LDAPException 087 { 088 super(entry, OperationType.DELETE); 089 090 final byte[][] deletedAttrBytes = 091 entry.getAttributeValueByteArrays(ATTR_DELETED_ATTRIBUTE); 092 if ((deletedAttrBytes == null) || (deletedAttrBytes.length == 0)) 093 { 094 deletedAttributes = Collections.emptyList(); 095 return; 096 } 097 098 final LinkedHashMap<String,List<Attribute>> attrMap = 099 new LinkedHashMap<String,List<Attribute>>(deletedAttrBytes.length); 100 for (final byte[] attrBytes : deletedAttrBytes) 101 { 102 int colonPos = -1; 103 for (int i=0; i < attrBytes.length; i++) 104 { 105 if (attrBytes[i] == ':') 106 { 107 colonPos = i; 108 break; 109 } 110 } 111 112 if (colonPos < 0) 113 { 114 throw new LDAPException(ResultCode.DECODING_ERROR, 115 ERR_LOGSCHEMA_DECODE_DELETE_OLD_ATTR_MISSING_COLON.get( 116 entry.getDN(), ATTR_DELETED_ATTRIBUTE, 117 StaticUtils.toUTF8String(attrBytes))); 118 } 119 else if (colonPos == 0) 120 { 121 throw new LDAPException(ResultCode.DECODING_ERROR, 122 ERR_LOGSCHEMA_DECODE_DELETE_OLD_ATTR_MISSING_ATTR.get( 123 entry.getDN(), ATTR_DELETED_ATTRIBUTE, 124 StaticUtils.toUTF8String(attrBytes))); 125 } 126 127 if ((colonPos == (attrBytes.length - 1)) || 128 (attrBytes[colonPos+1] != ' ')) 129 { 130 throw new LDAPException(ResultCode.DECODING_ERROR, 131 ERR_LOGSCHEMA_DECODE_DELETE_OLD_ATTR_MISSING_SPACE.get( 132 entry.getDN(), ATTR_DELETED_ATTRIBUTE, 133 StaticUtils.toUTF8String(attrBytes))); 134 } 135 136 final String attrName = 137 StaticUtils.toUTF8String(attrBytes, 0, colonPos); 138 final String lowerName = StaticUtils.toLowerCase(attrName); 139 140 List<Attribute> attrList = attrMap.get(lowerName); 141 if (attrList == null) 142 { 143 attrList = new ArrayList<Attribute>(10); 144 attrMap.put(lowerName, attrList); 145 } 146 147 final byte[] attrValue = new byte[attrBytes.length - colonPos - 2]; 148 if (attrValue.length > 0) 149 { 150 System.arraycopy(attrBytes, colonPos + 2, attrValue, 0, 151 attrValue.length); 152 } 153 154 attrList.add(new Attribute(attrName, attrValue)); 155 } 156 157 final ArrayList<Attribute> oldAttributes = 158 new ArrayList<Attribute>(attrMap.size()); 159 for (final List<Attribute> attrList : attrMap.values()) 160 { 161 if (attrList.size() == 1) 162 { 163 oldAttributes.addAll(attrList); 164 } 165 else 166 { 167 final byte[][] valueArray = new byte[attrList.size()][]; 168 for (int i=0; i < attrList.size(); i++) 169 { 170 valueArray[i] = attrList.get(i).getValueByteArray(); 171 } 172 oldAttributes.add(new Attribute(attrList.get(0).getName(), valueArray)); 173 } 174 } 175 176 deletedAttributes = Collections.unmodifiableList(oldAttributes); 177 } 178 179 180 181 /** 182 * Retrieves a list of the attributes from the entry that was deleted, if 183 * available. 184 * 185 * @return A list of the attributes from the entry that was deleted, or an 186 * empty list if no deleted attribute information was included in the 187 * access log entry. 188 */ 189 public List<Attribute> getDeletedAttributes() 190 { 191 return deletedAttributes; 192 } 193 194 195 196 /** 197 * Retrieves an {@code DeleteRequest} created from this delete access log 198 * entry. 199 * 200 * @return The {@code DeleteRequest} created from this delete access log 201 * entry. 202 */ 203 public DeleteRequest toDeleteRequest() 204 { 205 return new DeleteRequest(getTargetEntryDN(), getRequestControlArray()); 206 } 207}