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