Merge branch 'develop' into file_browsing_refactoring
[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.Log_OC;
24
25 import eu.alefzero.webdav.WebdavClient;
26 import android.content.Context;
27 import android.net.ConnectivityManager;
28
29 /**
30 * Operation to check the existence or absence of a path in a remote server.
31 *
32 * @author David A. Velasco
33 */
34 public class ExistenceCheckOperation extends RemoteOperation {
35
36 /** Maximum time to wait for a response from the server in MILLISECONDs. */
37 public static final int TIMEOUT = 10000;
38
39 private static final String TAG = ExistenceCheckOperation.class.getSimpleName();
40
41 private String mPath;
42 private Context mContext;
43 private boolean mSuccessIfAbsent;
44
45
46 /**
47 * Full constructor. Success of the operation will depend upon the value of successIfAbsent.
48 *
49 * @param path Path to append to the URL owned by the client instance.
50 * @param context Android application context.
51 * @param successIfAbsent When 'true', the operation finishes in success if the path does NOT exist in the remote server (HTTP 404).
52 */
53 public ExistenceCheckOperation(String path, Context context, boolean successIfAbsent) {
54 mPath = (path != null) ? path : "";
55 mContext = context;
56 mSuccessIfAbsent = successIfAbsent;
57 }
58
59
60 @Override
61 protected RemoteOperationResult run(WebdavClient client) {
62 if (!isOnline()) {
63 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
64 }
65 RemoteOperationResult result = null;
66 HeadMethod head = null;
67 try {
68 head = new HeadMethod(client.getBaseUri() + mPath);
69 int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
70 client.exhaustResponse(head.getResponseBodyAsStream());
71 boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
72 result = new RemoteOperationResult(success, status);
73 Log_OC.d(TAG, "Existence check for " + client.getBaseUri() + mPath + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":""));
74
75 } catch (Exception e) {
76 result = new RemoteOperationResult(e);
77 Log_OC.e(TAG, "Existence check for " + client.getBaseUri() + mPath + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException());
78
79 } finally {
80 if (head != null)
81 head.releaseConnection();
82 }
83 return result;
84 }
85
86 private boolean isOnline() {
87 ConnectivityManager cm = (ConnectivityManager) mContext
88 .getSystemService(Context.CONNECTIVITY_SERVICE);
89 return cm != null && cm.getActiveNetworkInfo() != null
90 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
91 }
92
93
94 }