View Javadoc

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