1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.codec.factory;
21
22 import java.util.Collection;
23 import java.util.Iterator;
24
25 import org.apache.directory.api.asn1.ber.tlv.BerValue;
26 import org.apache.directory.api.asn1.util.Asn1Buffer;
27 import org.apache.directory.api.ldap.codec.api.LdapApiService;
28 import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
29 import org.apache.directory.api.ldap.model.entry.Attribute;
30 import org.apache.directory.api.ldap.model.entry.Modification;
31 import org.apache.directory.api.ldap.model.entry.ModificationOperation;
32 import org.apache.directory.api.ldap.model.entry.Value;
33 import org.apache.directory.api.ldap.model.message.Message;
34 import org.apache.directory.api.ldap.model.message.ModifyRequest;
35 import org.apache.directory.api.util.CollectionUtils;
36
37
38
39
40
41
42 public final class ModifyRequestFactory implements Messagefactory
43 {
44
45 public static final ModifyRequestFactory INSTANCE = new ModifyRequestFactory();
46
47 private ModifyRequestFactory()
48 {
49
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64 private void encodeValues( Asn1Buffer buffer, Iterator<Value> values )
65 {
66 values = CollectionUtils.reverse( values );
67 while ( values.hasNext() )
68 {
69 Value value = values.next();
70
71
72 if ( value.isHumanReadable() )
73 {
74 BerValue.encodeOctetString( buffer, value.getString() );
75 }
76 else
77 {
78 BerValue.encodeOctetString( buffer, value.getBytes() );
79 }
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 private void encodeModifications( Asn1Buffer buffer, Iterator<Modification> modifications )
100 {
101 modifications = CollectionUtils.reverse( modifications );
102 while ( modifications.hasNext() )
103 {
104 Modification modification = modifications.next();
105
106 int start = buffer.getPos();
107
108
109 Attribute attribute = modification.getAttribute();
110
111
112 if ( modification.getAttribute().size() != 0 )
113 {
114 encodeValues( buffer, modification.getAttribute().iterator() );
115
116
117 BerValue.encodeSet( buffer, start );
118 }
119 else if ( modification.getOperation() != ModificationOperation.INCREMENT_ATTRIBUTE )
120 {
121
122 BerValue.encodeSet( buffer, start );
123 }
124
125
126 BerValue.encodeOctetString( buffer, attribute.getUpId() );
127
128
129 BerValue.encodeSequence( buffer, start );
130
131
132 BerValue.encodeEnumerated( buffer, modification.getOperation().getValue() );
133
134
135 BerValue.encodeSequence( buffer, start );
136 }
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 @Override
171 public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
172 {
173 int start = buffer.getPos();
174 ModifyRequest modifyRequest = ( ModifyRequest ) message;
175
176
177 Collection<Modification> modifications = modifyRequest.getModifications();
178
179 if ( ( modifications != null ) && ( !modifications.isEmpty() ) )
180 {
181 encodeModifications( buffer, modifications.iterator() );
182
183
184 BerValue.encodeSequence( buffer, start );
185 }
186
187
188 BerValue.encodeOctetString( buffer, modifyRequest.getName().getName() );
189
190
191 BerValue.encodeSequence( buffer, LdapCodecConstants.MODIFY_REQUEST_TAG, start );
192 }
193 }