Merge remote-tracking branch 'origin/develop' into develop
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / ExistenceCheckOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import org.apache.commons.httpclient.HttpStatus;
21 import org.apache.commons.httpclient.methods.HeadMethod;
22
23 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
24 import com.owncloud.android.oc_framework.operations.RemoteOperation;
25 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
26 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
27 import com.owncloud.android.utils.Log_OC;
28
29 import android.content.Context;
30 import android.net.ConnectivityManager;
31
32 /**
33 * Operation to check the existence or absence of a path in a remote server.
34 *
35 * @author David A. Velasco
36 */
37 public class ExistenceCheckOperation extends RemoteOperation {
38
39 /** Maximum time to wait for a response from the server in MILLISECONDs. */
40 public static final int TIMEOUT = 10000;
41
42 private static final String TAG = ExistenceCheckOperation.class.getSimpleName();
43
44 private String mPath;
45 private Context mContext;
46 private boolean mSuccessIfAbsent;
47
48
49 /**
50 * Full constructor. Success of the operation will depend upon the value of successIfAbsent.
51 *
52 * @param path Path to append to the URL owned by the client instance.
53 * @param context Android application context.
54 * @param successIfAbsent When 'true', the operation finishes in success if the path does NOT exist in the remote server (HTTP 404).
55 */
56 public ExistenceCheckOperation(String path, Context context, boolean successIfAbsent) {
57 mPath = (path != null) ? path : "";
58 mContext = context;
59 mSuccessIfAbsent = successIfAbsent;
60 }
61
62
63 @Override
64 protected RemoteOperationResult run(WebdavClient client) {
65 if (!isOnline()) {
66 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
67 }
68 RemoteOperationResult result = null;
69 HeadMethod head = null;
70 try {
71 head = new HeadMethod(client.getBaseUri() + WebdavUtils.encodePath(mPath));
72 int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
73 client.exhaustResponse(head.getResponseBodyAsStream());
74 boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
75 result = new RemoteOperationResult(success, status, head.getResponseHeaders());
76 Log_OC.d(TAG, "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":""));
77
78 } catch (Exception e) {
79 result = new RemoteOperationResult(e);
80 Log_OC.e(TAG, "Existence check for " + client.getBaseUri() + WebdavUtils.encodePath(mPath) + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException());
81
82 } finally {
83 if (head != null)
84 head.releaseConnection();
85 }
86 return result;
87 }
88
89 private boolean isOnline() {
90 ConnectivityManager cm = (ConnectivityManager) mContext
91 .getSystemService(Context.CONNECTIVITY_SERVICE);
92 return cm != null && cm.getActiveNetworkInfo() != null
93 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
94 }
95
96
97 }