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 java.io.IOException;
22  import java.net.URI;
23  
24  import org.apache.http.Header;
25  import org.apache.http.HttpException;
26  import org.apache.http.HttpResponse;
27  import org.apache.http.HttpResponseInterceptor;
28  import org.apache.http.HttpStatus;
29  import org.apache.http.conn.HttpHostConnectException;
30  import org.apache.http.protocol.HttpContext;
31  import org.exoplatform.utils.ExoConnectionUtils;
32  import org.exoplatform.utils.ExoConstants;
33  import org.exoplatform.utils.Log;
34  
35  import android.os.AsyncTask;
36  
37  /**
38   * Performs login
39   */
40  public class LoginTask extends AsyncTask<String, Void, Integer> {
41  
42    private AsyncTaskListener   mListener;
43  
44    private static final String TAG = "eXo____LoginTask____";
45  
46    @Override
47    public void onPreExecute() {
48      /** need to log out first */
49      ExoConnectionUtils.loggingOut();
50    }
51  
52    @Override
53    public Integer doInBackground(String... params) {
54      String username = params[0];
55      String password = params[1];
56      String domain = params[2];
57  
58      if (Log.LOGD)
59        Log.d(TAG, "Logging in with " + username + " at " + domain);
60  
61      try {
62        String versionUrl = domain + ExoConstants.DOMAIN_PLATFORM_VERSION;
63        HttpResponse response = ExoConnectionUtils.getPlatformResponse(username, password, versionUrl);
64  
65        setRedirectResponseInterceptor();
66        int statusCode = response.getStatusLine().getStatusCode();
67  
68        if (Log.LOGD)
69          Log.d(TAG, "response code: " + statusCode);
70  
71        if (statusCode == HttpStatus.SC_NOT_FOUND)
72          return ExoConnectionUtils.SIGNIN_SERVER_NAV;
73  
74        /** Login OK - check mobile compatibility */
75        if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) {
76          boolean isCompliant = ExoConnectionUtils.checkPLFVersion(response, domain, username);
77          if (!isCompliant)
78            return ExoConnectionUtils.LOGIN_INCOMPATIBLE;
79        }
80  
81        return ExoConnectionUtils.checkPlatformRespose(response);
82      } catch (HttpHostConnectException e) {
83        Log.d(TAG, "HttpHostConnectException: " + e.getLocalizedMessage());
84        return ExoConnectionUtils.SIGNIN_SERVER_NAV;
85      } catch (IOException e) {
86        Log.d(TAG, "IOException: " + e.getLocalizedMessage());
87        return ExoConnectionUtils.SIGNIN_SERVER_NAV;
88      } finally {
89        if (ExoConnectionUtils.httpClient != null) {
90          ExoConnectionUtils.httpClient.clearResponseInterceptors();
91        }
92      }
93    }
94  
95    @Override
96    protected void onCancelled() {
97      super.onCancelled();
98      if (mListener != null)
99        mListener.onCanceled();
100   }
101 
102   @Override
103   public void onPostExecute(Integer result) {
104     if (Log.LOGD)
105       Log.d(TAG, "Login result: " + result);
106 
107     if (mListener != null)
108       mListener.onLoggingInFinished(result);
109   }
110 
111   private void setRedirectResponseInterceptor() {
112     if (ExoConnectionUtils.httpClient == null)
113       return;
114 
115     ExoConnectionUtils.httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
116 
117       @Override
118       public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
119         int statusCode = response.getStatusLine().getStatusCode();
120         if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
121           if (mListener != null) {
122             Header location = response.getFirstHeader("Location");
123             if (location != null) {
124               URI newDomain = URI.create(location.getValue());
125               mListener.onUpdateDomain(newDomain.getScheme() + "://" + newDomain.getHost()
126                   + (newDomain.getPort() == -1 ? "" : ":" + newDomain.getPort()));
127             }
128           }
129         }
130       }
131     });
132   }
133 
134   public void setListener(AsyncTaskListener listener) {
135     mListener = listener;
136   }
137 
138   public interface AsyncTaskListener {
139 
140     /**
141      * Called when the task has finished to return the result.
142      * 
143      * @param result (1) for a successful login
144      */
145     void onLoggingInFinished(int result);
146 
147     /**
148      * Called when the task is canceled.
149      */
150     void onCanceled();
151 
152     /**
153      * Called when the original domain was redirected to a new one. <br/>
154      * 
155      * @param newDomain the new domain URL (scheme + host + optional port)
156      */
157     void onUpdateDomain(String newDomain);
158   }
159 }