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.protocol;
022    
023    
024    import java.io.IOException;
025    
026    import org.apache.directory.server.changepw.io.ChangePasswordErrorEncoder;
027    import org.apache.directory.server.changepw.io.ChangePasswordReplyEncoder;
028    import org.apache.directory.server.changepw.messages.ChangePasswordError;
029    import org.apache.directory.server.changepw.messages.ChangePasswordReply;
030    import org.apache.mina.core.buffer.IoBuffer;
031    import org.apache.mina.core.session.IoSession;
032    import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
033    import org.apache.mina.filter.codec.ProtocolEncoderOutput;
034    
035    
036    /**
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 725712 $, $Date: 2008-12-11 16:32:04 +0100 (Thu, 11 Dec 2008) $
039     */
040    public class ChangePasswordUdpEncoder extends ProtocolEncoderAdapter
041    {
042        public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws IOException
043        {
044            IoBuffer buf = IoBuffer.allocate( 512 );
045    
046            if ( message instanceof ChangePasswordReply )
047            {
048                encodeReply( ( ChangePasswordReply ) message, buf );
049            }
050            else
051            {
052                if ( message instanceof ChangePasswordError )
053                {
054                    encodeError( ( ChangePasswordError ) message, buf );
055                }
056            }
057    
058            buf.flip();
059    
060            out.write( buf );
061        }
062    
063    
064        private void encodeReply( ChangePasswordReply reply, IoBuffer buf ) throws IOException
065        {
066            ChangePasswordReplyEncoder encoder = new ChangePasswordReplyEncoder();
067    
068            encoder.encode( buf.buf(), reply );
069        }
070    
071    
072        private void encodeError( ChangePasswordError error, IoBuffer buf ) throws IOException
073        {
074            ChangePasswordErrorEncoder encoder = new ChangePasswordErrorEncoder();
075    
076            encoder.encode( buf.buf(), error );
077        }
078    }