Home | History | Annotate | Download | only in adapter
      1 /* Copyright (C) 2010 The Android Open Source Project.
      2  *
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 package com.android.exchange.adapter;
     17 
     18 import com.android.exchange.Eas;
     19 import com.android.mail.utils.LogUtils;
     20 
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 
     24 /**
     25  * Parse the result of a MoveItems command.
     26  */
     27 public class MoveItemsParser extends Parser {
     28     private static final String TAG = Eas.LOG_TAG;
     29     private int mStatusCode = 0;
     30     private String mNewServerId;
     31     private String mSourceServerId;
     32 
     33     // These are the EAS status codes for MoveItems
     34     private static final int STATUS_NO_SOURCE_FOLDER = 1;
     35     private static final int STATUS_NO_DESTINATION_FOLDER = 2;
     36     private static final int STATUS_SUCCESS = 3;
     37     private static final int STATUS_SOURCE_DESTINATION_SAME = 4;
     38     private static final int STATUS_INTERNAL_ERROR = 5;
     39     private static final int STATUS_ALREADY_EXISTS = 6;
     40     private static final int STATUS_LOCKED = 7;
     41 
     42     // These are the status values we return to callers
     43     public static final int STATUS_CODE_SUCCESS = 1;
     44     public static final int STATUS_CODE_REVERT = 2;
     45     public static final int STATUS_CODE_RETRY = 3;
     46 
     47     public MoveItemsParser(InputStream in) throws IOException {
     48         super(in);
     49     }
     50 
     51     public int getStatusCode() {
     52         return mStatusCode;
     53     }
     54 
     55     public String getNewServerId() {
     56         return mNewServerId;
     57     }
     58 
     59     public String getSourceServerId() {
     60         return mSourceServerId;
     61     }
     62 
     63     public void parseResponse() throws IOException {
     64         while (nextTag(Tags.MOVE_RESPONSE) != END) {
     65             if (tag == Tags.MOVE_STATUS) {
     66                 int status = getValueInt();
     67                 // Convert the EAS status code with our external codes
     68                 switch(status) {
     69                     case STATUS_SUCCESS:
     70                     case STATUS_SOURCE_DESTINATION_SAME:
     71                     case STATUS_ALREADY_EXISTS:
     72                         // Same destination and already exists are ok with us; we'll continue as
     73                         // if the move succeeded
     74                         mStatusCode = STATUS_CODE_SUCCESS;
     75                         break;
     76                     case STATUS_LOCKED:
     77                         // This sounds like a transient error, so we can safely retry
     78                         mStatusCode = STATUS_CODE_RETRY;
     79                         break;
     80                     case STATUS_NO_SOURCE_FOLDER:
     81                     case STATUS_NO_DESTINATION_FOLDER:
     82                     case STATUS_INTERNAL_ERROR:
     83                     default:
     84                         // These are non-recoverable, so we'll revert the message to its original
     85                         // mailbox.  If there's an unknown response, revert
     86                         mStatusCode = STATUS_CODE_REVERT;
     87                         break;
     88                 }
     89                 if (status != STATUS_SUCCESS) {
     90                     // There's not much to be done if this fails
     91                     LogUtils.w(TAG, "Error in MoveItems: %d", status);
     92                 }
     93             } else if (tag == Tags.MOVE_DSTMSGID) {
     94                 mNewServerId = getValue();
     95                 LogUtils.d(TAG, "Moved message id is now: %s", mNewServerId);
     96             } else if (tag == Tags.MOVE_SRCMSGID) {
     97                 mSourceServerId = getValue();
     98                 LogUtils.d(TAG, "Source message id is: %s", mNewServerId);
     99             } else {
    100                 skipTag();
    101             }
    102         }
    103     }
    104 
    105     @Override
    106     public boolean parse() throws IOException {
    107         boolean res = false;
    108         if (nextTag(START_DOCUMENT) != Tags.MOVE_MOVE_ITEMS) {
    109             throw new IOException();
    110         }
    111         while (nextTag(START_DOCUMENT) != END_DOCUMENT) {
    112             if (tag == Tags.MOVE_RESPONSE) {
    113                 parseResponse();
    114             } else {
    115                 skipTag();
    116             }
    117         }
    118         return res;
    119     }
    120 }
    121 
    122