Merge branch 'develop' into automationTest
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / GetServerInfoOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * @author masensio
6 * Copyright (C) 2015 ownCloud Inc.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 package com.owncloud.android.operations;
23
24 import java.util.ArrayList;
25
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;
36
37 import android.content.Context;
38
39 /**
40 * Get basic information from an ownCloud server given its URL.
41 *
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.
44 */
45
46 public class GetServerInfoOperation extends RemoteOperation {
47
48 private static final String TAG = GetServerInfoOperation.class.getSimpleName();
49
50 private String mUrl;
51 private Context mContext;
52
53 private ServerInfo mResultData;
54
55 /**
56 * Constructor.
57 *
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.
61 */
62 public GetServerInfoOperation(String url, Context context) {
63 mUrl = trimWebdavSuffix(url);
64 mContext = context;
65
66 mResultData = new ServerInfo();
67 }
68
69
70 /**
71 * Performs the operation
72 *
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.
76 */
77 @Override
78 protected RemoteOperationResult run(OwnCloudClient client) {
79
80 // first: check the status of the server (including its version)
81 GetRemoteStatusOperation getStatus = new GetRemoteStatusOperation(mContext);
82 RemoteOperationResult result = getStatus.execute(client);
83
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);
90
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);
97 result.setData(data);
98 } else {
99 result = detectAuthResult;
100 }
101 }
102 return result;
103 }
104
105
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);
111 }
112
113
114 private String trimWebdavSuffix(String url) {
115 if (url == null) {
116 url = "";
117 } else {
118 if (url.endsWith("/")) {
119 url = url.substring(0, url.length() - 1);
120 }
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());
127 }
128 }
129 return url;
130 }
131
132
133 private String normalizeProtocolPrefix(String url, boolean isSslConn) {
134 if (!url.toLowerCase().startsWith("http://") &&
135 !url.toLowerCase().startsWith("https://")) {
136 if (isSslConn) {
137 return "https://" + url;
138 } else {
139 return "http://" + url;
140 }
141 }
142 return url;
143 }
144
145
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;
151 }
152
153 }