Merge pull request #669 from grecep/master
[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.common.utils.Log_OC;
35 import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation;
36 import com.owncloud.android.lib.resources.status.OwnCloudVersion;
37 import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;
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 Context mContext;
57
58 private ServerInfo mResultData;
59
60 /**
61 * Constructor.
62 *
63 * @param url URL to an ownCloud server.
64 * @param context Android context; needed to check network state
65 * TODO ugly dependency, get rid of it.
66 */
67 public GetServerInfoOperation(String url, Context context) {
68 mUrl = trimWebdavSuffix(url);
69 mContext = context;
70
71 mResultData = new ServerInfo();
72 }
73
74
75 /**
76 * Performs the operation
77 *
78 * @return Result of the operation. If successful, includes an instance of
79 * {@link ServerInfo} with the information retrieved from the server.
80 * Call {@link RemoteOperationResult#getData()}.get(0) to get it.
81 */
82 @Override
83 protected RemoteOperationResult run(OwnCloudClient client) {
84
85 // first: check the status of the server (including its version)
86 GetRemoteStatusOperation getStatus = new GetRemoteStatusOperation(mContext);
87 RemoteOperationResult result = getStatus.execute(client);
88
89 if (result.isSuccess()) {
90 // second: get authentication method required by the server
91 mResultData.mVersion = (OwnCloudVersion)(result.getData().get(0));
92 mResultData.mIsSslConn = (result.getCode() == ResultCode.OK_SSL);
93 mResultData.mBaseUrl = normalizeProtocolPrefix(mUrl, mResultData.mIsSslConn);
94 RemoteOperationResult detectAuthResult = detectAuthorizationMethod(client);
95
96 // third: merge results
97 if (detectAuthResult.isSuccess()) {
98 mResultData.mAuthMethod =
99 (AuthenticationMethod)detectAuthResult.getData().get(0);
100 ArrayList<Object> data = new ArrayList<Object>();
101 data.add(mResultData);
102 result.setData(data);
103 } else {
104 result = detectAuthResult;
105 }
106 }
107 return result;
108 }
109
110
111 private RemoteOperationResult detectAuthorizationMethod(OwnCloudClient client) {
112 Log_OC.d(TAG, "Trying empty authorization to detect authentication method");
113 DetectAuthenticationMethodOperation operation =
114 new DetectAuthenticationMethodOperation(mContext);
115 return operation.execute(client);
116 }
117
118
119 private String trimWebdavSuffix(String url) {
120 if (url == null) {
121 url = "";
122 } else {
123 if (url.endsWith("/")) {
124 url = url.substring(0, url.length() - 1);
125 }
126 if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_4_0)){
127 url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_4_0.length());
128 } else if(url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_2_0)){
129 url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_2_0.length());
130 } else if (url.toLowerCase().endsWith(AccountUtils.WEBDAV_PATH_1_2)){
131 url = url.substring(0, url.length() - AccountUtils.WEBDAV_PATH_1_2.length());
132 }
133 }
134 return url;
135 }
136
137
138 private String normalizeProtocolPrefix(String url, boolean isSslConn) {
139 if (!url.toLowerCase().startsWith("http://") &&
140 !url.toLowerCase().startsWith("https://")) {
141 if (isSslConn) {
142 return "https://" + url;
143 } else {
144 return "http://" + url;
145 }
146 }
147 return url;
148 }
149
150
151 public static class ServerInfo {
152 public OwnCloudVersion mVersion = null;
153 public String mBaseUrl = "";
154 public AuthenticationMethod mAuthMethod = AuthenticationMethod.UNKNOWN;
155 public boolean mIsSslConn = false;
156 }
157
158 }