View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
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   * A Decorator for PasswordModifyResponse extended response.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
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      /** stores the length of the response*/
51      private int responseLength = 0;
52  
53  
54      /**
55       * 
56       * Creates a new instance of PasswordModifyResponseDecorator.
57       *
58       * @param codec The LDAP service instance
59       * @param decoratedMessage The decorated message
60       */
61      public PasswordModifyResponseDecorator( LdapApiService codec, PasswordModifyResponse decoratedMessage )
62      {
63          super( codec, decoratedMessage );
64          passwordModifyResponse = decoratedMessage;
65      }
66  
67  
68      /**
69       * {@inheritDoc}
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       * {@inheritDoc}
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      * {@inheritDoc}
123      */
124     @Override
125     public byte[] getGenPassword()
126     {
127         return getDecorated().getGenPassword();
128     }
129 
130 
131     /**
132      * @param genPassword the genPassword to set
133      */
134     public void setGenPassword( byte[] genPassword )
135     {
136         ( ( PasswordModifyResponseImpl ) getDecorated() ).setGenPassword( genPassword );
137     }
138 
139 
140     /**
141      * Overload the parent's getResponseName method, as the pwdModify response should not
142      * contain the responseName.
143      */
144     @Override
145     public String getResponseName()
146     {
147         return null;
148     }
149 
150 
151     /**
152      * Compute the PasswordModifyResponse extended operation length
153      * <pre>
154      * 0x30 L1 
155      *   | 
156      *  [+-- 0x80 L2 genPassword] 
157      * </pre>
158      */
159     /* no qualifier */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      * Encodes the PasswordModifyResponse extended operation.
175      * 
176      * @return A ByteBuffer that contains the encoded PDU
177      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
178      */
179     /* no qualifier */ByteBuffer encodeInternal() throws EncoderException
180     {
181         // Allocate the bytes buffer.
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 }