GetServerInfoOperation.java 6.1 KB

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