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.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   * A Decorator for PasswordModifyRequest extended request.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class PasswordModifyRequestDecorator extends ExtendedRequestDecorator<PasswordModifyRequest>
44      implements PasswordModifyRequest
45  {
46      private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyRequestDecorator.class );
47  
48      /** The internal PasswordModifyRequest */
49      private PasswordModifyRequest passwordModifyRequest;
50  
51      /** stores the length of the request*/
52      private int requestLength = 0;
53  
54  
55      /**
56       * Create a new decorator instance 
57       * @param codec The codec service
58       * @param decoratedMessage The decorated PwdModifyRequest
59       */
60      public PasswordModifyRequestDecorator( LdapApiService codec, PasswordModifyRequest decoratedMessage )
61      {
62          super( codec, decoratedMessage );
63          passwordModifyRequest = decoratedMessage;
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void setRequestValue( byte[] requestValue )
72      {
73          PasswordModifyRequestDecoder decoder = new PasswordModifyRequestDecoder();
74  
75          try
76          {
77              if ( requestValue != null )
78              {
79                  passwordModifyRequest = decoder.decode( requestValue );
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       * {@inheritDoc}
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      * {@inheritDoc}
122      */
123     @Override
124     public PasswordModifyResponse getResultResponse()
125     {
126         return ( PasswordModifyResponse ) passwordModifyRequest.getResultResponse();
127     }
128 
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public byte[] getUserIdentity()
135     {
136         return passwordModifyRequest.getUserIdentity();
137     }
138 
139 
140     /**
141      * @param userIdentity the userIdentity to set
142      */
143     @Override
144     public void setUserIdentity( byte[] userIdentity )
145     {
146         passwordModifyRequest.setUserIdentity( userIdentity );
147     }
148 
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public byte[] getOldPassword()
155     {
156         return passwordModifyRequest.getOldPassword();
157     }
158 
159 
160     /**
161      * @param oldPassword the oldPassword to set
162      */
163     @Override
164     public void setOldPassword( byte[] oldPassword )
165     {
166         passwordModifyRequest.setOldPassword( oldPassword );
167     }
168 
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public byte[] getNewPassword()
175     {
176         return passwordModifyRequest.getNewPassword();
177     }
178 
179 
180     /**
181      * @param newPassword the newPassword to set
182      */
183     @Override
184     public void setNewPassword( byte[] newPassword )
185     {
186         passwordModifyRequest.setNewPassword( newPassword );
187     }
188 
189 
190     /**
191      * Compute the PasswordModifyRequest extended operation length
192      * <pre>
193      * 0x30 L1 
194      *   | 
195      *  [+-- 0x80 L2 userIdentity] 
196      *  [+-- 0x81 L3 oldPassword] 
197      *  [+-- 0x82 L4 newPassword] 
198      * </pre>
199      */
200     /* No qualifier */int computeLengthInternal()
201     {
202         requestLength = 0;
203 
204         if ( passwordModifyRequest.getUserIdentity() != null )
205         {
206             int len = passwordModifyRequest.getUserIdentity().length;
207             requestLength = 1 + TLV.getNbBytes( len ) + len;
208         }
209 
210         if ( passwordModifyRequest.getOldPassword() != null )
211         {
212             int len = passwordModifyRequest.getOldPassword().length;
213             requestLength += 1 + TLV.getNbBytes( len ) + len;
214         }
215 
216         if ( passwordModifyRequest.getNewPassword() != null )
217         {
218             int len = passwordModifyRequest.getNewPassword().length;
219             requestLength += 1 + TLV.getNbBytes( len ) + len;
220         }
221 
222         return 1 + TLV.getNbBytes( requestLength ) + requestLength;
223     }
224 
225 
226     /**
227      * Encodes the PasswordModifyRequest extended operation.
228      * 
229      * @return A ByteBuffer that contains the encoded PDU
230      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
231      */
232     /* No qualifier */ByteBuffer encodeInternal() throws EncoderException
233     {
234         ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
235 
236         bb.put( UniversalTag.SEQUENCE.getValue() );
237         bb.put( TLV.getBytes( requestLength ) );
238 
239         if ( passwordModifyRequest.getUserIdentity() != null )
240         {
241             byte[] userIdentity = passwordModifyRequest.getUserIdentity();
242             bb.put( ( byte ) PasswordModifyRequestConstants.USER_IDENTITY_TAG );
243             bb.put( TLV.getBytes( userIdentity.length ) );
244             bb.put( userIdentity );
245         }
246 
247         if ( passwordModifyRequest.getOldPassword() != null )
248         {
249             byte[] oldPassword = passwordModifyRequest.getOldPassword();
250             bb.put( ( byte ) PasswordModifyRequestConstants.OLD_PASSWORD_TAG );
251             bb.put( TLV.getBytes( oldPassword.length ) );
252             bb.put( oldPassword );
253         }
254 
255         if ( passwordModifyRequest.getNewPassword() != null )
256         {
257             byte[] newPassword = passwordModifyRequest.getNewPassword();
258             bb.put( ( byte ) PasswordModifyRequestConstants.NEW_PASSWORD_TAG );
259             bb.put( TLV.getBytes( newPassword.length ) );
260             bb.put( newPassword );
261         }
262 
263         return bb;
264     }
265 }