PingParser.java revision 26b41fab64a805f3f938e135addbb9603f1637af
1/* Copyright (C) 2008-2009 Marc Blank 2 * Licensed to 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 17package com.android.exchange.adapter; 18 19import com.android.mail.utils.LogUtils; 20 21import java.io.IOException; 22import java.io.InputStream; 23import java.util.ArrayList; 24 25/** 26 * Parse the result of a Ping command. 27 * After {@link #parse()}, {@link #getPingStatus()} will give a valid status value. Also, when 28 * appropriate one of {@link #getSyncList()}, {@link #getMaxFolders()}, or 29 * {@link #getHeartbeatInterval()} will contain further detailed results of the parsing. 30 */ 31public class PingParser extends Parser { 32 private static final String TAG = "PingParser"; 33 34 /** Sentinel value, used when some property doesn't have a meaningful value. */ 35 public static final int NO_VALUE = -1; 36 37 // The following are the actual status codes from the Exchange server. 38 // See http://msdn.microsoft.com/en-us/library/gg663456(v=exchg.80).aspx for more details. 39 /** Indicates that the heartbeat interval expired before a change happened. */ 40 public static final int STATUS_EXPIRED = 1; 41 /** Indicates that one or more of the pinged folders changed. */ 42 public static final int STATUS_CHANGES_FOUND = 2; 43 /** Indicates that the ping request was missing required parameters. */ 44 public static final int STATUS_REQUEST_INCOMPLETE = 3; 45 /** Indicates that the ping request was malformed. */ 46 public static final int STATUS_REQUEST_MALFORMED = 4; 47 /** Indicates that the ping request specified a bad heartbeat (too small or too big). */ 48 public static final int STATUS_REQUEST_HEARTBEAT_OUT_OF_BOUNDS = 5; 49 /** Indicates that the ping requested more folders than the server will permit. */ 50 public static final int STATUS_REQUEST_TOO_MANY_FOLDERS = 6; 51 /** Indicates that the folder structure is out of sync. */ 52 public static final int STATUS_FOLDER_REFRESH_NEEDED = 7; 53 /** Indicates a server error. */ 54 public static final int STATUS_SERVER_ERROR = 8; 55 56 private int mPingStatus = NO_VALUE; 57 private final ArrayList<String> mSyncList = new ArrayList<String>(); 58 private int mMaxFolders = NO_VALUE; 59 private int mHeartbeatInterval = NO_VALUE; 60 61 public PingParser(final InputStream in) throws IOException { 62 super(in); 63 } 64 65 /** 66 * @return The status for this ping. 67 */ 68 public int getPingStatus() { 69 return mPingStatus; 70 } 71 72 /** 73 * If {@link #getPingStatus} indicates that there are folders to sync, this will return which 74 * folders need syncing. 75 * @return The list of folders to sync, or null if sync was not indicated in the response. 76 */ 77 public ArrayList<String> getSyncList() { 78 if (mPingStatus != STATUS_CHANGES_FOUND) { 79 return null; 80 } 81 return mSyncList; 82 } 83 84 /** 85 * If {@link #getPingStatus} indicates that we asked for too many folders, this will return the 86 * limit. 87 * @return The maximum number of folders we may ping, or {@link #NO_VALUE} if no maximum was 88 * indicated in the response. 89 */ 90 public int getMaxFolders() { 91 if (mPingStatus != STATUS_REQUEST_TOO_MANY_FOLDERS) { 92 return NO_VALUE; 93 } 94 return mMaxFolders; 95 } 96 97 /** 98 * If {@link #getPingStatus} indicates that we specified an invalid heartbeat, this will return 99 * a valid heartbeat to use. 100 * @return If our request asked for too small a heartbeat, this will return the minimum value 101 * permissible. If the request was too large, this will return the maximum value 102 * permissible. Otherwise, this returns {@link #NO_VALUE}. 103 */ 104 public int getHeartbeatInterval() { 105 if (mPingStatus != STATUS_REQUEST_HEARTBEAT_OUT_OF_BOUNDS) { 106 return NO_VALUE; 107 } 108 return mHeartbeatInterval; 109 } 110 111 /** 112 * Checks whether a status code implies we ought to send another ping immediately. 113 * @param pingStatus The ping status value we wish to check. 114 * @return Whether we should send another ping immediately. 115 */ 116 public static boolean shouldPingAgain(final int pingStatus) { 117 // Explanation for why we ping again for each case: 118 // - If the ping expired we should keep looping with pings. 119 // - The EAS spec says to handle incomplete and malformed request errors by pinging again 120 // with corrected request data. Since we always send a complete request, we simply 121 // repeat (and assume that some sort of network error is what caused the corruption). 122 // - Heartbeat errors are handled by pinging with a better heartbeat value. 123 // - Other server errors are considered transient and therefore we just reping for those. 124 return pingStatus == STATUS_EXPIRED 125 || pingStatus == STATUS_REQUEST_INCOMPLETE 126 || pingStatus == STATUS_REQUEST_MALFORMED 127 || pingStatus == STATUS_REQUEST_HEARTBEAT_OUT_OF_BOUNDS 128 || pingStatus == STATUS_SERVER_ERROR; 129 } 130 131 /** 132 * Parse the Folders element of the ping response, and store the results. 133 * @throws IOException 134 */ 135 private void parsePingFolders() throws IOException { 136 while (nextTag(Tags.PING_FOLDERS) != END) { 137 if (tag == Tags.PING_FOLDER) { 138 // Here we'll keep track of which mailboxes need syncing 139 String serverId = getValue(); 140 mSyncList.add(serverId); 141 LogUtils.i(TAG, "Changes found in: %s", serverId); 142 } else { 143 skipTag(); 144 } 145 } 146 } 147 148 /** 149 * Parse an integer value from the response for a particular property, and bounds check the 150 * new value. A property cannot be set more than once. 151 * @param name The name of the property we're parsing (for logging purposes). 152 * @param currentValue The current value of the property we're parsing. 153 * @param minValue The minimum value for the property we're parsing. 154 * @param maxValue The maximum value for the property we're parsing. 155 * @return The new value of the property we're parsing. 156 157 */ 158 private int getValue(final String name, final int currentValue, final int minValue, 159 final int maxValue) throws IOException { 160 if (currentValue != NO_VALUE) { 161 throw new IOException("Response has multiple values for " + name); 162 } 163 final int value = getValueInt(); 164 if (value < minValue || (maxValue > 0 && value > maxValue)) { 165 throw new IOException(name + " out of bounds: " + value); 166 } 167 return value; 168 } 169 170 /** 171 * Parse an integer value from the response for a particular property, and ensure it is 172 * positive. A value cannot be set more than once. 173 * @param name The name of the property we're parsing (for logging purposes). 174 * @param currentValue The current value of the property we're parsing. 175 * @return The new value of the property we're parsing. 176 * @throws IOException 177 */ 178 private int getValue(final String name, final int currentValue) throws IOException { 179 return getValue(name, currentValue, 1, -1); 180 } 181 182 /** 183 * Parse the entire response, and set our internal state accordingly. 184 * @return Whether the response was well-formed. 185 * @throws IOException 186 */ 187 @Override 188 public boolean parse() throws IOException { 189 if (nextTag(START_DOCUMENT) != Tags.PING_PING) { 190 throw new IOException("Ping response does not include a Ping element"); 191 } 192 while (nextTag(START_DOCUMENT) != END_DOCUMENT) { 193 if (tag == Tags.PING_STATUS) { 194 mPingStatus = getValue("Status", mPingStatus, STATUS_EXPIRED, 195 STATUS_SERVER_ERROR); 196 } else if (tag == Tags.PING_MAX_FOLDERS) { 197 mMaxFolders = getValue("MaxFolders", mMaxFolders); 198 } else if (tag == Tags.PING_FOLDERS) { 199 if (!mSyncList.isEmpty()) { 200 throw new IOException("Response has multiple values for Folders"); 201 } 202 parsePingFolders(); 203 final int count = mSyncList.size(); 204 LogUtils.i(TAG, "Folders has %d elements", count); 205 if (count == 0) { 206 throw new IOException("Folders was empty"); 207 } 208 } else if (tag == Tags.PING_HEARTBEAT_INTERVAL) { 209 mHeartbeatInterval = getValue("HeartbeatInterval", mHeartbeatInterval); 210 } else { 211 // TODO: Error? 212 skipTag(); 213 } 214 } 215 216 // Check the parse results for status values that don't match the other output. 217 218 switch (mPingStatus) { 219 case NO_VALUE: 220 throw new IOException("No status set in ping response"); 221 case STATUS_CHANGES_FOUND: 222 if (mSyncList.isEmpty()) { 223 throw new IOException("No changes found in ping response"); 224 } 225 break; 226 case STATUS_REQUEST_HEARTBEAT_OUT_OF_BOUNDS: 227 if (mHeartbeatInterval == NO_VALUE) { 228 throw new IOException("No value specified for heartbeat out of bounds"); 229 } 230 break; 231 case STATUS_REQUEST_TOO_MANY_FOLDERS: 232 if (mMaxFolders == NO_VALUE) { 233 throw new IOException("No value specified for too many folders"); 234 } 235 break; 236 } 237 return true; 238 } 239} 240