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 
17 package com.android.email.mail.store.imap;
18 
19 
20 /**
21  * Class represents an IMAP response.
22  */
23 public class ImapResponse extends ImapList {
24     private final String mTag;
25     private final boolean mIsContinuationRequest;
26 
ImapResponse(String tag, boolean isContinuationRequest)27     /* package */ ImapResponse(String tag, boolean isContinuationRequest) {
28         mTag = tag;
29         mIsContinuationRequest = isContinuationRequest;
30     }
31 
isStatusResponse(String symbol)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      */
isTagged()43     public boolean isTagged() {
44         return mTag != null;
45     }
46 
47     /**
48      * @return whether it's a continuation request.
49      */
isContinuationRequest()50     public boolean isContinuationRequest() {
51         return mIsContinuationRequest;
52     }
53 
isStatusResponse()54     public boolean isStatusResponse() {
55         return isStatusResponse(getStringOrEmpty(0).getString());
56     }
57 
58     /**
59      * @return whether it's an OK response.
60      */
isOk()61     public boolean isOk() {
62         return is(0, ImapConstants.OK);
63     }
64 
65     /**
66      * @return whether it's an BAD response.
67      */
isBad()68     public boolean isBad() {
69         return is(0, ImapConstants.BAD);
70     }
71 
72     /**
73      * @return whether it's an NO response.
74      */
isNo()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      */
isDataResponse(int index, String responseType)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      */
getResponseCodeOrEmpty()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      */
getAlertTextOrEmpty()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      */
getStatusResponseTextOrEmpty()116     public ImapString getStatusResponseTextOrEmpty() {
117         if (!isStatusResponse()) {
118             return ImapString.EMPTY;
119         }
120         return getStringOrEmpty(getElementOrNone(1).isList() ? 2 : 1);
121     }
122 
getStatusOrEmpty()123     public ImapString getStatusOrEmpty() {
124         if (!isStatusResponse()) {
125             return ImapString.EMPTY;
126         }
127         return getStringOrEmpty(0);
128     }
129 
130     @Override
toString()131     public String toString() {
132         String tag = mTag;
133         if (isContinuationRequest()) {
134             tag = "+";
135         }
136         return "#" + tag + "# " + super.toString();
137     }
138 
139     @Override
equalsForTest(ImapElement that)140     public boolean equalsForTest(ImapElement that) {
141         if (!super.equalsForTest(that)) {
142             return false;
143         }
144         final ImapResponse thatResponse = (ImapResponse) that;
145         if (mTag == null) {
146             if (thatResponse.mTag != null) {
147                 return false;
148             }
149         } else {
150             if (!mTag.equals(thatResponse.mTag)) {
151                 return false;
152             }
153         }
154         if (mIsContinuationRequest != thatResponse.mIsContinuationRequest) {
155             return false;
156         }
157         return true;
158     }
159 }
160