1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.operations
;
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
;
28 import javax
.net
.ssl
.SSLException
;
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
;
36 import android
.accounts
.Account
;
37 import android
.accounts
.AccountsException
;
39 import com
.owncloud
.android
.Log_OC
;
40 import com
.owncloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
;
41 import com
.owncloud
.android
.network
.CertificateCombinedException
;
44 * The result of a remote operation required to an ownCloud server.
46 * Provides a common classification of remote operation results for all the
49 * @author David A. Velasco
51 public class RemoteOperationResult
implements Serializable
{
53 /** Generated - should be refreshed every time the class changes!! */
54 private static final long serialVersionUID
= -4415103901492836870L;
57 private static final String TAG
= "RemoteOperationResult";
59 public enum ResultCode
{
67 INSTANCE_NOT_CONFIGURED
,
73 NO_NETWORK_CONNECTION
,
75 SSL_RECOVERABLE_PEER_UNVERIFIED
,
78 INVALID_LOCAL_FILE_NAME
,
84 LOCAL_STORAGE_NOT_MOVED
,
85 LOCAL_STORAGE_NOT_COPIED
,
86 OAUTH2_ERROR_ACCESS_DENIED
,
94 private boolean mSuccess
= false
;
95 private int mHttpCode
= -1;
96 private Exception mException
= null
;
97 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
98 private String mRedirectedLocation
;
100 public RemoteOperationResult(ResultCode code
) {
102 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
|| code
== ResultCode
.OK_NO_CHANGES_ON_DIR
);
105 private RemoteOperationResult(boolean success
, int httpCode
) {
107 mHttpCode
= httpCode
;
110 mCode
= ResultCode
.OK
;
112 } else if (httpCode
> 0) {
114 case HttpStatus
.SC_UNAUTHORIZED
:
115 mCode
= ResultCode
.UNAUTHORIZED
;
117 case HttpStatus
.SC_NOT_FOUND
:
118 mCode
= ResultCode
.FILE_NOT_FOUND
;
120 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
121 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
123 case HttpStatus
.SC_CONFLICT
:
124 mCode
= ResultCode
.CONFLICT
;
126 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
127 mCode
= ResultCode
.QUOTA_EXCEEDED
;
130 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
131 Log_OC
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
136 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
137 this(success
, httpCode
);
138 if (headers
!= null
) {
140 for (int i
=0; i
<headers
.length
; i
++) {
141 current
= headers
[i
];
142 if ("Location".equals(current
.getName())) {
143 mRedirectedLocation
= current
.getValue();
150 public RemoteOperationResult(Exception e
) {
153 if (e
instanceof OperationCancelledException
) {
154 mCode
= ResultCode
.CANCELLED
;
156 } else if (e
instanceof SocketException
) {
157 mCode
= ResultCode
.WRONG_CONNECTION
;
159 } else if (e
instanceof SocketTimeoutException
) {
160 mCode
= ResultCode
.TIMEOUT
;
162 } else if (e
instanceof ConnectTimeoutException
) {
163 mCode
= ResultCode
.TIMEOUT
;
165 } else if (e
instanceof MalformedURLException
) {
166 mCode
= ResultCode
.INCORRECT_ADDRESS
;
168 } else if (e
instanceof UnknownHostException
) {
169 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
171 } else if (e
instanceof AccountNotFoundException
) {
172 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
174 } else if (e
instanceof AccountsException
) {
175 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
177 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
178 CertificateCombinedException se
= getCertificateCombinedException(e
);
181 if (se
.isRecoverable()) {
182 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
184 } else if (e
instanceof RuntimeException
) {
185 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
188 mCode
= ResultCode
.SSL_ERROR
;
192 mCode
= ResultCode
.UNKNOWN_ERROR
;
197 public boolean isSuccess() {
201 public boolean isCancelled() {
202 return mCode
== ResultCode
.CANCELLED
;
205 public int getHttpCode() {
209 public ResultCode
getCode() {
213 public Exception
getException() {
217 public boolean isSslRecoverableException() {
218 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
221 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
222 CertificateCombinedException result
= null
;
223 if (e
instanceof CertificateCombinedException
) {
224 return (CertificateCombinedException
) e
;
226 Throwable cause
= mException
.getCause();
227 Throwable previousCause
= null
;
228 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
229 previousCause
= cause
;
230 cause
= cause
.getCause();
232 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
233 result
= (CertificateCombinedException
) cause
;
238 public String
getLogMessage() {
240 if (mException
!= null
) {
241 if (mException
instanceof OperationCancelledException
) {
242 return "Operation cancelled by the caller";
244 } else if (mException
instanceof SocketException
) {
245 return "Socket exception";
247 } else if (mException
instanceof SocketTimeoutException
) {
248 return "Socket timeout exception";
250 } else if (mException
instanceof ConnectTimeoutException
) {
251 return "Connect timeout exception";
253 } else if (mException
instanceof MalformedURLException
) {
254 return "Malformed URL exception";
256 } else if (mException
instanceof UnknownHostException
) {
257 return "Unknown host exception";
259 } else if (mException
instanceof CertificateCombinedException
) {
260 if (((CertificateCombinedException
) mException
).isRecoverable())
261 return "SSL recoverable exception";
263 return "SSL exception";
265 } else if (mException
instanceof SSLException
) {
266 return "SSL exception";
268 } else if (mException
instanceof DavException
) {
269 return "Unexpected WebDAV exception";
271 } else if (mException
instanceof HttpException
) {
272 return "HTTP violation";
274 } else if (mException
instanceof IOException
) {
275 return "Unrecovered transport exception";
277 } else if (mException
instanceof AccountNotFoundException
) {
278 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
279 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
281 } else if (mException
instanceof AccountsException
) {
282 return "Exception while using account";
285 return "Unexpected exception";
289 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
290 return "The ownCloud server is not configured!";
292 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
293 return "No network connection";
295 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
296 return "No valid ownCloud version was found at the server";
298 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
299 return "Local storage full";
301 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
302 return "Error while moving file to final directory";
304 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
305 return "Account already existing when creating a new one";
307 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
308 return "Authenticated with a different account than the one updating";
311 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
315 public boolean isServerFail() {
316 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
319 public boolean isException() {
320 return (mException
!= null
);
323 public boolean isTemporalRedirection() {
324 return (mHttpCode
== 302 || mHttpCode
== 307);
327 public String
getRedirectedLocation() {
328 return mRedirectedLocation
;
331 public boolean isIdPRedirection() {
332 return (mRedirectedLocation
!= null
&&
333 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
334 mRedirectedLocation
.toLowerCase().contains("wayf")));