Home | History | Annotate | Download | only in eas
      1 /*
      2  * Copyright (C) 2013 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.exchange.eas;
     18 
     19 import com.android.exchange.Eas;
     20 import com.android.exchange.EasResponse;
     21 import com.android.mail.utils.LogUtils;
     22 import com.google.common.collect.Sets;
     23 
     24 import org.apache.http.Header;
     25 import org.apache.http.HttpEntity;
     26 
     27 import java.util.HashSet;
     28 
     29 /**
     30  * Performs an HTTP Options request to the Exchange server, in order to get the protocol
     31  * version.
     32  */
     33 public class EasOptions extends EasOperation {
     34     private static final String LOG_TAG = Eas.LOG_TAG;
     35 
     36     /** Result code indicating we successfully got a protocol version. */
     37     public static final int RESULT_OK = 1;
     38 
     39     /** Set of Exchange protocol versions we understand. */
     40     private static final HashSet<String> SUPPORTED_PROTOCOL_VERSIONS = Sets.newHashSet(
     41             Eas.SUPPORTED_PROTOCOL_EX2003,
     42             Eas.SUPPORTED_PROTOCOL_EX2007, Eas.SUPPORTED_PROTOCOL_EX2007_SP1,
     43             Eas.SUPPORTED_PROTOCOL_EX2010, Eas.SUPPORTED_PROTOCOL_EX2010_SP1);
     44 
     45     private String mProtocolVersion = null;
     46 
     47     public EasOptions(final EasOperation parentOperation) {
     48         super(parentOperation);
     49     }
     50 
     51     /**
     52      * Perform the server request. If successful, callers should use
     53      * {@link #getProtocolVersionString} to get the actual protocol version value.
     54      * @return A result code; {@link #RESULT_OK} is the only value that indicates success.
     55      */
     56     public int getProtocolVersionFromServer() {
     57         return performOperation();
     58     }
     59 
     60     /**
     61      * @return The protocol version to use, or null if we did not successfully get one.
     62      */
     63     public String getProtocolVersionString() {
     64         return mProtocolVersion;
     65     }
     66 
     67     /**
     68      * Note that this operation does not actually use this name when forming the request.
     69      * @return A useful name for logging this operation.
     70      */
     71     @Override
     72     protected String getCommand() {
     73         return "OPTIONS";
     74     }
     75 
     76     @Override
     77     protected HttpEntity getRequestEntity() {
     78         return null;
     79     }
     80 
     81     @Override
     82     protected int handleResponse(final EasResponse response) {
     83         final Header commands = response.getHeader("MS-ASProtocolCommands");
     84         final Header versions = response.getHeader("ms-asprotocolversions");
     85         final boolean hasProtocolVersion;
     86         if (commands == null || versions == null) {
     87             LogUtils.e(LOG_TAG, "OPTIONS response without commands or versions");
     88             hasProtocolVersion = false;
     89         } else {
     90             mProtocolVersion = getProtocolVersionFromHeader(versions);
     91             hasProtocolVersion = (mProtocolVersion != null);
     92         }
     93         if (!hasProtocolVersion) {
     94             return RESULT_PROTOCOL_VERSION_UNSUPPORTED;
     95         }
     96 
     97         return RESULT_OK;
     98     }
     99 
    100     @Override
    101     protected String getRequestUri() {
    102         return null;
    103     }
    104 
    105     /**
    106      * Find the best protocol version to use from the header.
    107      * @param versionHeader The {@link Header} for the server's supported versions.
    108      * @return The best protocol version we mutually support, or null if none found.
    109      */
    110     private String getProtocolVersionFromHeader(final Header versionHeader) {
    111         // The string is a comma separated list of EAS versions in ascending order
    112         // e.g. 1.0,2.0,2.5,12.0,12.1,14.0,14.1
    113         final String supportedVersions = versionHeader.getValue();
    114         LogUtils.d(LOG_TAG, "Server supports versions: %s", supportedVersions);
    115         final String[] supportedVersionsArray = supportedVersions.split(",");
    116         // Find the most recent version we support
    117         String newProtocolVersion = null;
    118         for (final String version: supportedVersionsArray) {
    119             if (SUPPORTED_PROTOCOL_VERSIONS.contains(version)) {
    120                 newProtocolVersion = version;
    121             }
    122         }
    123         return newProtocolVersion;
    124     }
    125 }
    126