1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.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
;
28 import javax
.net
.ssl
.SSLException
;
30 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
31 import org
.apache
.commons
.httpclient
.HttpException
;
32 import org
.apache
.commons
.httpclient
.HttpStatus
;
33 import org
.apache
.jackrabbit
.webdav
.DavException
;
35 import com
.owncloud
.android
.network
.CertificateCombinedException
;
39 * The result of a remote operation required to an ownCloud server.
41 * Provides a common classification of remote operation results for all the application.
43 * @author David A. Velasco
45 public class RemoteOperationResult
implements Serializable
{
47 /** Generated - to refresh every time the class changes */
48 private static final long serialVersionUID
= -7805531062432602444L;
51 public enum ResultCode
{
58 INSTANCE_NOT_CONFIGURED
,
64 NO_NETWORK_CONNECTION
,
66 SSL_RECOVERABLE_PEER_UNVERIFIED
,
68 STORAGE_ERROR_MOVING_FROM_TMP
,
72 private boolean mSuccess
= false
;
73 private int mHttpCode
= -1;
74 private Exception mException
= null
;
75 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
77 public RemoteOperationResult(ResultCode code
) {
79 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
82 public RemoteOperationResult(boolean success
, int httpCode
) {
87 mCode
= ResultCode
.OK
;
89 } else if (httpCode
> 0) {
91 case HttpStatus
.SC_UNAUTHORIZED
:
92 mCode
= ResultCode
.UNAUTHORIZED
;
94 case HttpStatus
.SC_NOT_FOUND
:
95 mCode
= ResultCode
.FILE_NOT_FOUND
;
97 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
98 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
101 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
106 public RemoteOperationResult(Exception e
) {
109 if (e
instanceof OperationCancelledException
) {
110 mCode
= ResultCode
.CANCELLED
;
112 } else if (e
instanceof SocketException
) {
113 mCode
= ResultCode
.WRONG_CONNECTION
;
115 } else if (e
instanceof SocketTimeoutException
) {
116 mCode
= ResultCode
.TIMEOUT
;
118 } else if (e
instanceof ConnectTimeoutException
) {
119 mCode
= ResultCode
.TIMEOUT
;
121 } else if (e
instanceof MalformedURLException
) {
122 mCode
= ResultCode
.INCORRECT_ADDRESS
;
124 } else if (e
instanceof UnknownHostException
) {
125 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
127 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
128 CertificateCombinedException se
= getCertificateCombinedException(e
);
131 if (se
.isRecoverable()) {
132 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
136 mCode
= ResultCode
.SSL_ERROR
;
140 mCode
= ResultCode
.UNKNOWN_ERROR
;
146 public boolean isSuccess() {
150 public boolean isCancelled() {
151 return mCode
== ResultCode
.CANCELLED
;
154 public int getHttpCode() {
158 public ResultCode
getCode() {
162 public Exception
getException() {
166 public boolean isSslRecoverableException() {
167 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
170 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
171 CertificateCombinedException result
= null
;
172 if (e
instanceof CertificateCombinedException
) {
173 return (CertificateCombinedException
)e
;
175 Throwable cause
= mException
.getCause();
176 Throwable previousCause
= null
;
177 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
178 previousCause
= cause
;
179 cause
= cause
.getCause();
181 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
182 result
= (CertificateCombinedException
)cause
;
188 public String
getLogMessage() {
190 if (mException
!= null
) {
191 if (mException
instanceof OperationCancelledException
) {
192 return "Operation cancelled by the caller";
194 } else if (mException
instanceof SocketException
) {
195 return "Socket exception";
197 } else if (mException
instanceof SocketTimeoutException
) {
198 return "Socket timeout exception";
200 } else if (mException
instanceof ConnectTimeoutException
) {
201 return "Connect timeout exception";
203 } else if (mException
instanceof MalformedURLException
) {
204 return "Malformed URL exception";
206 } else if (mException
instanceof UnknownHostException
) {
207 return "Unknown host exception";
209 } else if (mException
instanceof CertificateCombinedException
) {
210 if (((CertificateCombinedException
) mException
).isRecoverable())
211 return "SSL recoverable exception";
213 return "SSL exception";
215 } else if (mException
instanceof SSLException
) {
216 return "SSL exception";
218 } else if (mException
instanceof DavException
) {
219 return "Unexpected WebDAV exception";
221 } else if (mException
instanceof HttpException
) {
222 return "HTTP violation";
224 } else if (mException
instanceof IOException
) {
225 return "Unrecovered transport exception";
228 return "Unexpected exception";
232 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
233 return "The ownCloud server is not configured!";
235 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
236 return "No network connection";
238 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
239 return "No valid ownCloud version was found at the server";
241 } else if (mCode
== ResultCode
.STORAGE_ERROR_MOVING_FROM_TMP
) {
242 return "Error while moving file from temporal to final directory";
245 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";