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