Home | History | Annotate | Download | only in imap
      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 mTag;
     22   private final boolean mIsContinuationRequest;
     23 
     24   /* package */ ImapResponse(String tag, boolean isContinuationRequest) {
     25     mTag = tag;
     26     mIsContinuationRequest = isContinuationRequest;
     27   }
     28 
     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. */
     38   public boolean isTagged() {
     39     return mTag != null;
     40   }
     41 
     42   /** @return whether it's a continuation request. */
     43   public boolean isContinuationRequest() {
     44     return mIsContinuationRequest;
     45   }
     46 
     47   public boolean isStatusResponse() {
     48     return isStatusResponse(getStringOrEmpty(0).getString());
     49   }
     50 
     51   /** @return whether it's an OK response. */
     52   public boolean isOk() {
     53     return is(0, ImapConstants.OK);
     54   }
     55 
     56   /** @return whether it's an BAD response. */
     57   public boolean isBad() {
     58     return is(0, ImapConstants.BAD);
     59   }
     60 
     61   /** @return whether it's an NO response. */
     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    */
     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    */
     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    */
     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. */
     99   public ImapString getStatusResponseTextOrEmpty() {
    100     if (!isStatusResponse()) {
    101       return ImapString.EMPTY;
    102     }
    103     return getStringOrEmpty(getElementOrNone(1).isList() ? 2 : 1);
    104   }
    105 
    106   public ImapString getStatusOrEmpty() {
    107     if (!isStatusResponse()) {
    108       return ImapString.EMPTY;
    109     }
    110     return getStringOrEmpty(0);
    111   }
    112 
    113   @Override
    114   public String toString() {
    115     String tag = mTag;
    116     if (isContinuationRequest()) {
    117       tag = "+";
    118     }
    119     return "#" + tag + "# " + super.toString();
    120   }
    121 
    122   @Override
    123   public boolean equalsForTest(ImapElement that) {
    124     if (!super.equalsForTest(that)) {
    125       return false;
    126     }
    127     final ImapResponse thatResponse = (ImapResponse) that;
    128     if (mTag == null) {
    129       if (thatResponse.mTag != null) {
    130         return false;
    131       }
    132     } else {
    133       if (!mTag.equals(thatResponse.mTag)) {
    134         return false;
    135       }
    136     }
    137     if (mIsContinuationRequest != thatResponse.mIsContinuationRequest) {
    138       return false;
    139     }
    140     return true;
    141   }
    142 }
    143