View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.ui.login.tasks;
20  
21  import android.os.AsyncTask;
22  import android.util.Log;
23  import org.apache.http.HttpResponse;
24  import org.apache.http.HttpStatus;
25  import org.exoplatform.utils.ExoConnectionUtils;
26  
27  import java.io.IOException;
28  
29  /**
30   * Request tenant for a given email address
31   */
32  public class RequestTenantTask extends AsyncTask<String, Void, Integer> {
33  
34    private static final String TAG = "eXo____RequestTenantTask____";
35  
36    private AsyncTaskListener mListener;
37  
38    private String[]          mResult;
39  
40    @Override
41    protected Integer doInBackground(String... params) {
42      Log.i(TAG, "request tenant for email: " + params[0]);
43      String email = params[0];
44      try {
45        HttpResponse response  = ExoConnectionUtils.requestTenantForEmail(email);
46        int responseCode = response.getStatusLine().getStatusCode();
47        Log.d(TAG, "status: " + responseCode);
48        mResult = ExoConnectionUtils.checkRequestTenant(response);
49  
50        /** check error */
51        if (mResult == null) {
52          if (responseCode == HttpStatus.SC_SERVICE_UNAVAILABLE
53              || responseCode == HttpStatus.SC_NOT_FOUND)
54            return ExoConnectionUtils.SIGNIN_SERVER_NAV;
55  
56          return ExoConnectionUtils.SIGNIN_NO_TENANT_FOR_EMAIL;
57        }
58  
59        return ExoConnectionUtils.TENANT_OK;
60      }
61      catch (IOException e) {
62        Log.d(TAG, "IOException: " + e.getLocalizedMessage());
63        return ExoConnectionUtils.SIGNIN_SERVER_NAV;
64      }
65    }
66  
67    public void onPostExecute(Integer result) {
68      if (mListener != null) mListener.onRequestingTenantFinished(result, mResult);
69    }
70  
71    public void setListener(AsyncTaskListener listener) {
72      mListener = listener;
73    }
74  
75    public interface AsyncTaskListener {
76  
77      void onRequestingTenantFinished(int result, String[] userAndTenant);
78    }
79  }