1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 package com
.owncloud
.android
.oc_framework
.operations
;
28 import java
.io
.IOException
;
29 import java
.io
.Serializable
;
30 import java
.net
.MalformedURLException
;
31 import java
.net
.SocketException
;
32 import java
.net
.SocketTimeoutException
;
33 import java
.net
.UnknownHostException
;
34 import java
.util
.ArrayList
;
36 import javax
.net
.ssl
.SSLException
;
38 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
39 import org
.apache
.commons
.httpclient
.Header
;
40 import org
.apache
.commons
.httpclient
.HttpException
;
41 import org
.apache
.commons
.httpclient
.HttpStatus
;
42 import org
.apache
.jackrabbit
.webdav
.DavException
;
44 import com
.owncloud
.android
.oc_framework
.accounts
.AccountUtils
.AccountNotFoundException
;
45 import com
.owncloud
.android
.oc_framework
.network
.CertificateCombinedException
;
47 import android
.accounts
.Account
;
48 import android
.accounts
.AccountsException
;
49 import android
.util
.Log
;
53 * The result of a remote operation required to an ownCloud server.
55 * Provides a common classification of remote operation results for all the
58 * @author David A. Velasco
60 public class RemoteOperationResult
implements Serializable
{
62 /** Generated - should be refreshed every time the class changes!! */
63 private static final long serialVersionUID
= -2469951225222759283L;
65 private static final String TAG
= "RemoteOperationResult";
67 public enum ResultCode
{
74 INSTANCE_NOT_CONFIGURED
,
80 NO_NETWORK_CONNECTION
,
82 SSL_RECOVERABLE_PEER_UNVERIFIED
,
85 INVALID_LOCAL_FILE_NAME
,
91 LOCAL_STORAGE_NOT_MOVED
,
92 LOCAL_STORAGE_NOT_COPIED
,
93 OAUTH2_ERROR_ACCESS_DENIED
,
99 INVALID_CHARACTER_IN_NAME
102 private boolean mSuccess
= false
;
103 private int mHttpCode
= -1;
104 private Exception mException
= null
;
105 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
106 private String mRedirectedLocation
;
108 private ArrayList
<RemoteFile
> mFiles
;
110 public RemoteOperationResult(ResultCode code
) {
112 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
116 private RemoteOperationResult(boolean success
, int httpCode
) {
118 mHttpCode
= httpCode
;
121 mCode
= ResultCode
.OK
;
123 } else if (httpCode
> 0) {
125 case HttpStatus
.SC_UNAUTHORIZED
:
126 mCode
= ResultCode
.UNAUTHORIZED
;
128 case HttpStatus
.SC_NOT_FOUND
:
129 mCode
= ResultCode
.FILE_NOT_FOUND
;
131 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
132 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
134 case HttpStatus
.SC_CONFLICT
:
135 mCode
= ResultCode
.CONFLICT
;
137 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
138 mCode
= ResultCode
.QUOTA_EXCEEDED
;
141 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
142 Log
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
147 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
148 this(success
, httpCode
);
149 if (headers
!= null
) {
151 for (int i
=0; i
<headers
.length
; i
++) {
152 current
= headers
[i
];
153 if ("Location".equals(current
.getName())) {
154 mRedirectedLocation
= current
.getValue();
161 public RemoteOperationResult(Exception e
) {
164 if (e
instanceof OperationCancelledException
) {
165 mCode
= ResultCode
.CANCELLED
;
167 } else if (e
instanceof SocketException
) {
168 mCode
= ResultCode
.WRONG_CONNECTION
;
170 } else if (e
instanceof SocketTimeoutException
) {
171 mCode
= ResultCode
.TIMEOUT
;
173 } else if (e
instanceof ConnectTimeoutException
) {
174 mCode
= ResultCode
.TIMEOUT
;
176 } else if (e
instanceof MalformedURLException
) {
177 mCode
= ResultCode
.INCORRECT_ADDRESS
;
179 } else if (e
instanceof UnknownHostException
) {
180 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
182 } else if (e
instanceof AccountNotFoundException
) {
183 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
185 } else if (e
instanceof AccountsException
) {
186 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
188 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
189 CertificateCombinedException se
= getCertificateCombinedException(e
);
192 if (se
.isRecoverable()) {
193 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
195 } else if (e
instanceof RuntimeException
) {
196 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
199 mCode
= ResultCode
.SSL_ERROR
;
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";
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")));