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 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 {@code responseType} data response. (i.e. not tagged). 67 * @param index where {@code responseType} should appear. e.g. 1 for "FETCH" 68 * @param responseType e.g. "FETCH" 69 */ 70 public final boolean isDataResponse(int index, String responseType) { 71 return !isTagged() && getStringOrEmpty(index).is(responseType); 72 } 73 74 /** 75 * @return Response code (RFC 3501 7.1) if it's a status response. 76 * 77 * e.g. "ALERT" for "* OK [ALERT] System shutdown in 10 minutes" 78 */ 79 public ImapString getResponseCodeOrEmpty() { 80 if (!isStatusResponse()) { 81 return ImapString.EMPTY; // Not a status response. 82 } 83 return getListOrEmpty(1).getStringOrEmpty(0); 84 } 85 86 /** 87 * @return Alert message it it has ALERT response code. 88 * 89 * e.g. "System shutdown in 10 minutes" for "* OK [ALERT] System shutdown in 10 minutes" 90 */ 91 public ImapString getAlertTextOrEmpty() { 92 if (!getResponseCodeOrEmpty().is(ImapConstants.ALERT)) { 93 return ImapString.EMPTY; // Not an ALERT 94 } 95 // The 3rd element contains all the rest of line. 96 return getStringOrEmpty(2); 97 } 98 99 /** 100 * @return Response text in a status response. 101 */ 102 public ImapString getStatusResponseTextOrEmpty() { 103 if (!isStatusResponse()) { 104 return ImapString.EMPTY; 105 } 106 return getStringOrEmpty(getElementOrNone(1).isList() ? 2 : 1); 107 } 108 109 @Override 110 public String toString() { 111 String tag = mTag; 112 if (isContinuationRequest()) { 113 tag = "+"; 114 } 115 return "#" + tag + "# " + super.toString(); 116 } 117 118 @Override 119 public boolean equalsForTest(ImapElement that) { 120 if (!super.equalsForTest(that)) { 121 return false; 122 } 123 final ImapResponse thatResponse = (ImapResponse) that; 124 if (mTag == null) { 125 if (thatResponse.mTag != null) { 126 return false; 127 } 128 } else { 129 if (!mTag.equals(thatResponse.mTag)) { 130 return false; 131 } 132 } 133 if (mIsContinuationRequest != thatResponse.mIsContinuationRequest) { 134 return false; 135 } 136 return true; 137 } 138 } 139