e2c936fd6cde6bbe02bbc81ee16b51a93c363c3c
[pub/Android/ownCloud.git] /
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.operations.remote;
26
27 import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.commons.httpclient.methods.HeadMethod;
29
30 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
31 import com.owncloud.android.oc_framework.operations.RemoteOperation;
32 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
33 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
34
35 import android.content.Context;
36 import android.net.ConnectivityManager;
37 import android.util.Log;
38
39 /**
40 * Operation to check the existence or absence of a path in a remote server.
41 *
42 * @author David A. Velasco
43 */
44 public class ExistenceCheckRemoteOperation extends RemoteOperation {
45
46 /** Maximum time to wait for a response from the server in MILLISECONDs. */
47 public static final int TIMEOUT = 10000;
48
49 private static final String TAG = ExistenceCheckRemoteOperation.class.getSimpleName();
50
51 private String mPath;
52 private Context mContext;
53 private boolean mSuccessIfAbsent;
54
55
56 /**
57 * Full constructor. Success of the operation will depend upon the value of successIfAbsent.
58 *
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).
62 */
63 public ExistenceCheckRemoteOperation(String path, Context context, boolean successIfAbsent) {
64 mPath = (path != null) ? path : "";
65 mContext = context;
66 mSuccessIfAbsent = successIfAbsent;
67 }
68
69
70 @Override
71 protected RemoteOperationResult run(WebdavClient client) {
72 if (!isOnline()) {
73 return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
74 }
75 RemoteOperationResult result = null;
76 HeadMethod head = null;
77 try {
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)":""));
84
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());
88
89 } finally {
90 if (head != null)
91 head.releaseConnection();
92 }
93 return result;
94 }
95
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();
101 }
102
103
104 }