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