1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.wallace;
20
21 /***
22 * This class represents a tagged or untagged server response to a command.
23 *
24 * A response is tagged if a constructor including a Message is used, it is
25 * untagged otherwise.
26 *
27 * @author rnewson
28 */
29 public final class ServerResponse {
30
31 public enum Type {
32 UNTAGGED("*"), CONTINUATION("+");
33
34 private final String string;
35
36 Type(String asString) {
37 string = asString;
38 }
39
40 public String toString() {
41 return string;
42 }
43 }
44
45 private static final String CRLF = "\r\n";
46
47 private final ClientMessage message;
48
49 private final Type type;
50
51 private final String responseText;
52
53 /***
54 * A tagged response with additional response text.
55 */
56 public ServerResponse(final ClientMessage newMessage, final String newResponseText) {
57 message = newMessage;
58 type = null;
59 responseText = newResponseText;
60 }
61
62 /***
63 * A tagged response with additional response text.
64 */
65 public ServerResponse(final Type newType, final String newResponseText) {
66 message = null;
67 type = newType;
68 responseText = newResponseText;
69 }
70
71 public String toString() {
72 final StringBuffer buffer = new StringBuffer(512);
73 if (message == null) {
74 buffer.append(type);
75 } else {
76 buffer.append(message.getTag());
77 }
78 buffer.append(' ');
79 buffer.append(responseText);
80 buffer.append(CRLF);
81 return buffer.toString();
82 }
83 }