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