1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 package com
.owncloud
.android
.operations
;
27 import java
.util
.ArrayList
;
29 import org
.apache
.commons
.httpclient
.HttpStatus
;
30 import org
.apache
.commons
.httpclient
.methods
.HeadMethod
;
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
;
39 import android
.content
.Context
;
40 import android
.net
.ConnectivityManager
;
41 import android
.util
.Log
;
44 * Operation to find out what authentication method requires
45 * the server to access files.
47 * Basically, tries to access to the root folder without authorization
48 * and analyzes the response.
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}.
55 * @author David A. Velasco
57 public class DetectAuthenticationMethodOperation
extends RemoteOperation
{
59 private static final String TAG
= DetectAuthenticationMethodOperation
.class.getSimpleName();
61 public enum AuthenticationMethod
{
70 private Context mContext
;
71 private boolean mSuccessIfAbsent
;
76 public DetectAuthenticationMethodOperation() {
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
);
88 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
90 RemoteOperationResult result = null;
91 HeadMethod head = null;
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)":""));
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());
106 head.releaseConnection();
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();