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 legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2009 Sun Microsystems, Inc. 025 * Portions Copyright 2014 ForgeRock AS 026 */ 027package org.opends.server.tools; 028 029import java.io.BufferedOutputStream; 030import java.io.Closeable; 031import java.io.IOException; 032import java.net.Socket; 033 034import org.forgerock.i18n.slf4j.LocalizedLogger; 035import org.forgerock.opendj.io.ASN1; 036import org.forgerock.opendj.io.ASN1Writer; 037import org.forgerock.opendj.ldap.ByteString; 038import org.opends.server.protocols.ldap.LDAPMessage; 039import org.opends.server.types.RecordingOutputStream; 040import org.opends.server.util.ServerConstants; 041import org.opends.server.util.StaticUtils; 042 043/** 044 * This class defines a utility that can be used to write LDAP messages over a 045 * provided socket. 046 */ 047public class LDAPWriter implements Closeable 048{ 049 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 050 051 private Socket socket; 052 private ASN1Writer asn1Writer; 053 private RecordingOutputStream debugOutputStream; 054 055 056 /** 057 * Creates a new LDAP writer that will write messages to the provided 058 * socket and trace the messages using a provided tracer. 059 * 060 * @param socket The socket to use to write LDAP messages. 061 * 062 * @throws IOException If a problem occurs while attempting to obtain an 063 * output stream for the socket. 064 */ 065 public LDAPWriter(Socket socket) 066 throws IOException 067 { 068 this.socket = socket; 069 this.debugOutputStream = 070 new RecordingOutputStream( 071 new BufferedOutputStream(socket.getOutputStream(), 4096)); 072 this.asn1Writer = ASN1.getWriter(debugOutputStream); 073 } 074 075 /** 076 * Writes an LDAP message to the associated output stream. 077 * 078 * @param message The message to be written. 079 * 080 * @throws IOException If a problem occurs while trying to write the 081 * information over the output stream. 082 */ 083 public void writeMessage(LDAPMessage message) 084 throws IOException 085 { 086 if(logger.isTraceEnabled()) 087 { 088 logger.trace(message.toString()); 089 debugOutputStream.setRecordingEnabled(true); 090 } 091 092 message.write(asn1Writer); 093 asn1Writer.flush(); 094 095 if(debugOutputStream.isRecordingEnabled()) 096 { 097 ByteString bytesRead = debugOutputStream.getRecordedBytes(); 098 debugOutputStream.clearRecordedBytes(); 099 100 logger.trace("bytes written to wire(len=" + bytesRead.length() + "):" 101 + ServerConstants.EOL + bytesRead.toHexPlusAsciiString(4)); 102 } 103 } 104 105 /** 106 * Closes this LDAP writer and the underlying socket. 107 */ 108 @Override 109 public void close() 110 { 111 StaticUtils.close(asn1Writer, debugOutputStream); 112 StaticUtils.close(socket); 113 } 114}