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
;
28 import javax
.net
.ssl
.SSLPeerUnverifiedException
;
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
{
46 public enum ResultCode
{
52 INSTANCE_NOT_CONFIGURED
,
58 NO_NETWORK_CONNECTION
,
60 SSL_RECOVERABLE_PEER_UNVERIFIED
,
64 private boolean mSuccess
= false
;
65 private int mHttpCode
= -1;
66 private Exception mException
= null
;
67 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
69 public RemoteOperationResult(ResultCode code
) {
71 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
74 public RemoteOperationResult(boolean success
, int httpCode
) {
79 mCode
= ResultCode
.OK
;
81 } else if (httpCode
> 0) {
83 case HttpStatus
.SC_NOT_FOUND
:
84 mCode
= ResultCode
.FILE_NOT_FOUND
;
86 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
87 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
90 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
95 public RemoteOperationResult(Exception e
) {
98 if (e
instanceof SocketException
) {
99 mCode
= ResultCode
.WRONG_CONNECTION
;
101 } else if (e
instanceof SocketTimeoutException
) {
102 mCode
= ResultCode
.TIMEOUT
;
104 } else if (e
instanceof ConnectTimeoutException
) {
105 mCode
= ResultCode
.TIMEOUT
;
107 } else if (e
instanceof MalformedURLException
) {
108 mCode
= ResultCode
.INCORRECT_ADDRESS
;
110 } else if (e
instanceof UnknownHostException
) {
111 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
113 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
114 CertificateCombinedException se
= getCertificateCombinedException(e
);
117 if (se
.isRecoverable()) {
118 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
122 mCode
= ResultCode
.SSL_ERROR
;
126 mCode
= ResultCode
.UNKNOWN_ERROR
;
132 public boolean isSuccess() {
136 public int getHttpCode() {
140 public ResultCode
getCode() {
144 public Exception
getException() {
148 public boolean isSslRecoverableException() {
149 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
152 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
153 CertificateCombinedException result
= null
;
154 if (e
instanceof CertificateCombinedException
) {
155 return (CertificateCombinedException
)e
;
157 Throwable cause
= mException
.getCause();
158 Throwable previousCause
= null
;
159 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
160 previousCause
= cause
;
161 cause
= cause
.getCause();
163 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
164 result
= (CertificateCombinedException
)cause
;
170 public String
getLogMessage() {
172 if (mException
!= null
) {
173 if (mException
instanceof SocketException
) {
174 return "Socket exception";
176 } else if (mException
instanceof SocketTimeoutException
) {
177 return "Socket timeout exception";
179 } else if (mException
instanceof ConnectTimeoutException
) {
180 return "Connect timeout exception";
182 } else if (mException
instanceof MalformedURLException
) {
183 return "Malformed URL exception";
185 } else if (mException
instanceof UnknownHostException
) {
186 return "Unknown host exception";
188 } else if (mException
instanceof SSLException
) {
189 if (mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
)
190 return "SSL recoverable exception";
192 return "SSL exception";
194 } else if (mException
instanceof HttpException
) {
195 return "HTTP violation";
197 } else if (mException
instanceof IOException
) {
198 return "Unrecovered transport exception";
201 return "Unexpected exception";
205 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
206 return "The ownCloud server is not configured!";
208 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
209 return "No network connection";
211 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
212 return "No valid ownCloud version was found at the server";
215 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";