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 request.
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 request*/
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       * {@inheritDoc}
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       * {@inheritDoc}
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      * {@inheritDoc}
121      */
122     public byte[] getGenPassword()
123     {
124         return getDecorated().getGenPassword();
125     }
126 
127 
128     /**
129      * @param genPassword the genPassword to set
130      */
131     public void setGenPassword( byte[] genPassword )
132     {
133         ( ( PasswordModifyResponseImpl ) getDecorated() ).setGenPassword( genPassword );
134     }
135 
136 
137     /**
138      * Overload the parent's getResponseName method, as the pwdModify response should not
139      * contain the responseName.
140      */
141     public String getResponseName()
142     {
143         return null;
144     }
145 
146 
147     /**
148      * Compute the PasswordModifyResponse extended operation length
149      * <pre>
150      * 0x30 L1 
151      *   | 
152      *  [+-- 0x80 L2 genPassword] 
153      * </pre>
154      */
155     /* no qualifier */ 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      * Encodes the PasswordModifyResponse extended operation.
171      * 
172      * @return A ByteBuffer that contains the encoded PDU
173      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
174      */
175     /* no qualifier */ ByteBuffer encodeInternal() throws EncoderException
176     {
177         // Allocate the bytes buffer.
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 }