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
.oc_framework
.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
;
27 import java
.util
.ArrayList
;
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
;
36 import org
.json
.JSONException
;
38 import com
.owncloud
.android
.oc_framework
.accounts
.AccountUtils
.AccountNotFoundException
;
39 import com
.owncloud
.android
.oc_framework
.network
.CertificateCombinedException
;
41 import android
.accounts
.Account
;
42 import android
.accounts
.AccountsException
;
43 import android
.util
.Log
;
47 * The result of a remote operation required to an ownCloud server.
49 * Provides a common classification of remote operation results for all the
52 * @author David A. Velasco
54 public class RemoteOperationResult
implements Serializable
{
56 /** Generated - should be refreshed every time the class changes!! */
57 private static final long serialVersionUID
= -2469951225222759283L;
59 private static final String TAG
= "RemoteOperationResult";
61 public enum ResultCode
{
68 INSTANCE_NOT_CONFIGURED
,
74 NO_NETWORK_CONNECTION
,
76 SSL_RECOVERABLE_PEER_UNVERIFIED
,
79 INVALID_LOCAL_FILE_NAME
,
85 LOCAL_STORAGE_NOT_MOVED
,
86 LOCAL_STORAGE_NOT_COPIED
,
87 OAUTH2_ERROR_ACCESS_DENIED
,
93 INVALID_CHARACTER_IN_NAME
,
97 private boolean mSuccess
= false
;
98 private int mHttpCode
= -1;
99 private Exception mException
= null
;
100 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
101 private String mRedirectedLocation
;
103 private ArrayList
<RemoteFile
> mFiles
;
105 public RemoteOperationResult(ResultCode code
) {
107 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
111 private RemoteOperationResult(boolean success
, int httpCode
) {
113 mHttpCode
= httpCode
;
116 mCode
= ResultCode
.OK
;
118 } else if (httpCode
> 0) {
120 case HttpStatus
.SC_UNAUTHORIZED
:
121 mCode
= ResultCode
.UNAUTHORIZED
;
123 case HttpStatus
.SC_NOT_FOUND
:
124 mCode
= ResultCode
.FILE_NOT_FOUND
;
126 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
127 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
129 case HttpStatus
.SC_CONFLICT
:
130 mCode
= ResultCode
.CONFLICT
;
132 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
133 mCode
= ResultCode
.QUOTA_EXCEEDED
;
136 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
137 Log
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
142 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
143 this(success
, httpCode
);
144 if (headers
!= null
) {
146 for (int i
=0; i
<headers
.length
; i
++) {
147 current
= headers
[i
];
148 if ("Location".equals(current
.getName())) {
149 mRedirectedLocation
= current
.getValue();
156 public RemoteOperationResult(Exception e
) {
159 if (e
instanceof OperationCancelledException
) {
160 mCode
= ResultCode
.CANCELLED
;
162 } else if (e
instanceof SocketException
) {
163 mCode
= ResultCode
.WRONG_CONNECTION
;
165 } else if (e
instanceof SocketTimeoutException
) {
166 mCode
= ResultCode
.TIMEOUT
;
168 } else if (e
instanceof ConnectTimeoutException
) {
169 mCode
= ResultCode
.TIMEOUT
;
171 } else if (e
instanceof MalformedURLException
) {
172 mCode
= ResultCode
.INCORRECT_ADDRESS
;
174 } else if (e
instanceof UnknownHostException
) {
175 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
177 } else if (e
instanceof AccountNotFoundException
) {
178 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
180 } else if (e
instanceof AccountsException
) {
181 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
183 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
184 CertificateCombinedException se
= getCertificateCombinedException(e
);
187 if (se
.isRecoverable()) {
188 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
190 } else if (e
instanceof RuntimeException
) {
191 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
194 mCode
= ResultCode
.SSL_ERROR
;
197 } else if (e
instanceof JSONException
) {
198 mCode
= ResultCode
.JSON_EXCEPTION
;
201 mCode
= ResultCode
.UNKNOWN_ERROR
;
207 public void setData(ArrayList
<RemoteFile
> files
){
211 public ArrayList
<RemoteFile
> getData(){
215 public boolean isSuccess() {
219 public boolean isCancelled() {
220 return mCode
== ResultCode
.CANCELLED
;
223 public int getHttpCode() {
227 public ResultCode
getCode() {
231 public Exception
getException() {
235 public boolean isSslRecoverableException() {
236 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
239 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
240 CertificateCombinedException result
= null
;
241 if (e
instanceof CertificateCombinedException
) {
242 return (CertificateCombinedException
) e
;
244 Throwable cause
= mException
.getCause();
245 Throwable previousCause
= null
;
246 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
247 previousCause
= cause
;
248 cause
= cause
.getCause();
250 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
251 result
= (CertificateCombinedException
) cause
;
256 public String
getLogMessage() {
258 if (mException
!= null
) {
259 if (mException
instanceof OperationCancelledException
) {
260 return "Operation cancelled by the caller";
262 } else if (mException
instanceof SocketException
) {
263 return "Socket exception";
265 } else if (mException
instanceof SocketTimeoutException
) {
266 return "Socket timeout exception";
268 } else if (mException
instanceof ConnectTimeoutException
) {
269 return "Connect timeout exception";
271 } else if (mException
instanceof MalformedURLException
) {
272 return "Malformed URL exception";
274 } else if (mException
instanceof UnknownHostException
) {
275 return "Unknown host exception";
277 } else if (mException
instanceof CertificateCombinedException
) {
278 if (((CertificateCombinedException
) mException
).isRecoverable())
279 return "SSL recoverable exception";
281 return "SSL exception";
283 } else if (mException
instanceof SSLException
) {
284 return "SSL exception";
286 } else if (mException
instanceof DavException
) {
287 return "Unexpected WebDAV exception";
289 } else if (mException
instanceof HttpException
) {
290 return "HTTP violation";
292 } else if (mException
instanceof IOException
) {
293 return "Unrecovered transport exception";
295 } else if (mException
instanceof AccountNotFoundException
) {
296 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
297 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
299 } else if (mException
instanceof AccountsException
) {
300 return "Exception while using account";
302 } else if (mException
instanceof JSONException
) {
303 return "JSON exception";
305 return "Unexpected exception";
309 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
310 return "The ownCloud server is not configured!";
312 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
313 return "No network connection";
315 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
316 return "No valid ownCloud version was found at the server";
318 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
319 return "Local storage full";
321 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
322 return "Error while moving file to final directory";
324 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
325 return "Account already existing when creating a new one";
327 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
328 return "Authenticated with a different account than the one updating";
329 } else if (mCode
== ResultCode
.INVALID_CHARACTER_IN_NAME
) {
330 return "The file name contains an forbidden character";
333 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
337 public boolean isServerFail() {
338 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
341 public boolean isException() {
342 return (mException
!= null
);
345 public boolean isTemporalRedirection() {
346 return (mHttpCode
== 302 || mHttpCode
== 307);
349 public String
getRedirectedLocation() {
350 return mRedirectedLocation
;
353 public boolean isIdPRedirection() {
354 return (mRedirectedLocation
!= null
&&
355 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
356 mRedirectedLocation
.toLowerCase().contains("wayf")));