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
;
37 import com
.owncloud
.android
.oc_framework
.accounts
.AccountUtils
.AccountNotFoundException
;
38 import com
.owncloud
.android
.oc_framework
.network
.CertificateCombinedException
;
40 import android
.accounts
.Account
;
41 import android
.accounts
.AccountsException
;
42 import android
.util
.Log
;
46 * The result of a remote operation required to an ownCloud server.
48 * Provides a common classification of remote operation results for all the
51 * @author David A. Velasco
53 public class RemoteOperationResult
implements Serializable
{
55 /** Generated - should be refreshed every time the class changes!! */
56 private static final long serialVersionUID
= -2469951225222759283L;
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 INVALID_CHARACTER_IN_NAME
95 private boolean mSuccess
= false
;
96 private int mHttpCode
= -1;
97 private Exception mException
= null
;
98 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
99 private String mRedirectedLocation
;
101 private RemoteFile mFile
;
102 private ArrayList
<RemoteFile
> mFiles
;
104 public RemoteOperationResult(ResultCode code
) {
106 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
;
198 mCode
= ResultCode
.UNKNOWN_ERROR
;
204 public void setFile(RemoteFile file
){
207 public RemoteFile
getFile(){
210 public void setData(ArrayList
<RemoteFile
> files
){
214 public ArrayList
<RemoteFile
> getData(){
218 public boolean isSuccess() {
222 public boolean isCancelled() {
223 return mCode
== ResultCode
.CANCELLED
;
226 public int getHttpCode() {
230 public ResultCode
getCode() {
234 public Exception
getException() {
238 public boolean isSslRecoverableException() {
239 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
242 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
243 CertificateCombinedException result
= null
;
244 if (e
instanceof CertificateCombinedException
) {
245 return (CertificateCombinedException
) e
;
247 Throwable cause
= mException
.getCause();
248 Throwable previousCause
= null
;
249 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
250 previousCause
= cause
;
251 cause
= cause
.getCause();
253 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
254 result
= (CertificateCombinedException
) cause
;
259 public String
getLogMessage() {
261 if (mException
!= null
) {
262 if (mException
instanceof OperationCancelledException
) {
263 return "Operation cancelled by the caller";
265 } else if (mException
instanceof SocketException
) {
266 return "Socket exception";
268 } else if (mException
instanceof SocketTimeoutException
) {
269 return "Socket timeout exception";
271 } else if (mException
instanceof ConnectTimeoutException
) {
272 return "Connect timeout exception";
274 } else if (mException
instanceof MalformedURLException
) {
275 return "Malformed URL exception";
277 } else if (mException
instanceof UnknownHostException
) {
278 return "Unknown host exception";
280 } else if (mException
instanceof CertificateCombinedException
) {
281 if (((CertificateCombinedException
) mException
).isRecoverable())
282 return "SSL recoverable exception";
284 return "SSL exception";
286 } else if (mException
instanceof SSLException
) {
287 return "SSL exception";
289 } else if (mException
instanceof DavException
) {
290 return "Unexpected WebDAV exception";
292 } else if (mException
instanceof HttpException
) {
293 return "HTTP violation";
295 } else if (mException
instanceof IOException
) {
296 return "Unrecovered transport exception";
298 } else if (mException
instanceof AccountNotFoundException
) {
299 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
300 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
302 } else if (mException
instanceof AccountsException
) {
303 return "Exception while using account";
306 return "Unexpected exception";
310 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
311 return "The ownCloud server is not configured!";
313 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
314 return "No network connection";
316 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
317 return "No valid ownCloud version was found at the server";
319 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
320 return "Local storage full";
322 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
323 return "Error while moving file to final directory";
325 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
326 return "Account already existing when creating a new one";
328 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
329 return "Authenticated with a different account than the one updating";
330 } else if (mCode
== ResultCode
.INVALID_CHARACTER_IN_NAME
) {
331 return "The file name contains an forbidden character";
334 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
338 public boolean isServerFail() {
339 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
342 public boolean isException() {
343 return (mException
!= null
);
346 public boolean isTemporalRedirection() {
347 return (mHttpCode
== 302 || mHttpCode
== 307);
350 public String
getRedirectedLocation() {
351 return mRedirectedLocation
;
354 public boolean isIdPRedirection() {
355 return (mRedirectedLocation
!= null
&&
356 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
357 mRedirectedLocation
.toLowerCase().contains("wayf")));