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
= -8257349554488668693L;
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
,
96 private boolean mSuccess
= false
;
97 private int mHttpCode
= -1;
98 private Exception mException
= null
;
99 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
100 private String mRedirectedLocation
;
102 private ArrayList
<RemoteFile
> mFiles
;
104 public RemoteOperationResult(ResultCode code
) {
106 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
110 private RemoteOperationResult(boolean success
, int httpCode
) {
112 mHttpCode
= httpCode
;
115 mCode
= ResultCode
.OK
;
117 } else if (httpCode
> 0) {
119 case HttpStatus
.SC_UNAUTHORIZED
:
120 mCode
= ResultCode
.UNAUTHORIZED
;
122 case HttpStatus
.SC_NOT_FOUND
:
123 mCode
= ResultCode
.FILE_NOT_FOUND
;
125 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
126 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
128 case HttpStatus
.SC_CONFLICT
:
129 mCode
= ResultCode
.CONFLICT
;
131 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
132 mCode
= ResultCode
.QUOTA_EXCEEDED
;
135 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
136 Log
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
141 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
142 this(success
, httpCode
);
143 if (headers
!= null
) {
145 for (int i
=0; i
<headers
.length
; i
++) {
146 current
= headers
[i
];
147 if ("Location".equals(current
.getName())) {
148 mRedirectedLocation
= current
.getValue();
155 public RemoteOperationResult(Exception e
) {
158 if (e
instanceof OperationCancelledException
) {
159 mCode
= ResultCode
.CANCELLED
;
161 } else if (e
instanceof SocketException
) {
162 mCode
= ResultCode
.WRONG_CONNECTION
;
164 } else if (e
instanceof SocketTimeoutException
) {
165 mCode
= ResultCode
.TIMEOUT
;
167 } else if (e
instanceof ConnectTimeoutException
) {
168 mCode
= ResultCode
.TIMEOUT
;
170 } else if (e
instanceof MalformedURLException
) {
171 mCode
= ResultCode
.INCORRECT_ADDRESS
;
173 } else if (e
instanceof UnknownHostException
) {
174 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
176 } else if (e
instanceof AccountNotFoundException
) {
177 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
179 } else if (e
instanceof AccountsException
) {
180 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
182 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
183 CertificateCombinedException se
= getCertificateCombinedException(e
);
186 if (se
.isRecoverable()) {
187 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
189 } else if (e
instanceof RuntimeException
) {
190 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
193 mCode
= ResultCode
.SSL_ERROR
;
197 mCode
= ResultCode
.UNKNOWN_ERROR
;
203 public void setData(ArrayList
<RemoteFile
> files
){
207 public ArrayList
<RemoteFile
> getData(){
211 public boolean isSuccess() {
215 public boolean isCancelled() {
216 return mCode
== ResultCode
.CANCELLED
;
219 public int getHttpCode() {
223 public ResultCode
getCode() {
227 public Exception
getException() {
231 public boolean isSslRecoverableException() {
232 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
235 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
236 CertificateCombinedException result
= null
;
237 if (e
instanceof CertificateCombinedException
) {
238 return (CertificateCombinedException
) e
;
240 Throwable cause
= mException
.getCause();
241 Throwable previousCause
= null
;
242 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
243 previousCause
= cause
;
244 cause
= cause
.getCause();
246 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
247 result
= (CertificateCombinedException
) cause
;
252 public String
getLogMessage() {
254 if (mException
!= null
) {
255 if (mException
instanceof OperationCancelledException
) {
256 return "Operation cancelled by the caller";
258 } else if (mException
instanceof SocketException
) {
259 return "Socket exception";
261 } else if (mException
instanceof SocketTimeoutException
) {
262 return "Socket timeout exception";
264 } else if (mException
instanceof ConnectTimeoutException
) {
265 return "Connect timeout exception";
267 } else if (mException
instanceof MalformedURLException
) {
268 return "Malformed URL exception";
270 } else if (mException
instanceof UnknownHostException
) {
271 return "Unknown host exception";
273 } else if (mException
instanceof CertificateCombinedException
) {
274 if (((CertificateCombinedException
) mException
).isRecoverable())
275 return "SSL recoverable exception";
277 return "SSL exception";
279 } else if (mException
instanceof SSLException
) {
280 return "SSL exception";
282 } else if (mException
instanceof DavException
) {
283 return "Unexpected WebDAV exception";
285 } else if (mException
instanceof HttpException
) {
286 return "HTTP violation";
288 } else if (mException
instanceof IOException
) {
289 return "Unrecovered transport exception";
291 } else if (mException
instanceof AccountNotFoundException
) {
292 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
293 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
295 } else if (mException
instanceof AccountsException
) {
296 return "Exception while using account";
298 } else if (mException
instanceof JSONException
) {
299 return "JSON exception";
302 return "Unexpected exception";
306 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
307 return "The ownCloud server is not configured!";
309 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
310 return "No network connection";
312 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
313 return "No valid ownCloud version was found at the server";
315 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
316 return "Local storage full";
318 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
319 return "Error while moving file to final directory";
321 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
322 return "Account already existing when creating a new one";
324 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
325 return "Authenticated with a different account than the one updating";
326 } else if (mCode
== ResultCode
.INVALID_CHARACTER_IN_NAME
) {
327 return "The file name contains an forbidden character";
330 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
334 public boolean isServerFail() {
335 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
338 public boolean isException() {
339 return (mException
!= null
);
342 public boolean isTemporalRedirection() {
343 return (mHttpCode
== 302 || mHttpCode
== 307);
346 public String
getRedirectedLocation() {
347 return mRedirectedLocation
;
350 public boolean isIdPRedirection() {
351 return (mRedirectedLocation
!= null
&&
352 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
353 mRedirectedLocation
.toLowerCase().contains("wayf")));