- HeadMethod head = null;
- try {
- head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath));
- int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
- client.exhaustResponse(head.getResponseBodyAsStream());
- boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
- result = new RemoteOperationResult(success, status, head.getResponseHeaders());
- Log.d(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":""));
-
- } catch (Exception e) {
- result = new RemoteOperationResult(e);
- Log.e(TAG, "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException());
+ AuthenticationMethod authMethod = AuthenticationMethod.UNKNOWN;
+
+ RemoteOperation operation = new ExistenceCheckRemoteOperation("", mContext, false);
+ client.setWebdavUri(Uri.parse(mWebDavUrl));
+ client.setBasicCredentials("", "");
+ client.setFollowRedirects(false);
+
+ // try to access the root folder, following redirections but not SAML SSO redirections
+ result = operation.execute(client);
+ String redirectedLocation = result.getRedirectedLocation();
+ while (redirectedLocation != null && redirectedLocation.length() > 0 && !result.isIdPRedirection()) {
+ client.setWebdavUri(Uri.parse(result.getRedirectedLocation()));
+ result = operation.execute(client);
+ redirectedLocation = result.getRedirectedLocation();
+ }
+
+ // analyze response
+ if (result.getCode() == ResultCode.UNAUTHORIZED) {
+ String authRequest = ((result.getAuthenticateHeader()).trim()).toLowerCase();
+ if (authRequest.startsWith("basic")) {
+ authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
+
+ } else if (authRequest.startsWith("bearer")) {
+ authMethod = AuthenticationMethod.BEARER_TOKEN;
+ }
+ // else - fall back to UNKNOWN
+
+ } else if (result.isSuccess()) {
+ authMethod = AuthenticationMethod.NONE;