1 package com
.owncloud
.android
.oc_framework
.operations
;
2 /* ownCloud Android client application
3 * Copyright (C) 2012 Bartek Przybylski
4 * Copyright (C) 2012-2013 ownCloud Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import java
.io
.IOException
;
23 import java
.io
.Serializable
;
24 import java
.net
.MalformedURLException
;
25 import java
.net
.SocketException
;
26 import java
.net
.SocketTimeoutException
;
27 import java
.net
.UnknownHostException
;
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
.authentication
.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
= -4415103901492836870L;
60 private static final String TAG
= "RemoteOperationResult";
62 public enum ResultCode
{
69 INSTANCE_NOT_CONFIGURED
,
75 NO_NETWORK_CONNECTION
,
77 SSL_RECOVERABLE_PEER_UNVERIFIED
,
80 INVALID_LOCAL_FILE_NAME
,
86 LOCAL_STORAGE_NOT_MOVED
,
87 LOCAL_STORAGE_NOT_COPIED
,
88 OAUTH2_ERROR_ACCESS_DENIED
,
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 public RemoteOperationResult(ResultCode code
) {
104 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
107 private RemoteOperationResult(boolean success
, int httpCode
) {
109 mHttpCode
= httpCode
;
112 mCode
= ResultCode
.OK
;
114 } else if (httpCode
> 0) {
116 case HttpStatus
.SC_UNAUTHORIZED
:
117 mCode
= ResultCode
.UNAUTHORIZED
;
119 case HttpStatus
.SC_NOT_FOUND
:
120 mCode
= ResultCode
.FILE_NOT_FOUND
;
122 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
123 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
125 case HttpStatus
.SC_CONFLICT
:
126 mCode
= ResultCode
.CONFLICT
;
128 case HttpStatus
.SC_INSUFFICIENT_STORAGE
:
129 mCode
= ResultCode
.QUOTA_EXCEEDED
;
132 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
133 Log
.d(TAG
, "RemoteOperationResult has processed UNHANDLED_HTTP_CODE: " + httpCode
);
138 public RemoteOperationResult(boolean success
, int httpCode
, Header
[] headers
) {
139 this(success
, httpCode
);
140 if (headers
!= null
) {
142 for (int i
=0; i
<headers
.length
; i
++) {
143 current
= headers
[i
];
144 if ("Location".equals(current
.getName())) {
145 mRedirectedLocation
= current
.getValue();
152 public RemoteOperationResult(Exception e
) {
155 if (e
instanceof OperationCancelledException
) {
156 mCode
= ResultCode
.CANCELLED
;
158 } else if (e
instanceof SocketException
) {
159 mCode
= ResultCode
.WRONG_CONNECTION
;
161 } else if (e
instanceof SocketTimeoutException
) {
162 mCode
= ResultCode
.TIMEOUT
;
164 } else if (e
instanceof ConnectTimeoutException
) {
165 mCode
= ResultCode
.TIMEOUT
;
167 } else if (e
instanceof MalformedURLException
) {
168 mCode
= ResultCode
.INCORRECT_ADDRESS
;
170 } else if (e
instanceof UnknownHostException
) {
171 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
173 } else if (e
instanceof AccountNotFoundException
) {
174 mCode
= ResultCode
.ACCOUNT_NOT_FOUND
;
176 } else if (e
instanceof AccountsException
) {
177 mCode
= ResultCode
.ACCOUNT_EXCEPTION
;
179 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
180 CertificateCombinedException se
= getCertificateCombinedException(e
);
183 if (se
.isRecoverable()) {
184 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
186 } else if (e
instanceof RuntimeException
) {
187 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
190 mCode
= ResultCode
.SSL_ERROR
;
194 mCode
= ResultCode
.UNKNOWN_ERROR
;
199 public boolean isSuccess() {
203 public boolean isCancelled() {
204 return mCode
== ResultCode
.CANCELLED
;
207 public int getHttpCode() {
211 public ResultCode
getCode() {
215 public Exception
getException() {
219 public boolean isSslRecoverableException() {
220 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
223 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
224 CertificateCombinedException result
= null
;
225 if (e
instanceof CertificateCombinedException
) {
226 return (CertificateCombinedException
) e
;
228 Throwable cause
= mException
.getCause();
229 Throwable previousCause
= null
;
230 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
231 previousCause
= cause
;
232 cause
= cause
.getCause();
234 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
235 result
= (CertificateCombinedException
) cause
;
240 public String
getLogMessage() {
242 if (mException
!= null
) {
243 if (mException
instanceof OperationCancelledException
) {
244 return "Operation cancelled by the caller";
246 } else if (mException
instanceof SocketException
) {
247 return "Socket exception";
249 } else if (mException
instanceof SocketTimeoutException
) {
250 return "Socket timeout exception";
252 } else if (mException
instanceof ConnectTimeoutException
) {
253 return "Connect timeout exception";
255 } else if (mException
instanceof MalformedURLException
) {
256 return "Malformed URL exception";
258 } else if (mException
instanceof UnknownHostException
) {
259 return "Unknown host exception";
261 } else if (mException
instanceof CertificateCombinedException
) {
262 if (((CertificateCombinedException
) mException
).isRecoverable())
263 return "SSL recoverable exception";
265 return "SSL exception";
267 } else if (mException
instanceof SSLException
) {
268 return "SSL exception";
270 } else if (mException
instanceof DavException
) {
271 return "Unexpected WebDAV exception";
273 } else if (mException
instanceof HttpException
) {
274 return "HTTP violation";
276 } else if (mException
instanceof IOException
) {
277 return "Unrecovered transport exception";
279 } else if (mException
instanceof AccountNotFoundException
) {
280 Account failedAccount
= ((AccountNotFoundException
)mException
).getFailedAccount();
281 return mException
.getMessage() + " (" + (failedAccount
!= null ? failedAccount
.name
: "NULL") + ")";
283 } else if (mException
instanceof AccountsException
) {
284 return "Exception while using account";
287 return "Unexpected exception";
291 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
292 return "The ownCloud server is not configured!";
294 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
295 return "No network connection";
297 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
298 return "No valid ownCloud version was found at the server";
300 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
301 return "Local storage full";
303 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
304 return "Error while moving file to final directory";
306 } else if (mCode
== ResultCode
.ACCOUNT_NOT_NEW
) {
307 return "Account already existing when creating a new one";
309 } else if (mCode
== ResultCode
.ACCOUNT_NOT_THE_SAME
) {
310 return "Authenticated with a different account than the one updating";
313 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess() ?
"success" : "fail") + ")";
317 public boolean isServerFail() {
318 return (mHttpCode
>= HttpStatus
.SC_INTERNAL_SERVER_ERROR
);
321 public boolean isException() {
322 return (mException
!= null
);
325 public boolean isTemporalRedirection() {
326 return (mHttpCode
== 302 || mHttpCode
== 307);
329 public String
getRedirectedLocation() {
330 return mRedirectedLocation
;
333 public boolean isIdPRedirection() {
334 return (mRedirectedLocation
!= null
&&
335 (mRedirectedLocation
.toUpperCase().contains("SAML") ||
336 mRedirectedLocation
.toLowerCase().contains("wayf")));