2 * ownCloud Android client application
4 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.operations
;
24 import java
.util
.ArrayList
;
26 import com
.owncloud
.android
.MainApp
;
27 import com
.owncloud
.android
.authentication
.AccountUtils
;
28 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
30 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
31 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
32 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
33 import com
.owncloud
.android
.lib
.resources
.status
.GetRemoteStatusOperation
;
34 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
35 import com
.owncloud
.android
.operations
.DetectAuthenticationMethodOperation
.AuthenticationMethod
;
37 import android
.content
.Context
;
40 * Get basic information from an ownCloud server given its URL.
42 * Checks the existence of a configured ownCloud server in the URL, gets its version
43 * and finds out what authentication method is needed to access files in it.
46 public class GetServerInfoOperation
extends RemoteOperation
{
48 private static final String TAG
= GetServerInfoOperation
.class.getSimpleName();
51 private Context mContext
;
53 private ServerInfo mResultData
;
58 * @param url URL to an ownCloud server.
59 * @param context Android context; needed to check network state
60 * TODO ugly dependency, get rid of it.
62 public GetServerInfoOperation(String url
, Context context
) {
63 mUrl
= trimWebdavSuffix(url
);
66 mResultData
= new ServerInfo();
71 * Performs the operation
73 * @return Result of the operation. If successful, includes an instance of
74 * {@link ServerInfo} with the information retrieved from the server.
75 * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
78 protected RemoteOperationResult
run(OwnCloudClient client
) {
80 // first: check the status of the server (including its version)
81 GetRemoteStatusOperation getStatus
= new GetRemoteStatusOperation(mContext
);
82 RemoteOperationResult result
= getStatus
.execute(client
, MainApp
.getUserAgent());
84 if (result
.isSuccess()) {
85 // second: get authentication method required by the server
86 mResultData
.mVersion
= (OwnCloudVersion
)(result
.getData().get(0));
87 mResultData
.mIsSslConn
= (result
.getCode() == ResultCode
.OK_SSL
);
88 mResultData
.mBaseUrl
= normalizeProtocolPrefix(mUrl
, mResultData
.mIsSslConn
);
89 RemoteOperationResult detectAuthResult
= detectAuthorizationMethod(client
);
91 // third: merge results
92 if (detectAuthResult
.isSuccess()) {
93 mResultData
.mAuthMethod
=
94 (AuthenticationMethod
)detectAuthResult
.getData().get(0);
95 ArrayList
<Object
> data
= new ArrayList
<Object
>();
96 data
.add(mResultData
);
99 result
= detectAuthResult
;
106 private RemoteOperationResult
detectAuthorizationMethod(OwnCloudClient client
) {
107 Log_OC
.d(TAG
, "Trying empty authorization to detect authentication method");
108 DetectAuthenticationMethodOperation operation
=
109 new DetectAuthenticationMethodOperation(mContext
);
110 return operation
.execute(client
, MainApp
.getUserAgent());
114 private String
trimWebdavSuffix(String url
) {
118 if (url
.endsWith("/")) {
119 url
= url
.substring(0, url
.length() - 1);
121 if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_4_0
)){
122 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_4_0
.length());
123 } else if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_2_0
)){
124 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_2_0
.length());
125 } else if (url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_1_2
)){
126 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_1_2
.length());
133 private String
normalizeProtocolPrefix(String url
, boolean isSslConn
) {
134 if (!url
.toLowerCase().startsWith("http://") &&
135 !url
.toLowerCase().startsWith("https://")) {
137 return "https://" + url
;
139 return "http://" + url
;
146 public static class ServerInfo
{
147 public OwnCloudVersion mVersion
= null
;
148 public String mBaseUrl
= "";
149 public AuthenticationMethod mAuthMethod
= AuthenticationMethod
.UNKNOWN
;
150 public boolean mIsSslConn
= false
;