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.ChangePasswordRequest;
27  import org.apache.directory.server.kerberos.shared.io.encoder.ApplicationRequestEncoder;
28  import org.apache.directory.server.kerberos.shared.io.encoder.PrivateMessageEncoder;
29  import org.apache.directory.server.kerberos.shared.messages.ApplicationRequest;
30  import org.apache.directory.server.kerberos.shared.messages.application.PrivateMessage;
31  
32  
33  /**
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev$, $Date$
36   */
37  public class ChangePasswordRequestEncoder
38  {
39      private static final int HEADER_LENGTH = 6;
40  
41  
42      /**
43       * Encodes a {@link ChangePasswordRequest} into a {@link ByteBuffer}.
44       *
45       * @param buf
46       * @param message
47       * @throws IOException
48       */
49      public void encode( ByteBuffer buf, ChangePasswordRequest message ) throws IOException
50      {
51          // Build application request bytes
52          ApplicationRequest appRequest = message.getAuthHeader();
53          ApplicationRequestEncoder appEncoder = new ApplicationRequestEncoder();
54          byte[] encodedAppRequest = appEncoder.encode( appRequest );
55  
56          // Build private message bytes
57          PrivateMessage privateMessage = message.getPrivateMessage();
58          PrivateMessageEncoder privateEncoder = new PrivateMessageEncoder();
59          byte[] privateBytes = privateEncoder.encode( privateMessage );
60  
61          short messageLength = ( short ) ( HEADER_LENGTH + encodedAppRequest.length + privateBytes.length );
62  
63          short protocolVersion = 1;
64  
65          buf.putShort( messageLength );
66          buf.putShort( protocolVersion );
67          buf.putShort( ( short ) encodedAppRequest.length );
68  
69          buf.put( encodedAppRequest );
70          buf.put( privateBytes );
71      }
72  }