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    package org.apache.directory.server.changepw.io;
021    
022    
023    import java.io.IOException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.server.changepw.messages.ChangePasswordRequest;
027    import org.apache.directory.server.kerberos.shared.io.encoder.ApplicationRequestEncoder;
028    import org.apache.directory.server.kerberos.shared.io.encoder.PrivateMessageEncoder;
029    import org.apache.directory.server.kerberos.shared.messages.ApplicationRequest;
030    import org.apache.directory.server.kerberos.shared.messages.application.PrivateMessage;
031    
032    
033    /**
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$, $Date$
036     */
037    public class ChangePasswordRequestEncoder
038    {
039        private static final int HEADER_LENGTH = 6;
040    
041    
042        /**
043         * Encodes a {@link ChangePasswordRequest} into a {@link ByteBuffer}.
044         *
045         * @param buf
046         * @param message
047         * @throws IOException
048         */
049        public void encode( ByteBuffer buf, ChangePasswordRequest message ) throws IOException
050        {
051            // Build application request bytes
052            ApplicationRequest appRequest = message.getAuthHeader();
053            ApplicationRequestEncoder appEncoder = new ApplicationRequestEncoder();
054            byte[] encodedAppRequest = appEncoder.encode( appRequest );
055    
056            // Build private message bytes
057            PrivateMessage privateMessage = message.getPrivateMessage();
058            PrivateMessageEncoder privateEncoder = new PrivateMessageEncoder();
059            byte[] privateBytes = privateEncoder.encode( privateMessage );
060    
061            short messageLength = ( short ) ( HEADER_LENGTH + encodedAppRequest.length + privateBytes.length );
062    
063            short protocolVersion = 1;
064    
065            buf.putShort( messageLength );
066            buf.putShort( protocolVersion );
067            buf.putShort( ( short ) encodedAppRequest.length );
068    
069            buf.put( encodedAppRequest );
070            buf.put( privateBytes );
071        }
072    }