506636f73742e7b935712ba73dad161c236e8256
[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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.operations;
20
21 import org.apache.commons.httpclient.HttpStatus;
22 import org.apache.commons.httpclient.methods.HeadMethod;
23
24 import eu.alefzero.webdav.WebdavClient;
25 import android.content.Context;
26 import android.net.ConnectivityManager;
27 import android.util.Log;
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 private String mAccessToken;
45
46
47 /**
48 * Simple constructor. Success when the path in the server exists.
49 *
50 * @param path Path to append to the URL owned by the client instance.
51 * @param context Android application context.
52 * @param accessToken Access token for Bearer Authentication -> TODO: move to other place
53 */
54 public ExistenceCheckOperation(String path, Context context, String accessToken) {
55 this(path, context, false);
56 mAccessToken = accessToken;
57 }
58
59
60 /**
61 * Full constructor. Success of the operation will depend upon the value of successIfAbsent.
62 *
63 * @param path Path to append to the URL owned by the client instance.
64 * @param context Android application context.
65 * @param successIfAbsent When 'true', the operation finishes in success if the path does NOT exist in the remote server (HTTP 404).
66 */
67 public ExistenceCheckOperation(String path, Context context, boolean successIfAbsent) {
68 mPath = (path != null) ? path : "";
69 mContext = context;
70 mSuccessIfAbsent = successIfAbsent;
71 }
72
73
74 @Override
75 protected RemoteOperationResult run(WebdavClient client) {
76 if (!isOnline()) {
77 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
78 }
79 RemoteOperationResult result = null;
80 HeadMethod head = null;
81 try {
82 head = new HeadMethod(client.getBaseUri() + mPath);
83 head.addRequestHeader("Authorization", "Bearer " + mAccessToken); // TODO put in some general place
84
85 int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
86 client.exhaustResponse(head.getResponseBodyAsStream());
87 boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent) || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
88 result = new RemoteOperationResult(success, status);
89 Log.d(TAG, "Existence check for " + client.getBaseUri() + mPath + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + "finished with HTTP status " + status + (!success?"(FAIL)":""));
90
91 } catch (Exception e) {
92 result = new RemoteOperationResult(e);
93 Log.e(TAG, "Existence check for " + client.getBaseUri() + mPath + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " + result.getLogMessage(), result.getException());
94
95 } finally {
96 if (head != null)
97 head.releaseConnection();
98 }
99 return result;
100 }
101
102 private boolean isOnline() {
103 ConnectivityManager cm = (ConnectivityManager) mContext
104 .getSystemService(Context.CONNECTIVITY_SERVICE);
105 return cm != null && cm.getActiveNetworkInfo() != null
106 && cm.getActiveNetworkInfo().isConnectedOrConnecting();
107 }
108
109 }