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