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.ExtendedResponseDecorator;
31 import org.apache.directory.api.ldap.codec.api.LdapApiService;
32 import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse;
33 import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43 public class PasswordModifyResponseDecorator extends ExtendedResponseDecorator<PasswordModifyResponse>
44 implements PasswordModifyResponse
45 {
46 private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyResponseDecorator.class );
47
48 private PasswordModifyResponse passwordModifyResponse;
49
50
51 private int responseLength = 0;
52
53
54
55
56
57
58
59
60
61 public PasswordModifyResponseDecorator( LdapApiService codec, PasswordModifyResponse decoratedMessage )
62 {
63 super( codec, decoratedMessage );
64 passwordModifyResponse = decoratedMessage;
65 }
66
67
68
69
70
71 @Override
72 public void setResponseValue( byte[] responseValue )
73 {
74 PasswordModifyResponseDecoder decoder = new PasswordModifyResponseDecoder();
75
76 try
77 {
78 if ( responseValue != null )
79 {
80 passwordModifyResponse = decoder.decode( responseValue );
81
82 this.responseValue = new byte[responseValue.length];
83 System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length );
84 }
85 else
86 {
87 this.responseValue = null;
88 }
89 }
90 catch ( DecoderException e )
91 {
92 LOG.error( I18n.err( I18n.ERR_04165 ), e );
93 throw new RuntimeException( e );
94 }
95 }
96
97
98
99
100
101 @Override
102 public byte[] getResponseValue()
103 {
104 if ( responseValue == null )
105 {
106 try
107 {
108 responseValue = encodeInternal().array();
109 }
110 catch ( EncoderException e )
111 {
112 LOG.error( I18n.err( I18n.ERR_04167 ), e );
113 throw new RuntimeException( e );
114 }
115 }
116
117 return responseValue;
118 }
119
120
121
122
123
124 @Override
125 public byte[] getGenPassword()
126 {
127 return getDecorated().getGenPassword();
128 }
129
130
131
132
133
134 public void setGenPassword( byte[] genPassword )
135 {
136 ( ( PasswordModifyResponseImpl ) getDecorated() ).setGenPassword( genPassword );
137 }
138
139
140
141
142
143
144 @Override
145 public String getResponseName()
146 {
147 return null;
148 }
149
150
151
152
153
154
155
156
157
158
159 int computeLengthInternal()
160 {
161 responseLength = 0;
162
163 if ( passwordModifyResponse.getGenPassword() != null )
164 {
165 int len = passwordModifyResponse.getGenPassword().length;
166 responseLength = 1 + TLV.getNbBytes( len ) + len;
167 }
168
169 return 1 + TLV.getNbBytes( responseLength ) + responseLength;
170 }
171
172
173
174
175
176
177
178
179 ByteBuffer encodeInternal() throws EncoderException
180 {
181
182 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
183
184 bb.put( UniversalTag.SEQUENCE.getValue() );
185 bb.put( TLV.getBytes( responseLength ) );
186
187 if ( passwordModifyResponse.getGenPassword() != null )
188 {
189 byte[] userIdentity = passwordModifyResponse.getGenPassword();
190 bb.put( ( byte ) PasswordModifyResponseConstants.GEN_PASSWORD_TAG );
191 bb.put( TLV.getBytes( userIdentity.length ) );
192 bb.put( userIdentity );
193 }
194
195 return bb;
196 }
197 }