ada12d252302d05a46997948366e6b6ac0b77da8
[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 private String mUserName;
105
106 public RemoteOperationResult(ResultCode code) {
107 mCode = code;
108 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
109 mFiles = null;
110 setUserName("");
111 }
112
113 private RemoteOperationResult(boolean success, int httpCode) {
114 mSuccess = success;
115 mHttpCode = httpCode;
116
117 if (success) {
118 mCode = ResultCode.OK;
119
120 } else if (httpCode > 0) {
121 switch (httpCode) {
122 case HttpStatus.SC_UNAUTHORIZED:
123 mCode = ResultCode.UNAUTHORIZED;
124 break;
125 case HttpStatus.SC_NOT_FOUND:
126 mCode = ResultCode.FILE_NOT_FOUND;
127 break;
128 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
129 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
130 break;
131 case HttpStatus.SC_CONFLICT:
132 mCode = ResultCode.CONFLICT;
133 break;
134 case HttpStatus.SC_INSUFFICIENT_STORAGE:
135 mCode = ResultCode.QUOTA_EXCEEDED;
136 break;
137 default:
138 mCode = ResultCode.UNHANDLED_HTTP_CODE;
139 Log.d(TAG, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode);
140 }
141 }
142 }
143
144 public RemoteOperationResult(boolean success, int httpCode, Header[] headers) {
145 this(success, httpCode);
146 if (headers != null) {
147 Header current;
148 for (int i=0; i<headers.length; i++) {
149 current = headers[i];
150 if ("Location".equals(current.getName())) {
151 mRedirectedLocation = current.getValue();
152 break;
153 }
154 }
155 }
156 }
157
158 public RemoteOperationResult(Exception e) {
159 mException = e;
160
161 if (e instanceof OperationCancelledException) {
162 mCode = ResultCode.CANCELLED;
163
164 } else if (e instanceof SocketException) {
165 mCode = ResultCode.WRONG_CONNECTION;
166
167 } else if (e instanceof SocketTimeoutException) {
168 mCode = ResultCode.TIMEOUT;
169
170 } else if (e instanceof ConnectTimeoutException) {
171 mCode = ResultCode.TIMEOUT;
172
173 } else if (e instanceof MalformedURLException) {
174 mCode = ResultCode.INCORRECT_ADDRESS;
175
176 } else if (e instanceof UnknownHostException) {
177 mCode = ResultCode.HOST_NOT_AVAILABLE;
178
179 } else if (e instanceof AccountNotFoundException) {
180 mCode = ResultCode.ACCOUNT_NOT_FOUND;
181
182 } else if (e instanceof AccountsException) {
183 mCode = ResultCode.ACCOUNT_EXCEPTION;
184
185 } else if (e instanceof SSLException || e instanceof RuntimeException) {
186 CertificateCombinedException se = getCertificateCombinedException(e);
187 if (se != null) {
188 mException = se;
189 if (se.isRecoverable()) {
190 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
191 }
192 } else if (e instanceof RuntimeException) {
193 mCode = ResultCode.HOST_NOT_AVAILABLE;
194
195 } else {
196 mCode = ResultCode.SSL_ERROR;
197 }
198
199 } else if (e instanceof JSONException) {
200 mCode = ResultCode.JSON_EXCEPTION;
201
202 } else {
203 mCode = ResultCode.UNKNOWN_ERROR;
204 }
205
206 }
207
208
209 public void setData(ArrayList<RemoteFile> files){
210 mFiles = files;
211 }
212
213 public ArrayList<RemoteFile> getData(){
214 return mFiles;
215 }
216
217 public boolean isSuccess() {
218 return mSuccess;
219 }
220
221 public boolean isCancelled() {
222 return mCode == ResultCode.CANCELLED;
223 }
224
225 public int getHttpCode() {
226 return mHttpCode;
227 }
228
229 public ResultCode getCode() {
230 return mCode;
231 }
232
233 public Exception getException() {
234 return mException;
235 }
236
237 public boolean isSslRecoverableException() {
238 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
239 }
240
241 private CertificateCombinedException getCertificateCombinedException(Exception e) {
242 CertificateCombinedException result = null;
243 if (e instanceof CertificateCombinedException) {
244 return (CertificateCombinedException) e;
245 }
246 Throwable cause = mException.getCause();
247 Throwable previousCause = null;
248 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
249 previousCause = cause;
250 cause = cause.getCause();
251 }
252 if (cause != null && cause instanceof CertificateCombinedException) {
253 result = (CertificateCombinedException) cause;
254 }
255 return result;
256 }
257
258 public String getLogMessage() {
259
260 if (mException != null) {
261 if (mException instanceof OperationCancelledException) {
262 return "Operation cancelled by the caller";
263
264 } else if (mException instanceof SocketException) {
265 return "Socket exception";
266
267 } else if (mException instanceof SocketTimeoutException) {
268 return "Socket timeout exception";
269
270 } else if (mException instanceof ConnectTimeoutException) {
271 return "Connect timeout exception";
272
273 } else if (mException instanceof MalformedURLException) {
274 return "Malformed URL exception";
275
276 } else if (mException instanceof UnknownHostException) {
277 return "Unknown host exception";
278
279 } else if (mException instanceof CertificateCombinedException) {
280 if (((CertificateCombinedException) mException).isRecoverable())
281 return "SSL recoverable exception";
282 else
283 return "SSL exception";
284
285 } else if (mException instanceof SSLException) {
286 return "SSL exception";
287
288 } else if (mException instanceof DavException) {
289 return "Unexpected WebDAV exception";
290
291 } else if (mException instanceof HttpException) {
292 return "HTTP violation";
293
294 } else if (mException instanceof IOException) {
295 return "Unrecovered transport exception";
296
297 } else if (mException instanceof AccountNotFoundException) {
298 Account failedAccount = ((AccountNotFoundException)mException).getFailedAccount();
299 return mException.getMessage() + " (" + (failedAccount != null ? failedAccount.name : "NULL") + ")";
300
301 } else if (mException instanceof AccountsException) {
302 return "Exception while using account";
303
304 } else if (mException instanceof JSONException) {
305 return "JSON exception";
306 } else {
307 return "Unexpected exception";
308 }
309 }
310
311 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
312 return "The ownCloud server is not configured!";
313
314 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
315 return "No network connection";
316
317 } else if (mCode == ResultCode.BAD_OC_VERSION) {
318 return "No valid ownCloud version was found at the server";
319
320 } else if (mCode == ResultCode.LOCAL_STORAGE_FULL) {
321 return "Local storage full";
322
323 } else if (mCode == ResultCode.LOCAL_STORAGE_NOT_MOVED) {
324 return "Error while moving file to final directory";
325
326 } else if (mCode == ResultCode.ACCOUNT_NOT_NEW) {
327 return "Account already existing when creating a new one";
328
329 } else if (mCode == ResultCode.ACCOUNT_NOT_THE_SAME) {
330 return "Authenticated with a different account than the one updating";
331 } else if (mCode == ResultCode.INVALID_CHARACTER_IN_NAME) {
332 return "The file name contains an forbidden character";
333 }
334
335 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";
336
337 }
338
339 public boolean isServerFail() {
340 return (mHttpCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR);
341 }
342
343 public boolean isException() {
344 return (mException != null);
345 }
346
347 public boolean isTemporalRedirection() {
348 return (mHttpCode == 302 || mHttpCode == 307);
349 }
350
351 public String getRedirectedLocation() {
352 return mRedirectedLocation;
353 }
354
355 public boolean isIdPRedirection() {
356 return (mRedirectedLocation != null &&
357 (mRedirectedLocation.toUpperCase().contains("SAML") ||
358 mRedirectedLocation.toLowerCase().contains("wayf")));
359 }
360
361 public String getUserName() {
362 return mUserName;
363 }
364
365 public void setUserName(String mUserName) {
366 this.mUserName = mUserName;
367 }
368
369 }