1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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
142
143
144
145 void onLoggingInFinished(int result);
146
147
148
149
150 void onCanceled();
151
152
153
154
155
156
157 void onUpdateDomain(String newDomain);
158 }
159 }