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.smtp;
14  
15  import java.io.ByteArrayOutputStream;
16  import java.io.IOException;
17  import java.io.OutputStream;
18  
19  /**
20   * SMTP response
21   *
22   * @author Daniel Sendula
23   */
24  public class SMTPResponse {
25  
26      /** CR as byte array */
27      protected static final byte[] CRLF = new byte[] { '\r', '\n' };
28  
29      /** Response code */
30      protected int code;
31  
32      /** List of messages */
33      protected String[] msg;
34  
35      /** Empty message */
36      private static final String[] empty = new String[] { "" };
37  
38      /**
39       * Constructor
40       * @param code code
41       */
42      public SMTPResponse(int code) {
43          this.code = code;
44          msg = empty;
45      }
46  
47      /**
48       * Constructor
49       * @param code code
50       * @param msg message
51       */
52      public SMTPResponse(int code, String msg) {
53          this.code = code;
54          if (msg == null) {
55              msg = "";
56          }
57          this.msg = new String[] { msg };
58      }
59  
60      /**
61       * Constructor
62       * @param code code
63       * @param msg array of strings
64       */
65      public SMTPResponse(int code, String[] msg) {
66          this.code = code;
67          this.msg = msg;
68      }
69  
70      /**
71       * Sets line
72       * @param line line
73       */
74      public void setLine(String line) {
75          msg = new String[] { line };
76      }
77  
78      /**
79       * Adds new line
80       * @param line line
81       */
82      public void addLine(String line) {
83          String[] n = new String[msg.length + 1];
84          n[msg.length] = line;
85          msg = n;
86      }
87  
88      /**
89       * Submits response
90       * @param out output stream
91       * @throws IOException
92       */
93      public void submit(OutputStream out) throws IOException {
94          if (msg.length > 1) {
95              for (int i = 0; i < msg.length - 1; i++) {
96                  out.write((code + "-" + msg[i]).getBytes());
97                  out.write(CRLF);
98              }
99          }
100         out.write((code + " " + msg[msg.length - 1]).getBytes());
101         out.write(CRLF);
102         out.flush();
103     }
104 
105     /**
106      * Returns string representation of response
107      * @return string representation of response
108      */
109     public String toString() {
110         ByteArrayOutputStream res = new ByteArrayOutputStream();
111         try {
112             submit(res);
113         } catch (IOException e) {
114         }
115         return new String(res.toByteArray());
116     }
117 
118     /**
119      * Returns code.
120      * @return code
121      */
122     public int getCode() {
123         return code;
124     }
125 }