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 com
.owncloud
.android
.authentication
.AccountUtils
;
30 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
31 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
32 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
33 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
34 import com
.owncloud
.android
.lib
.resources
.status
.GetRemoteStatusOperation
;
35 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
;
36 import com
.owncloud
.android
.operations
.DetectAuthenticationMethodOperation
.AuthenticationMethod
;
37 import com
.owncloud
.android
.utils
.Log_OC
;
39 import android
.content
.Context
;
42 * Get basic information from an ownCloud server given its URL.
44 * Checks the existence of a configured ownCloud server in the URL, gets its version
45 * and finds out what authentication method is needed to access files in it.
47 * @author David A. Velasco
51 public class GetServerInfoOperation
extends RemoteOperation
{
53 private static final String TAG
= GetServerInfoOperation
.class.getSimpleName();
56 private Context mContext
;
58 private ServerInfo mResultData
;
63 * @param url URL to an ownCloud server.
64 * @param context Android context; needed to check network state
65 * TODO ugly dependency, get rid of it.
67 public GetServerInfoOperation(String url
, Context context
) {
68 mUrl
= trimWebdavSuffix(url
);
71 mResultData
= new ServerInfo();
76 * Performs the operation
78 * @return Result of the operation. If successful, includes an instance of
79 * {@link ServerInfo} with the information retrieved from the server.
80 * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
83 protected RemoteOperationResult
run(OwnCloudClient client
) {
85 // first: check the status of the server (including its version)
86 GetRemoteStatusOperation getStatus
= new GetRemoteStatusOperation(mContext
);
87 RemoteOperationResult result
= getStatus
.execute(client
);
89 if (result
.isSuccess()) {
90 // second: get authentication method required by the server
91 mResultData
.mVersion
= (OwnCloudVersion
)(result
.getData().get(0));
92 mResultData
.mIsSslConn
= (result
.getCode() == ResultCode
.OK_SSL
);
93 mResultData
.mBaseUrl
= normalizeProtocolPrefix(mUrl
, mResultData
.mIsSslConn
);
94 RemoteOperationResult detectAuthResult
= detectAuthorizationMethod(client
);
96 // third: merge results
97 if (detectAuthResult
.isSuccess()) {
98 mResultData
.mAuthMethod
=
99 (AuthenticationMethod
)detectAuthResult
.getData().get(0);
100 ArrayList
<Object
> data
= new ArrayList
<Object
>();
101 data
.add(mResultData
);
102 result
.setData(data
);
104 result
= detectAuthResult
;
111 private RemoteOperationResult
detectAuthorizationMethod(OwnCloudClient client
) {
112 Log_OC
.d(TAG
, "Trying empty authorization to detect authentication method");
113 DetectAuthenticationMethodOperation operation
=
114 new DetectAuthenticationMethodOperation(mContext
);
115 return operation
.execute(client
);
119 private String
trimWebdavSuffix(String url
) {
123 if (url
.endsWith("/")) {
124 url
= url
.substring(0, url
.length() - 1);
126 if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_4_0
)){
127 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_4_0
.length());
128 } else if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_2_0
)){
129 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_2_0
.length());
130 } else if (url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_1_2
)){
131 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_1_2
.length());
138 private String
normalizeProtocolPrefix(String url
, boolean isSslConn
) {
139 if (!url
.toLowerCase().startsWith("http://") &&
140 !url
.toLowerCase().startsWith("https://")) {
142 return "https://" + url
;
144 return "http://" + url
;
151 public static class ServerInfo
{
152 public OwnCloudVersion mVersion
= null
;
153 public String mBaseUrl
= "";
154 public AuthenticationMethod mAuthMethod
= AuthenticationMethod
.UNKNOWN
;
155 public boolean mIsSslConn
= false
;