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 com
.owncloud
.android
.Log_OC
;
36 import com
.owncloud
.android
.network
.CertificateCombinedException
;
39 * The result of a remote operation required to an ownCloud server.
41 * Provides a common classification of remote operation results for all the
44 * @author David A. Velasco
46 public class RemoteOperationResult
implements Serializable
{
48 /** Generated - should be refreshed every time the class changes!! */
49 private static final long serialVersionUID
= -7805531062432602444L;
51 private static final String TAG
= "RemoteOperationResult";
53 public enum ResultCode
{
60 INSTANCE_NOT_CONFIGURED
,
66 NO_NETWORK_CONNECTION
,
68 SSL_RECOVERABLE_PEER_UNVERIFIED
,
71 INVALID_LOCAL_FILE_NAME
,
77 LOCAL_STORAGE_NOT_MOVED
,
78 LOCAL_STORAGE_NOT_COPIED
,
79 OAUTH2_ERROR_ACCESS_DENIED
,
83 private boolean mSuccess
= false
;
84 private int mHttpCode
= -1;
85 private Exception mException
= null
;
86 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
88 public RemoteOperationResult(ResultCode code
) {
90 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
93 public RemoteOperationResult(boolean success
, int httpCode
) {
98 mCode
= ResultCode
.OK
;
100 } else if (httpCode
> 0) {
102 case HttpStatus
.SC_UNAUTHORIZED
:
103 mCode
= ResultCode
.UNAUTHORIZED
;
105 case HttpStatus
.SC_NOT_FOUND
:
106 mCode
= ResultCode
.FILE_NOT_FOUND
;
108 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
109 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
111 case HttpStatus
.SC_CONFLICT
:
112 mCode
= ResultCode
.CONFLICT
;
114 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
115 mCode
= ResultCode
.QUOTA_EXCEEDED
;
118 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
119 Log_OC
.d(TAG
, "RemoteOperationResult has prcessed UNHANDLED_HTTP_CODE: " + httpCode
);
124 public RemoteOperationResult(Exception e
) {
127 if (e
instanceof OperationCancelledException
) {
128 mCode
= ResultCode
.CANCELLED
;
130 } else if (e
instanceof SocketException
) {
131 mCode
= ResultCode
.WRONG_CONNECTION
;
133 } else if (e
instanceof SocketTimeoutException
) {
134 mCode
= ResultCode
.TIMEOUT
;
136 } else if (e
instanceof ConnectTimeoutException
) {
137 mCode
= ResultCode
.TIMEOUT
;
139 } else if (e
instanceof MalformedURLException
) {
140 mCode
= ResultCode
.INCORRECT_ADDRESS
;
142 } else if (e
instanceof UnknownHostException
) {
143 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
145 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
146 CertificateCombinedException se
= getCertificateCombinedException(e
);
149 if (se
.isRecoverable()) {
150 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
152 } else if (e
instanceof RuntimeException
) {
153 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
156 mCode
= ResultCode
.SSL_ERROR
;
160 mCode
= ResultCode
.UNKNOWN_ERROR
;
165 public boolean isSuccess() {
169 public boolean isCancelled() {
170 return mCode
== ResultCode
.CANCELLED
;
173 public int getHttpCode() {
177 public ResultCode
getCode() {
181 public Exception
getException() {
185 public boolean isSslRecoverableException() {
186 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
189 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
190 CertificateCombinedException result
= null
;
191 if (e
instanceof CertificateCombinedException
) {
192 return (CertificateCombinedException
) e
;
194 Throwable cause
= mException
.getCause();
195 Throwable previousCause
= null
;
196 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
197 previousCause
= cause
;
198 cause
= cause
.getCause();
200 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
201 result
= (CertificateCombinedException
) cause
;
206 public String
getLogMessage() {
208 if (mException
!= null
) {
209 if (mException
instanceof OperationCancelledException
) {
210 return "Operation cancelled by the caller";
212 } else if (mException
instanceof SocketException
) {
213 return "Socket exception";
215 } else if (mException
instanceof SocketTimeoutException
) {
216 return "Socket timeout exception";
218 } else if (mException
instanceof ConnectTimeoutException
) {
219 return "Connect timeout exception";
221 } else if (mException
instanceof MalformedURLException
) {
222 return "Malformed URL exception";
224 } else if (mException
instanceof UnknownHostException
) {
225 return "Unknown host exception";
227 } else if (mException
instanceof CertificateCombinedException
) {
228 if (((CertificateCombinedException
) mException
).isRecoverable())
229 return "SSL recoverable exception";
231 return "SSL exception";
233 } else if (mException
instanceof SSLException
) {
234 return "SSL exception";
236 } else if (mException
instanceof DavException
) {
237 return "Unexpected WebDAV exception";
239 } else if (mException
instanceof HttpException
) {
240 return "HTTP violation";
242 } else if (mException
instanceof IOException
) {
243 return "Unrecovered transport exception";
246 return "Unexpected exception";
250 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
251 return "The ownCloud server is not configured!";
253 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
254 return "No network connection";
256 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
257 return "No valid ownCloud version was found at the server";
259 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
260 return "Local storage full";
262 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
263 return "Error while moving file to final directory";
266 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
270 public boolean isServerFail() {
271 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
274 public boolean isException() {
275 return (mException
!= null
);