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 */
020package org.apache.directory.shared.ldap.codec.protocol.mina;
021
022
023import java.nio.ByteBuffer;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.directory.shared.ldap.codec.api.LdapDecoder;
028import org.apache.directory.shared.ldap.codec.api.LdapMessageContainer;
029import org.apache.directory.shared.ldap.codec.api.MessageDecorator;
030import org.apache.directory.shared.ldap.model.message.Message;
031import org.apache.mina.core.buffer.IoBuffer;
032import org.apache.mina.core.session.IoSession;
033import org.apache.mina.filter.codec.ProtocolDecoder;
034import org.apache.mina.filter.codec.ProtocolDecoderOutput;
035
036
037/**
038 * A LDAP message decoder. It is based on shared-ldap decoder.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class LdapProtocolDecoder implements ProtocolDecoder
043{
044    /** The stateful decoder */
045    private LdapDecoder decoder;
046    
047    
048    /**
049     * Creates a new instance of LdapProtocolEncoder.
050     *
051     * @param codec The LDAP codec service associated with this encoder.
052     */
053    public LdapProtocolDecoder()
054    {
055        this.decoder = new LdapDecoder();
056    }
057
058
059    /**
060     * {@inheritDoc}
061     */
062    public void decode( IoSession session, IoBuffer in, ProtocolDecoderOutput out ) throws Exception
063    {
064        @SuppressWarnings("unchecked")
065        LdapMessageContainer<MessageDecorator<? extends Message>> messageContainer =
066            (LdapMessageContainer<MessageDecorator<? extends Message>> ) 
067            session.getAttribute( "messageContainer" );
068
069        if ( session.containsAttribute( "maxPDUSize" ) )
070        {
071            int maxPDUSize = ( Integer ) session.getAttribute( "maxPDUSize" );
072
073            messageContainer.setMaxPDUSize( maxPDUSize );
074        }
075
076        List<Message> decodedMessages = new ArrayList<Message>();
077        ByteBuffer buf = in.buf();
078
079        decoder.decode( buf, messageContainer, decodedMessages );
080        
081        for ( Message message : decodedMessages )
082        {
083            out.write( message );
084        }
085    }
086
087
088    /**
089     * {@inheritDoc}
090     */
091    public void finishDecode( IoSession session, ProtocolDecoderOutput out ) throws Exception
092    {
093        // Nothing to do
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    public void dispose( IoSession session ) throws Exception
101    {
102        // Nothing to do
103    }
104}