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
{
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
;
96 public RemoteOperationResult(ResultCode code
) {
98 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
|| code
== ResultCode
.OK_NO_CHANGES_ON_DIR
);
101 public RemoteOperationResult(boolean success
, int httpCode
) {
103 mHttpCode
= httpCode
;
106 mCode
= ResultCode
.OK
;
108 } else if (httpCode
> 0) {
110 case HttpStatus
.SC_UNAUTHORIZED
:
111 mCode
= ResultCode
.UNAUTHORIZED
;
113 case HttpStatus
.SC_NOT_FOUND
:
114 mCode
= ResultCode
.FILE_NOT_FOUND
;
116 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
117 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
119 case HttpStatus
.SC_CONFLICT
:
120 mCode
= ResultCode
.CONFLICT
;
122 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
123 mCode
= ResultCode
.QUOTA_EXCEEDED
;
126 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
127 Log_OC
.d(TAG
, "RemoteOperationResult has prcessed UNHANDLED_HTTP_CODE: " + httpCode
);
132 public RemoteOperationResult(Exception e
) {
135 if (e
instanceof OperationCancelledException
) {
136 mCode
= ResultCode
.CANCELLED
;
138 } else if (e
instanceof SocketException
) {
139 mCode
= ResultCode
.WRONG_CONNECTION
;
141 } else if (e
instanceof SocketTimeoutException
) {
142 mCode
= ResultCode
.TIMEOUT
;
144 } else if (e
instanceof ConnectTimeoutException
) {
145 mCode
= ResultCode
.TIMEOUT
;
147 } else if (e
instanceof MalformedURLException
) {
148 mCode
= ResultCode
.INCORRECT_ADDRESS
;
150 } else if (e
instanceof UnknownHostException
) {
151 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
153 } else if (e
instanceof AccountNotFoundException
) {
154 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
156 } else if (e
instanceof AccountsException
) {
157 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
159 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
160 CertificateCombinedException se
= getCertificateCombinedException(e
);
163 if (se
.isRecoverable()) {
164 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
166 } else if (e
instanceof RuntimeException
) {
167 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
170 mCode
= ResultCode
.SSL_ERROR
;
174 mCode
= ResultCode
.UNKNOWN_ERROR
;
179 public boolean isSuccess() {
183 public boolean isCancelled() {
184 return mCode
== ResultCode
.CANCELLED
;
187 public int getHttpCode() {
191 public ResultCode
getCode() {
195 public Exception
getException() {
199 public boolean isSslRecoverableException() {
200 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
203 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
204 CertificateCombinedException result
= null
;
205 if (e
instanceof CertificateCombinedException
) {
206 return (CertificateCombinedException
) e
;
208 Throwable cause
= mException
.getCause();
209 Throwable previousCause
= null
;
210 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
211 previousCause
= cause
;
212 cause
= cause
.getCause();
214 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
215 result
= (CertificateCombinedException
) cause
;
220 public String
getLogMessage() {
222 if (mException
!= null
) {
223 if (mException
instanceof OperationCancelledException
) {
224 return "Operation cancelled by the caller";
226 } else if (mException
instanceof SocketException
) {
227 return "Socket exception";
229 } else if (mException
instanceof SocketTimeoutException
) {
230 return "Socket timeout exception";
232 } else if (mException
instanceof ConnectTimeoutException
) {
233 return "Connect timeout exception";
235 } else if (mException
instanceof MalformedURLException
) {
236 return "Malformed URL exception";
238 } else if (mException
instanceof UnknownHostException
) {
239 return "Unknown host exception";
241 } else if (mException
instanceof CertificateCombinedException
) {
242 if (((CertificateCombinedException
) mException
).isRecoverable())
243 return "SSL recoverable exception";
245 return "SSL exception";
247 } else if (mException
instanceof SSLException
) {
248 return "SSL exception";
250 } else if (mException
instanceof DavException
) {
251 return "Unexpected WebDAV exception";
253 } else if (mException
instanceof HttpException
) {
254 return "HTTP violation";
256 } else if (mException
instanceof IOException
) {
257 return "Unrecovered transport exception";
259 } else if (mException
instanceof AccountNotFoundException
) {
260 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
261 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
263 } else if (mException
instanceof AccountsException
) {
264 return "Exception while using account";
267 return "Unexpected exception";
271 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
272 return "The ownCloud server is not configured!";
274 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
275 return "No network connection";
277 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
278 return "No valid ownCloud version was found at the server";
280 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
281 return "Local storage full";
283 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
284 return "Error while moving file to final directory";
287 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
291 public boolean isServerFail() {
292 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
295 public boolean isException() {
296 return (mException
!= null
);