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
;
34 import com
.owncloud
.android
.network
.CertificateCombinedException
;
38 * The result of a remote operation required to an ownCloud server.
40 * Provides a common classification of remote operation results for all the application.
42 * @author David A. Velasco
44 public class RemoteOperationResult
implements Serializable
{
46 /** Generated - to refresh every time the class changes */
47 private static final long serialVersionUID
= -7805531062432602444L;
50 public enum ResultCode
{
57 INSTANCE_NOT_CONFIGURED
,
63 NO_NETWORK_CONNECTION
,
65 SSL_RECOVERABLE_PEER_UNVERIFIED
,
67 STORAGE_ERROR_MOVING_FROM_TMP
,
71 private boolean mSuccess
= false
;
72 private int mHttpCode
= -1;
73 private Exception mException
= null
;
74 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
76 public RemoteOperationResult(ResultCode code
) {
78 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
81 public RemoteOperationResult(boolean success
, int httpCode
) {
86 mCode
= ResultCode
.OK
;
88 } else if (httpCode
> 0) {
90 case HttpStatus
.SC_UNAUTHORIZED
:
91 mCode
= ResultCode
.UNAUTHORIZED
;
93 case HttpStatus
.SC_NOT_FOUND
:
94 mCode
= ResultCode
.FILE_NOT_FOUND
;
96 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
97 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
100 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
105 public RemoteOperationResult(Exception e
) {
108 if (e
instanceof OperationCancelledException
) {
109 mCode
= ResultCode
.CANCELLED
;
111 } else if (e
instanceof SocketException
) {
112 mCode
= ResultCode
.WRONG_CONNECTION
;
114 } else if (e
instanceof SocketTimeoutException
) {
115 mCode
= ResultCode
.TIMEOUT
;
117 } else if (e
instanceof ConnectTimeoutException
) {
118 mCode
= ResultCode
.TIMEOUT
;
120 } else if (e
instanceof MalformedURLException
) {
121 mCode
= ResultCode
.INCORRECT_ADDRESS
;
123 } else if (e
instanceof UnknownHostException
) {
124 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
126 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
127 CertificateCombinedException se
= getCertificateCombinedException(e
);
130 if (se
.isRecoverable()) {
131 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
135 mCode
= ResultCode
.SSL_ERROR
;
139 mCode
= ResultCode
.UNKNOWN_ERROR
;
145 public boolean isSuccess() {
149 public boolean isCancelled() {
150 return mCode
== ResultCode
.CANCELLED
;
153 public int getHttpCode() {
157 public ResultCode
getCode() {
161 public Exception
getException() {
165 public boolean isSslRecoverableException() {
166 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
169 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
170 CertificateCombinedException result
= null
;
171 if (e
instanceof CertificateCombinedException
) {
172 return (CertificateCombinedException
)e
;
174 Throwable cause
= mException
.getCause();
175 Throwable previousCause
= null
;
176 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
177 previousCause
= cause
;
178 cause
= cause
.getCause();
180 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
181 result
= (CertificateCombinedException
)cause
;
187 public String
getLogMessage() {
189 if (mException
!= null
) {
190 if (mException
instanceof OperationCancelledException
) {
191 return "Operation cancelled by the caller";
193 } else if (mException
instanceof SocketException
) {
194 return "Socket exception";
196 } else if (mException
instanceof SocketTimeoutException
) {
197 return "Socket timeout exception";
199 } else if (mException
instanceof ConnectTimeoutException
) {
200 return "Connect timeout exception";
202 } else if (mException
instanceof MalformedURLException
) {
203 return "Malformed URL exception";
205 } else if (mException
instanceof UnknownHostException
) {
206 return "Unknown host exception";
208 } else if (mException
instanceof SSLException
) {
209 if (mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)
210 return "SSL recoverable exception";
212 return "SSL exception";
214 } else if (mException
instanceof HttpException
) {
215 return "HTTP violation";
217 } else if (mException
instanceof IOException
) {
218 return "Unrecovered transport exception";
221 return "Unexpected exception";
225 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
226 return "The ownCloud server is not configured!";
228 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
229 return "No network connection";
231 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
232 return "No valid ownCloud version was found at the server";
234 } else if (mCode
== ResultCode
.STORAGE_ERROR_MOVING_FROM_TMP
) {
235 return "Error while moving file from temporal to final directory";
238 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";