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