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.shared.asn1.codec;
021
022
023 import java.util.Collection;
024 import java.util.Enumeration;
025 import java.util.Iterator;
026
027 import org.apache.directory.shared.asn1.codec.stateful.EncoderCallback;
028 import org.apache.directory.shared.asn1.codec.stateful.StatefulEncoder;
029 import org.apache.mina.core.buffer.IoBuffer;
030 import org.apache.mina.core.session.IoSession;
031 import org.apache.mina.filter.codec.ProtocolEncoder;
032 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
033
034
035 /**
036 * Adapts {@link StatefulEncoder} to MINA <tt>ProtocolEncoder</tt>
037 *
038 * @author The Apache Directory Project (mina-dev@directory.apache.org)
039 * @version $Rev: 725712 $, $Date: 2008-12-11 16:32:04 +0100 (Jeu, 11 déc 2008) $,
040 */
041 public class Asn1CodecEncoder implements ProtocolEncoder
042 {
043 private final StatefulEncoder encoder;
044 private final EncoderCallbackImpl callback = new EncoderCallbackImpl();
045
046
047 public Asn1CodecEncoder( StatefulEncoder encoder )
048 {
049 encoder.setCallback( callback );
050 this.encoder = encoder;
051 }
052
053
054 public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws EncoderException
055 {
056 callback.encOut = out;
057 encoder.encode( message );
058 }
059
060
061 public void dispose( IoSession session ) throws Exception
062 {
063 }
064
065
066 private class EncoderCallbackImpl implements EncoderCallback
067 {
068 private ProtocolEncoderOutput encOut;
069
070
071 public void encodeOccurred( StatefulEncoder codec, Object encoded )
072 {
073 if( encoded instanceof java.nio.ByteBuffer )
074 {
075 java.nio.ByteBuffer buf = ( java.nio.ByteBuffer ) encoded;
076 IoBuffer wrappedBuf = IoBuffer.wrap( buf );
077 encOut.write( wrappedBuf );
078 }
079 else if( encoded instanceof Object[] )
080 {
081 Object[] bufArray = ( Object[] ) encoded;
082 for ( Object buf : bufArray )
083 {
084 this.encodeOccurred( codec, buf );
085 }
086
087 encOut.mergeAll();
088 }
089 else if( encoded instanceof Iterator )
090 {
091 Iterator it = ( Iterator ) encoded;
092 while( it.hasNext() )
093 {
094 this.encodeOccurred( codec, it.next() );
095 }
096
097 encOut.mergeAll();
098 }
099 else if( encoded instanceof Collection )
100 {
101 for ( Object o : ( ( Collection ) encoded ) )
102 {
103 this.encodeOccurred( codec, o );
104 }
105
106 encOut.mergeAll();
107 }
108 else if( encoded instanceof Enumeration )
109 {
110 Enumeration e = ( Enumeration ) encoded;
111 while( e.hasMoreElements() )
112 {
113 this.encodeOccurred( codec, e.nextElement() );
114 }
115
116 encOut.mergeAll();
117 }
118 else
119 {
120 throw new IllegalArgumentException(
121 "Encoded result is not a ByteBuffer: " +
122 encoded.getClass() );
123 }
124 }
125 }
126 }