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.server.changepw.io;
21  
22  
23  import java.io.IOException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.server.changepw.messages.ChangePasswordReply;
27  import org.apache.directory.server.changepw.messages.ChangePasswordReplyModifier;
28  import org.apache.directory.server.kerberos.shared.io.decoder.ApplicationReplyDecoder;
29  import org.apache.directory.server.kerberos.shared.io.decoder.PrivateMessageDecoder;
30  import org.apache.directory.server.kerberos.shared.messages.application.ApplicationReply;
31  import org.apache.directory.server.kerberos.shared.messages.application.PrivateMessage;
32  
33  
34  /**
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev$, $Date$
37   */
38  public class ChangePasswordReplyDecoder
39  {
40      private static final int HEADER_LENGTH = 6;
41  
42  
43      /**
44       * Decodes a {@link ByteBuffer} into a {@link ChangePasswordReply}.
45       *
46       * @param buf
47       * @return The {@link ChangePasswordReply}.
48       * @throws IOException
49       */
50      public ChangePasswordReply decode( ByteBuffer buf ) throws IOException
51      {
52          ChangePasswordReplyModifier modifier = new ChangePasswordReplyModifier();
53  
54          short messageLength = buf.getShort();
55          short protocolVersion = buf.getShort();
56          short encodedAppReplyLength = buf.getShort();
57  
58          modifier.setProtocolVersionNumber( protocolVersion );
59  
60          byte[] encodedAppReply = new byte[encodedAppReplyLength];
61          buf.get( encodedAppReply );
62  
63          ApplicationReplyDecoder appDecoder = new ApplicationReplyDecoder();
64          ApplicationReply applicationReply = appDecoder.decode( encodedAppReply );
65          modifier.setApplicationReply( applicationReply );
66  
67          int privateBytesLength = messageLength - HEADER_LENGTH - encodedAppReplyLength;
68          byte[] encodedPrivateMessage = new byte[privateBytesLength];
69          buf.get( encodedPrivateMessage );
70  
71          PrivateMessageDecoder privateDecoder = new PrivateMessageDecoder();
72          PrivateMessage privateMessage = privateDecoder.decode( encodedPrivateMessage );
73          modifier.setPrivateMessage( privateMessage );
74  
75          return modifier.getChangePasswordReply();
76      }
77  }