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
.HttpException
;
32 import org
.apache
.commons
.httpclient
.HttpStatus
;
33 import org
.apache
.jackrabbit
.webdav
.DavException
;
35 import android
.accounts
.Account
;
36 import android
.accounts
.AccountsException
;
38 import com
.owncloud
.android
.Log_OC
;
39 import com
.owncloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
;
40 import com
.owncloud
.android
.network
.CertificateCombinedException
;
43 * The result of a remote operation required to an ownCloud server.
45 * Provides a common classification of remote operation results for all the
48 * @author David A. Velasco
50 public class RemoteOperationResult
implements Serializable
{
52 /** Generated - should be refreshed every time the class changes!! */
53 private static final long serialVersionUID
= 6106167714625712390L;
56 private static final String TAG
= "RemoteOperationResult";
58 public enum ResultCode
{
65 INSTANCE_NOT_CONFIGURED
,
71 NO_NETWORK_CONNECTION
,
73 SSL_RECOVERABLE_PEER_UNVERIFIED
,
76 INVALID_LOCAL_FILE_NAME
,
82 LOCAL_STORAGE_NOT_MOVED
,
83 LOCAL_STORAGE_NOT_COPIED
,
84 OAUTH2_ERROR_ACCESS_DENIED
,
90 private boolean mSuccess
= false
;
91 private int mHttpCode
= -1;
92 private Exception mException
= null
;
93 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
95 public RemoteOperationResult(ResultCode code
) {
97 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
100 public RemoteOperationResult(boolean success
, int httpCode
) {
102 mHttpCode
= httpCode
;
105 mCode
= ResultCode
.OK
;
107 } else if (httpCode
> 0) {
109 case HttpStatus
.SC_UNAUTHORIZED
:
110 mCode
= ResultCode
.UNAUTHORIZED
;
112 case HttpStatus
.SC_NOT_FOUND
:
113 mCode
= ResultCode
.FILE_NOT_FOUND
;
115 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
116 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
118 case HttpStatus
.SC_CONFLICT
:
119 mCode
= ResultCode
.CONFLICT
;
121 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
122 mCode
= ResultCode
.QUOTA_EXCEEDED
;
125 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
126 Log_OC
.d(TAG
, "RemoteOperationResult has prcessed UNHANDLED_HTTP_CODE: " + httpCode
);
131 public RemoteOperationResult(Exception e
) {
134 if (e
instanceof OperationCancelledException
) {
135 mCode
= ResultCode
.CANCELLED
;
137 } else if (e
instanceof SocketException
) {
138 mCode
= ResultCode
.WRONG_CONNECTION
;
140 } else if (e
instanceof SocketTimeoutException
) {
141 mCode
= ResultCode
.TIMEOUT
;
143 } else if (e
instanceof ConnectTimeoutException
) {
144 mCode
= ResultCode
.TIMEOUT
;
146 } else if (e
instanceof MalformedURLException
) {
147 mCode
= ResultCode
.INCORRECT_ADDRESS
;
149 } else if (e
instanceof UnknownHostException
) {
150 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
152 } else if (e
instanceof AccountNotFoundException
) {
153 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
155 } else if (e
instanceof AccountsException
) {
156 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
158 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
159 CertificateCombinedException se
= getCertificateCombinedException(e
);
162 if (se
.isRecoverable()) {
163 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
165 } else if (e
instanceof RuntimeException
) {
166 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
169 mCode
= ResultCode
.SSL_ERROR
;
173 mCode
= ResultCode
.UNKNOWN_ERROR
;
178 public boolean isSuccess() {
182 public boolean isCancelled() {
183 return mCode
== ResultCode
.CANCELLED
;
186 public int getHttpCode() {
190 public ResultCode
getCode() {
194 public Exception
getException() {
198 public boolean isSslRecoverableException() {
199 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
202 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
203 CertificateCombinedException result
= null
;
204 if (e
instanceof CertificateCombinedException
) {
205 return (CertificateCombinedException
) e
;
207 Throwable cause
= mException
.getCause();
208 Throwable previousCause
= null
;
209 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
210 previousCause
= cause
;
211 cause
= cause
.getCause();
213 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
214 result
= (CertificateCombinedException
) cause
;
219 public String
getLogMessage() {
221 if (mException
!= null
) {
222 if (mException
instanceof OperationCancelledException
) {
223 return "Operation cancelled by the caller";
225 } else if (mException
instanceof SocketException
) {
226 return "Socket exception";
228 } else if (mException
instanceof SocketTimeoutException
) {
229 return "Socket timeout exception";
231 } else if (mException
instanceof ConnectTimeoutException
) {
232 return "Connect timeout exception";
234 } else if (mException
instanceof MalformedURLException
) {
235 return "Malformed URL exception";
237 } else if (mException
instanceof UnknownHostException
) {
238 return "Unknown host exception";
240 } else if (mException
instanceof CertificateCombinedException
) {
241 if (((CertificateCombinedException
) mException
).isRecoverable())
242 return "SSL recoverable exception";
244 return "SSL exception";
246 } else if (mException
instanceof SSLException
) {
247 return "SSL exception";
249 } else if (mException
instanceof DavException
) {
250 return "Unexpected WebDAV exception";
252 } else if (mException
instanceof HttpException
) {
253 return "HTTP violation";
255 } else if (mException
instanceof IOException
) {
256 return "Unrecovered transport exception";
258 } else if (mException
instanceof AccountNotFoundException
) {
259 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
260 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
262 } else if (mException
instanceof AccountsException
) {
263 return "Exception while using account";
266 return "Unexpected exception";
270 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
271 return "The ownCloud server is not configured!";
273 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
274 return "No network connection";
276 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
277 return "No valid ownCloud version was found at the server";
279 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
280 return "Local storage full";
282 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
283 return "Error while moving file to final directory";
286 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
290 public boolean isServerFail() {
291 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
294 public boolean isException() {
295 return (mException
!= null
);