1/*
2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package sun.net.ftp;
26
27/**
28 * This class describes a FTP protocol reply code and associates a meaning
29 * to the numerical value according to the various RFCs (RFC 959 in
30 * particular).
31 *
32 */
33public enum FtpReplyCode {
34
35    RESTART_MARKER(110),
36    SERVICE_READY_IN(120),
37    DATA_CONNECTION_ALREADY_OPEN(125),
38    FILE_STATUS_OK(150),
39    COMMAND_OK(200),
40    NOT_IMPLEMENTED(202),
41    SYSTEM_STATUS(211),
42    DIRECTORY_STATUS(212),
43    FILE_STATUS(213),
44    HELP_MESSAGE(214),
45    NAME_SYSTEM_TYPE(215),
46    SERVICE_READY(220),
47    SERVICE_CLOSING(221),
48    DATA_CONNECTION_OPEN(225),
49    CLOSING_DATA_CONNECTION(226),
50    ENTERING_PASSIVE_MODE(227),
51    ENTERING_EXT_PASSIVE_MODE(229),
52    LOGGED_IN(230),
53    SECURELY_LOGGED_IN(232),
54    SECURITY_EXCHANGE_OK(234),
55    SECURITY_EXCHANGE_COMPLETE(235),
56    FILE_ACTION_OK(250),
57    PATHNAME_CREATED(257),
58    NEED_PASSWORD(331),
59    NEED_ACCOUNT(332),
60    NEED_ADAT(334),
61    NEED_MORE_ADAT(335),
62    FILE_ACTION_PENDING(350),
63    SERVICE_NOT_AVAILABLE(421),
64    CANT_OPEN_DATA_CONNECTION(425),
65    CONNECTION_CLOSED(426),
66    NEED_SECURITY_RESOURCE(431),
67    FILE_ACTION_NOT_TAKEN(450),
68    ACTION_ABORTED(451),
69    INSUFFICIENT_STORAGE(452),
70    COMMAND_UNRECOGNIZED(500),
71    INVALID_PARAMETER(501),
72    BAD_SEQUENCE(503),
73    NOT_IMPLEMENTED_FOR_PARAMETER(504),
74    NOT_LOGGED_IN(530),
75    NEED_ACCOUNT_FOR_STORING(532),
76    PROT_LEVEL_DENIED(533),
77    REQUEST_DENIED(534),
78    FAILED_SECURITY_CHECK(535),
79    UNSUPPORTED_PROT_LEVEL(536),
80    PROT_LEVEL_NOT_SUPPORTED_BY_SECURITY(537),
81    FILE_UNAVAILABLE(550),
82    PAGE_TYPE_UNKNOWN(551),
83    EXCEEDED_STORAGE(552),
84    FILE_NAME_NOT_ALLOWED(553),
85    PROTECTED_REPLY(631),
86    UNKNOWN_ERROR(999);
87    private final int value;
88
89    FtpReplyCode(int val) {
90        this.value = val;
91    }
92
93    /**
94     * Returns the numerical value of the code.
95     *
96     * @return the numerical value.
97     */
98    public int getValue() {
99        return value;
100    }
101
102    /**
103     * Determines if the code is a Positive Preliminary response.
104     * This means beginning with a 1 (which means a value between 100 and 199)
105     *
106     * @return <code>true</code> if the reply code is a positive preliminary
107     *         response.
108     */
109    public boolean isPositivePreliminary() {
110        return value >= 100 && value < 200;
111    }
112
113    /**
114     * Determines if the code is a Positive Completion response.
115     * This means beginning with a 2 (which means a value between 200 and 299)
116     *
117     * @return <code>true</code> if the reply code is a positive completion
118     *         response.
119     */
120    public boolean isPositiveCompletion() {
121        return value >= 200 && value < 300;
122    }
123
124    /**
125     * Determines if the code is a positive internediate response.
126     * This means beginning with a 3 (which means a value between 300 and 399)
127     *
128     * @return <code>true</code> if the reply code is a positive intermediate
129     *         response.
130     */
131    public boolean isPositiveIntermediate() {
132        return value >= 300 && value < 400;
133    }
134
135    /**
136     * Determines if the code is a transient negative response.
137     * This means beginning with a 4 (which means a value between 400 and 499)
138     *
139     * @return <code>true</code> if the reply code is a transient negative
140     *         response.
141     */
142    public boolean isTransientNegative() {
143        return value >= 400 && value < 500;
144    }
145
146    /**
147     * Determines if the code is a permanent negative response.
148     * This means beginning with a 5 (which means a value between 500 and 599)
149     *
150     * @return <code>true</code> if the reply code is a permanent negative
151     *         response.
152     */
153    public boolean isPermanentNegative() {
154        return value >= 500 && value < 600;
155    }
156
157    /**
158     * Determines if the code is a protected reply response.
159     * This means beginning with a 6 (which means a value between 600 and 699)
160     *
161     * @return <code>true</code> if the reply code is a protected reply
162     *         response.
163     */
164    public boolean isProtectedReply() {
165        return value >= 600 && value < 700;
166    }
167
168    /**
169     * Determines if the code is a syntax related response.
170     * This means the second digit is a 0.
171     *
172     * @return <code>true</code> if the reply code is a syntax related
173     *         response.
174     */
175    public boolean isSyntax() {
176        return ((value / 10) - ((value / 100) * 10)) == 0;
177    }
178
179    /**
180     * Determines if the code is an information related response.
181     * This means the second digit is a 1.
182     *
183     * @return <code>true</code> if the reply code is an information related
184     *         response.
185     */
186    public boolean isInformation() {
187        return ((value / 10) - ((value / 100) * 10)) == 1;
188    }
189
190    /**
191     * Determines if the code is a connection related response.
192     * This means the second digit is a 2.
193     *
194     * @return <code>true</code> if the reply code is a connection related
195     *         response.
196     */
197    public boolean isConnection() {
198        return ((value / 10) - ((value / 100) * 10)) == 2;
199    }
200
201    /**
202     * Determines if the code is an authentication related response.
203     * This means the second digit is a 3.
204     *
205     * @return <code>true</code> if the reply code is an authentication related
206     *         response.
207     */
208    public boolean isAuthentication() {
209        return ((value / 10) - ((value / 100) * 10)) == 3;
210    }
211
212    /**
213     * Determines if the code is an unspecified type of response.
214     * This means the second digit is a 4.
215     *
216     * @return <code>true</code> if the reply code is an unspecified type of
217     *         response.
218     */
219    public boolean isUnspecified() {
220        return ((value / 10) - ((value / 100) * 10)) == 4;
221    }
222
223    /**
224     * Determines if the code is a file system related response.
225     * This means the second digit is a 5.
226     *
227     * @return <code>true</code> if the reply code is a file system related
228     *         response.
229     */
230    public boolean isFileSystem() {
231        return ((value / 10) - ((value / 100) * 10)) == 5;
232    }
233
234    /**
235     * Static utility method to convert a value into a FtpReplyCode.
236     *
237     * @param v the value to convert
238     * @return the <code>FtpReplyCode</code> associated with the value.
239     */
240    public static FtpReplyCode find(int v) {
241        for (FtpReplyCode code : FtpReplyCode.values()) {
242            if (code.getValue() == v) {
243                return code;
244            }
245        }
246        return UNKNOWN_ERROR;
247    }
248}
249