1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29
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
41
42
43
44
45 @SuppressWarnings("resource")
46 public PrintCommandListener(final PrintStream printStream) {
47 this(Util.newPrintWriter(printStream));
48 }
49
50
51
52
53
54
55
56
57
58 @SuppressWarnings("resource")
59 public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin) {
60 this(Util.newPrintWriter(printStream), suppressLogin);
61 }
62
63
64
65
66
67
68
69
70
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
79
80
81
82
83
84
85
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
94
95
96
97 public PrintCommandListener(final PrintWriter writer) {
98 this(writer, false);
99 }
100
101
102
103
104
105
106
107
108
109 public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin) {
110 this(writer, suppressLogin, (char) 0);
111 }
112
113
114
115
116
117
118
119
120
121
122 public PrintCommandListener(final PrintWriter writer, final boolean suppressLogin, final char eolMarker) {
123 this(writer, suppressLogin, eolMarker, false);
124 }
125
126
127
128
129
130
131
132
133
134
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(" *******");
168 } else {
169 final String IMAP_LOGIN = "LOGIN";
170 if (IMAP_LOGIN.equalsIgnoreCase(cmd)) {
171 String msg = event.getMessage();
172 msg = msg.substring(0, msg.indexOf(IMAP_LOGIN) + IMAP_LOGIN.length());
173 writer.print(msg);
174 writer.println(" *******");
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 }