1 /* ownCloud Android client application
3 * @author David A. Velasco
5 * Copyright (C) 2014 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.operations
;
23 import java
.util
.ArrayList
;
25 import com
.owncloud
.android
.authentication
.AccountUtils
;
26 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
27 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
28 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
30 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
31 import com
.owncloud
.android
.lib
.resources
.status
.GetRemoteStatusOperation
;
32 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
33 import com
.owncloud
.android
.operations
.DetectAuthenticationMethodOperation
.AuthenticationMethod
;
35 import android
.content
.Context
;
38 * Get basic information from an ownCloud server given its URL.
40 * Checks the existence of a configured ownCloud server in the URL, gets its version
41 * and finds out what authentication method is needed to access files in it.
44 public class GetServerInfoOperation
extends RemoteOperation
{
46 private static final String TAG
= GetServerInfoOperation
.class.getSimpleName();
49 private Context mContext
;
51 private ServerInfo mResultData
;
56 * @param url URL to an ownCloud server.
57 * @param context Android context; needed to check network state
58 * TODO ugly dependency, get rid of it.
60 public GetServerInfoOperation(String url
, Context context
) {
61 mUrl
= trimWebdavSuffix(url
);
64 mResultData
= new ServerInfo();
69 * Performs the operation
71 * @return Result of the operation. If successful, includes an instance of
72 * {@link ServerInfo} with the information retrieved from the server.
73 * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
76 protected RemoteOperationResult
run(OwnCloudClient client
) {
78 // first: check the status of the server (including its version)
79 GetRemoteStatusOperation getStatus
= new GetRemoteStatusOperation(mContext
);
80 RemoteOperationResult result
= getStatus
.execute(client
);
82 if (result
.isSuccess()) {
83 // second: get authentication method required by the server
84 mResultData
.mVersion
= (OwnCloudVersion
)(result
.getData().get(0));
85 mResultData
.mIsSslConn
= (result
.getCode() == ResultCode
.OK_SSL
);
86 mResultData
.mBaseUrl
= normalizeProtocolPrefix(mUrl
, mResultData
.mIsSslConn
);
87 RemoteOperationResult detectAuthResult
= detectAuthorizationMethod(client
);
89 // third: merge results
90 if (detectAuthResult
.isSuccess()) {
91 mResultData
.mAuthMethod
=
92 (AuthenticationMethod
)detectAuthResult
.getData().get(0);
93 ArrayList
<Object
> data
= new ArrayList
<Object
>();
94 data
.add(mResultData
);
97 result
= detectAuthResult
;
104 private RemoteOperationResult
detectAuthorizationMethod(OwnCloudClient client
) {
105 Log_OC
.d(TAG
, "Trying empty authorization to detect authentication method");
106 DetectAuthenticationMethodOperation operation
=
107 new DetectAuthenticationMethodOperation(mContext
);
108 return operation
.execute(client
);
112 private String
trimWebdavSuffix(String url
) {
116 if (url
.endsWith("/")) {
117 url
= url
.substring(0, url
.length() - 1);
119 if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_4_0
)){
120 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_4_0
.length());
121 } else if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_2_0
)){
122 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_2_0
.length());
123 } else if (url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_1_2
)){
124 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_1_2
.length());
131 private String
normalizeProtocolPrefix(String url
, boolean isSslConn
) {
132 if (!url
.toLowerCase().startsWith("http://") &&
133 !url
.toLowerCase().startsWith("https://")) {
135 return "https://" + url
;
137 return "http://" + url
;
144 public static class ServerInfo
{
145 public OwnCloudVersion mVersion
= null
;
146 public String mBaseUrl
= "";
147 public AuthenticationMethod mAuthMethod
= AuthenticationMethod
.UNKNOWN
;
148 public boolean mIsSslConn
= false
;