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