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
,
63 private boolean mSuccess
= false
;
64 private int mHttpCode
= -1;
65 private Exception mException
= null
;
66 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
68 public RemoteOperationResult(ResultCode code
) {
70 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
73 public RemoteOperationResult(boolean success
, int httpCode
) {
78 mCode
= ResultCode
.OK
;
80 } else if (httpCode
> 0) {
82 case HttpStatus
.SC_NOT_FOUND
:
83 mCode
= ResultCode
.FILE_NOT_FOUND
;
85 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
86 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
89 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
94 public RemoteOperationResult(Exception e
) {
97 if (e
instanceof SocketException
) {
98 mCode
= ResultCode
.WRONG_CONNECTION
;
100 } else if (e
instanceof SocketTimeoutException
) {
101 mCode
= ResultCode
.TIMEOUT
;
103 } else if (e
instanceof ConnectTimeoutException
) {
104 mCode
= ResultCode
.TIMEOUT
;
106 } else if (e
instanceof MalformedURLException
) {
107 mCode
= ResultCode
.INCORRECT_ADDRESS
;
109 } else if (e
instanceof UnknownHostException
) {
110 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
112 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
113 CertificateCombinedException se
= getCertificateCombinedException(e
);
116 if (se
.isRecoverable()) {
117 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
121 mCode
= ResultCode
.SSL_ERROR
;
125 mCode
= ResultCode
.UNKNOWN_ERROR
;
131 public boolean isSuccess() {
135 public int getHttpCode() {
139 public ResultCode
getCode() {
143 public Exception
getException() {
147 public boolean isSslRecoverableException() {
148 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
151 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
152 CertificateCombinedException result
= null
;
153 if (e
instanceof CertificateCombinedException
) {
154 return (CertificateCombinedException
)e
;
156 Throwable cause
= mException
.getCause();
157 Throwable previousCause
= null
;
158 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
159 previousCause
= cause
;
160 cause
= cause
.getCause();
162 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
163 result
= (CertificateCombinedException
)cause
;
169 public String
getLogMessage() {
171 if (mException
!= null
) {
172 if (mException
instanceof SocketException
) {
173 return "Socket exception";
175 } else if (mException
instanceof SocketTimeoutException
) {
176 return "Socket timeout exception";
178 } else if (mException
instanceof ConnectTimeoutException
) {
179 return "Connect timeout exception";
181 } else if (mException
instanceof MalformedURLException
) {
182 return "Malformed URL exception";
184 } else if (mException
instanceof UnknownHostException
) {
185 return "Unknown host exception";
187 } else if (mException
instanceof SSLException
) {
188 if (mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)
189 return "SSL recoverable exception";
191 return "SSL exception";
193 } else if (mException
instanceof HttpException
) {
194 return "HTTP violation";
196 } else if (mException
instanceof IOException
) {
197 return "Unrecovered transport exception";
200 return "Unexpected exception";
204 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
205 return "The ownCloud server is not configured!";
207 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
208 return "No network connection";
210 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
211 return "No valid ownCloud version was found at the server";
214 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";