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
{
66 INSTANCE_NOT_CONFIGURED
,
72 NO_NETWORK_CONNECTION
,
74 SSL_RECOVERABLE_PEER_UNVERIFIED
,
77 INVALID_LOCAL_FILE_NAME
,
83 LOCAL_STORAGE_NOT_MOVED
,
84 LOCAL_STORAGE_NOT_COPIED
,
85 OAUTH2_ERROR_ACCESS_DENIED
,
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
;
99 public RemoteOperationResult(ResultCode code
) {
101 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
104 private RemoteOperationResult(boolean success
, int httpCode
) {
106 mHttpCode
= httpCode
;
109 mCode
= ResultCode
.OK
;
111 } else if (httpCode
> 0) {
113 case HttpStatus
.SC_UNAUTHORIZED
:
114 mCode
= ResultCode
.UNAUTHORIZED
;
116 case HttpStatus
.SC_NOT_FOUND
:
117 mCode
= ResultCode
.FILE_NOT_FOUND
;
119 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
120 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
122 case HttpStatus
.SC_CONFLICT
:
123 mCode
= ResultCode
.CONFLICT
;
125 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
126 mCode
= ResultCode
.QUOTA_EXCEEDED
;
129 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
130 Log_OC
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
135 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
136 this(success
, httpCode
);
137 if (headers
!= null
) {
139 for (int i
=0; i
<headers
.length
; i
++) {
140 current
= headers
[i
];
141 if ("Location".equals(current
.getName())) {
142 mRedirectedLocation
= current
.getValue();
149 public RemoteOperationResult(Exception e
) {
152 if (e
instanceof OperationCancelledException
) {
153 mCode
= ResultCode
.CANCELLED
;
155 } else if (e
instanceof SocketException
) {
156 mCode
= ResultCode
.WRONG_CONNECTION
;
158 } else if (e
instanceof SocketTimeoutException
) {
159 mCode
= ResultCode
.TIMEOUT
;
161 } else if (e
instanceof ConnectTimeoutException
) {
162 mCode
= ResultCode
.TIMEOUT
;
164 } else if (e
instanceof MalformedURLException
) {
165 mCode
= ResultCode
.INCORRECT_ADDRESS
;
167 } else if (e
instanceof UnknownHostException
) {
168 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
170 } else if (e
instanceof AccountNotFoundException
) {
171 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
173 } else if (e
instanceof AccountsException
) {
174 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
176 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
177 CertificateCombinedException se
= getCertificateCombinedException(e
);
180 if (se
.isRecoverable()) {
181 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
183 } else if (e
instanceof RuntimeException
) {
184 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
187 mCode
= ResultCode
.SSL_ERROR
;
191 mCode
= ResultCode
.UNKNOWN_ERROR
;
196 public boolean isSuccess() {
200 public boolean isCancelled() {
201 return mCode
== ResultCode
.CANCELLED
;
204 public int getHttpCode() {
208 public ResultCode
getCode() {
212 public Exception
getException() {
216 public boolean isSslRecoverableException() {
217 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
220 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
221 CertificateCombinedException result
= null
;
222 if (e
instanceof CertificateCombinedException
) {
223 return (CertificateCombinedException
) e
;
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();
231 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
232 result
= (CertificateCombinedException
) cause
;
237 public String
getLogMessage() {
239 if (mException
!= null
) {
240 if (mException
instanceof OperationCancelledException
) {
241 return "Operation cancelled by the caller";
243 } else if (mException
instanceof SocketException
) {
244 return "Socket exception";
246 } else if (mException
instanceof SocketTimeoutException
) {
247 return "Socket timeout exception";
249 } else if (mException
instanceof ConnectTimeoutException
) {
250 return "Connect timeout exception";
252 } else if (mException
instanceof MalformedURLException
) {
253 return "Malformed URL exception";
255 } else if (mException
instanceof UnknownHostException
) {
256 return "Unknown host exception";
258 } else if (mException
instanceof CertificateCombinedException
) {
259 if (((CertificateCombinedException
) mException
).isRecoverable())
260 return "SSL recoverable exception";
262 return "SSL exception";
264 } else if (mException
instanceof SSLException
) {
265 return "SSL exception";
267 } else if (mException
instanceof DavException
) {
268 return "Unexpected WebDAV exception";
270 } else if (mException
instanceof HttpException
) {
271 return "HTTP violation";
273 } else if (mException
instanceof IOException
) {
274 return "Unrecovered transport exception";
276 } else if (mException
instanceof AccountNotFoundException
) {
277 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
278 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
280 } else if (mException
instanceof AccountsException
) {
281 return "Exception while using account";
284 return "Unexpected exception";
288 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
289 return "The ownCloud server is not configured!";
291 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
292 return "No network connection";
294 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
295 return "No valid ownCloud version was found at the server";
297 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
298 return "Local storage full";
300 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
301 return "Error while moving file to final directory";
303 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
304 return "Account already existing when creating a new one";
306 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
307 return "Authenticated with a different account than the one updating";
310 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
314 public boolean isServerFail() {
315 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
318 public boolean isException() {
319 return (mException
!= null
);
322 public boolean isTemporalRedirection() {
323 return (mHttpCode
== 302 || mHttpCode
== 307);
326 public String
getRedirectedLocation() {
327 return mRedirectedLocation
;
330 public boolean isIdPRedirection() {
331 return (mRedirectedLocation
!= null
&&
332 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
333 mRedirectedLocation
.toLowerCase().contains("wayf")));