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
.authentication
.AccountUtils
;
27 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
28 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
31 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
32 import com
.owncloud
.android
.lib
.resources
.status
.GetRemoteStatusOperation
;
33 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
34 import com
.owncloud
.android
.operations
.DetectAuthenticationMethodOperation
.AuthenticationMethod
;
36 import android
.content
.Context
;
39 * Get basic information from an ownCloud server given its URL.
41 * Checks the existence of a configured ownCloud server in the URL, gets its version
42 * and finds out what authentication method is needed to access files in it.
45 public class GetServerInfoOperation
extends RemoteOperation
{
47 private static final String TAG
= GetServerInfoOperation
.class.getSimpleName();
50 private Context mContext
;
52 private ServerInfo mResultData
;
57 * @param url URL to an ownCloud server.
58 * @param context Android context; needed to check network state
59 * TODO ugly dependency, get rid of it.
61 public GetServerInfoOperation(String url
, Context context
) {
62 mUrl
= trimWebdavSuffix(url
);
65 mResultData
= new ServerInfo();
70 * Performs the operation
72 * @return Result of the operation. If successful, includes an instance of
73 * {@link ServerInfo} with the information retrieved from the server.
74 * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
77 protected RemoteOperationResult
run(OwnCloudClient client
) {
79 // first: check the status of the server (including its version)
80 GetRemoteStatusOperation getStatus
= new GetRemoteStatusOperation(mContext
);
81 RemoteOperationResult result
= getStatus
.execute(client
);
83 if (result
.isSuccess()) {
84 // second: get authentication method required by the server
85 mResultData
.mVersion
= (OwnCloudVersion
)(result
.getData().get(0));
86 mResultData
.mIsSslConn
= (result
.getCode() == ResultCode
.OK_SSL
);
87 mResultData
.mBaseUrl
= normalizeProtocolPrefix(mUrl
, mResultData
.mIsSslConn
);
88 RemoteOperationResult detectAuthResult
= detectAuthorizationMethod(client
);
90 // third: merge results
91 if (detectAuthResult
.isSuccess()) {
92 mResultData
.mAuthMethod
=
93 (AuthenticationMethod
)detectAuthResult
.getData().get(0);
94 ArrayList
<Object
> data
= new ArrayList
<Object
>();
95 data
.add(mResultData
);
98 result
= detectAuthResult
;
105 private RemoteOperationResult
detectAuthorizationMethod(OwnCloudClient client
) {
106 Log_OC
.d(TAG
, "Trying empty authorization to detect authentication method");
107 DetectAuthenticationMethodOperation operation
=
108 new DetectAuthenticationMethodOperation(mContext
);
109 return operation
.execute(client
);
113 private String
trimWebdavSuffix(String url
) {
117 if (url
.endsWith("/")) {
118 url
= url
.substring(0, url
.length() - 1);
120 if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_4_0_AND_LATER
)){
121 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_4_0_AND_LATER
.length());
128 private String
normalizeProtocolPrefix(String url
, boolean isSslConn
) {
129 if (!url
.toLowerCase().startsWith("http://") &&
130 !url
.toLowerCase().startsWith("https://")) {
132 return "https://" + url
;
134 return "http://" + url
;
141 public static class ServerInfo
{
142 public OwnCloudVersion mVersion
= null
;
143 public String mBaseUrl
= "";
144 public AuthenticationMethod mAuthMethod
= AuthenticationMethod
.UNKNOWN
;
145 public boolean mIsSslConn
= false
;