5c857fb149f2621366f2dcffd640d141e2ac8547
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / 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.oc_framework.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 import java.util.ArrayList;
28
29 import javax.net.ssl.SSLException;
30
31 import org.apache.commons.httpclient.ConnectTimeoutException;
32 import org.apache.commons.httpclient.Header;
33 import org.apache.commons.httpclient.HttpException;
34 import org.apache.commons.httpclient.HttpStatus;
35 import org.apache.jackrabbit.webdav.DavException;
36 import org.json.JSONException;
37
38 import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
39 import com.owncloud.android.oc_framework.network.CertificateCombinedException;
40
41 import android.accounts.Account;
42 import android.accounts.AccountsException;
43 import android.util.Log;
44
45
46 /**
47 * The result of a remote operation required to an ownCloud server.
48 *
49 * Provides a common classification of remote operation results for all the
50 * application.
51 *
52 * @author David A. Velasco
53 */
54 public class RemoteOperationResult implements Serializable {
55
56 /** Generated - should be refreshed every time the class changes!! */
57 private static final long serialVersionUID = -2469951225222759283L;
58
59 private static final String TAG = "RemoteOperationResult";
60
61 public enum ResultCode {
62 OK,
63 OK_SSL,
64 OK_NO_SSL,
65 UNHANDLED_HTTP_CODE,
66 UNAUTHORIZED,
67 FILE_NOT_FOUND,
68 INSTANCE_NOT_CONFIGURED,
69 UNKNOWN_ERROR,
70 WRONG_CONNECTION,
71 TIMEOUT,
72 INCORRECT_ADDRESS,
73 HOST_NOT_AVAILABLE,
74 NO_NETWORK_CONNECTION,
75 SSL_ERROR,
76 SSL_RECOVERABLE_PEER_UNVERIFIED,
77 BAD_OC_VERSION,
78 CANCELLED,
79 INVALID_LOCAL_FILE_NAME,
80 INVALID_OVERWRITE,
81 CONFLICT,
82 OAUTH2_ERROR,
83 SYNC_CONFLICT,
84 LOCAL_STORAGE_FULL,
85 LOCAL_STORAGE_NOT_MOVED,
86 LOCAL_STORAGE_NOT_COPIED,
87 OAUTH2_ERROR_ACCESS_DENIED,
88 QUOTA_EXCEEDED,
89 ACCOUNT_NOT_FOUND,
90 ACCOUNT_EXCEPTION,
91 ACCOUNT_NOT_NEW,
92 ACCOUNT_NOT_THE_SAME,
93 INVALID_CHARACTER_IN_NAME,
94 JSON_EXCEPTION
95 }
96
97 private boolean mSuccess = false;
98 private int mHttpCode = -1;
99 private Exception mException = null;
100 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
101 private String mRedirectedLocation;
102
103 private ArrayList<RemoteFile> mFiles;
104
105 public RemoteOperationResult(ResultCode code) {
106 mCode = code;
107 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
108 mFiles = null;
109 }
110
111 private RemoteOperationResult(boolean success, int httpCode) {
112 mSuccess = success;
113 mHttpCode = httpCode;
114
115 if (success) {
116 mCode = ResultCode.OK;
117
118 } else if (httpCode > 0) {
119 switch (httpCode) {
120 case HttpStatus.SC_UNAUTHORIZED:
121 mCode = ResultCode.UNAUTHORIZED;
122 break;
123 case HttpStatus.SC_NOT_FOUND:
124 mCode = ResultCode.FILE_NOT_FOUND;
125 break;
126 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
127 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
128 break;
129 case HttpStatus.SC_CONFLICT:
130 mCode = ResultCode.CONFLICT;
131 break;
132 case HttpStatus.SC_INSUFFICIENT_STORAGE:
133 mCode = ResultCode.QUOTA_EXCEEDED;
134 break;
135 default:
136 mCode = ResultCode.UNHANDLED_HTTP_CODE;
137 Log.d(TAG, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode);
138 }
139 }
140 }
141
142 public RemoteOperationResult(boolean success, int httpCode, Header[] headers) {
143 this(success, httpCode);
144 if (headers != null) {
145 Header current;
146 for (int i=0; i<headers.length; i++) {
147 current = headers[i];
148 if ("Location".equals(current.getName())) {
149 mRedirectedLocation = current.getValue();
150 break;
151 }
152 }
153 }
154 }
155
156 public RemoteOperationResult(Exception e) {
157 mException = e;
158
159 if (e instanceof OperationCancelledException) {
160 mCode = ResultCode.CANCELLED;
161
162 } else if (e instanceof SocketException) {
163 mCode = ResultCode.WRONG_CONNECTION;
164
165 } else if (e instanceof SocketTimeoutException) {
166 mCode = ResultCode.TIMEOUT;
167
168 } else if (e instanceof ConnectTimeoutException) {
169 mCode = ResultCode.TIMEOUT;
170
171 } else if (e instanceof MalformedURLException) {
172 mCode = ResultCode.INCORRECT_ADDRESS;
173
174 } else if (e instanceof UnknownHostException) {
175 mCode = ResultCode.HOST_NOT_AVAILABLE;
176
177 } else if (e instanceof AccountNotFoundException) {
178 mCode = ResultCode.ACCOUNT_NOT_FOUND;
179
180 } else if (e instanceof AccountsException) {
181 mCode = ResultCode.ACCOUNT_EXCEPTION;
182
183 } else if (e instanceof SSLException || e instanceof RuntimeException) {
184 CertificateCombinedException se = getCertificateCombinedException(e);
185 if (se != null) {
186 mException = se;
187 if (se.isRecoverable()) {
188 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
189 }
190 } else if (e instanceof RuntimeException) {
191 mCode = ResultCode.HOST_NOT_AVAILABLE;
192
193 } else {
194 mCode = ResultCode.SSL_ERROR;
195 }
196
197 } else if (e instanceof JSONException) {
198 mCode = ResultCode.JSON_EXCEPTION;
199
200 } else {
201 mCode = ResultCode.UNKNOWN_ERROR;
202 }
203
204 }
205
206
207 public void setData(ArrayList<RemoteFile> files){
208 mFiles = files;
209 }
210
211 public ArrayList<RemoteFile> getData(){
212 return mFiles;
213 }
214
215 public boolean isSuccess() {
216 return mSuccess;
217 }
218
219 public boolean isCancelled() {
220 return mCode == ResultCode.CANCELLED;
221 }
222
223 public int getHttpCode() {
224 return mHttpCode;
225 }
226
227 public ResultCode getCode() {
228 return mCode;
229 }
230
231 public Exception getException() {
232 return mException;
233 }
234
235 public boolean isSslRecoverableException() {
236 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
237 }
238
239 private CertificateCombinedException getCertificateCombinedException(Exception e) {
240 CertificateCombinedException result = null;
241 if (e instanceof CertificateCombinedException) {
242 return (CertificateCombinedException) e;
243 }
244 Throwable cause = mException.getCause();
245 Throwable previousCause = null;
246 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
247 previousCause = cause;
248 cause = cause.getCause();
249 }
250 if (cause != null && cause instanceof CertificateCombinedException) {
251 result = (CertificateCombinedException) cause;
252 }
253 return result;
254 }
255
256 public String getLogMessage() {
257
258 if (mException != null) {
259 if (mException instanceof OperationCancelledException) {
260 return "Operation cancelled by the caller";
261
262 } else if (mException instanceof SocketException) {
263 return "Socket exception";
264
265 } else if (mException instanceof SocketTimeoutException) {
266 return "Socket timeout exception";
267
268 } else if (mException instanceof ConnectTimeoutException) {
269 return "Connect timeout exception";
270
271 } else if (mException instanceof MalformedURLException) {
272 return "Malformed URL exception";
273
274 } else if (mException instanceof UnknownHostException) {
275 return "Unknown host exception";
276
277 } else if (mException instanceof CertificateCombinedException) {
278 if (((CertificateCombinedException) mException).isRecoverable())
279 return "SSL recoverable exception";
280 else
281 return "SSL exception";
282
283 } else if (mException instanceof SSLException) {
284 return "SSL exception";
285
286 } else if (mException instanceof DavException) {
287 return "Unexpected WebDAV exception";
288
289 } else if (mException instanceof HttpException) {
290 return "HTTP violation";
291
292 } else if (mException instanceof IOException) {
293 return "Unrecovered transport exception";
294
295 } else if (mException instanceof AccountNotFoundException) {
296 Account failedAccount = ((AccountNotFoundException)mException).getFailedAccount();
297 return mException.getMessage() + " (" + (failedAccount != null ? failedAccount.name : "NULL") + ")";
298
299 } else if (mException instanceof AccountsException) {
300 return "Exception while using account";
301
302 } else if (mException instanceof JSONException) {
303 return "JSON exception";
304 } else {
305 return "Unexpected exception";
306 }
307 }
308
309 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
310 return "The ownCloud server is not configured!";
311
312 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
313 return "No network connection";
314
315 } else if (mCode == ResultCode.BAD_OC_VERSION) {
316 return "No valid ownCloud version was found at the server";
317
318 } else if (mCode == ResultCode.LOCAL_STORAGE_FULL) {
319 return "Local storage full";
320
321 } else if (mCode == ResultCode.LOCAL_STORAGE_NOT_MOVED) {
322 return "Error while moving file to final directory";
323
324 } else if (mCode == ResultCode.ACCOUNT_NOT_NEW) {
325 return "Account already existing when creating a new one";
326
327 } else if (mCode == ResultCode.ACCOUNT_NOT_THE_SAME) {
328 return "Authenticated with a different account than the one updating";
329 } else if (mCode == ResultCode.INVALID_CHARACTER_IN_NAME) {
330 return "The file name contains an forbidden character";
331 }
332
333 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";
334
335 }
336
337 public boolean isServerFail() {
338 return (mHttpCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR);
339 }
340
341 public boolean isException() {
342 return (mException != null);
343 }
344
345 public boolean isTemporalRedirection() {
346 return (mHttpCode == 302 || mHttpCode == 307);
347 }
348
349 public String getRedirectedLocation() {
350 return mRedirectedLocation;
351 }
352
353 public boolean isIdPRedirection() {
354 return (mRedirectedLocation != null &&
355 (mRedirectedLocation.toUpperCase().contains("SAML") ||
356 mRedirectedLocation.toLowerCase().contains("wayf")));
357 }
358
359 }