Added skeleton of new operation to detect authentication method in the server side
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / DetectAuthenticationMethodOperation.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.operations;
26
27 import java.util.ArrayList;
28
29 import org.apache.commons.httpclient.HttpStatus;
30 import org.apache.commons.httpclient.methods.HeadMethod;
31
32 import com.owncloud.android.lib.common.OwnCloudClient;
33 import com.owncloud.android.lib.common.network.WebdavUtils;
34 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
35 import com.owncloud.android.lib.common.operations.RemoteOperation;
36 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
37 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
38
39 import android.content.Context;
40 import android.net.ConnectivityManager;
41 import android.util.Log;
42
43 /**
44 * Operation to find out what authentication method requires
45 * the server to access files.
46 *
47 * Basically, tries to access to the root folder without authorization
48 * and analyzes the response.
49 *
50 * When successful, the instance of {@link RemoteOperationResult} passed
51 * through {@link OnRemoteOperationListener#onRemoteOperationFinish(RemoteOperation,
52 * RemoteOperationResult)} returns in {@link RemoteOperationResult#getData()}
53 * a value of {@link AuthenticationMethod}.
54 *
55 * @author David A. Velasco
56 */
57 public class DetectAuthenticationMethodOperation extends RemoteOperation {
58
59 private static final String TAG = DetectAuthenticationMethodOperation.class.getSimpleName();
60
61 public enum AuthenticationMethod {
62 UNKNOWN,
63 NONE,
64 BASIC_HTTP_AUTH,
65 SAML_WEB_SSO,
66 BEARER_TOKEN
67 }
68
69 private String mPath;
70 private Context mContext;
71 private boolean mSuccessIfAbsent;
72
73 /**
74 * Constructor
75 */
76 public DetectAuthenticationMethodOperation() {
77 }
78
79
80 @Override
81 protected RemoteOperationResult run(OwnCloudClient client) {
82 RemoteOperationResult result = new RemoteOperationResult(ResultCode.OK);
83 ArrayList<Object> data = new ArrayList<Object>();
84 data.add(AuthenticationMethod.UNKNOWN);
85 result.setData(data);
86 /*
87 if (!isOnline()) {
88 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
89 }
90 RemoteOperationResult result = null;
91 HeadMethod head = null;
92 try {
93 head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath));
94 int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
95 client.exhaustResponse(head.getResponseBodyAsStream());
96 boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
97 result = new RemoteOperationResult(success, status, head.getResponseHeaders());
98 Log.d(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":""));
99
100 } catch (Exception e) {
101 result = new RemoteOperationResult(e);
102 Log.e(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException());
103
104 } finally {
105 if (head != null)
106 head.releaseConnection();
107 }
108 */
109 return result;
110 }
111
112 private boolean isOnline() {
113 ConnectivityManager cm = (ConnectivityManager) mContext
114 .getSystemService(Context.CONNECTIVITY_SERVICE);
115 return cm != null && cm.getActiveNetworkInfo() != null
116 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
117 }
118
119
120 }