View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.imap.response;
14  
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import org.abstracthorizon.mercury.imap.IMAPSession;
18  
19  /**
20   * IMAP response
21   *
22   * @author Daniel Sendula
23   */
24  public abstract class Response {
25  
26      public static final int TAGGED_RESPONSE = 1;
27      public static final int UNTAGGED_RESPONSE = 2;
28      public static final int CONTINUATION_RESPONSE = 3;
29  
30      /** IMAP session */
31      protected IMAPSession session;
32  
33      /** Message */
34      private StringBuffer msg = new StringBuffer();
35  
36      /**
37       * Response
38       *
39       * @param session session
40       * @param type type of response
41       */
42      public Response(IMAPSession session, int type) {
43          this.session = session;
44          if (type == TAGGED_RESPONSE) {
45              this.msg.append(session.getTag()).append(' ');
46          } else if (type == UNTAGGED_RESPONSE) {
47              this.msg.append("* ");
48          } else if (type == CONTINUATION_RESPONSE) {
49              this.msg.append("+ ");
50          }
51      }
52  
53      /**
54       * Response with message
55       *
56       * @param session session
57       * @param type type of response
58       * @param msg message
59       */
60      public Response(IMAPSession session, int type, String msg) {
61          this(session, type);
62          this.msg.append(msg);
63      }
64  
65      /**
66       * Appends string to response
67       * @param string string to be appended
68       * @return this response
69       */
70      public Response append(String string) {
71          msg.append(string);
72          return this;
73      }
74  
75      /**
76       * Appends content string buffer
77       * @param sbuf string buffer
78       * @return this response
79       */
80      public Response append(StringBuffer sbuf) {
81          msg.append(sbuf);
82          return this;
83      }
84  
85      /**
86       * Appends integer
87       * @param i integer
88       * @return this response
89       */
90      public Response append(int i) {
91          msg.append(i);
92          return this;
93      }
94  
95      /**
96       * Appends long number
97       * @param l long
98       * @return this response
99       */
100     public Response append(long l) {
101         msg.append(l);
102         return this;
103     }
104 
105     /**
106      * Appends char
107      * @param c char
108      * @return this response
109      */
110     public Response append(char c) {
111         msg.append(c);
112         return this;
113     }
114 
115     /**
116      * Appends object
117      * @param object object
118      * @return this response
119      */
120     public Response append(Object object) {
121         msg.append(object);
122         return this;
123     }
124 
125     /**
126      * Sends directly buffer to output
127      * @param buf buffer
128      * @throws IOException
129      */
130     public void append(byte[] buf) throws IOException {
131         append(buf, 0, buf.length);
132     }
133 
134     /**
135      * Appends buffer
136      * @param buf buffer
137      * @param off offset
138      * @param len lebgth
139      * @throws IOException
140      */
141     public void append(byte[] buf, int off, int len) throws IOException {
142         OutputStream out = (OutputStream)session.adapt(OutputStream.class);
143         out.write(buf, off, len);
144         out.flush();
145     }
146 
147     /**
148      * Commits response
149      * @throws IOException
150      */
151     protected void commit() throws IOException {
152         OutputStream out = (OutputStream)session.adapt(OutputStream.class);
153         synchronized (out) {
154             out.write(msg.toString().getBytes());
155             out.write(13);
156             out.write(10);
157             out.flush();
158         }
159         msg.delete(0, msg.length());
160     }
161 
162     /**
163      * Submits response
164      * @throws IOException
165      */
166     public void submit() throws IOException {
167         if (msg.length() > 0) {
168             OutputStream out = (OutputStream)session.adapt(OutputStream.class);
169             synchronized (out) {
170                 out.write(msg.toString().getBytes());
171                 out.write(13);
172                 out.write(10);
173                 out.flush();
174             }
175             msg.delete(0, msg.length());
176         }
177     }
178 
179 }