ImapResponse.java revision 5c523858385176c33a7456bb84035de78552d22d
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.mail.store.imap;
18
19
20/**
21 * Class represents an IMAP response.
22 */
23public class ImapResponse extends ImapList {
24    private final String mTag;
25    private final boolean mIsContinuationRequest;
26
27    /* package */ ImapResponse(String tag, boolean isContinuationRequest) {
28        mTag = tag;
29        mIsContinuationRequest = isContinuationRequest;
30    }
31
32    /* package */ static boolean isStatusResponse(String symbol) {
33        return     ImapConstants.OK.equalsIgnoreCase(symbol)
34                || ImapConstants.NO.equalsIgnoreCase(symbol)
35                || ImapConstants.BAD.equalsIgnoreCase(symbol)
36                || ImapConstants.PREAUTH.equalsIgnoreCase(symbol)
37                || ImapConstants.BYE.equalsIgnoreCase(symbol);
38    }
39
40    /**
41     * @return whether it's a tagged response.
42     */
43    public boolean isTagged() {
44        return mTag != null;
45    }
46
47    /**
48     * @return whether it's a continuation request.
49     */
50    public boolean isContinuationRequest() {
51        return mIsContinuationRequest;
52    }
53
54    public boolean isStatusResponse() {
55        return isStatusResponse(getStringOrEmpty(0).getString());
56    }
57
58    /**
59     * @return whether it's an OK response.
60     */
61    public boolean isOk() {
62        return is(0, ImapConstants.OK);
63    }
64
65    /**
66     * @return whether it's an BAD response.
67     */
68    public boolean isBad() {
69        return is(0, ImapConstants.BAD);
70    }
71
72    /**
73     * @return whether it's an NO response.
74     */
75    public boolean isNo() {
76        return is(0, ImapConstants.NO);
77    }
78
79    /**
80     * @return whether it's an {@code responseType} data response.  (i.e. not tagged).
81     * @param index where {@code responseType} should appear.  e.g. 1 for "FETCH"
82     * @param responseType e.g. "FETCH"
83     */
84    public final boolean isDataResponse(int index, String responseType) {
85        return !isTagged() && getStringOrEmpty(index).is(responseType);
86    }
87
88    /**
89     * @return Response code (RFC 3501 7.1) if it's a status response.
90     *
91     * e.g. "ALERT" for "* OK [ALERT] System shutdown in 10 minutes"
92     */
93    public ImapString getResponseCodeOrEmpty() {
94        if (!isStatusResponse()) {
95            return ImapString.EMPTY; // Not a status response.
96        }
97        return getListOrEmpty(1).getStringOrEmpty(0);
98    }
99
100    /**
101     * @return Alert message it it has ALERT response code.
102     *
103     * e.g. "System shutdown in 10 minutes" for "* OK [ALERT] System shutdown in 10 minutes"
104     */
105    public ImapString getAlertTextOrEmpty() {
106        if (!getResponseCodeOrEmpty().is(ImapConstants.ALERT)) {
107            return ImapString.EMPTY; // Not an ALERT
108        }
109        // The 3rd element contains all the rest of line.
110        return getStringOrEmpty(2);
111    }
112
113    /**
114     * @return Response text in a status response.
115     */
116    public ImapString getStatusResponseTextOrEmpty() {
117        if (!isStatusResponse()) {
118            return ImapString.EMPTY;
119        }
120        return getStringOrEmpty(getElementOrNone(1).isList() ? 2 : 1);
121    }
122
123    @Override
124    public String toString() {
125        String tag = mTag;
126        if (isContinuationRequest()) {
127            tag = "+";
128        }
129        return "#" + tag + "# " + super.toString();
130    }
131
132    @Override
133    public boolean equalsForTest(ImapElement that) {
134        if (!super.equalsForTest(that)) {
135            return false;
136        }
137        final ImapResponse thatResponse = (ImapResponse) that;
138        if (mTag == null) {
139            if (thatResponse.mTag != null) {
140                return false;
141            }
142        } else {
143            if (!mTag.equals(thatResponse.mTag)) {
144                return false;
145            }
146        }
147        if (mIsContinuationRequest != thatResponse.mIsContinuationRequest) {
148            return false;
149        }
150        return true;
151    }
152}
153