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.api;
21  
22  
23  import java.io.InputStream;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.DecoderException;
27  import org.apache.directory.api.asn1.ber.Asn1Decoder;
28  import org.apache.directory.api.asn1.ber.tlv.TLVStateEnum;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.model.message.Message;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * The LdapDecoder decodes ASN.1 BER encoded PDUs into LDAP messages
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class LdapDecoder
41  {
42      /** The logger */
43      private static final Logger LOG = LoggerFactory.getLogger( LdapDecoder.class );
44  
45      /** The name of the LdapSession's attribute for the LDAP container used during the decoding */
46      public static final String MESSAGE_CONTAINER_ATTR = "LDAP-container";
47  
48      /** The maximum PDU size, stored into the LDAPSession's attribute */
49      public static final String MAX_PDU_SIZE_ATTR = "LDAP-maxPduSize";
50  
51  
52      /**
53       * Creates an instance of a Ldap Decoder implementation.
54       */
55      public LdapDecoder()
56      {
57      }
58  
59  
60      /**
61       * Decodes a PDU from an input stream into a Ldap message container. We can only
62       * decode one complete message.
63       *
64       * @param in The input stream to read and decode PDU bytes from
65       * @param container The LdapMessageContainer containing the message to decode
66       * @return return The decoded message
67       * @throws DecoderException If the decoding failed
68       */
69      public Message decode( InputStream in, LdapMessageContainer<? extends Message> container )
70          throws DecoderException
71      {
72          try
73          {
74              int amount;
75  
76              while ( in.available() > 0 )
77              {
78                  byte[] buf = new byte[in.available()];
79  
80                  amount = in.read( buf );
81                  
82                  if ( amount == -1 )
83                  {
84                      break;
85                  }
86  
87                  Asn1Decoder.decode( ByteBuffer.wrap( buf, 0, amount ), container );
88              }
89          }
90          catch ( Exception e )
91          {
92              String message = I18n.err( I18n.ERR_05204_LDAP_DECODER_FAILURE, e.getLocalizedMessage() );
93              LOG.error( message );
94              throw new DecoderException( message, e );
95          }
96  
97          if ( container.getState() == TLVStateEnum.PDU_DECODED )
98          {
99              if ( LOG.isDebugEnabled() )
100             {
101                 LOG.debug( I18n.msg( I18n.MSG_5200_DECODED_LDAP_MESSAGE, container ) );
102             }
103 
104             return container.getMessage();
105         }
106         else
107         {
108             LOG.error( I18n.err( I18n.ERR_05205_PDU_DOES_NOT_CONTAIN_ENOUGH_DATA ) );
109             throw new DecoderException( I18n.err( I18n.ERR_05206_INPUT_STREAM_TOO_SHORT_PDU ) );
110         }
111     }
112 }