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