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 String mAuthTokenType
;
57 private Context mContext
;
59 private ServerInfo mResultData
;
64 * @param url URL to an ownCloud server.
65 * @param authTokenType Identifies the authorization token supported by the caller;
66 * TODO ugly dependency, get rid of it.
67 * @param context Android context; needed to check network state
68 * TODO ugly dependency, get rid of it.
70 public GetServerInfoOperation(String url
, String authTokenType
, Context context
) {
71 mUrl
= trimWebdavSuffix(url
);
72 mAuthTokenType
= authTokenType
;
75 mResultData
= new ServerInfo();
80 * Performs the operation
82 * @return Result of the operation. If successful, includes an instance of
83 * {@link ServerInfo} with the information retrieved from the server.
84 * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
87 protected RemoteOperationResult
run(OwnCloudClient client
) {
89 // first: check the status of the server (including its version)
90 GetRemoteStatusOperation getStatus
= new GetRemoteStatusOperation(mUrl
, mContext
);
91 RemoteOperationResult result
= getStatus
.execute(client
);
93 if (result
.isSuccess()) {
94 // second: get authentication method required by the server
95 mResultData
.mVersion
= (OwnCloudVersion
)(result
.getData().get(0));
96 mResultData
.mIsSslConn
= (result
.getCode() == ResultCode
.OK_SSL
);
97 mResultData
.mBaseUrl
= normalizeProtocolPrefix(mUrl
, mResultData
.mIsSslConn
);
98 RemoteOperationResult detectAuthResult
= detectAuthorizationMethod(client
);
100 // third: merge results
101 if (detectAuthResult
.isSuccess()) {
102 mResultData
.mAuthMethod
=
103 (AuthenticationMethod
)detectAuthResult
.getData().get(0);
104 ArrayList
<Object
> data
= new ArrayList
<Object
>();
105 data
.add(mResultData
);
106 result
.setData(data
);
108 result
= detectAuthResult
;
115 private RemoteOperationResult
detectAuthorizationMethod(OwnCloudClient client
) {
116 Log_OC
.d(TAG
, "Trying empty authorization to detect authentication method");
117 String webdav_path
= AccountUtils
.getWebdavPath(mResultData
.mVersion
, mAuthTokenType
);
118 String webdav_url
= mResultData
.mBaseUrl
+ webdav_path
;
119 DetectAuthenticationMethodOperation operation
=
120 new DetectAuthenticationMethodOperation(mContext
, webdav_url
);
121 return operation
.execute(client
);
125 private String
trimWebdavSuffix(String url
) {
129 if (url
.endsWith("/")) {
130 url
= url
.substring(0, url
.length() - 1);
132 if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_4_0
)){
133 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_4_0
.length());
134 } else if(url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_2_0
)){
135 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_2_0
.length());
136 } else if (url
.toLowerCase().endsWith(AccountUtils
.WEBDAV_PATH_1_2
)){
137 url
= url
.substring(0, url
.length() - AccountUtils
.WEBDAV_PATH_1_2
.length());
144 private String
normalizeProtocolPrefix(String url
, boolean isSslConn
) {
145 if (!url
.toLowerCase().startsWith("http://") &&
146 !url
.toLowerCase().startsWith("https://")) {
148 return "https://" + url
;
150 return "http://" + url
;
157 public static class ServerInfo
{
158 public OwnCloudVersion mVersion
= null
;
159 public String mBaseUrl
= "";
160 public AuthenticationMethod mAuthMethod
= AuthenticationMethod
.UNKNOWN
;
161 public boolean mIsSslConn
= false
;