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.ASN1Writer;
031 import org.opends.server.protocols.asn1.ASN1Element;
032 import org.opends.server.protocols.ldap.LDAPMessage;
033
034 import java.net.Socket;
035 import java.io.IOException;
036
037 /**
038 * This class defines a utility that can be used to write LDAP messages over a
039 * provided socket.
040 */
041 public class LDAPWriter
042 {
043 ASN1Writer asn1Writer;
044 VerboseTracer tracer;
045
046 /**
047 * Creates a new LDAP writer that will write messages to the provided
048 * socket.
049 *
050 * @param socket The socket to use to write LDAP messages.
051 *
052 * @throws IOException If a problem occurs while attempting to obtain an
053 * ASN.1 reader for the socket.
054 */
055 public LDAPWriter(Socket socket)
056 throws IOException
057 {
058 this(socket, null);
059 }
060
061
062 /**
063 * Creates a new LDAP writer that will write messages to the provided
064 * socket and trace the messages using a provided tracer.
065 *
066 * @param socket The socket to use to write LDAP messages.
067 *
068 * @param tracer Specifies a tracer to be used for tracing messages written.
069 *
070 * @throws IOException If a problem occurs while attempting to obtain an
071 * output stream for the socket.
072 */
073 public LDAPWriter(Socket socket, VerboseTracer tracer)
074 throws IOException
075 {
076 this.asn1Writer = new ASN1Writer(socket);
077 this.tracer = tracer;
078 }
079
080 /**
081 * Writes an LDAP message to the associated output stream.
082 *
083 * @param message The message to be written.
084 *
085 * @throws IOException If a problem occurs while trying to write the
086 * information over the output stream.
087 */
088 public void writeMessage(LDAPMessage message)
089 throws IOException
090 {
091 ASN1Element element = message.encode();
092 if (tracer != null)
093 {
094 tracer.traceOutgoingMessage(message, element);
095 }
096 asn1Writer.writeElement(element);
097 }
098
099 /**
100 * Closes this LDAP writer and the underlying socket.
101 */
102 public void close()
103 {
104 asn1Writer.close();
105 }
106
107 /**
108 * Get the underlying ASN1 writer.
109 *
110 * @return The underlying ASN1 writer.
111 */
112 public ASN1Writer getASN1Writer()
113 {
114 return asn1Writer;
115 }
116
117 }