OC-1230
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoteOperationResult.java
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 version 2,
7 * as published by the Free Software Foundation.
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 java.io.IOException;
22 import java.io.Serializable;
23 import java.net.MalformedURLException;
24 import java.net.SocketException;
25 import java.net.SocketTimeoutException;
26 import java.net.UnknownHostException;
27
28 import javax.net.ssl.SSLException;
29
30 import org.apache.commons.httpclient.ConnectTimeoutException;
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.commons.httpclient.HttpStatus;
33 import org.apache.jackrabbit.webdav.DavException;
34
35 import android.accounts.Account;
36 import android.accounts.AccountsException;
37
38 import com.owncloud.android.Log_OC;
39 import com.owncloud.android.authentication.AccountUtils.AccountNotFoundException;
40 import com.owncloud.android.network.CertificateCombinedException;
41
42 /**
43 * The result of a remote operation required to an ownCloud server.
44 *
45 * Provides a common classification of remote operation results for all the
46 * application.
47 *
48 * @author David A. Velasco
49 */
50 public class RemoteOperationResult implements Serializable {
51
52 /** Generated - should be refreshed every time the class changes!! */
53 private static final long serialVersionUID = 6106167714625712390L;
54
55
56 private static final String TAG = "RemoteOperationResult";
57
58 public enum ResultCode {
59 OK,
60 OK_SSL,
61 OK_NO_SSL,
62 OK_NO_CHANGES_ON_DIR,
63 UNHANDLED_HTTP_CODE,
64 UNAUTHORIZED,
65 FILE_NOT_FOUND,
66 INSTANCE_NOT_CONFIGURED,
67 UNKNOWN_ERROR,
68 WRONG_CONNECTION,
69 TIMEOUT,
70 INCORRECT_ADDRESS,
71 HOST_NOT_AVAILABLE,
72 NO_NETWORK_CONNECTION,
73 SSL_ERROR,
74 SSL_RECOVERABLE_PEER_UNVERIFIED,
75 BAD_OC_VERSION,
76 CANCELLED,
77 INVALID_LOCAL_FILE_NAME,
78 INVALID_OVERWRITE,
79 CONFLICT,
80 OAUTH2_ERROR,
81 SYNC_CONFLICT,
82 LOCAL_STORAGE_FULL,
83 LOCAL_STORAGE_NOT_MOVED,
84 LOCAL_STORAGE_NOT_COPIED,
85 OAUTH2_ERROR_ACCESS_DENIED,
86 QUOTA_EXCEEDED,
87 ACCOUNT_NOT_FOUND,
88 ACCOUNT_EXCEPTION
89 }
90
91 private boolean mSuccess = false;
92 private int mHttpCode = -1;
93 private Exception mException = null;
94 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
95
96 public RemoteOperationResult(ResultCode code) {
97 mCode = code;
98 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL || code == ResultCode.OK_NO_CHANGES_ON_DIR);
99 }
100
101 public RemoteOperationResult(boolean success, int httpCode) {
102 mSuccess = success;
103 mHttpCode = httpCode;
104
105 if (success) {
106 mCode = ResultCode.OK;
107
108 } else if (httpCode > 0) {
109 switch (httpCode) {
110 case HttpStatus.SC_UNAUTHORIZED:
111 mCode = ResultCode.UNAUTHORIZED;
112 break;
113 case HttpStatus.SC_NOT_FOUND:
114 mCode = ResultCode.FILE_NOT_FOUND;
115 break;
116 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
117 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
118 break;
119 case HttpStatus.SC_CONFLICT:
120 mCode = ResultCode.CONFLICT;
121 break;
122 case HttpStatus.SC_INSUFFICIENT_STORAGE:
123 mCode = ResultCode.QUOTA_EXCEEDED;
124 break;
125 default:
126 mCode = ResultCode.UNHANDLED_HTTP_CODE;
127 Log_OC.d(TAG, "RemoteOperationResult has prcessed UNHANDLED_HTTP_CODE: " + httpCode);
128 }
129 }
130 }
131
132 public RemoteOperationResult(Exception e) {
133 mException = e;
134
135 if (e instanceof OperationCancelledException) {
136 mCode = ResultCode.CANCELLED;
137
138 } else if (e instanceof SocketException) {
139 mCode = ResultCode.WRONG_CONNECTION;
140
141 } else if (e instanceof SocketTimeoutException) {
142 mCode = ResultCode.TIMEOUT;
143
144 } else if (e instanceof ConnectTimeoutException) {
145 mCode = ResultCode.TIMEOUT;
146
147 } else if (e instanceof MalformedURLException) {
148 mCode = ResultCode.INCORRECT_ADDRESS;
149
150 } else if (e instanceof UnknownHostException) {
151 mCode = ResultCode.HOST_NOT_AVAILABLE;
152
153 } else if (e instanceof AccountNotFoundException) {
154 mCode = ResultCode.ACCOUNT_NOT_FOUND;
155
156 } else if (e instanceof AccountsException) {
157 mCode = ResultCode.ACCOUNT_EXCEPTION;
158
159 } else if (e instanceof SSLException || e instanceof RuntimeException) {
160 CertificateCombinedException se = getCertificateCombinedException(e);
161 if (se != null) {
162 mException = se;
163 if (se.isRecoverable()) {
164 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
165 }
166 } else if (e instanceof RuntimeException) {
167 mCode = ResultCode.HOST_NOT_AVAILABLE;
168
169 } else {
170 mCode = ResultCode.SSL_ERROR;
171 }
172
173 } else {
174 mCode = ResultCode.UNKNOWN_ERROR;
175 }
176
177 }
178
179 public boolean isSuccess() {
180 return mSuccess;
181 }
182
183 public boolean isCancelled() {
184 return mCode == ResultCode.CANCELLED;
185 }
186
187 public int getHttpCode() {
188 return mHttpCode;
189 }
190
191 public ResultCode getCode() {
192 return mCode;
193 }
194
195 public Exception getException() {
196 return mException;
197 }
198
199 public boolean isSslRecoverableException() {
200 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
201 }
202
203 private CertificateCombinedException getCertificateCombinedException(Exception e) {
204 CertificateCombinedException result = null;
205 if (e instanceof CertificateCombinedException) {
206 return (CertificateCombinedException) e;
207 }
208 Throwable cause = mException.getCause();
209 Throwable previousCause = null;
210 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
211 previousCause = cause;
212 cause = cause.getCause();
213 }
214 if (cause != null && cause instanceof CertificateCombinedException) {
215 result = (CertificateCombinedException) cause;
216 }
217 return result;
218 }
219
220 public String getLogMessage() {
221
222 if (mException != null) {
223 if (mException instanceof OperationCancelledException) {
224 return "Operation cancelled by the caller";
225
226 } else if (mException instanceof SocketException) {
227 return "Socket exception";
228
229 } else if (mException instanceof SocketTimeoutException) {
230 return "Socket timeout exception";
231
232 } else if (mException instanceof ConnectTimeoutException) {
233 return "Connect timeout exception";
234
235 } else if (mException instanceof MalformedURLException) {
236 return "Malformed URL exception";
237
238 } else if (mException instanceof UnknownHostException) {
239 return "Unknown host exception";
240
241 } else if (mException instanceof CertificateCombinedException) {
242 if (((CertificateCombinedException) mException).isRecoverable())
243 return "SSL recoverable exception";
244 else
245 return "SSL exception";
246
247 } else if (mException instanceof SSLException) {
248 return "SSL exception";
249
250 } else if (mException instanceof DavException) {
251 return "Unexpected WebDAV exception";
252
253 } else if (mException instanceof HttpException) {
254 return "HTTP violation";
255
256 } else if (mException instanceof IOException) {
257 return "Unrecovered transport exception";
258
259 } else if (mException instanceof AccountNotFoundException) {
260 Account failedAccount = ((AccountNotFoundException)mException).getFailedAccount();
261 return mException.getMessage() + " (" + (failedAccount != null ? failedAccount.name : "NULL") + ")";
262
263 } else if (mException instanceof AccountsException) {
264 return "Exception while using account";
265
266 } else {
267 return "Unexpected exception";
268 }
269 }
270
271 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
272 return "The ownCloud server is not configured!";
273
274 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
275 return "No network connection";
276
277 } else if (mCode == ResultCode.BAD_OC_VERSION) {
278 return "No valid ownCloud version was found at the server";
279
280 } else if (mCode == ResultCode.LOCAL_STORAGE_FULL) {
281 return "Local storage full";
282
283 } else if (mCode == ResultCode.LOCAL_STORAGE_NOT_MOVED) {
284 return "Error while moving file to final directory";
285 }
286
287 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";
288
289 }
290
291 public boolean isServerFail() {
292 return (mHttpCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR);
293 }
294
295 public boolean isException() {
296 return (mException != null);
297 }
298
299 }