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