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