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