3c650010ce79c872ad873701dec63e111cc4c15f
[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
37 import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
38 import com.owncloud.android.oc_framework.network.CertificateCombinedException;
39
40 import android.accounts.Account;
41 import android.accounts.AccountsException;
42 import android.util.Log;
43
44
45 /**
46 * The result of a remote operation required to an ownCloud server.
47 *
48 * Provides a common classification of remote operation results for all the
49 * application.
50 *
51 * @author David A. Velasco
52 */
53 public class RemoteOperationResult implements Serializable {
54
55 /** Generated - should be refreshed every time the class changes!! */
56 private static final long serialVersionUID = -2469951225222759283L;
57
58 private static final String TAG = "RemoteOperationResult";
59
60 public enum ResultCode {
61 OK,
62 OK_SSL,
63 OK_NO_SSL,
64 UNHANDLED_HTTP_CODE,
65 UNAUTHORIZED,
66 FILE_NOT_FOUND,
67 INSTANCE_NOT_CONFIGURED,
68 UNKNOWN_ERROR,
69 WRONG_CONNECTION,
70 TIMEOUT,
71 INCORRECT_ADDRESS,
72 HOST_NOT_AVAILABLE,
73 NO_NETWORK_CONNECTION,
74 SSL_ERROR,
75 SSL_RECOVERABLE_PEER_UNVERIFIED,
76 BAD_OC_VERSION,
77 CANCELLED,
78 INVALID_LOCAL_FILE_NAME,
79 INVALID_OVERWRITE,
80 CONFLICT,
81 OAUTH2_ERROR,
82 SYNC_CONFLICT,
83 LOCAL_STORAGE_FULL,
84 LOCAL_STORAGE_NOT_MOVED,
85 LOCAL_STORAGE_NOT_COPIED,
86 OAUTH2_ERROR_ACCESS_DENIED,
87 QUOTA_EXCEEDED,
88 ACCOUNT_NOT_FOUND,
89 ACCOUNT_EXCEPTION,
90 ACCOUNT_NOT_NEW,
91 ACCOUNT_NOT_THE_SAME,
92 INVALID_CHARACTER_IN_NAME
93 }
94
95 private boolean mSuccess = false;
96 private int mHttpCode = -1;
97 private Exception mException = null;
98 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
99 private String mRedirectedLocation;
100
101 private RemoteFile mFile;
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 mFile= null;
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 {
198 mCode = ResultCode.UNKNOWN_ERROR;
199 }
200
201 }
202
203
204 public void setFile(RemoteFile file){
205 mFile = file;
206 }
207 public RemoteFile getFile(){
208 return mFile;
209 }
210 public void setData(ArrayList<RemoteFile> files){
211 mFiles = files;
212 }
213
214 public ArrayList<RemoteFile> getData(){
215 return mFiles;
216 }
217
218 public boolean isSuccess() {
219 return mSuccess;
220 }
221
222 public boolean isCancelled() {
223 return mCode == ResultCode.CANCELLED;
224 }
225
226 public int getHttpCode() {
227 return mHttpCode;
228 }
229
230 public ResultCode getCode() {
231 return mCode;
232 }
233
234 public Exception getException() {
235 return mException;
236 }
237
238 public boolean isSslRecoverableException() {
239 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
240 }
241
242 private CertificateCombinedException getCertificateCombinedException(Exception e) {
243 CertificateCombinedException result = null;
244 if (e instanceof CertificateCombinedException) {
245 return (CertificateCombinedException) e;
246 }
247 Throwable cause = mException.getCause();
248 Throwable previousCause = null;
249 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
250 previousCause = cause;
251 cause = cause.getCause();
252 }
253 if (cause != null && cause instanceof CertificateCombinedException) {
254 result = (CertificateCombinedException) cause;
255 }
256 return result;
257 }
258
259 public String getLogMessage() {
260
261 if (mException != null) {
262 if (mException instanceof OperationCancelledException) {
263 return "Operation cancelled by the caller";
264
265 } else if (mException instanceof SocketException) {
266 return "Socket exception";
267
268 } else if (mException instanceof SocketTimeoutException) {
269 return "Socket timeout exception";
270
271 } else if (mException instanceof ConnectTimeoutException) {
272 return "Connect timeout exception";
273
274 } else if (mException instanceof MalformedURLException) {
275 return "Malformed URL exception";
276
277 } else if (mException instanceof UnknownHostException) {
278 return "Unknown host exception";
279
280 } else if (mException instanceof CertificateCombinedException) {
281 if (((CertificateCombinedException) mException).isRecoverable())
282 return "SSL recoverable exception";
283 else
284 return "SSL exception";
285
286 } else if (mException instanceof SSLException) {
287 return "SSL exception";
288
289 } else if (mException instanceof DavException) {
290 return "Unexpected WebDAV exception";
291
292 } else if (mException instanceof HttpException) {
293 return "HTTP violation";
294
295 } else if (mException instanceof IOException) {
296 return "Unrecovered transport exception";
297
298 } else if (mException instanceof AccountNotFoundException) {
299 Account failedAccount = ((AccountNotFoundException)mException).getFailedAccount();
300 return mException.getMessage() + " (" + (failedAccount != null ? failedAccount.name : "NULL") + ")";
301
302 } else if (mException instanceof AccountsException) {
303 return "Exception while using account";
304
305 } else {
306 return "Unexpected exception";
307 }
308 }
309
310 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
311 return "The ownCloud server is not configured!";
312
313 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
314 return "No network connection";
315
316 } else if (mCode == ResultCode.BAD_OC_VERSION) {
317 return "No valid ownCloud version was found at the server";
318
319 } else if (mCode == ResultCode.LOCAL_STORAGE_FULL) {
320 return "Local storage full";
321
322 } else if (mCode == ResultCode.LOCAL_STORAGE_NOT_MOVED) {
323 return "Error while moving file to final directory";
324
325 } else if (mCode == ResultCode.ACCOUNT_NOT_NEW) {
326 return "Account already existing when creating a new one";
327
328 } else if (mCode == ResultCode.ACCOUNT_NOT_THE_SAME) {
329 return "Authenticated with a different account than the one updating";
330 } else if (mCode == ResultCode.INVALID_CHARACTER_IN_NAME) {
331 return "The file name contains an forbidden character";
332 }
333
334 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";
335
336 }
337
338 public boolean isServerFail() {
339 return (mHttpCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR);
340 }
341
342 public boolean isException() {
343 return (mException != null);
344 }
345
346 public boolean isTemporalRedirection() {
347 return (mHttpCode == 302 || mHttpCode == 307);
348 }
349
350 public String getRedirectedLocation() {
351 return mRedirectedLocation;
352 }
353
354 public boolean isIdPRedirection() {
355 return (mRedirectedLocation != null &&
356 (mRedirectedLocation.toUpperCase().contains("SAML") ||
357 mRedirectedLocation.toLowerCase().contains("wayf")));
358 }
359
360 }