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
27 private final String reply;
28
29
30 private final int code;
31
32
33 private final String message;
34
35
36 private boolean continued;
37
38 SMTPReply(String s) throws MalformedSMTPReplyException {
39
40 reply = s;
41
42
43
44
45
46
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
58
59
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
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 }