/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.net;

import java.io.PrintWriter;
import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;

/***
 * This is a support class for some of the example programs.  It is
 * a sample implementation of the ProtocolCommandListener interface
 * which just prints out to a specified stream all command/reply traffic.
 * <p>
 *
 * @since 2.0
 ***/

public class PrintCommandListener implements ProtocolCommandListener
{
    private final PrintWriter __writer;
    private final boolean __nologin;
    private final char __eolMarker;

    /**
     * Create the default instance which prints everything.
     *
     * @param writer where to write the commands and responses
     */
    public PrintCommandListener(PrintWriter writer)
    {
        this(writer, false);
    }

    /**
     * Create an instance which optionally suppresses login command text.
     *
     * @param writer where to write the commands and responses
     * @param suppressLogin if {@code true}, only print command name for login
     *
     * @since 3.0
     */
    public PrintCommandListener(PrintWriter writer, boolean suppressLogin)
    {
        this(writer, suppressLogin, (char) 0);
    }

    /**
     * Create an instance which optionally suppresses login command text
     * and indicates where the EOL starts with the specified character.
     *
     * @param writer where to write the commands and responses
     * @param suppressLogin if {@code true}, only print command name for login
     * @param eolMarker if non-zero, add a marker just before the EOL.
     *
     * @since 3.0
     */
    public PrintCommandListener(PrintWriter writer, boolean suppressLogin, char eolMarker)
    {
        __writer = writer;
        __nologin = suppressLogin;
        __eolMarker = eolMarker;
    }

    public void protocolCommandSent(ProtocolCommandEvent event)
    {
        if (__nologin) {
            String cmd = event.getCommand();
            if ("PASS".equalsIgnoreCase(cmd) || "USER".equalsIgnoreCase(cmd)) {
                __writer.print(cmd);
                __writer.println(" *******"); // Don't bother with EOL marker for this!
            } else {
                __writer.print(getPrintableString(event.getMessage()));
            }
        } else {
            __writer.print(getPrintableString(event.getMessage()));
        }
        __writer.flush();
    }

    private String getPrintableString(String msg){
        if (__eolMarker == 0) {
            return msg;
        }
        int pos = msg.indexOf(SocketClient.NETASCII_EOL);
        if (pos > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(msg.substring(0,pos));
            sb.append(__eolMarker);
            sb.append(msg.substring(pos));
            return sb.toString();
        }
        return msg;
    }
    public void protocolReplyReceived(ProtocolCommandEvent event)
    {
        __writer.print(event.getMessage());
        __writer.flush();
    }
}


@@ -830,16 +830,44 @@
 s.net;%0A%0A
+import java.io.PrintStream;%0A
 import j
@@ -1425,16 +1425,1351 @@
 arker;%0A%0A
+    /**%0A     * Create the default instance which prints everything.%0A     *%0A     * @param stream where to write the commands and responses%0A     * e.g. System.out%0A     * @since 3.0%0A     */%0A    public PrintCommandListener(PrintStream stream)%0A    %7B%0A        this(new PrintWriter(stream));%0A    %7D%0A%0A    /**%0A     * Create an instance which optionally suppresses login command text%0A     * and indicates where the EOL starts with the specified character.%0A     *%0A     * @param stream where to write the commands and responses%0A     * @param suppressLogin if %7B@code true%7D, only print command name for login%0A     *%0A     * @since 3.0%0A     */%0A    public PrintCommandListener(PrintStream stream, boolean suppressLogin) %7B%0A        this(new PrintWriter(stream), suppressLogin);%0A    %7D%0A%0A    /**%0A     * Create an instance which optionally suppresses login command text%0A     * and indicates where the EOL starts with the specified character.%0A     *%0A     * @param stream where to write the commands and responses%0A     * @param suppressLogin if %7B@code true%7D, only print command name for login%0A     * @param eolMarker if non-zero, add a marker just before the EOL.%0A     *%0A     * @since 3.0%0A     */%0A    public PrintCommandListener(PrintStream stream, boolean suppressLogin, char eolMarker) %7B%0A        this(new PrintWriter(stream), suppressLogin, eolMarker);%0A    %7D%0A%0A
     /**%0A

Add versions for a PrintStream (e.g. System.out)