001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.tools;
029    
030    import org.opends.server.protocols.asn1.ASN1Reader;
031    import org.opends.server.protocols.asn1.ASN1Element;
032    import org.opends.server.protocols.asn1.ASN1Exception;
033    import org.opends.server.protocols.asn1.ASN1Sequence;
034    import org.opends.server.protocols.ldap.LDAPMessage;
035    import org.opends.server.types.LDAPException;
036    
037    import java.io.IOException;
038    import java.net.Socket;
039    
040    /**
041     * This class defines a utility that can be used to read LDAP messages from a
042     * provided socket.
043     */
044    public class LDAPReader
045    {
046      private ASN1Reader asn1Reader;
047      private VerboseTracer tracer;
048    
049      /**
050       * Creates a new LDAP reader that will read messages from the provided
051       * socket.
052       *
053       * @param  socket  The socket from which to read the LDAP messages.
054       *
055       * @throws  IOException  If a problem occurs while attempting to obtain an
056       *                       ASN.1 reader for the socket.
057       */
058      public LDAPReader(Socket socket)
059           throws IOException
060      {
061        this(socket, null);
062      }
063    
064      /**
065       * Creates a new LDAP reader that will read messages from the provided
066       * socket and trace the messages using a provided tracer.
067       *
068       * @param  socket   The socket from which to read the LDAP messages.
069       *
070       * @param  tracer   Specifies a tracer to be used for tracing messages read.
071       *
072       * @throws  IOException  If a problem occurs while attempting to obtain an
073       *                       input stream for the socket.
074       */
075      public LDAPReader(Socket socket, VerboseTracer tracer)
076           throws IOException
077      {
078        this.asn1Reader = new ASN1Reader(socket);
079        this.tracer = tracer;
080      }
081    
082      /**
083       * Reads an LDAP message from the associated input stream.
084       *
085       * @return  The LDAP message read from the associated input stream, or
086       *          <CODE>null</CODE> if the end of the stream has been reached.
087       *
088       * @throws  IOException  If a problem occurs while attempting to read from the
089       *                       input stream.
090       *
091       * @throws  ASN1Exception  If a problem occurs while attempting to decode the
092       *                         data read as an ASN.1 sequence.
093    
094       * @throws  LDAPException  If a problem occurs while attempting to decode the
095       *                         LDAP message.
096       */
097      public LDAPMessage readMessage()
098           throws IOException, ASN1Exception, LDAPException
099      {
100        ASN1Element element = asn1Reader.readElement();
101        if (element == null)
102        {
103          return null;
104        }
105    
106        ASN1Sequence sequence = ASN1Sequence.decodeAsSequence(element);
107        LDAPMessage message = LDAPMessage.decode(sequence);
108        if (tracer != null)
109        {
110          tracer.traceIncomingMessage(message, sequence);
111        }
112    
113        return message;
114      }
115    
116      /**
117       * Closes this LDAP reader and the underlying socket.
118       */
119      public void close()
120      {
121        asn1Reader.close();
122      }
123    
124      /**
125       * Get the underlying ASN1 reader.
126       *
127       * @return  The underlying ASN1 reader.
128       */
129      public ASN1Reader getASN1Reader()
130      {
131        return asn1Reader;
132      }
133    
134    
135    }