001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    
021    package org.apache.directory.server.changepw.io;
022    
023    
024    import java.io.IOException;
025    import java.nio.ByteBuffer;
026    
027    import org.apache.directory.server.changepw.messages.ChangePasswordReply;
028    import org.apache.directory.server.kerberos.shared.io.encoder.ApplicationReplyEncoder;
029    import org.apache.directory.server.kerberos.shared.io.encoder.PrivateMessageEncoder;
030    import org.apache.directory.server.kerberos.shared.messages.application.ApplicationReply;
031    import org.apache.directory.server.kerberos.shared.messages.application.PrivateMessage;
032    
033    
034    /**
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 546375 $, $Date: 2007-06-12 06:05:00 +0200 (Tue, 12 Jun 2007) $
037     */
038    public class ChangePasswordReplyEncoder
039    {
040        private static final int HEADER_LENGTH = 6;
041    
042    
043        /**
044         * Encodes a {@link ChangePasswordReply} into a {@link ByteBuffer}.
045         *
046         * @param buf
047         * @param message
048         * @throws IOException
049         */
050        public void encode( ByteBuffer buf, ChangePasswordReply message ) throws IOException
051        {
052            // Build application reply bytes
053            ApplicationReply appReply = message.getApplicationReply();
054            ApplicationReplyEncoder appEncoder = new ApplicationReplyEncoder();
055            byte[] encodedAppReply = appEncoder.encode( appReply );
056    
057            // Build private message bytes
058            PrivateMessage privateMessage = message.getPrivateMessage();
059            PrivateMessageEncoder privateEncoder = new PrivateMessageEncoder();
060            byte[] privateBytes = privateEncoder.encode( privateMessage );
061    
062            short messageLength = ( short ) ( HEADER_LENGTH + encodedAppReply.length + privateBytes.length );
063    
064            short protocolVersion = 1;
065    
066            buf.putShort( messageLength );
067            buf.putShort( protocolVersion );
068            buf.putShort( ( short ) encodedAppReply.length );
069    
070            buf.put( encodedAppReply );
071            buf.put( privateBytes );
072        }
073    }