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