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
;
104 private String mUserName
;
106 public RemoteOperationResult(ResultCode code
) {
108 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
113 private RemoteOperationResult(boolean success
, int httpCode
) {
115 mHttpCode
= httpCode
;
118 mCode
= ResultCode
.OK
;
120 } else if (httpCode
> 0) {
122 case HttpStatus
.SC_UNAUTHORIZED
:
123 mCode
= ResultCode
.UNAUTHORIZED
;
125 case HttpStatus
.SC_NOT_FOUND
:
126 mCode
= ResultCode
.FILE_NOT_FOUND
;
128 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
129 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
131 case HttpStatus
.SC_CONFLICT
:
132 mCode
= ResultCode
.CONFLICT
;
134 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
135 mCode
= ResultCode
.QUOTA_EXCEEDED
;
138 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
139 Log
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
144 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
145 this(success
, httpCode
);
146 if (headers
!= null
) {
148 for (int i
=0; i
<headers
.length
; i
++) {
149 current
= headers
[i
];
150 if ("Location".equals(current
.getName())) {
151 mRedirectedLocation
= current
.getValue();
158 public RemoteOperationResult(Exception e
) {
161 if (e
instanceof OperationCancelledException
) {
162 mCode
= ResultCode
.CANCELLED
;
164 } else if (e
instanceof SocketException
) {
165 mCode
= ResultCode
.WRONG_CONNECTION
;
167 } else if (e
instanceof SocketTimeoutException
) {
168 mCode
= ResultCode
.TIMEOUT
;
170 } else if (e
instanceof ConnectTimeoutException
) {
171 mCode
= ResultCode
.TIMEOUT
;
173 } else if (e
instanceof MalformedURLException
) {
174 mCode
= ResultCode
.INCORRECT_ADDRESS
;
176 } else if (e
instanceof UnknownHostException
) {
177 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
179 } else if (e
instanceof AccountNotFoundException
) {
180 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
182 } else if (e
instanceof AccountsException
) {
183 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
185 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
186 CertificateCombinedException se
= getCertificateCombinedException(e
);
189 if (se
.isRecoverable()) {
190 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
192 } else if (e
instanceof RuntimeException
) {
193 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
196 mCode
= ResultCode
.SSL_ERROR
;
199 } else if (e
instanceof JSONException
) {
200 mCode
= ResultCode
.JSON_EXCEPTION
;
203 mCode
= ResultCode
.UNKNOWN_ERROR
;
209 public void setData(ArrayList
<RemoteFile
> files
){
213 public ArrayList
<RemoteFile
> getData(){
217 public boolean isSuccess() {
221 public boolean isCancelled() {
222 return mCode
== ResultCode
.CANCELLED
;
225 public int getHttpCode() {
229 public ResultCode
getCode() {
233 public Exception
getException() {
237 public boolean isSslRecoverableException() {
238 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
241 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
242 CertificateCombinedException result
= null
;
243 if (e
instanceof CertificateCombinedException
) {
244 return (CertificateCombinedException
) e
;
246 Throwable cause
= mException
.getCause();
247 Throwable previousCause
= null
;
248 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
249 previousCause
= cause
;
250 cause
= cause
.getCause();
252 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
253 result
= (CertificateCombinedException
) cause
;
258 public String
getLogMessage() {
260 if (mException
!= null
) {
261 if (mException
instanceof OperationCancelledException
) {
262 return "Operation cancelled by the caller";
264 } else if (mException
instanceof SocketException
) {
265 return "Socket exception";
267 } else if (mException
instanceof SocketTimeoutException
) {
268 return "Socket timeout exception";
270 } else if (mException
instanceof ConnectTimeoutException
) {
271 return "Connect timeout exception";
273 } else if (mException
instanceof MalformedURLException
) {
274 return "Malformed URL exception";
276 } else if (mException
instanceof UnknownHostException
) {
277 return "Unknown host exception";
279 } else if (mException
instanceof CertificateCombinedException
) {
280 if (((CertificateCombinedException
) mException
).isRecoverable())
281 return "SSL recoverable exception";
283 return "SSL exception";
285 } else if (mException
instanceof SSLException
) {
286 return "SSL exception";
288 } else if (mException
instanceof DavException
) {
289 return "Unexpected WebDAV exception";
291 } else if (mException
instanceof HttpException
) {
292 return "HTTP violation";
294 } else if (mException
instanceof IOException
) {
295 return "Unrecovered transport exception";
297 } else if (mException
instanceof AccountNotFoundException
) {
298 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
299 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
301 } else if (mException
instanceof AccountsException
) {
302 return "Exception while using account";
304 } else if (mException
instanceof JSONException
) {
305 return "JSON exception";
307 return "Unexpected exception";
311 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
312 return "The ownCloud server is not configured!";
314 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
315 return "No network connection";
317 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
318 return "No valid ownCloud version was found at the server";
320 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
321 return "Local storage full";
323 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
324 return "Error while moving file to final directory";
326 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
327 return "Account already existing when creating a new one";
329 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
330 return "Authenticated with a different account than the one updating";
331 } else if (mCode
== ResultCode
.INVALID_CHARACTER_IN_NAME
) {
332 return "The file name contains an forbidden character";
335 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
339 public boolean isServerFail() {
340 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
343 public boolean isException() {
344 return (mException
!= null
);
347 public boolean isTemporalRedirection() {
348 return (mHttpCode
== 302 || mHttpCode
== 307);
351 public String
getRedirectedLocation() {
352 return mRedirectedLocation
;
355 public boolean isIdPRedirection() {
356 return (mRedirectedLocation
!= null
&&
357 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
358 mRedirectedLocation
.toLowerCase().contains("wayf")));
361 public String
getUserName() {
365 public void setUserName(String mUserName
) {
366 this.mUserName
= mUserName
;