1 /* 2 * Copyright (C) 2015 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.phone.common.mail.store.imap; 18 19 /** 20 * Class represents an IMAP response. 21 */ 22 public class ImapResponse extends ImapList { 23 private final String mTag; 24 private final boolean mIsContinuationRequest; 25 26 /* package */ ImapResponse(String tag, boolean isContinuationRequest) { 27 mTag = tag; 28 mIsContinuationRequest = isContinuationRequest; 29 } 30 31 /* package */ static boolean isStatusResponse(String symbol) { 32 return ImapConstants.OK.equalsIgnoreCase(symbol) 33 || ImapConstants.NO.equalsIgnoreCase(symbol) 34 || ImapConstants.BAD.equalsIgnoreCase(symbol) 35 || ImapConstants.PREAUTH.equalsIgnoreCase(symbol) 36 || ImapConstants.BYE.equalsIgnoreCase(symbol); 37 } 38 39 /** 40 * @return whether it's a tagged response. 41 */ 42 public boolean isTagged() { 43 return mTag != null; 44 } 45 46 /** 47 * @return whether it's a continuation request. 48 */ 49 public boolean isContinuationRequest() { 50 return mIsContinuationRequest; 51 } 52 53 public boolean isStatusResponse() { 54 return isStatusResponse(getStringOrEmpty(0).getString()); 55 } 56 57 /** 58 * @return whether it's an OK response. 59 */ 60 public boolean isOk() { 61 return is(0, ImapConstants.OK); 62 } 63 64 /** 65 * @return whether it's an BAD response. 66 */ 67 public boolean isBad() { 68 return is(0, ImapConstants.BAD); 69 } 70 71 /** 72 * @return whether it's an NO response. 73 */ 74 public boolean isNo() { 75 return is(0, ImapConstants.NO); 76 } 77 78 /** 79 * @return whether it's an {@code responseType} data response. (i.e. not tagged). 80 * @param index where {@code responseType} should appear. e.g. 1 for "FETCH" 81 * @param responseType e.g. "FETCH" 82 */ 83 public final boolean isDataResponse(int index, String responseType) { 84 return !isTagged() && getStringOrEmpty(index).is(responseType); 85 } 86 87 /** 88 * @return Response code (RFC 3501 7.1) if it's a status response. 89 * 90 * e.g. "ALERT" for "* OK [ALERT] System shutdown in 10 minutes" 91 */ 92 public ImapString getResponseCodeOrEmpty() { 93 if (!isStatusResponse()) { 94 return ImapString.EMPTY; // Not a status response. 95 } 96 return getListOrEmpty(1).getStringOrEmpty(0); 97 } 98 99 /** 100 * @return Alert message it it has ALERT response code. 101 * 102 * e.g. "System shutdown in 10 minutes" for "* OK [ALERT] System shutdown in 10 minutes" 103 */ 104 public ImapString getAlertTextOrEmpty() { 105 if (!getResponseCodeOrEmpty().is(ImapConstants.ALERT)) { 106 return ImapString.EMPTY; // Not an ALERT 107 } 108 // The 3rd element contains all the rest of line. 109 return getStringOrEmpty(2); 110 } 111 112 /** 113 * @return Response text in a status response. 114 */ 115 public ImapString getStatusResponseTextOrEmpty() { 116 if (!isStatusResponse()) { 117 return ImapString.EMPTY; 118 } 119 return getStringOrEmpty(getElementOrNone(1).isList() ? 2 : 1); 120 } 121 122 public ImapString getStatusOrEmpty() { 123 if (!isStatusResponse()) { 124 return ImapString.EMPTY; 125 } 126 return getStringOrEmpty(0); 127 } 128 129 @Override 130 public String toString() { 131 String tag = mTag; 132 if (isContinuationRequest()) { 133 tag = "+"; 134 } 135 return "#" + tag + "# " + super.toString(); 136 } 137 138 @Override 139 public boolean equalsForTest(ImapElement that) { 140 if (!super.equalsForTest(that)) { 141 return false; 142 } 143 final ImapResponse thatResponse = (ImapResponse) that; 144 if (mTag == null) { 145 if (thatResponse.mTag != null) { 146 return false; 147 } 148 } else { 149 if (!mTag.equals(thatResponse.mTag)) { 150 return false; 151 } 152 } 153 if (mIsContinuationRequest != thatResponse.mIsContinuationRequest) { 154 return false; 155 } 156 return true; 157 } 158 }