[tx-robot] updated from transifex
[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.authentication.AccountUtils;
27 import com.owncloud.android.lib.common.OwnCloudClient;
28 import com.owncloud.android.lib.common.operations.RemoteOperation;
29 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
30 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
31 import com.owncloud.android.lib.common.utils.Log_OC;
32 import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation;
33 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
34 import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;
35
36 import android.content.Context;
37
38 /**
39 * Get basic information from an ownCloud server given its URL.
40 *
41 * Checks the existence of a configured ownCloud server in the URL, gets its version
42 * and finds out what authentication method is needed to access files in it.
43 */
44
45 public class GetServerInfoOperation extends RemoteOperation {
46
47 private static final String TAG = GetServerInfoOperation.class.getSimpleName();
48
49 private String mUrl;
50 private Context mContext;
51
52 private ServerInfo mResultData;
53
54 /**
55 * Constructor.
56 *
57 * @param url URL to an ownCloud server.
58 * @param context Android context; needed to check network state
59 * TODO ugly dependency, get rid of it.
60 */
61 public GetServerInfoOperation(String url, Context context) {
62 mUrl = trimWebdavSuffix(url);
63 mContext = context;
64
65 mResultData = new ServerInfo();
66 }
67
68
69 /**
70 * Performs the operation
71 *
72 * @return Result of the operation. If successful, includes an instance of
73 * {@link ServerInfo} with the information retrieved from the server.
74 * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
75 */
76 @Override
77 protected RemoteOperationResult run(OwnCloudClient client) {
78
79 // first: check the status of the server (including its version)
80 GetRemoteStatusOperation getStatus = new GetRemoteStatusOperation(mContext);
81 RemoteOperationResult result = getStatus.execute(client);
82
83 if (result.isSuccess()) {
84 // second: get authentication method required by the server
85 mResultData.mVersion = (OwnCloudVersion)(result.getData().get(0));
86 mResultData.mIsSslConn = (result.getCode() == ResultCode.OK_SSL);
87 mResultData.mBaseUrl = normalizeProtocolPrefix(mUrl, mResultData.mIsSslConn);
88 RemoteOperationResult detectAuthResult = detectAuthorizationMethod(client);
89
90 // third: merge results
91 if (detectAuthResult.isSuccess()) {
92 mResultData.mAuthMethod =
93 (AuthenticationMethod)detectAuthResult.getData().get(0);
94 ArrayList<Object> data = new ArrayList<Object>();
95 data.add(mResultData);
96 result.setData(data);
97 } else {
98 result = detectAuthResult;
99 }
100 }
101 return result;
102 }
103
104
105 private RemoteOperationResult detectAuthorizationMethod(OwnCloudClient client) {
106 Log_OC.d(TAG, "Trying empty authorization to detect authentication method");
107 DetectAuthenticationMethodOperation operation =
108 new DetectAuthenticationMethodOperation(mContext);
109 return operation.execute(client);
110 }
111
112
113 private String trimWebdavSuffix(String url) {
114 if (url == null) {
115 url = "";
116 } else {
117 if (url.endsWith("/")) {
118 url = url.substring(0, url.length() - 1);
119 }
120 if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0_AND_LATER)){
121 url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0_AND_LATER.length());
122 }
123 }
124 return url;
125 }
126
127
128 private String normalizeProtocolPrefix(String url, boolean isSslConn) {
129 if (!url.toLowerCase().startsWith("http://") &&
130 !url.toLowerCase().startsWith("https://")) {
131 if (isSslConn) {
132 return "https://" + url;
133 } else {
134 return "http://" + url;
135 }
136 }
137 return url;
138 }
139
140
141 public static class ServerInfo {
142 public OwnCloudVersion mVersion = null;
143 public String mBaseUrl = "";
144 public AuthenticationMethod mAuthMethod = AuthenticationMethod.UNKNOWN;
145 public boolean mIsSslConn = false;
146 }
147
148 }