543b3a4b0b11732c54078b29252756a5e5b38741
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / RemoteOperationResult.java
1 package com.owncloud.android.oc_framework.operations;
2 /* ownCloud Android client application
3 * Copyright (C) 2012 Bartek Przybylski
4 * Copyright (C) 2012-2013 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20
21
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.net.MalformedURLException;
25 import java.net.SocketException;
26 import java.net.SocketTimeoutException;
27 import java.net.UnknownHostException;
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.authentication.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 = -4415103901492836870L;
57
58
59
60 private static final String TAG = "RemoteOperationResult";
61
62 public enum ResultCode {
63 OK,
64 OK_SSL,
65 OK_NO_SSL,
66 UNHANDLED_HTTP_CODE,
67 UNAUTHORIZED,
68 FILE_NOT_FOUND,
69 INSTANCE_NOT_CONFIGURED,
70 UNKNOWN_ERROR,
71 WRONG_CONNECTION,
72 TIMEOUT,
73 INCORRECT_ADDRESS,
74 HOST_NOT_AVAILABLE,
75 NO_NETWORK_CONNECTION,
76 SSL_ERROR,
77 SSL_RECOVERABLE_PEER_UNVERIFIED,
78 BAD_OC_VERSION,
79 CANCELLED,
80 INVALID_LOCAL_FILE_NAME,
81 INVALID_OVERWRITE,
82 CONFLICT,
83 OAUTH2_ERROR,
84 SYNC_CONFLICT,
85 LOCAL_STORAGE_FULL,
86 LOCAL_STORAGE_NOT_MOVED,
87 LOCAL_STORAGE_NOT_COPIED,
88 OAUTH2_ERROR_ACCESS_DENIED,
89 QUOTA_EXCEEDED,
90 ACCOUNT_NOT_FOUND,
91 ACCOUNT_EXCEPTION,
92 ACCOUNT_NOT_NEW,
93 ACCOUNT_NOT_THE_SAME
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 }
312
313 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";
314
315 }
316
317 public boolean isServerFail() {
318 return (mHttpCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR);
319 }
320
321 public boolean isException() {
322 return (mException != null);
323 }
324
325 public boolean isTemporalRedirection() {
326 return (mHttpCode == 302 || mHttpCode == 307);
327 }
328
329 public String getRedirectedLocation() {
330 return mRedirectedLocation;
331 }
332
333 public boolean isIdPRedirection() {
334 return (mRedirectedLocation != null &&
335 (mRedirectedLocation.toUpperCase().contains("SAML") ||
336 mRedirectedLocation.toLowerCase().contains("wayf")));
337 }
338
339 }