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 - should be refreshed every time the class changes!! */
48 private static final long serialVersionUID
= 5336333154035462033L;
51 public enum ResultCode
{
58 INSTANCE_NOT_CONFIGURED
,
64 NO_NETWORK_CONNECTION
,
66 SSL_RECOVERABLE_PEER_UNVERIFIED
,
69 INVALID_LOCAL_FILE_NAME
,
74 LOCAL_STORAGE_NOT_MOVED
77 private boolean mSuccess
= false
;
78 private int mHttpCode
= -1;
79 private Exception mException
= null
;
80 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
81 private Object mExtraData
= null
;
83 public RemoteOperationResult(ResultCode code
) {
85 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
88 public RemoteOperationResult(boolean success
, int httpCode
) {
93 mCode
= ResultCode
.OK
;
95 } else if (httpCode
> 0) {
97 case HttpStatus
.SC_UNAUTHORIZED
:
98 mCode
= ResultCode
.UNAUTHORIZED
;
100 case HttpStatus
.SC_NOT_FOUND
:
101 mCode
= ResultCode
.FILE_NOT_FOUND
;
103 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
104 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
106 case HttpStatus
.SC_CONFLICT
:
107 mCode
= ResultCode
.CONFLICT
;
110 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
115 public RemoteOperationResult(Exception e
) {
118 if (e
instanceof OperationCancelledException
) {
119 mCode
= ResultCode
.CANCELLED
;
121 } else if (e
instanceof SocketException
) {
122 mCode
= ResultCode
.WRONG_CONNECTION
;
124 } else if (e
instanceof SocketTimeoutException
) {
125 mCode
= ResultCode
.TIMEOUT
;
127 } else if (e
instanceof ConnectTimeoutException
) {
128 mCode
= ResultCode
.TIMEOUT
;
130 } else if (e
instanceof MalformedURLException
) {
131 mCode
= ResultCode
.INCORRECT_ADDRESS
;
133 } else if (e
instanceof UnknownHostException
) {
134 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
136 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
137 CertificateCombinedException se
= getCertificateCombinedException(e
);
140 if (se
.isRecoverable()) {
141 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
145 mCode
= ResultCode
.SSL_ERROR
;
149 mCode
= ResultCode
.UNKNOWN_ERROR
;
155 public boolean isSuccess() {
159 public boolean isCancelled() {
160 return mCode
== ResultCode
.CANCELLED
;
163 public int getHttpCode() {
167 public ResultCode
getCode() {
171 public Exception
getException() {
175 public boolean isSslRecoverableException() {
176 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
179 public void setExtraData(Object data
) {
183 public Object
getExtraData() {
187 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
188 CertificateCombinedException result
= null
;
189 if (e
instanceof CertificateCombinedException
) {
190 return (CertificateCombinedException
)e
;
192 Throwable cause
= mException
.getCause();
193 Throwable previousCause
= null
;
194 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
195 previousCause
= cause
;
196 cause
= cause
.getCause();
198 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
199 result
= (CertificateCombinedException
)cause
;
205 public String
getLogMessage() {
207 if (mException
!= null
) {
208 if (mException
instanceof OperationCancelledException
) {
209 return "Operation cancelled by the caller";
211 } else if (mException
instanceof SocketException
) {
212 return "Socket exception";
214 } else if (mException
instanceof SocketTimeoutException
) {
215 return "Socket timeout exception";
217 } else if (mException
instanceof ConnectTimeoutException
) {
218 return "Connect timeout exception";
220 } else if (mException
instanceof MalformedURLException
) {
221 return "Malformed URL exception";
223 } else if (mException
instanceof UnknownHostException
) {
224 return "Unknown host exception";
226 } else if (mException
instanceof CertificateCombinedException
) {
227 if (((CertificateCombinedException
) mException
).isRecoverable())
228 return "SSL recoverable exception";
230 return "SSL exception";
232 } else if (mException
instanceof SSLException
) {
233 return "SSL exception";
235 } else if (mException
instanceof DavException
) {
236 return "Unexpected WebDAV exception";
238 } else if (mException
instanceof HttpException
) {
239 return "HTTP violation";
241 } else if (mException
instanceof IOException
) {
242 return "Unrecovered transport exception";
245 return "Unexpected exception";
249 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
250 return "The ownCloud server is not configured!";
252 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
253 return "No network connection";
255 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
256 return "No valid ownCloud version was found at the server";
258 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
259 return "Local storage full";
261 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
262 return "Error while moving file to final directory";
265 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";