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.extras.extended.ads_impl.pwdModify;
21
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.directory.api.asn1.DecoderException;
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.ber.tlv.TLV;
28 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
29 import org.apache.directory.api.i18n.I18n;
30 import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
31 import org.apache.directory.api.ldap.codec.api.LdapApiService;
32 import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyRequest;
33 import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43 public class PasswordModifyRequestDecorator extends ExtendedRequestDecorator<PasswordModifyRequest>
44 implements PasswordModifyRequest
45 {
46 private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyRequestDecorator.class );
47
48
49 private PasswordModifyRequest passwordModifyRequest;
50
51
52 private int requestLength = 0;
53
54
55
56
57
58
59
60 public PasswordModifyRequestDecorator( LdapApiService codec, PasswordModifyRequest decoratedMessage )
61 {
62 super( codec, decoratedMessage );
63 passwordModifyRequest = decoratedMessage;
64 }
65
66
67
68
69
70 @Override
71 public void setRequestValue( byte[] requestValue )
72 {
73 PasswordModifyRequestDecoder decoder = new PasswordModifyRequestDecoder();
74
75 try
76 {
77 passwordModifyRequest = decoder.decode( requestValue );
78
79 if ( requestValue != null )
80 {
81 this.requestValue = new byte[requestValue.length];
82 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
83 }
84 else
85 {
86 this.requestValue = null;
87 }
88 }
89 catch ( DecoderException e )
90 {
91 LOG.error( I18n.err( I18n.ERR_04165 ), e );
92 throw new RuntimeException( e );
93 }
94 }
95
96
97
98
99
100 @Override
101 public byte[] getRequestValue()
102 {
103 if ( requestValue == null )
104 {
105 try
106 {
107 requestValue = encodeInternal().array();
108 }
109 catch ( EncoderException e )
110 {
111 LOG.error( I18n.err( I18n.ERR_04167 ), e );
112 throw new RuntimeException( e );
113 }
114 }
115
116 return requestValue;
117 }
118
119
120
121
122
123 @Override
124 public PasswordModifyResponse getResultResponse()
125 {
126 return ( PasswordModifyResponse ) passwordModifyRequest.getResultResponse();
127 }
128
129
130
131
132
133 public byte[] getUserIdentity()
134 {
135 return passwordModifyRequest.getUserIdentity();
136 }
137
138
139
140
141
142 public void setUserIdentity( byte[] userIdentity )
143 {
144 passwordModifyRequest.setUserIdentity( userIdentity );
145 }
146
147
148
149
150
151 public byte[] getOldPassword()
152 {
153 return passwordModifyRequest.getOldPassword();
154 }
155
156
157
158
159
160 public void setOldPassword( byte[] oldPassword )
161 {
162 passwordModifyRequest.setOldPassword( oldPassword );
163 }
164
165
166
167
168
169 public byte[] getNewPassword()
170 {
171 return passwordModifyRequest.getNewPassword();
172 }
173
174
175
176
177
178 public void setNewPassword( byte[] newPassword )
179 {
180 passwordModifyRequest.setNewPassword( newPassword );
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194 int computeLengthInternal()
195 {
196 requestLength = 0;
197
198 if ( passwordModifyRequest.getUserIdentity() != null )
199 {
200 int len = passwordModifyRequest.getUserIdentity().length;
201 requestLength = 1 + TLV.getNbBytes( len ) + len;
202 }
203
204 if ( passwordModifyRequest.getOldPassword() != null )
205 {
206 int len = passwordModifyRequest.getOldPassword().length;
207 requestLength += 1 + TLV.getNbBytes( len ) + len;
208 }
209
210 if ( passwordModifyRequest.getNewPassword() != null )
211 {
212 int len = passwordModifyRequest.getNewPassword().length;
213 requestLength += 1 + TLV.getNbBytes( len ) + len;
214 }
215
216 return 1 + TLV.getNbBytes( requestLength ) + requestLength;
217 }
218
219
220
221
222
223
224
225
226 ByteBuffer encodeInternal() throws EncoderException
227 {
228 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
229
230 bb.put( UniversalTag.SEQUENCE.getValue() );
231 bb.put( TLV.getBytes( requestLength ) );
232
233 if ( passwordModifyRequest.getUserIdentity() != null )
234 {
235 byte[] userIdentity = passwordModifyRequest.getUserIdentity();
236 bb.put( ( byte ) PasswordModifyRequestConstants.USER_IDENTITY_TAG );
237 bb.put( TLV.getBytes( userIdentity.length ) );
238 bb.put( userIdentity );
239 }
240
241 if ( passwordModifyRequest.getOldPassword() != null )
242 {
243 byte[] oldPassword = passwordModifyRequest.getOldPassword();
244 bb.put( ( byte ) PasswordModifyRequestConstants.OLD_PASSWORD_TAG );
245 bb.put( TLV.getBytes( oldPassword.length ) );
246 bb.put( oldPassword );
247 }
248
249 if ( passwordModifyRequest.getNewPassword() != null )
250 {
251 byte[] newPassword = passwordModifyRequest.getNewPassword();
252 bb.put( ( byte ) PasswordModifyRequestConstants.NEW_PASSWORD_TAG );
253 bb.put( TLV.getBytes( newPassword.length ) );
254 bb.put( newPassword );
255 }
256
257 return bb;
258 }
259 }