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.changepw.messages.ChangePasswordRequestModifier;
028    import org.apache.directory.server.kerberos.shared.io.decoder.ApplicationRequestDecoder;
029    import org.apache.directory.server.kerberos.shared.io.decoder.PrivateMessageDecoder;
030    import org.apache.directory.server.kerberos.shared.messages.ApplicationRequest;
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: 557127 $, $Date: 2007-07-18 05:02:51 +0200 (Wed, 18 Jul 2007) $
037     */
038    public class ChangePasswordRequestDecoder
039    {
040        /**
041         * Decodes a {@link ByteBuffer} into a {@link ChangePasswordRequest}.
042         *
043         * @param buf
044         * @return The {@link ChangePasswordRequest}.
045         * @throws IOException
046         */
047        public ChangePasswordRequest decode( ByteBuffer buf ) throws IOException
048        {
049            ChangePasswordRequestModifier modifier = new ChangePasswordRequestModifier();
050    
051            buf.getShort(); // message length
052    
053            modifier.setProtocolVersionNumber( buf.getShort() );
054    
055            short authHeaderLength = buf.getShort();
056    
057            byte[] undecodedAuthHeader = new byte[authHeaderLength];
058            buf.get( undecodedAuthHeader, 0, authHeaderLength );
059    
060            ApplicationRequestDecoder decoder = new ApplicationRequestDecoder();
061            ApplicationRequest authHeader = decoder.decode( undecodedAuthHeader );
062    
063            modifier.setAuthHeader( authHeader );
064    
065            byte[] encodedPrivate = new byte[buf.remaining()];
066            buf.get( encodedPrivate, 0, buf.remaining() );
067    
068            PrivateMessageDecoder privateDecoder = new PrivateMessageDecoder();
069            PrivateMessage privMessage = privateDecoder.decode( encodedPrivate );
070    
071            modifier.setPrivateMessage( privMessage );
072    
073            return modifier.getChangePasswordMessage();
074        }
075    }