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.nntp;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.mail.MessagingException;
26  
27  /**
28   * Util class to represent a reply from a NNTP server
29   * 
30   * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
31   */
32  public class NNTPReply {
33      // general server responses
34      public static final int POSTING_ALLOWED = 200;
35  
36      public static final int NO_POSTING_ALLOWED = 201;
37  
38      public static final int EXTENSIONS_SUPPORTED = 202;
39  
40      public static final int SERVICE_DISCONTINUED = 400;
41  
42      public static final int COMMAND_NOT_RECOGNIZED = 500;
43  
44      public static final int COMMAND_SYNTAX_ERROR = 501;
45  
46      public static final int PERMISSION_DENIED = 502;
47  
48      public static final int PROGRAM_FAULT = 503;
49  
50      // article responses
51      public static final int ARTICLE_FOLLOWS = 220;
52  
53      public static final int HEAD_FOLLOWS = 221;
54  
55      public static final int BODY_FOLLOWS = 222;
56  
57      public static final int REQUEST_TEXT_SEPARATELY = 223;
58  
59      public static final int OVERVIEW_FOLLOWS = 224;
60  
61      public static final int NEW_ARTICLES_FOLLOWS = 230;
62  
63      public static final int NEW_GROUPS_FOLLOWS = 231;
64  
65      public static final int ARTICLE_TRANSFERRED = 235;
66  
67      public static final int NO_NEWSGROUP_SELECTED = 412;
68  
69      public static final int NO_ARTICLE_SELECTED = 420;
70  
71      public static final int NO_ARTICLE_NUMBER = 423;
72  
73      public static final int NO_ARTICLE_FOUND = 430;
74  
75      // group responses
76      public static final int GROUP_SELECTED = 211;
77  
78      public static final int NO_SUCH_NEWSGROUP = 411;
79  
80      // post responses
81      public static final int POSTED_OK = 240;
82  
83      public static final int SEND_ARTICLE = 340;
84  
85      public static final int POSTING_NOT_ALLOWED = 440;
86  
87      public static final int POSTING_FAILED = 441;
88  
89      // quit responses
90      public static final int CLOSING_CONNECTION = 205;
91  
92      // authentication responses
93      public static final int AUTHINFO_ACCEPTED = 250;
94  
95      public static final int AUTHINFO_ACCEPTED_FINAL = 251;
96  
97      public static final int AUTHINFO_CONTINUE = 350;
98  
99      public static final int AUTHINFO_CHALLENGE = 350;
100 
101     public static final int AUTHINFO_SIMPLE_REJECTED = 402;
102 
103     public static final int AUTHENTICATION_ACCEPTED = 281;
104 
105     public static final int MORE_AUTHENTICATION_REQUIRED = 381;
106 
107     public static final int AUTHINFO_REQUIRED = 480;
108 
109     public static final int AUTHINFO_SIMPLE_REQUIRED = 450;
110 
111     public static final int AUTHENTICATION_REJECTED = 482;
112 
113     // list active reponses
114     public static final int LIST_FOLLOWS = 215;
115 
116     // The original reply string
117     private final String reply;
118 
119     // returned message code
120     private final int code;
121 
122     // the returned message text
123     private final String message;
124 
125     // data associated with a long response command.
126     private ArrayList data;
127 
128     NNTPReply(String s) throws MessagingException {
129         // save the reply
130         reply = s;
131 
132         // In a normal response, the first 3 must be the return code. However,
133         // the response back from a QUIT command is frequently a null string.
134         // Therefore, if the result is
135         // too short, just default the code to -1 and use the entire text for
136         // the message.
137         if (s == null || s.length() < 3) {
138             code = -1;
139             message = s;
140             return;
141         }
142 
143         try {
144             code = Integer.parseInt(s.substring(0, 3));
145 
146             // message should be separated by a space OR a continuation
147             // character if this is a
148             // multi-line response.
149             if (s.length() > 4) {
150                 message = s.substring(4);
151             } else {
152                 message = "";
153             }
154         } catch (NumberFormatException e) {
155             throw new MessagingException("error in parsing reply code", e);
156         }
157     }
158 
159     /**
160      * Retrieve data associated with a multi-line reponse from a server stream.
161      * 
162      * @param in
163      *            The reader that's the source of the additional lines.
164      * 
165      * @exception IOException
166      */
167     public void retrieveData(BufferedReader in) throws MessagingException {
168         try {
169             data = new ArrayList();
170 
171             String line = in.readLine();
172             // read until the end of file or until we see the end of data
173             // marker.
174             while (line != null && !line.equals(".")) {
175                 // this line is not the terminator, but it may have been byte
176                 // stuffed. If it starts with
177                 // '.', throw away the leading one.
178                 if (line.startsWith(".")) {
179                     line = line.substring(1);
180                 }
181 
182                 // just add the line to the list
183                 data.add(line);
184                 line = in.readLine();
185             }
186         } catch (IOException e) {
187             throw new MessagingException("Error reading message reply", e);
188         }
189     }
190 
191     /**
192      * Retrieve the long-command data from this response.
193      * 
194      * @return The data list. Returns null if there is no associated data.
195      */
196     public List getData() {
197         return data;
198     }
199 
200     /**
201      * Return the code value associated with the reply.
202      * 
203      * @return The integer code associated with the reply.
204      */
205     public int getCode() {
206         return this.code;
207     }
208 
209     /**
210      * Get the message text associated with the reply.
211      * 
212      * @return The string value of the message from the reply.
213      */
214     public String getMessage() {
215         return this.message;
216     }
217 
218     /**
219      * Retrieve the raw reply string for the reponse.
220      * 
221      * @return The original reply string from the server.
222      */
223     public String getReply() {
224         return reply;
225     }
226 
227     /**
228      * Indicates if reply is an error condition
229      */
230     boolean isError() {
231         // error codes are all above 400
232         return code >= 400;
233     }
234 
235     public String toString() {
236         return "CODE = " + getCode() + " : MSG = " + getMessage();
237     }
238 }