PrintCommandListener.java

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

  17. package org.apache.commons.net;

  18. import java.io.PrintStream;
  19. import java.io.PrintWriter;

  20. import org.apache.commons.net.io.Util;

  21. /**
  22.  * This is a support class for some example programs. It is a sample implementation of the ProtocolCommandListener interface which just prints out to a
  23.  * specified stream all command/reply traffic.
  24.  *
  25.  * @since 2.0
  26.  */
  27. public class PrintCommandListener implements ProtocolCommandListener {

  28.     private final PrintWriter writer;
  29.     private final boolean nologin;
  30.     private final char eolMarker;

  31.     private final boolean directionMarker;

  32.     /**
  33.      * Constructs an instance which prints everything using the default Charset.
  34.      *
  35.      * @param printStream where to write the commands and responses e.g. System.out
  36.      * @since 3.0
  37.      */
  38.     @SuppressWarnings("resource")
  39.     public PrintCommandListener(final PrintStream printStream) {
  40.         this(Util.newPrintWriter(printStream));
  41.     }

  42.     /**
  43.      * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
  44.      *
  45.      * @param printStream        where to write the commands and responses
  46.      * @param suppressLogin if {@code true}, only print command name for login
  47.      *
  48.      * @since 3.0
  49.      */
  50.     @SuppressWarnings("resource")
  51.     public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin) {
  52.         this(Util.newPrintWriter(printStream), suppressLogin);
  53.     }

  54.     /**
  55.      * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
  56.      *
  57.      * @param printStream        where to write the commands and responses
  58.      * @param suppressLogin if {@code true}, only print command name for login
  59.      * @param eolMarker     if non-zero, add a marker just before the EOL.
  60.      *
  61.      * @since 3.0
  62.      */
  63.     @SuppressWarnings("resource")
  64.     public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin, final char eolMarker) {
  65.         this(Util.newPrintWriter(printStream), suppressLogin, eolMarker);
  66.     }

  67.     /**
  68.      * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
  69.      *
  70.      * @param printStream        where to write the commands and responses
  71.      * @param suppressLogin if {@code true}, only print command name for login
  72.      * @param eolMarker     if non-zero, add a marker just before the EOL.
  73.      * @param showDirection if {@code true}, add {@code "> "} or {@code "< "} as appropriate to the output
  74.      *
  75.      * @since 3.0
  76.      */
  77.     @SuppressWarnings("resource")
  78.     public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin, final char eolMarker, final boolean showDirection) {
  79.         this(Util.newPrintWriter(printStream), suppressLogin, eolMarker, showDirection);
  80.     }

  81.     /**
  82.      * Constructs the default instance which prints everything.
  83.      *
  84.      * @param writer where to write the commands and responses
  85.      */
  86.     public PrintCommandListener(final PrintWriter writer) {
  87.         this(writer, false);
  88.     }

  89.     /**
  90.      * Constructs an instance which optionally suppresses login command text.
  91.      *
  92.      * @param writer        where to write the commands and responses
  93.      * @param suppressLogin if {@code true}, only print command name for login
  94.      *
  95.      * @since 3.0
  96.      */
  97.     public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin) {
  98.         this(writer, suppressLogin, (char) 0);
  99.     }

  100.     /**
  101.      * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
  102.      *
  103.      * @param writer        where to write the commands and responses
  104.      * @param suppressLogin if {@code true}, only print command name for login
  105.      * @param eolMarker     if non-zero, add a marker just before the EOL.
  106.      *
  107.      * @since 3.0
  108.      */
  109.     public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin, final char eolMarker) {
  110.         this(writer, suppressLogin, eolMarker, false);
  111.     }

  112.     /**
  113.      * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
  114.      *
  115.      * @param writer        where to write the commands and responses
  116.      * @param suppressLogin if {@code true}, only print command name for login
  117.      * @param eolMarker     if non-zero, add a marker just before the EOL.
  118.      * @param showDirection if {@code true}, add {@code ">} " or {@code "< "} as appropriate to the output
  119.      *
  120.      * @since 3.0
  121.      */
  122.     public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin, final char eolMarker, final boolean showDirection) {
  123.         this.writer = writer;
  124.         this.nologin = suppressLogin;
  125.         this.eolMarker = eolMarker;
  126.         this.directionMarker = showDirection;
  127.     }

  128.     private String getPrintableString(final String msg) {
  129.         if (eolMarker == 0) {
  130.             return msg;
  131.         }
  132.         final int pos = msg.indexOf(SocketClient.NETASCII_EOL);
  133.         if (pos > 0) {
  134.             final StringBuilder sb = new StringBuilder();
  135.             sb.append(msg.substring(0, pos));
  136.             sb.append(eolMarker);
  137.             sb.append(msg.substring(pos));
  138.             return sb.toString();
  139.         }
  140.         return msg;
  141.     }

  142.     @Override
  143.     public void protocolCommandSent(final ProtocolCommandEvent event) {
  144.         if (directionMarker) {
  145.             writer.print("> ");
  146.         }
  147.         if (nologin) {
  148.             final String cmd = event.getCommand();
  149.             if ("PASS".equalsIgnoreCase(cmd) || "USER".equalsIgnoreCase(cmd)) {
  150.                 writer.print(cmd);
  151.                 writer.println(" *******"); // Don't bother with EOL marker for this!
  152.             } else {
  153.                 final String IMAP_LOGIN = "LOGIN";
  154.                 if (IMAP_LOGIN.equalsIgnoreCase(cmd)) { // IMAP
  155.                     String msg = event.getMessage();
  156.                     msg = msg.substring(0, msg.indexOf(IMAP_LOGIN) + IMAP_LOGIN.length());
  157.                     writer.print(msg);
  158.                     writer.println(" *******"); // Don't bother with EOL marker for this!
  159.                 } else {
  160.                     writer.print(getPrintableString(event.getMessage()));
  161.                 }
  162.             }
  163.         } else {
  164.             writer.print(getPrintableString(event.getMessage()));
  165.         }
  166.         writer.flush();
  167.     }

  168.     @Override
  169.     public void protocolReplyReceived(final ProtocolCommandEvent event) {
  170.         if (directionMarker) {
  171.             writer.print("< ");
  172.         }
  173.         writer.print(event.getMessage());
  174.         writer.flush();
  175.     }
  176. }