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