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.ChangePasswordError;
028    import org.apache.directory.server.kerberos.shared.io.encoder.ErrorMessageEncoder;
029    import org.apache.directory.server.kerberos.shared.messages.ErrorMessage;
030    
031    
032    /**
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev: 546375 $, $Date: 2007-06-12 06:05:00 +0200 (Tue, 12 Jun 2007) $
035     */
036    public class ChangePasswordErrorEncoder
037    {
038        private static final int HEADER_LENGTH = 6;
039    
040    
041        /**
042         * Encodes a {@link ChangePasswordError} into a {@link ByteBuffer}.
043         *
044         * @param buf
045         * @param message
046         * @throws IOException
047         */
048        public void encode( ByteBuffer buf, ChangePasswordError message ) throws IOException
049        {
050            // Build error message bytes
051            ErrorMessage errorMessage = message.getErrorMessage();
052            ErrorMessageEncoder errorEncoder = new ErrorMessageEncoder();
053            byte[] errorBytes = errorEncoder.encode( errorMessage );
054    
055            short messageLength = ( short ) ( HEADER_LENGTH + errorBytes.length );
056            buf.putShort( messageLength );
057    
058            short protocolVersion = 1;
059            buf.putShort( protocolVersion );
060    
061            short zeroIndicatesError = 0;
062            buf.putShort( zeroIndicatesError );
063    
064            buf.put( errorBytes );
065        }
066    }