Save target location of server behind a permanent redirection after password validation
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorAsyncTask.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio on 09/02/2015.
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package com.owncloud.android.authentication;
21
22 import android.app.Activity;
23 import android.content.Context;
24 import android.net.Uri;
25 import android.os.AsyncTask;
26
27 import com.owncloud.android.lib.common.OwnCloudClient;
28 import com.owncloud.android.lib.common.OwnCloudClientFactory;
29 import com.owncloud.android.lib.common.OwnCloudCredentials;
30 import com.owncloud.android.lib.common.network.RedirectionPath;
31 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
32 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
33
34 import java.lang.ref.WeakReference;
35
36
37 /**
38 * Async Task to verify the credentials of a user
39 */
40 public class AuthenticatorAsyncTask extends AsyncTask<Object, Void, RemoteOperationResult> {
41
42 private static String REMOTE_PATH = "/";
43 private static boolean SUCCESS_IF_ABSENT = false;
44
45 private Context mContext;
46 private final WeakReference<OnAuthenticatorTaskListener> mListener;
47 protected Activity mActivity;
48
49 public AuthenticatorAsyncTask(Activity activity) {
50 mContext = activity.getApplicationContext();
51 mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
52 }
53
54 @Override
55 protected RemoteOperationResult doInBackground(Object... params) {
56
57 RemoteOperationResult result;
58 if (params!= null && params.length==2) {
59 String url = (String)params[0];
60 OwnCloudCredentials credentials = (OwnCloudCredentials)params[1];
61
62 // Client
63 Uri uri = Uri.parse(url);
64 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, true);
65 client.setCredentials(credentials);
66
67 // Operation
68 ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(
69 REMOTE_PATH,
70 mContext,
71 SUCCESS_IF_ABSENT
72 );
73 result = operation.execute(client);
74
75 if (operation.wasRedirected()) {
76 RedirectionPath redirectionPath = operation.getRedirectionPath();
77 String permanentLocation = redirectionPath.getLastPermanentLocation();
78 result.setLastPermanentLocation(permanentLocation);
79 }
80
81 } else {
82 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
83 }
84
85 return result;
86 }
87
88 @Override
89 protected void onPostExecute(RemoteOperationResult result) {
90
91 if (result!= null)
92 {
93 OnAuthenticatorTaskListener listener = mListener.get();
94 if (listener!= null)
95 {
96 listener.onAuthenticatorTaskCallback(result);
97 }
98 }
99 }
100 /*
101 * Interface to retrieve data from recognition task
102 */
103 public interface OnAuthenticatorTaskListener{
104
105 void onAuthenticatorTaskCallback(RemoteOperationResult result);
106 }
107 }