View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.codec.protocol.mina;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.EncoderException;
26  import org.apache.directory.api.asn1.util.Asn1Buffer;
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.codec.api.LdapApiService;
29  import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
30  import org.apache.directory.api.ldap.codec.api.LdapEncoder;
31  import org.apache.directory.api.ldap.model.constants.Loggers;
32  import org.apache.directory.api.ldap.model.message.Message;
33  import org.apache.directory.api.util.Strings;
34  import org.apache.mina.core.buffer.IoBuffer;
35  import org.apache.mina.core.session.IoSession;
36  import org.apache.mina.filter.codec.ProtocolEncoder;
37  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  
42  /**
43   * A LDAP message encoder. It is based on api-ldap encoder.
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  public class LdapProtocolEncoder implements ProtocolEncoder
48  {
49      /** logger for reporting errors that might not be handled properly upstream */
50      private static final Logger CODEC_LOG = LoggerFactory.getLogger( Loggers.CODEC_LOG.getName() );
51  
52      /** The LDAP API Service instance */
53      private LdapApiService codec;
54      
55      /** A thread local storage used to store the Asn1Buffer instance */
56      private ThreadLocal<Asn1Buffer> threadLocalStorage = new ThreadLocal<>();
57      
58      /**
59       * Creates a new instance of LdapProtocolEncoder.
60       */
61      public LdapProtocolEncoder()
62      {
63          this( LdapApiServiceFactory.getSingleton() );
64      }
65  
66      /**
67       * Creates a new instance of LdapProtocolEncoder.
68       *
69       * @param ldapApiService The Service to use
70       */
71      public LdapProtocolEncoder( LdapApiService ldapApiService )
72      {
73          codec = ldapApiService;
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void encode( IoSession session, Object message, ProtocolEncoderOutput out ) throws Exception
82      {
83          Asn1Buffer asn1Buffer = threadLocalStorage.get();
84          
85          if ( asn1Buffer == null )
86          {
87              asn1Buffer = new Asn1Buffer();
88              threadLocalStorage.set( asn1Buffer );
89          }
90  
91          ByteBuffer encoded;
92          
93          try
94          { 
95              LdapEncoder.encodeMessage( asn1Buffer, codec, ( Message ) message );
96              encoded = asn1Buffer.getBytes();
97          }
98          catch ( EncoderException e )
99          {
100             CODEC_LOG.error( I18n.err( I18n.ERR_14000_ERROR_ENCODING_MESSAGE, message, e.getMessage() ) );
101             throw e;
102         }
103         finally 
104         {
105             asn1Buffer.clear();
106         }
107         
108         IoBuffer ioBuffer = IoBuffer.wrap( encoded );
109     
110         if ( CODEC_LOG.isDebugEnabled() )
111         {
112             byte[] dumpBuffer = new byte[encoded.limit()];
113             encoded.get( dumpBuffer );
114             encoded.flip();
115             CODEC_LOG.debug( I18n.msg( I18n.MSG_14003_ENCODED_LDAP_MESSAGE, message, Strings.dumpBytes( dumpBuffer ) ) );
116         }
117 
118         out.write( ioBuffer );
119     }
120 
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public void dispose( IoSession session ) throws Exception
127     {
128         // Nothing to do
129     }
130 }