Add author's line in license
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / GetServerInfoOperation.java
1 /* ownCloud Android client application
2 *
3 * @author David A. Velasco
4 * @author masensio
5 * Copyright (C) 2014 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 package com.owncloud.android.operations;
22
23 import java.util.ArrayList;
24
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;
34
35 import android.content.Context;
36
37 /**
38 * Get basic information from an ownCloud server given its URL.
39 *
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.
42 */
43
44 public class GetServerInfoOperation extends RemoteOperation {
45
46 private static final String TAG = GetServerInfoOperation.class.getSimpleName();
47
48 private String mUrl;
49 private Context mContext;
50
51 private ServerInfo mResultData;
52
53 /**
54 * Constructor.
55 *
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.
59 */
60 public GetServerInfoOperation(String url, Context context) {
61 mUrl = trimWebdavSuffix(url);
62 mContext = context;
63
64 mResultData = new ServerInfo();
65 }
66
67
68 /**
69 * Performs the operation
70 *
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.
74 */
75 @Override
76 protected RemoteOperationResult run(OwnCloudClient client) {
77
78 // first: check the status of the server (including its version)
79 GetRemoteStatusOperation getStatus = new GetRemoteStatusOperation(mContext);
80 RemoteOperationResult result = getStatus.execute(client);
81
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);
88
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);
95 result.setData(data);
96 } else {
97 result = detectAuthResult;
98 }
99 }
100 return result;
101 }
102
103
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);
109 }
110
111
112 private String trimWebdavSuffix(String url) {
113 if (url == null) {
114 url = "";
115 } else {
116 if (url.endsWith("/")) {
117 url = url.substring(0, url.length() - 1);
118 }
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());
125 }
126 }
127 return url;
128 }
129
130
131 private String normalizeProtocolPrefix(String url, boolean isSslConn) {
132 if (!url.toLowerCase().startsWith("http://") &&
133 !url.toLowerCase().startsWith("https://")) {
134 if (isSslConn) {
135 return "https://" + url;
136 } else {
137 return "http://" + url;
138 }
139 }
140 return url;
141 }
142
143
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;
149 }
150
151 }