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 android
.util
.Log
;
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 resulst for all the application.
43 * @author David A. Velasco
45 public class RemoteOperationResult
{
47 public enum ResultCode
{
53 INSTANCE_NOT_CONFIGURED
,
59 NO_NETWORK_CONNECTION
,
64 private static final String TAG
= null
;
66 private boolean mSuccess
= false
;
67 private int mHttpCode
= -1;
68 private Exception mException
= null
;
69 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
71 public RemoteOperationResult(ResultCode code
) {
73 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
76 public RemoteOperationResult(boolean success
, int httpCode
) {
81 mCode
= ResultCode
.OK
;
83 } else if (httpCode
> 0) {
85 case HttpStatus
.SC_NOT_FOUND
:
86 mCode
= ResultCode
.FILE_NOT_FOUND
;
88 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
89 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
92 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
97 public RemoteOperationResult(Exception e
) {
100 if (e
instanceof SocketException
) {
101 mCode
= ResultCode
.WRONG_CONNECTION
;
103 } else if (e
instanceof SocketTimeoutException
) {
104 mCode
= ResultCode
.TIMEOUT
;
106 } else if (e
instanceof ConnectTimeoutException
) {
107 mCode
= ResultCode
.TIMEOUT
;
109 } else if (e
instanceof MalformedURLException
) {
110 mCode
= ResultCode
.INCORRECT_ADDRESS
;
112 } else if (e
instanceof UnknownHostException
) {
113 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
115 } else if (e
instanceof SSLException
) {
116 mCode
= ResultCode
.SSL_ERROR
;
119 mCode
= ResultCode
.UNKNOWN_ERROR
;
125 public boolean isSuccess() {
129 public int getHttpCode() {
133 public ResultCode
getCode() {
137 public Exception
getException() {
141 public boolean isSslRecoverableException() {
142 return (getSslRecoverableException() != null
);
145 public CertificateCombinedException
getSslRecoverableException() {
146 CertificateCombinedException result
= null
;
147 if (mCode
== ResultCode
.SSL_ERROR
) {
148 if (mException
instanceof CertificateCombinedException
)
149 result
= (CertificateCombinedException
)mException
;
150 Throwable cause
= mException
.getCause();
151 Throwable previousCause
= null
;
152 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
153 previousCause
= cause
;
154 cause
= cause
.getCause();
156 if (cause
!= null
&& cause
instanceof CertificateCombinedException
)
157 result
= (CertificateCombinedException
)cause
;
159 if (result
!= null
&& result
.isRecoverable())
166 public String
getLogMessage() {
168 if (mException
!= null
) {
169 if (mException
instanceof SocketException
) {
170 return "Socket exception";
172 } else if (mException
instanceof SocketTimeoutException
) {
173 return "Socket timeout exception";
175 } else if (mException
instanceof ConnectTimeoutException
) {
176 return "Connect timeout exception";
178 } else if (mException
instanceof MalformedURLException
) {
179 return "Malformed URL exception";
181 } else if (mException
instanceof UnknownHostException
) {
182 return "Unknown host exception";
184 } else if (mException
instanceof SSLException
) {
185 return "SSL exception";
187 } else if (mException
instanceof HttpException
) {
188 return "HTTP violation";
190 } else if (mException
instanceof IOException
) {
191 return "Unrecovered transport exception";
194 return "Unexpected exception";
198 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";