1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 package com
.owncloud
.android
.lib
.operations
.remote
;
27 import org
.apache
.commons
.httpclient
.HttpStatus
;
28 import org
.apache
.commons
.httpclient
.methods
.HeadMethod
;
30 import com
.owncloud
.android
.lib
.network
.OwnCloudClient
;
31 import com
.owncloud
.android
.lib
.network
.webdav
.WebdavUtils
;
32 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
33 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
35 import android
.content
.Context
;
36 import android
.net
.ConnectivityManager
;
37 import android
.util
.Log
;
40 * Operation to check the existence or absence of a path in a remote server.
42 * @author David A. Velasco
44 public class ExistenceCheckRemoteOperation
extends RemoteOperation
{
46 /** Maximum time to wait for a response from the server in MILLISECONDs. */
47 public static final int TIMEOUT
= 10000;
49 private static final String TAG
= ExistenceCheckRemoteOperation
.class.getSimpleName();
52 private Context mContext
;
53 private boolean mSuccessIfAbsent
;
57 * Full constructor. Success of the operation will depend upon the value of successIfAbsent.
59 * @param path Path to append to the URL owned by the client instance.
60 * @param context Android application context.
61 * @param successIfAbsent When 'true', the operation finishes in success if the path does NOT exist in the remote server (HTTP 404).
63 public ExistenceCheckRemoteOperation(String path
, Context context
, boolean successIfAbsent
) {
64 mPath
= (path
!= null
) ? path
: "";
66 mSuccessIfAbsent
= successIfAbsent
;
71 protected RemoteOperationResult
run(OwnCloudClient client
) {
73 return new RemoteOperationResult(RemoteOperationResult
.ResultCode
.NO_NETWORK_CONNECTION
);
75 RemoteOperationResult result
= null
;
76 HeadMethod head
= null
;
78 head
= new HeadMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mPath
));
79 int status
= client
.executeMethod(head
, TIMEOUT
, TIMEOUT
);
80 client
.exhaustResponse(head
.getResponseBodyAsStream());
81 boolean success
= (status
== HttpStatus
.SC_OK
&& !mSuccessIfAbsent
) || (status
== HttpStatus
.SC_NOT_FOUND
&& mSuccessIfAbsent
);
82 result
= new RemoteOperationResult(success
, status
, head
.getResponseHeaders());
83 Log
.d(TAG
, "Existence check for " + client
.getBaseUri() + WebdavUtils
.encodePath(mPath
) + " targeting for " + (mSuccessIfAbsent ?
" absence " : " existence ") + "finished with HTTP status " + status
+ (!success?
"(FAIL)":""));
85 } catch (Exception e
) {
86 result
= new RemoteOperationResult(e
);
87 Log
.e(TAG
, "Existence check for " + client
.getBaseUri() + WebdavUtils
.encodePath(mPath
) + " targeting for " + (mSuccessIfAbsent ?
" absence " : " existence ") + ": " + result
.getLogMessage(), result
.getException());
91 head
.releaseConnection();
96 private boolean isOnline() {
97 ConnectivityManager cm
= (ConnectivityManager
) mContext
98 .getSystemService(Context
.CONNECTIVITY_SERVICE
);
99 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
100 && cm
.getActiveNetworkInfo().isConnectedOrConnecting();