View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.javamail.transport.smtp;
19  
20  /**
21   * Util class to represent a reply from a SMTP server
22   * 
23   * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
24   */
25  class SMTPReply {
26      // The original reply string
27      private final String reply;
28  
29      // returned message code
30      private final int code;
31  
32      // the returned message text
33      private final String message;
34  
35      // indicates that this is a continuation response
36      private boolean continued;
37  
38      SMTPReply(String s) throws MalformedSMTPReplyException {
39          // save the reply
40          reply = s;
41  
42          // In a normal response, the first 3 must be the return code. However,
43          // the response back from a QUIT command is frequently a null string.
44          // Therefore, if the result is
45          // too short, just default the code to -1 and use the entire text for
46          // the message.
47          if (s == null || s.length() < 3) {
48              code = -1;
49              message = s;
50              return;
51          }
52  
53          try {
54              continued = false;
55              code = Integer.parseInt(s.substring(0, 3));
56  
57              // message should be separated by a space OR a continuation
58              // character if this is a
59              // multi-line response.
60              if (s.length() > 4) {
61                  //
62                  if (s.charAt(3) == '-') {
63                      continued = true;
64                  }
65                  message = s.substring(4);
66              } else {
67                  message = "";
68              }
69          } catch (NumberFormatException e) {
70              throw new MalformedSMTPReplyException("error in parsing code", e);
71          }
72      }
73  
74      /**
75       * Return the code value associated with the reply.
76       * 
77       * @return The integer code associated with the reply.
78       */
79      public int getCode() {
80          return this.code;
81      }
82  
83      /**
84       * Get the message text associated with the reply.
85       * 
86       * @return The string value of the message from the reply.
87       */
88      public String getMessage() {
89          return this.message;
90      }
91  
92      /**
93       * Retrieve the raw reply string for the reponse.
94       * 
95       * @return The original reply string from the server.
96       */
97      public String getReply() {
98          return reply;
99      }
100 
101     /**
102      * Indicates if reply is an error condition
103      */
104     boolean isError() {
105         // error codes are all above 400
106         return code >= 400;
107     }
108 
109     /**
110      * Indicates whether this response is flagged as part of a multiple line
111      * response.
112      * 
113      * @return true if the response has multiple lines, false if this is the
114      *         last line of the response.
115      */
116     public boolean isContinued() {
117         return continued;
118     }
119 
120     public String toString() {
121         return "CODE = " + getCode() + " : MSG = " + getMessage();
122     }
123 }