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
;
29 import javax
.net
.ssl
.SSLException
;
31 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
32 import org
.apache
.commons
.httpclient
.Header
;
33 import org
.apache
.commons
.httpclient
.HttpException
;
34 import org
.apache
.commons
.httpclient
.HttpStatus
;
35 import org
.apache
.jackrabbit
.webdav
.DavException
;
37 import android
.accounts
.Account
;
38 import android
.accounts
.AccountsException
;
40 import com
.owncloud
.android
.Log_OC
;
41 import com
.owncloud
.android
.authentication
.AccountUtils
.AccountNotFoundException
;
42 import com
.owncloud
.android
.network
.CertificateCombinedException
;
45 * The result of a remote operation required to an ownCloud server.
47 * Provides a common classification of remote operation results for all the
50 * @author David A. Velasco
52 public class RemoteOperationResult
implements Serializable
{
54 /** Generated - should be refreshed every time the class changes!! */
55 private static final long serialVersionUID
= 3267227833178885664L;
58 private static final String TAG
= "RemoteOperationResult";
60 public enum ResultCode
{
67 INSTANCE_NOT_CONFIGURED
,
73 NO_NETWORK_CONNECTION
,
75 SSL_RECOVERABLE_PEER_UNVERIFIED
,
78 INVALID_LOCAL_FILE_NAME
,
84 LOCAL_STORAGE_NOT_MOVED
,
85 LOCAL_STORAGE_NOT_COPIED
,
86 OAUTH2_ERROR_ACCESS_DENIED
,
92 private boolean mSuccess
= false
;
93 private int mHttpCode
= -1;
94 private Exception mException
= null
;
95 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
96 private String mRedirectedLocation
;
98 public RemoteOperationResult(ResultCode code
) {
100 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
103 public RemoteOperationResult(boolean success
, int httpCode
) {
105 mHttpCode
= httpCode
;
108 mCode
= ResultCode
.OK
;
110 } else if (httpCode
> 0) {
112 case HttpStatus
.SC_UNAUTHORIZED
:
113 mCode
= ResultCode
.UNAUTHORIZED
;
115 case HttpStatus
.SC_NOT_FOUND
:
116 mCode
= ResultCode
.FILE_NOT_FOUND
;
118 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
119 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
121 case HttpStatus
.SC_CONFLICT
:
122 mCode
= ResultCode
.CONFLICT
;
124 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
125 mCode
= ResultCode
.QUOTA_EXCEEDED
;
128 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
129 Log_OC
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
134 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
135 this(success
, httpCode
);
136 if (headers
!= null
) {
138 for (int i
=0; i
<headers
.length
; i
++) {
139 current
= headers
[i
];
140 if ("Location".equals(current
.getName())) {
141 mRedirectedLocation
= current
.getValue();
148 public RemoteOperationResult(Exception e
) {
151 if (e
instanceof OperationCancelledException
) {
152 mCode
= ResultCode
.CANCELLED
;
154 } else if (e
instanceof SocketException
) {
155 mCode
= ResultCode
.WRONG_CONNECTION
;
157 } else if (e
instanceof SocketTimeoutException
) {
158 mCode
= ResultCode
.TIMEOUT
;
160 } else if (e
instanceof ConnectTimeoutException
) {
161 mCode
= ResultCode
.TIMEOUT
;
163 } else if (e
instanceof MalformedURLException
) {
164 mCode
= ResultCode
.INCORRECT_ADDRESS
;
166 } else if (e
instanceof UnknownHostException
) {
167 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
169 } else if (e
instanceof AccountNotFoundException
) {
170 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
172 } else if (e
instanceof AccountsException
) {
173 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
175 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
176 CertificateCombinedException se
= getCertificateCombinedException(e
);
179 if (se
.isRecoverable()) {
180 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
182 } else if (e
instanceof RuntimeException
) {
183 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
186 mCode
= ResultCode
.SSL_ERROR
;
190 mCode
= ResultCode
.UNKNOWN_ERROR
;
195 public boolean isSuccess() {
199 public boolean isCancelled() {
200 return mCode
== ResultCode
.CANCELLED
;
203 public int getHttpCode() {
207 public ResultCode
getCode() {
211 public Exception
getException() {
215 public boolean isSslRecoverableException() {
216 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
219 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
220 CertificateCombinedException result
= null
;
221 if (e
instanceof CertificateCombinedException
) {
222 return (CertificateCombinedException
) e
;
224 Throwable cause
= mException
.getCause();
225 Throwable previousCause
= null
;
226 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
227 previousCause
= cause
;
228 cause
= cause
.getCause();
230 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
231 result
= (CertificateCombinedException
) cause
;
236 public String
getLogMessage() {
238 if (mException
!= null
) {
239 if (mException
instanceof OperationCancelledException
) {
240 return "Operation cancelled by the caller";
242 } else if (mException
instanceof SocketException
) {
243 return "Socket exception";
245 } else if (mException
instanceof SocketTimeoutException
) {
246 return "Socket timeout exception";
248 } else if (mException
instanceof ConnectTimeoutException
) {
249 return "Connect timeout exception";
251 } else if (mException
instanceof MalformedURLException
) {
252 return "Malformed URL exception";
254 } else if (mException
instanceof UnknownHostException
) {
255 return "Unknown host exception";
257 } else if (mException
instanceof CertificateCombinedException
) {
258 if (((CertificateCombinedException
) mException
).isRecoverable())
259 return "SSL recoverable exception";
261 return "SSL exception";
263 } else if (mException
instanceof SSLException
) {
264 return "SSL exception";
266 } else if (mException
instanceof DavException
) {
267 return "Unexpected WebDAV exception";
269 } else if (mException
instanceof HttpException
) {
270 return "HTTP violation";
272 } else if (mException
instanceof IOException
) {
273 return "Unrecovered transport exception";
275 } else if (mException
instanceof AccountNotFoundException
) {
276 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
277 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
279 } else if (mException
instanceof AccountsException
) {
280 return "Exception while using account";
283 return "Unexpected exception";
287 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
288 return "The ownCloud server is not configured!";
290 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
291 return "No network connection";
293 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
294 return "No valid ownCloud version was found at the server";
296 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
297 return "Local storage full";
299 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
300 return "Error while moving file to final directory";
303 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
307 public boolean isServerFail() {
308 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
311 public boolean isException() {
312 return (mException
!= null
);
315 public boolean isTemporalRedirection() {
316 return (mHttpCode
== 302 || mHttpCode
== 307);
319 public String
getRedirectedLocation() {
320 return mRedirectedLocation
;