Merge pull request #458 from owncloud/operations_service
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / GetServerInfoOperation.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.operations;
26
27 import java.util.ArrayList;
28
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;
38
39 import android.content.Context;
40
41 /**
42 * Get basic information from an ownCloud server given its URL.
43 *
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.
46 *
47 * @author David A. Velasco
48 * @author masensio
49 */
50
51 public class GetServerInfoOperation extends RemoteOperation {
52
53 private static final String TAG = GetServerInfoOperation.class.getSimpleName();
54
55 private String mUrl;
56 private String mAuthTokenType;
57 private Context mContext;
58
59 private ServerInfo mResultData;
60
61 /**
62 * Constructor.
63 *
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.
69 */
70 public GetServerInfoOperation(String url, String authTokenType, Context context) {
71 mUrl = trimWebdavSuffix(url);
72 mAuthTokenType = authTokenType;
73 mContext = context;
74
75 mResultData = new ServerInfo();
76 }
77
78
79 /**
80 * Performs the operation
81 *
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.
85 */
86 @Override
87 protected RemoteOperationResult run(OwnCloudClient client) {
88
89 // first: check the status of the server (including its version)
90 GetRemoteStatusOperation getStatus = new GetRemoteStatusOperation(mUrl, mContext);
91 RemoteOperationResult result = getStatus.execute(client);
92
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);
99
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);
107 } else {
108 result = detectAuthResult;
109 }
110 }
111 return result;
112 }
113
114
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);
122 }
123
124
125 private String trimWebdavSuffix(String url) {
126 if (url == null) {
127 url = "";
128 } else {
129 if (url.endsWith("/")) {
130 url = url.substring(0, url.length() - 1);
131 }
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());
138 }
139 }
140 return url;
141 }
142
143
144 private String normalizeProtocolPrefix(String url, boolean isSslConn) {
145 if (!url.toLowerCase().startsWith("http://") &&
146 !url.toLowerCase().startsWith("https://")) {
147 if (isSslConn) {
148 return "https://" + url;
149 } else {
150 return "http://" + url;
151 }
152 }
153 return url;
154 }
155
156
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;
162 }
163
164 }