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.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.AddRequest; 046import com.unboundid.ldap.sdk.Attribute; 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.StaticUtils; 053import com.unboundid.util.ThreadSafety; 054import com.unboundid.util.ThreadSafetyLevel; 055 056import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*; 057 058 059 060/** 061 * This class represents an entry that holds information about an add operation 062 * processed by an LDAP server, as per the specification described in 063 * draft-chu-ldap-logschema-00. 064 */ 065@NotMutable() 066@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 067public final class DraftChuLDAPLogSchema00AddEntry 068 extends DraftChuLDAPLogSchema00Entry 069{ 070 /** 071 * The name of the attribute used to hold the attribute changes represented by 072 * this add operation. 073 */ 074 public static final String ATTR_ATTRIBUTE_CHANGES = "reqMod"; 075 076 077 078 /** 079 * The serial version UID for this serializable class. 080 */ 081 private static final long serialVersionUID = 1236828283266120444L; 082 083 084 085 // The set of attributes included in the add request. 086 private final List<Attribute> attributes; 087 088 089 090 /** 091 * Creates a new instance of this add access log entry from the provided 092 * entry. 093 * 094 * @param entry The entry used to create this add access log entry. 095 * 096 * @throws LDAPException If the provided entry cannot be decoded as a valid 097 * add access log entry as per the specification 098 * contained in draft-chu-ldap-logschema-00. 099 */ 100 public DraftChuLDAPLogSchema00AddEntry(final Entry entry) 101 throws LDAPException 102 { 103 super(entry, OperationType.ADD); 104 105 final byte[][] changes = 106 entry.getAttributeValueByteArrays(ATTR_ATTRIBUTE_CHANGES); 107 if ((changes == null) || (changes.length == 0)) 108 { 109 throw new LDAPException(ResultCode.DECODING_ERROR, 110 ERR_LOGSCHEMA_DECODE_MISSING_REQUIRED_ATTR.get(entry.getDN(), 111 ATTR_ATTRIBUTE_CHANGES)); 112 } 113 114 final LinkedHashMap<String,List<Attribute>> attrMap = 115 new LinkedHashMap<>(StaticUtils.computeMapCapacity(changes.length)); 116 for (final byte[] changeBytes : changes) 117 { 118 int colonPos = -1; 119 for (int i=0; i < changeBytes.length; i++) 120 { 121 if (changeBytes[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_ADD_CHANGE_MISSING_COLON.get(entry.getDN(), 132 ATTR_ATTRIBUTE_CHANGES, 133 StaticUtils.toUTF8String(changeBytes))); 134 } 135 else if (colonPos == 0) 136 { 137 throw new LDAPException(ResultCode.DECODING_ERROR, 138 ERR_LOGSCHEMA_DECODE_ADD_CHANGE_MISSING_ATTR.get(entry.getDN(), 139 ATTR_ATTRIBUTE_CHANGES, 140 StaticUtils.toUTF8String(changeBytes))); 141 } 142 143 if ((colonPos == (changeBytes.length - 1)) || 144 (changeBytes[colonPos+1] != '+')) 145 { 146 throw new LDAPException(ResultCode.DECODING_ERROR, 147 ERR_LOGSCHEMA_DECODE_ADD_CHANGE_TYPE_NOT_PLUS.get(entry.getDN(), 148 ATTR_ATTRIBUTE_CHANGES, 149 StaticUtils.toUTF8String(changeBytes))); 150 } 151 152 if ((colonPos == (changeBytes.length - 2)) || 153 (changeBytes[colonPos+2] != ' ')) 154 { 155 throw new LDAPException(ResultCode.DECODING_ERROR, 156 ERR_LOGSCHEMA_DECODE_ADD_CHANGE_NO_SPACE_AFTER_PLUS.get( 157 entry.getDN(), ATTR_ATTRIBUTE_CHANGES, 158 StaticUtils.toUTF8String(changeBytes))); 159 } 160 161 162 final String attrName = 163 StaticUtils.toUTF8String(changeBytes, 0, colonPos); 164 final String lowerName = StaticUtils.toLowerCase(attrName); 165 166 List<Attribute> attrList = attrMap.get(lowerName); 167 if (attrList == null) 168 { 169 attrList = new ArrayList<>(10); 170 attrMap.put(lowerName, attrList); 171 } 172 173 final byte[] attrValue = new byte[changeBytes.length - colonPos - 3]; 174 if (attrValue.length > 0) 175 { 176 System.arraycopy(changeBytes, (colonPos+3), attrValue, 0, 177 attrValue.length); 178 } 179 180 attrList.add(new Attribute(attrName, attrValue)); 181 } 182 183 final ArrayList<Attribute> addAttributes = new ArrayList<>(attrMap.size()); 184 for (final List<Attribute> attrList : attrMap.values()) 185 { 186 if (attrList.size() == 1) 187 { 188 addAttributes.addAll(attrList); 189 } 190 else 191 { 192 final byte[][] valueArray = new byte[attrList.size()][]; 193 for (int i=0; i < attrList.size(); i++) 194 { 195 valueArray[i] = attrList.get(i).getValueByteArray(); 196 } 197 addAttributes.add(new Attribute(attrList.get(0).getName(), valueArray)); 198 } 199 } 200 201 attributes = Collections.unmodifiableList(addAttributes); 202 } 203 204 205 206 /** 207 * Retrieves a list of the attributes included in the add request described 208 * by this add access log entry. 209 * 210 * @return A list of the attributes included in the add request described by 211 * this add access log entry. 212 */ 213 public List<Attribute> getAddAttributes() 214 { 215 return attributes; 216 } 217 218 219 220 /** 221 * Retrieves an {@code AddRequest} created from this add access log entry. 222 * 223 * @return The {@code AddRequest} created from this add access log entry. 224 */ 225 public AddRequest toAddRequest() 226 { 227 return new AddRequest(getTargetEntryDN(), attributes, 228 getRequestControlArray()); 229 } 230}