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