ExistenceCheckOperation.java 3.7 KB

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