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