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.EasSyncService; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 23 /** 24 * Parse the result of a MoveItems command. 25 */ 26 public class MoveItemsParser extends Parser { 27 private final EasSyncService mService; 28 private int mStatusCode = 0; 29 private String mNewServerId; 30 31 // These are the EAS status codes for MoveItems 32 private static final int STATUS_NO_SOURCE_FOLDER = 1; 33 private static final int STATUS_NO_DESTINATION_FOLDER = 2; 34 private static final int STATUS_SUCCESS = 3; 35 private static final int STATUS_SOURCE_DESTINATION_SAME = 4; 36 private static final int STATUS_INTERNAL_ERROR = 5; 37 private static final int STATUS_ALREADY_EXISTS = 6; 38 private static final int STATUS_LOCKED = 7; 39 40 // These are the status values we return to callers 41 public static final int STATUS_CODE_SUCCESS = 1; 42 public static final int STATUS_CODE_REVERT = 2; 43 public static final int STATUS_CODE_RETRY = 3; 44 45 public MoveItemsParser(InputStream in, EasSyncService service) throws IOException { 46 super(in); 47 mService = service; 48 } 49 50 public int getStatusCode() { 51 return mStatusCode; 52 } 53 54 public String getNewServerId() { 55 return mNewServerId; 56 } 57 58 public void parseResponse() throws IOException { 59 while (nextTag(Tags.MOVE_RESPONSE) != END) { 60 if (tag == Tags.MOVE_STATUS) { 61 int status = getValueInt(); 62 // Convert the EAS status code with our external codes 63 switch(status) { 64 case STATUS_SUCCESS: 65 case STATUS_SOURCE_DESTINATION_SAME: 66 case STATUS_ALREADY_EXISTS: 67 // Same destination and already exists are ok with us; we'll continue as 68 // if the move succeeded 69 mStatusCode = STATUS_CODE_SUCCESS; 70 break; 71 case STATUS_LOCKED: 72 // This sounds like a transient error, so we can safely retry 73 mStatusCode = STATUS_CODE_RETRY; 74 break; 75 case STATUS_NO_SOURCE_FOLDER: 76 case STATUS_NO_DESTINATION_FOLDER: 77 case STATUS_INTERNAL_ERROR: 78 default: 79 // These are non-recoverable, so we'll revert the message to its original 80 // mailbox. If there's an unknown response, revert 81 mStatusCode = STATUS_CODE_REVERT; 82 break; 83 } 84 if (status != STATUS_SUCCESS) { 85 // There's not much to be done if this fails 86 mService.userLog("Error in MoveItems: " + status); 87 } 88 } else if (tag == Tags.MOVE_DSTMSGID) { 89 mNewServerId = getValue(); 90 mService.userLog("Moved message id is now: " + mNewServerId); 91 } else { 92 skipTag(); 93 } 94 } 95 } 96 97 @Override 98 public boolean parse() throws IOException { 99 boolean res = false; 100 if (nextTag(START_DOCUMENT) != Tags.MOVE_MOVE_ITEMS) { 101 throw new IOException(); 102 } 103 while (nextTag(START_DOCUMENT) != END_DOCUMENT) { 104 if (tag == Tags.MOVE_RESPONSE) { 105 parseResponse(); 106 } else { 107 skipTag(); 108 } 109 } 110 return res; 111 } 112 } 113 114