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
,
74 private boolean mSuccess
= false
;
75 private int mHttpCode
= -1;
76 private Exception mException
= null
;
77 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
79 public RemoteOperationResult(ResultCode code
) {
81 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
84 public RemoteOperationResult(boolean success
, int httpCode
) {
89 mCode
= ResultCode
.OK
;
91 } else if (httpCode
> 0) {
93 case HttpStatus
.SC_UNAUTHORIZED
:
94 mCode
= ResultCode
.UNAUTHORIZED
;
96 case HttpStatus
.SC_NOT_FOUND
:
97 mCode
= ResultCode
.FILE_NOT_FOUND
;
99 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
100 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
103 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
108 public RemoteOperationResult(Exception e
) {
111 if (e
instanceof OperationCancelledException
) {
112 mCode
= ResultCode
.CANCELLED
;
114 } else if (e
instanceof SocketException
) {
115 mCode
= ResultCode
.WRONG_CONNECTION
;
117 } else if (e
instanceof SocketTimeoutException
) {
118 mCode
= ResultCode
.TIMEOUT
;
120 } else if (e
instanceof ConnectTimeoutException
) {
121 mCode
= ResultCode
.TIMEOUT
;
123 } else if (e
instanceof MalformedURLException
) {
124 mCode
= ResultCode
.INCORRECT_ADDRESS
;
126 } else if (e
instanceof UnknownHostException
) {
127 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
129 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
130 CertificateCombinedException se
= getCertificateCombinedException(e
);
133 if (se
.isRecoverable()) {
134 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
138 mCode
= ResultCode
.SSL_ERROR
;
142 mCode
= ResultCode
.UNKNOWN_ERROR
;
148 public boolean isSuccess() {
152 public boolean isCancelled() {
153 return mCode
== ResultCode
.CANCELLED
;
156 public int getHttpCode() {
160 public ResultCode
getCode() {
164 public Exception
getException() {
168 public boolean isSslRecoverableException() {
169 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
172 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
173 CertificateCombinedException result
= null
;
174 if (e
instanceof CertificateCombinedException
) {
175 return (CertificateCombinedException
)e
;
177 Throwable cause
= mException
.getCause();
178 Throwable previousCause
= null
;
179 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
180 previousCause
= cause
;
181 cause
= cause
.getCause();
183 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
184 result
= (CertificateCombinedException
)cause
;
190 public String
getLogMessage() {
192 if (mException
!= null
) {
193 if (mException
instanceof OperationCancelledException
) {
194 return "Operation cancelled by the caller";
196 } else if (mException
instanceof SocketException
) {
197 return "Socket exception";
199 } else if (mException
instanceof SocketTimeoutException
) {
200 return "Socket timeout exception";
202 } else if (mException
instanceof ConnectTimeoutException
) {
203 return "Connect timeout exception";
205 } else if (mException
instanceof MalformedURLException
) {
206 return "Malformed URL exception";
208 } else if (mException
instanceof UnknownHostException
) {
209 return "Unknown host exception";
211 } else if (mException
instanceof CertificateCombinedException
) {
212 if (((CertificateCombinedException
) mException
).isRecoverable())
213 return "SSL recoverable exception";
215 return "SSL exception";
217 } else if (mException
instanceof SSLException
) {
218 return "SSL exception";
220 } else if (mException
instanceof DavException
) {
221 return "Unexpected WebDAV exception";
223 } else if (mException
instanceof HttpException
) {
224 return "HTTP violation";
226 } else if (mException
instanceof IOException
) {
227 return "Unrecovered transport exception";
230 return "Unexpected exception";
234 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
235 return "The ownCloud server is not configured!";
237 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
238 return "No network connection";
240 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
241 return "No valid ownCloud version was found at the server";
243 } else if (mCode
== ResultCode
.STORAGE_ERROR_MOVING_FROM_TMP
) {
244 return "Error while moving file from temporal to final directory";
247 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";