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 ArrayList
<RemoteFile
> mFiles
;
103 public RemoteOperationResult(ResultCode code
) {
105 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
109 private RemoteOperationResult(boolean success
, int httpCode
) {
111 mHttpCode
= httpCode
;
114 mCode
= ResultCode
.OK
;
116 } else if (httpCode
> 0) {
118 case HttpStatus
.SC_UNAUTHORIZED
:
119 mCode
= ResultCode
.UNAUTHORIZED
;
121 case HttpStatus
.SC_NOT_FOUND
:
122 mCode
= ResultCode
.FILE_NOT_FOUND
;
124 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
125 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
127 case HttpStatus
.SC_CONFLICT
:
128 mCode
= ResultCode
.CONFLICT
;
130 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
131 mCode
= ResultCode
.QUOTA_EXCEEDED
;
134 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
135 Log
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
140 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
141 this(success
, httpCode
);
142 if (headers
!= null
) {
144 for (int i
=0; i
<headers
.length
; i
++) {
145 current
= headers
[i
];
146 if ("Location".equals(current
.getName())) {
147 mRedirectedLocation
= current
.getValue();
154 public RemoteOperationResult(Exception e
) {
157 if (e
instanceof OperationCancelledException
) {
158 mCode
= ResultCode
.CANCELLED
;
160 } else if (e
instanceof SocketException
) {
161 mCode
= ResultCode
.WRONG_CONNECTION
;
163 } else if (e
instanceof SocketTimeoutException
) {
164 mCode
= ResultCode
.TIMEOUT
;
166 } else if (e
instanceof ConnectTimeoutException
) {
167 mCode
= ResultCode
.TIMEOUT
;
169 } else if (e
instanceof MalformedURLException
) {
170 mCode
= ResultCode
.INCORRECT_ADDRESS
;
172 } else if (e
instanceof UnknownHostException
) {
173 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
175 } else if (e
instanceof AccountNotFoundException
) {
176 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
178 } else if (e
instanceof AccountsException
) {
179 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
181 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
182 CertificateCombinedException se
= getCertificateCombinedException(e
);
185 if (se
.isRecoverable()) {
186 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
188 } else if (e
instanceof RuntimeException
) {
189 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
192 mCode
= ResultCode
.SSL_ERROR
;
196 mCode
= ResultCode
.UNKNOWN_ERROR
;
202 public void setData(ArrayList
<RemoteFile
> files
){
206 public ArrayList
<RemoteFile
> getData(){
210 public boolean isSuccess() {
214 public boolean isCancelled() {
215 return mCode
== ResultCode
.CANCELLED
;
218 public int getHttpCode() {
222 public ResultCode
getCode() {
226 public Exception
getException() {
230 public boolean isSslRecoverableException() {
231 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
234 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
235 CertificateCombinedException result
= null
;
236 if (e
instanceof CertificateCombinedException
) {
237 return (CertificateCombinedException
) e
;
239 Throwable cause
= mException
.getCause();
240 Throwable previousCause
= null
;
241 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
242 previousCause
= cause
;
243 cause
= cause
.getCause();
245 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
246 result
= (CertificateCombinedException
) cause
;
251 public String
getLogMessage() {
253 if (mException
!= null
) {
254 if (mException
instanceof OperationCancelledException
) {
255 return "Operation cancelled by the caller";
257 } else if (mException
instanceof SocketException
) {
258 return "Socket exception";
260 } else if (mException
instanceof SocketTimeoutException
) {
261 return "Socket timeout exception";
263 } else if (mException
instanceof ConnectTimeoutException
) {
264 return "Connect timeout exception";
266 } else if (mException
instanceof MalformedURLException
) {
267 return "Malformed URL exception";
269 } else if (mException
instanceof UnknownHostException
) {
270 return "Unknown host exception";
272 } else if (mException
instanceof CertificateCombinedException
) {
273 if (((CertificateCombinedException
) mException
).isRecoverable())
274 return "SSL recoverable exception";
276 return "SSL exception";
278 } else if (mException
instanceof SSLException
) {
279 return "SSL exception";
281 } else if (mException
instanceof DavException
) {
282 return "Unexpected WebDAV exception";
284 } else if (mException
instanceof HttpException
) {
285 return "HTTP violation";
287 } else if (mException
instanceof IOException
) {
288 return "Unrecovered transport exception";
290 } else if (mException
instanceof AccountNotFoundException
) {
291 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
292 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
294 } else if (mException
instanceof AccountsException
) {
295 return "Exception while using account";
298 return "Unexpected exception";
302 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
303 return "The ownCloud server is not configured!";
305 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
306 return "No network connection";
308 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
309 return "No valid ownCloud version was found at the server";
311 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
312 return "Local storage full";
314 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
315 return "Error while moving file to final directory";
317 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
318 return "Account already existing when creating a new one";
320 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
321 return "Authenticated with a different account than the one updating";
322 } else if (mCode
== ResultCode
.INVALID_CHARACTER_IN_NAME
) {
323 return "The file name contains an forbidden character";
326 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
330 public boolean isServerFail() {
331 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
334 public boolean isException() {
335 return (mException
!= null
);
338 public boolean isTemporalRedirection() {
339 return (mHttpCode
== 302 || mHttpCode
== 307);
342 public String
getRedirectedLocation() {
343 return mRedirectedLocation
;
346 public boolean isIdPRedirection() {
347 return (mRedirectedLocation
!= null
&&
348 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
349 mRedirectedLocation
.toLowerCase().contains("wayf")));