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
,
70 INVALID_LOCAL_FILE_NAME
,
75 private boolean mSuccess
= false
;
76 private int mHttpCode
= -1;
77 private Exception mException
= null
;
78 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
79 private Object mExtraData
= null
;
81 public RemoteOperationResult(ResultCode code
) {
83 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
86 public RemoteOperationResult(boolean success
, int httpCode
) {
91 mCode
= ResultCode
.OK
;
93 } else if (httpCode
> 0) {
95 case HttpStatus
.SC_UNAUTHORIZED
:
96 mCode
= ResultCode
.UNAUTHORIZED
;
98 case HttpStatus
.SC_NOT_FOUND
:
99 mCode
= ResultCode
.FILE_NOT_FOUND
;
101 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
102 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
104 case HttpStatus
.SC_CONFLICT
:
105 mCode
= ResultCode
.CONFLICT
;
108 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
113 public RemoteOperationResult(Exception e
) {
116 if (e
instanceof OperationCancelledException
) {
117 mCode
= ResultCode
.CANCELLED
;
119 } else if (e
instanceof SocketException
) {
120 mCode
= ResultCode
.WRONG_CONNECTION
;
122 } else if (e
instanceof SocketTimeoutException
) {
123 mCode
= ResultCode
.TIMEOUT
;
125 } else if (e
instanceof ConnectTimeoutException
) {
126 mCode
= ResultCode
.TIMEOUT
;
128 } else if (e
instanceof MalformedURLException
) {
129 mCode
= ResultCode
.INCORRECT_ADDRESS
;
131 } else if (e
instanceof UnknownHostException
) {
132 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
134 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
135 CertificateCombinedException se
= getCertificateCombinedException(e
);
138 if (se
.isRecoverable()) {
139 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
143 mCode
= ResultCode
.SSL_ERROR
;
147 mCode
= ResultCode
.UNKNOWN_ERROR
;
153 public boolean isSuccess() {
157 public boolean isCancelled() {
158 return mCode
== ResultCode
.CANCELLED
;
161 public int getHttpCode() {
165 public ResultCode
getCode() {
169 public Exception
getException() {
173 public boolean isSslRecoverableException() {
174 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
177 public void setExtraData(Object data
) {
181 public Object
getExtraData() {
185 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
186 CertificateCombinedException result
= null
;
187 if (e
instanceof CertificateCombinedException
) {
188 return (CertificateCombinedException
)e
;
190 Throwable cause
= mException
.getCause();
191 Throwable previousCause
= null
;
192 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
193 previousCause
= cause
;
194 cause
= cause
.getCause();
196 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
197 result
= (CertificateCombinedException
)cause
;
203 public String
getLogMessage() {
205 if (mException
!= null
) {
206 if (mException
instanceof OperationCancelledException
) {
207 return "Operation cancelled by the caller";
209 } else if (mException
instanceof SocketException
) {
210 return "Socket exception";
212 } else if (mException
instanceof SocketTimeoutException
) {
213 return "Socket timeout exception";
215 } else if (mException
instanceof ConnectTimeoutException
) {
216 return "Connect timeout exception";
218 } else if (mException
instanceof MalformedURLException
) {
219 return "Malformed URL exception";
221 } else if (mException
instanceof UnknownHostException
) {
222 return "Unknown host exception";
224 } else if (mException
instanceof CertificateCombinedException
) {
225 if (((CertificateCombinedException
) mException
).isRecoverable())
226 return "SSL recoverable exception";
228 return "SSL exception";
230 } else if (mException
instanceof SSLException
) {
231 return "SSL exception";
233 } else if (mException
instanceof DavException
) {
234 return "Unexpected WebDAV exception";
236 } else if (mException
instanceof HttpException
) {
237 return "HTTP violation";
239 } else if (mException
instanceof IOException
) {
240 return "Unrecovered transport exception";
243 return "Unexpected exception";
247 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
248 return "The ownCloud server is not configured!";
250 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
251 return "No network connection";
253 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
254 return "No valid ownCloud version was found at the server";
256 } else if (mCode
== ResultCode
.STORAGE_ERROR_MOVING_FROM_TMP
) {
257 return "Error while moving file from temporal to final directory";
260 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";