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
.net
.MalformedURLException
;
23 import java
.net
.SocketException
;
24 import java
.net
.SocketTimeoutException
;
25 import java
.net
.UnknownHostException
;
27 import javax
.net
.ssl
.SSLException
;
29 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
30 import org
.apache
.commons
.httpclient
.HttpException
;
31 import org
.apache
.commons
.httpclient
.HttpStatus
;
33 import com
.owncloud
.android
.network
.CertificateCombinedException
;
37 * The result of a remote operation required to an ownCloud server.
39 * Provides a common classification of remote operation results for all the application.
41 * @author David A. Velasco
43 public class RemoteOperationResult
{
45 public enum ResultCode
{
51 INSTANCE_NOT_CONFIGURED
,
57 NO_NETWORK_CONNECTION
,
59 SSL_RECOVERABLE_PEER_UNVERIFIED
,
61 STORAGE_ERROR_MOVING_FROM_TMP
,
65 private boolean mSuccess
= false
;
66 private int mHttpCode
= -1;
67 private Exception mException
= null
;
68 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
70 public RemoteOperationResult(ResultCode code
) {
72 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
75 public RemoteOperationResult(boolean success
, int httpCode
) {
80 mCode
= ResultCode
.OK
;
82 } else if (httpCode
> 0) {
84 case HttpStatus
.SC_NOT_FOUND
:
85 mCode
= ResultCode
.FILE_NOT_FOUND
;
87 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
88 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
91 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
96 public RemoteOperationResult(Exception e
) {
99 if (e
instanceof OperationCancelledException
) {
100 mCode
= ResultCode
.CANCELLED
;
102 } else if (e
instanceof SocketException
) {
103 mCode
= ResultCode
.WRONG_CONNECTION
;
105 } else if (e
instanceof SocketTimeoutException
) {
106 mCode
= ResultCode
.TIMEOUT
;
108 } else if (e
instanceof ConnectTimeoutException
) {
109 mCode
= ResultCode
.TIMEOUT
;
111 } else if (e
instanceof MalformedURLException
) {
112 mCode
= ResultCode
.INCORRECT_ADDRESS
;
114 } else if (e
instanceof UnknownHostException
) {
115 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
117 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
118 CertificateCombinedException se
= getCertificateCombinedException(e
);
121 if (se
.isRecoverable()) {
122 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
126 mCode
= ResultCode
.SSL_ERROR
;
130 mCode
= ResultCode
.UNKNOWN_ERROR
;
136 public boolean isSuccess() {
140 public boolean isCancelled() {
141 return mCode
== ResultCode
.CANCELLED
;
144 public int getHttpCode() {
148 public ResultCode
getCode() {
152 public Exception
getException() {
156 public boolean isSslRecoverableException() {
157 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
160 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
161 CertificateCombinedException result
= null
;
162 if (e
instanceof CertificateCombinedException
) {
163 return (CertificateCombinedException
)e
;
165 Throwable cause
= mException
.getCause();
166 Throwable previousCause
= null
;
167 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
168 previousCause
= cause
;
169 cause
= cause
.getCause();
171 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
172 result
= (CertificateCombinedException
)cause
;
178 public String
getLogMessage() {
180 if (mException
!= null
) {
181 if (mException
instanceof OperationCancelledException
) {
182 return "Operation cancelled by the caller";
184 } else if (mException
instanceof SocketException
) {
185 return "Socket exception";
187 } else if (mException
instanceof SocketTimeoutException
) {
188 return "Socket timeout exception";
190 } else if (mException
instanceof ConnectTimeoutException
) {
191 return "Connect timeout exception";
193 } else if (mException
instanceof MalformedURLException
) {
194 return "Malformed URL exception";
196 } else if (mException
instanceof UnknownHostException
) {
197 return "Unknown host exception";
199 } else if (mException
instanceof SSLException
) {
200 if (mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)
201 return "SSL recoverable exception";
203 return "SSL exception";
205 } else if (mException
instanceof HttpException
) {
206 return "HTTP violation";
208 } else if (mException
instanceof IOException
) {
209 return "Unrecovered transport exception";
212 return "Unexpected exception";
216 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
217 return "The ownCloud server is not configured!";
219 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
220 return "No network connection";
222 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
223 return "No valid ownCloud version was found at the server";
225 } else if (mCode
== ResultCode
.STORAGE_ERROR_MOVING_FROM_TMP
) {
226 return "Error while moving file from temporal to final directory";
229 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";