View Javadoc
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  
18  package org.apache.commons.net;
19  
20  import java.io.PrintStream;
21  import java.io.PrintWriter;
22  
23  import org.apache.commons.net.io.Util;
24  
25  /**
26   * This is a support class for some example programs. It is a sample implementation of the ProtocolCommandListener interface which just prints out to a
27   * specified stream all command/reply traffic.
28   *
29   * @since 2.0
30   */
31  public class PrintCommandListener implements ProtocolCommandListener {
32  
33      private final PrintWriter writer;
34      private final boolean nologin;
35      private final char eolMarker;
36  
37      private final boolean directionMarker;
38  
39      /**
40       * Constructs an instance which prints everything using the default Charset.
41       *
42       * @param printStream where to write the commands and responses e.g. System.out
43       * @since 3.0
44       */
45      @SuppressWarnings("resource")
46      public PrintCommandListener(final PrintStream printStream) {
47          this(Util.newPrintWriter(printStream));
48      }
49  
50      /**
51       * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
52       *
53       * @param printStream        where to write the commands and responses
54       * @param suppressLogin if {@code true}, only print command name for login
55       *
56       * @since 3.0
57       */
58      @SuppressWarnings("resource")
59      public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin) {
60          this(Util.newPrintWriter(printStream), suppressLogin);
61      }
62  
63      /**
64       * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
65       *
66       * @param printStream        where to write the commands and responses
67       * @param suppressLogin if {@code true}, only print command name for login
68       * @param eolMarker     if non-zero, add a marker just before the EOL.
69       *
70       * @since 3.0
71       */
72      @SuppressWarnings("resource")
73      public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin, final char eolMarker) {
74          this(Util.newPrintWriter(printStream), suppressLogin, eolMarker);
75      }
76  
77      /**
78       * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
79       *
80       * @param printStream        where to write the commands and responses
81       * @param suppressLogin if {@code true}, only print command name for login
82       * @param eolMarker     if non-zero, add a marker just before the EOL.
83       * @param showDirection if {@code true}, add {@code "> "} or {@code "< "} as appropriate to the output
84       *
85       * @since 3.0
86       */
87      @SuppressWarnings("resource")
88      public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin, final char eolMarker, final boolean showDirection) {
89          this(Util.newPrintWriter(printStream), suppressLogin, eolMarker, showDirection);
90      }
91  
92      /**
93       * Constructs the default instance which prints everything.
94       *
95       * @param writer where to write the commands and responses
96       */
97      public PrintCommandListener(final PrintWriter writer) {
98          this(writer, false);
99      }
100 
101     /**
102      * Constructs an instance which optionally suppresses login command text.
103      *
104      * @param writer        where to write the commands and responses
105      * @param suppressLogin if {@code true}, only print command name for login
106      *
107      * @since 3.0
108      */
109     public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin) {
110         this(writer, suppressLogin, (char) 0);
111     }
112 
113     /**
114      * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
115      *
116      * @param writer        where to write the commands and responses
117      * @param suppressLogin if {@code true}, only print command name for login
118      * @param eolMarker     if non-zero, add a marker just before the EOL.
119      *
120      * @since 3.0
121      */
122     public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin, final char eolMarker) {
123         this(writer, suppressLogin, eolMarker, false);
124     }
125 
126     /**
127      * Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
128      *
129      * @param writer        where to write the commands and responses
130      * @param suppressLogin if {@code true}, only print command name for login
131      * @param eolMarker     if non-zero, add a marker just before the EOL.
132      * @param showDirection if {@code true}, add {@code ">} " or {@code "< "} as appropriate to the output
133      *
134      * @since 3.0
135      */
136     public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin, final char eolMarker, final boolean showDirection) {
137         this.writer = writer;
138         this.nologin = suppressLogin;
139         this.eolMarker = eolMarker;
140         this.directionMarker = showDirection;
141     }
142 
143     private String getPrintableString(final String msg) {
144         if (eolMarker == 0) {
145             return msg;
146         }
147         final int pos = msg.indexOf(SocketClient.NETASCII_EOL);
148         if (pos > 0) {
149             final StringBuilder sb = new StringBuilder();
150             sb.append(msg.substring(0, pos));
151             sb.append(eolMarker);
152             sb.append(msg.substring(pos));
153             return sb.toString();
154         }
155         return msg;
156     }
157 
158     @Override
159     public void protocolCommandSent(final ProtocolCommandEvent event) {
160         if (directionMarker) {
161             writer.print("> ");
162         }
163         if (nologin) {
164             final String cmd = event.getCommand();
165             if ("PASS".equalsIgnoreCase(cmd) || "USER".equalsIgnoreCase(cmd)) {
166                 writer.print(cmd);
167                 writer.println(" *******"); // Don't bother with EOL marker for this!
168             } else {
169                 final String IMAP_LOGIN = "LOGIN";
170                 if (IMAP_LOGIN.equalsIgnoreCase(cmd)) { // IMAP
171                     String msg = event.getMessage();
172                     msg = msg.substring(0, msg.indexOf(IMAP_LOGIN) + IMAP_LOGIN.length());
173                     writer.print(msg);
174                     writer.println(" *******"); // Don't bother with EOL marker for this!
175                 } else {
176                     writer.print(getPrintableString(event.getMessage()));
177                 }
178             }
179         } else {
180             writer.print(getPrintableString(event.getMessage()));
181         }
182         writer.flush();
183     }
184 
185     @Override
186     public void protocolReplyReceived(final ProtocolCommandEvent event) {
187         if (directionMarker) {
188             writer.print("< ");
189         }
190         writer.print(event.getMessage());
191         writer.flush();
192     }
193 }