Home | History | Annotate | Download | only in eas
      1 package com.android.exchange.eas;
      2 
      3 import android.content.Context;
      4 
      5 import com.android.emailcommon.provider.Account;
      6 import com.android.exchange.CommandStatusException;
      7 import com.android.exchange.EasResponse;
      8 import com.android.exchange.adapter.GalParser;
      9 import com.android.exchange.adapter.Serializer;
     10 
     11 import org.apache.http.HttpEntity;
     12 import org.apache.http.HttpStatus;
     13 
     14 import com.android.exchange.adapter.Tags;
     15 import com.android.exchange.provider.GalResult;
     16 import com.android.mail.utils.LogUtils;
     17 
     18 import java.io.IOException;
     19 import java.io.InputStream;
     20 
     21 public class EasSearchGal extends EasOperation {
     22 
     23     public static final int RESULT_OK = 1;
     24 
     25     final private String mFilter;
     26     final private int mLimit;
     27     private GalResult mResult;
     28 
     29     public EasSearchGal(Context context, final Account account, final String filter,
     30                         final int limit) {
     31         super(context, account);
     32         mFilter = filter;
     33         mLimit = limit;
     34     }
     35 
     36     @Override
     37     protected String getCommand() {
     38         return "Search";
     39     }
     40 
     41     @Override
     42     protected HttpEntity getRequestEntity() throws IOException, MessageInvalidException {
     43         /*
     44          * TODO: shorter timeout for interactive lookup
     45          * TODO: make watchdog actually work (it doesn't understand our service w/Mailbox == 0)
     46          * TODO: figure out why sendHttpClientPost() hangs - possibly pool exhaustion
     47          */
     48         try {
     49             final Serializer s = new Serializer();
     50             s.start(Tags.SEARCH_SEARCH).start(Tags.SEARCH_STORE);
     51             s.data(Tags.SEARCH_NAME, "GAL").data(Tags.SEARCH_QUERY, mFilter);
     52             s.start(Tags.SEARCH_OPTIONS);
     53             s.data(Tags.SEARCH_RANGE, "0-" + Integer.toString(mLimit - 1));
     54             s.end().end().end().done();
     55             return makeEntity(s);
     56         } catch (final IOException e) {
     57             // TODO: what do we do for exceptions?
     58         } catch (final IllegalArgumentException e) {
     59         } catch (final IllegalStateException e) {
     60         }
     61         return null;
     62     }
     63 
     64     protected int handleResponse(final EasResponse response) throws
     65             IOException, CommandStatusException {
     66         final int code = response.getStatus();
     67         if (code == HttpStatus.SC_OK) {
     68             InputStream is = response.getInputStream();
     69             try {
     70                 final GalParser gp = new GalParser(is);
     71                 if (gp.parse()) {
     72                     mResult = gp.getGalResult();
     73                 } else {
     74                     LogUtils.wtf(LogUtils.TAG, "Failure to parse GalResult");
     75                 }
     76             } finally {
     77                 is.close();
     78             }
     79             return RESULT_OK;
     80         } else {
     81             LogUtils.d(LogUtils.TAG, "GAL lookup returned %d", code);
     82             return RESULT_OTHER_FAILURE;
     83         }
     84     }
     85 
     86     public GalResult getResult() {
     87         return mResult;
     88     }
     89 
     90 }
     91