View Javadoc

1   /*
2    * Wallace IMAP Server
3    * Copyright (C) 2004  Robert Newson
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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  }