RemoteOperationResult.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.operations;
  20. import java.io.IOException;
  21. import java.io.Serializable;
  22. import java.net.MalformedURLException;
  23. import java.net.SocketException;
  24. import java.net.SocketTimeoutException;
  25. import java.net.UnknownHostException;
  26. import javax.net.ssl.SSLException;
  27. import org.apache.commons.httpclient.ConnectTimeoutException;
  28. import org.apache.commons.httpclient.HttpException;
  29. import org.apache.commons.httpclient.HttpStatus;
  30. import org.apache.jackrabbit.webdav.DavException;
  31. import android.util.Log;
  32. import com.owncloud.android.network.CertificateCombinedException;
  33. /**
  34. * The result of a remote operation required to an ownCloud server.
  35. *
  36. * Provides a common classification of remote operation results for all the
  37. * application.
  38. *
  39. * @author David A. Velasco
  40. */
  41. public class RemoteOperationResult implements Serializable {
  42. /** Generated - should be refreshed every time the class changes!! */
  43. private static final long serialVersionUID = -7805531062432602444L;
  44. private static final String TAG = "RemoteOperationResult";
  45. public enum ResultCode {
  46. OK, OK_SSL, OK_NO_SSL, UNHANDLED_HTTP_CODE, UNAUTHORIZED, FILE_NOT_FOUND, INSTANCE_NOT_CONFIGURED, UNKNOWN_ERROR, WRONG_CONNECTION, TIMEOUT, INCORRECT_ADDRESS, HOST_NOT_AVAILABLE, NO_NETWORK_CONNECTION, SSL_ERROR, SSL_RECOVERABLE_PEER_UNVERIFIED, BAD_OC_VERSION, CANCELLED, INVALID_LOCAL_FILE_NAME, INVALID_OVERWRITE, CONFLICT, SYNC_CONFLICT, LOCAL_STORAGE_FULL, LOCAL_STORAGE_NOT_MOVED, LOCAL_STORAGE_NOT_COPIED, QUOTA_EXCEEDED
  47. }
  48. private boolean mSuccess = false;
  49. private int mHttpCode = -1;
  50. private Exception mException = null;
  51. private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
  52. public RemoteOperationResult(ResultCode code) {
  53. mCode = code;
  54. mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
  55. }
  56. public RemoteOperationResult(boolean success, int httpCode) {
  57. mSuccess = success;
  58. mHttpCode = httpCode;
  59. if (success) {
  60. mCode = ResultCode.OK;
  61. } else if (httpCode > 0) {
  62. switch (httpCode) {
  63. case HttpStatus.SC_UNAUTHORIZED:
  64. mCode = ResultCode.UNAUTHORIZED;
  65. break;
  66. case HttpStatus.SC_NOT_FOUND:
  67. mCode = ResultCode.FILE_NOT_FOUND;
  68. break;
  69. case HttpStatus.SC_INTERNAL_SERVER_ERROR:
  70. mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
  71. break;
  72. case HttpStatus.SC_CONFLICT:
  73. mCode = ResultCode.CONFLICT;
  74. break;
  75. case HttpStatus.SC_INSUFFICIENT_STORAGE:
  76. mCode = ResultCode.QUOTA_EXCEEDED;
  77. break;
  78. default:
  79. mCode = ResultCode.UNHANDLED_HTTP_CODE;
  80. Log.d(TAG, "RemoteOperationResult has prcessed UNHANDLED_HTTP_CODE: " + httpCode);
  81. }
  82. }
  83. }
  84. public RemoteOperationResult(Exception e) {
  85. mException = e;
  86. if (e instanceof OperationCancelledException) {
  87. mCode = ResultCode.CANCELLED;
  88. } else if (e instanceof SocketException) {
  89. mCode = ResultCode.WRONG_CONNECTION;
  90. } else if (e instanceof SocketTimeoutException) {
  91. mCode = ResultCode.TIMEOUT;
  92. } else if (e instanceof ConnectTimeoutException) {
  93. mCode = ResultCode.TIMEOUT;
  94. } else if (e instanceof MalformedURLException) {
  95. mCode = ResultCode.INCORRECT_ADDRESS;
  96. } else if (e instanceof UnknownHostException) {
  97. mCode = ResultCode.HOST_NOT_AVAILABLE;
  98. } else if (e instanceof SSLException || e instanceof RuntimeException) {
  99. CertificateCombinedException se = getCertificateCombinedException(e);
  100. if (se != null) {
  101. mException = se;
  102. if (se.isRecoverable()) {
  103. mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
  104. }
  105. } else if (e instanceof RuntimeException) {
  106. mCode = ResultCode.HOST_NOT_AVAILABLE;
  107. } else {
  108. mCode = ResultCode.SSL_ERROR;
  109. }
  110. } else {
  111. mCode = ResultCode.UNKNOWN_ERROR;
  112. }
  113. }
  114. public boolean isSuccess() {
  115. return mSuccess;
  116. }
  117. public boolean isCancelled() {
  118. return mCode == ResultCode.CANCELLED;
  119. }
  120. public int getHttpCode() {
  121. return mHttpCode;
  122. }
  123. public ResultCode getCode() {
  124. return mCode;
  125. }
  126. public Exception getException() {
  127. return mException;
  128. }
  129. public boolean isSslRecoverableException() {
  130. return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
  131. }
  132. private CertificateCombinedException getCertificateCombinedException(Exception e) {
  133. CertificateCombinedException result = null;
  134. if (e instanceof CertificateCombinedException) {
  135. return (CertificateCombinedException) e;
  136. }
  137. Throwable cause = mException.getCause();
  138. Throwable previousCause = null;
  139. while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
  140. previousCause = cause;
  141. cause = cause.getCause();
  142. }
  143. if (cause != null && cause instanceof CertificateCombinedException) {
  144. result = (CertificateCombinedException) cause;
  145. }
  146. return result;
  147. }
  148. public String getLogMessage() {
  149. if (mException != null) {
  150. if (mException instanceof OperationCancelledException) {
  151. return "Operation cancelled by the caller";
  152. } else if (mException instanceof SocketException) {
  153. return "Socket exception";
  154. } else if (mException instanceof SocketTimeoutException) {
  155. return "Socket timeout exception";
  156. } else if (mException instanceof ConnectTimeoutException) {
  157. return "Connect timeout exception";
  158. } else if (mException instanceof MalformedURLException) {
  159. return "Malformed URL exception";
  160. } else if (mException instanceof UnknownHostException) {
  161. return "Unknown host exception";
  162. } else if (mException instanceof CertificateCombinedException) {
  163. if (((CertificateCombinedException) mException).isRecoverable())
  164. return "SSL recoverable exception";
  165. else
  166. return "SSL exception";
  167. } else if (mException instanceof SSLException) {
  168. return "SSL exception";
  169. } else if (mException instanceof DavException) {
  170. return "Unexpected WebDAV exception";
  171. } else if (mException instanceof HttpException) {
  172. return "HTTP violation";
  173. } else if (mException instanceof IOException) {
  174. return "Unrecovered transport exception";
  175. } else {
  176. return "Unexpected exception";
  177. }
  178. }
  179. if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
  180. return "The ownCloud server is not configured!";
  181. } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
  182. return "No network connection";
  183. } else if (mCode == ResultCode.BAD_OC_VERSION) {
  184. return "No valid ownCloud version was found at the server";
  185. } else if (mCode == ResultCode.LOCAL_STORAGE_FULL) {
  186. return "Local storage full";
  187. } else if (mCode == ResultCode.LOCAL_STORAGE_NOT_MOVED) {
  188. return "Error while moving file to final directory";
  189. }
  190. return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";
  191. }
  192. }