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
= 3267227833178885664L;
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
,
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
;
97 public RemoteOperationResult(ResultCode code
) {
99 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
102 private RemoteOperationResult(boolean success
, int httpCode
) {
104 mHttpCode
= httpCode
;
107 mCode
= ResultCode
.OK
;
109 } else if (httpCode
> 0) {
111 case HttpStatus
.SC_UNAUTHORIZED
:
112 mCode
= ResultCode
.UNAUTHORIZED
;
114 case HttpStatus
.SC_NOT_FOUND
:
115 mCode
= ResultCode
.FILE_NOT_FOUND
;
117 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
118 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
120 case HttpStatus
.SC_CONFLICT
:
121 mCode
= ResultCode
.CONFLICT
;
123 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
124 mCode
= ResultCode
.QUOTA_EXCEEDED
;
127 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
128 Log_OC
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
133 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
134 this(success
, httpCode
);
135 if (headers
!= null
) {
137 for (int i
=0; i
<headers
.length
; i
++) {
138 current
= headers
[i
];
139 if ("Location".equals(current
.getName())) {
140 mRedirectedLocation
= current
.getValue();
147 public RemoteOperationResult(Exception e
) {
150 if (e
instanceof OperationCancelledException
) {
151 mCode
= ResultCode
.CANCELLED
;
153 } else if (e
instanceof SocketException
) {
154 mCode
= ResultCode
.WRONG_CONNECTION
;
156 } else if (e
instanceof SocketTimeoutException
) {
157 mCode
= ResultCode
.TIMEOUT
;
159 } else if (e
instanceof ConnectTimeoutException
) {
160 mCode
= ResultCode
.TIMEOUT
;
162 } else if (e
instanceof MalformedURLException
) {
163 mCode
= ResultCode
.INCORRECT_ADDRESS
;
165 } else if (e
instanceof UnknownHostException
) {
166 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
168 } else if (e
instanceof AccountNotFoundException
) {
169 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
171 } else if (e
instanceof AccountsException
) {
172 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
174 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
175 CertificateCombinedException se
= getCertificateCombinedException(e
);
178 if (se
.isRecoverable()) {
179 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
181 } else if (e
instanceof RuntimeException
) {
182 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
185 mCode
= ResultCode
.SSL_ERROR
;
189 mCode
= ResultCode
.UNKNOWN_ERROR
;
194 public boolean isSuccess() {
198 public boolean isCancelled() {
199 return mCode
== ResultCode
.CANCELLED
;
202 public int getHttpCode() {
206 public ResultCode
getCode() {
210 public Exception
getException() {
214 public boolean isSslRecoverableException() {
215 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
218 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
219 CertificateCombinedException result
= null
;
220 if (e
instanceof CertificateCombinedException
) {
221 return (CertificateCombinedException
) e
;
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();
229 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
230 result
= (CertificateCombinedException
) cause
;
235 public String
getLogMessage() {
237 if (mException
!= null
) {
238 if (mException
instanceof OperationCancelledException
) {
239 return "Operation cancelled by the caller";
241 } else if (mException
instanceof SocketException
) {
242 return "Socket exception";
244 } else if (mException
instanceof SocketTimeoutException
) {
245 return "Socket timeout exception";
247 } else if (mException
instanceof ConnectTimeoutException
) {
248 return "Connect timeout exception";
250 } else if (mException
instanceof MalformedURLException
) {
251 return "Malformed URL exception";
253 } else if (mException
instanceof UnknownHostException
) {
254 return "Unknown host exception";
256 } else if (mException
instanceof CertificateCombinedException
) {
257 if (((CertificateCombinedException
) mException
).isRecoverable())
258 return "SSL recoverable exception";
260 return "SSL exception";
262 } else if (mException
instanceof SSLException
) {
263 return "SSL exception";
265 } else if (mException
instanceof DavException
) {
266 return "Unexpected WebDAV exception";
268 } else if (mException
instanceof HttpException
) {
269 return "HTTP violation";
271 } else if (mException
instanceof IOException
) {
272 return "Unrecovered transport exception";
274 } else if (mException
instanceof AccountNotFoundException
) {
275 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
276 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
278 } else if (mException
instanceof AccountsException
) {
279 return "Exception while using account";
282 return "Unexpected exception";
286 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
287 return "The ownCloud server is not configured!";
289 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
290 return "No network connection";
292 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
293 return "No valid ownCloud version was found at the server";
295 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
296 return "Local storage full";
298 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
299 return "Error while moving file to final directory";
302 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
306 public boolean isServerFail() {
307 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
310 public boolean isException() {
311 return (mException
!= null
);
314 public boolean isTemporalRedirection() {
315 return (mHttpCode
== 302 || mHttpCode
== 307);
318 public String
getRedirectedLocation() {
319 return mRedirectedLocation
;
322 public boolean isIdPRedirection() {
323 return (mRedirectedLocation
.toUpperCase().contains("SAML") ||
324 mRedirectedLocation
.toLowerCase().contains("wayf"));