1 package com
.owncloud
.android
.operations
;
3 import java
.net
.MalformedURLException
;
4 import java
.net
.SocketException
;
5 import java
.net
.SocketTimeoutException
;
6 import java
.net
.UnknownHostException
;
8 import javax
.net
.ssl
.SSLException
;
10 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
11 import org
.apache
.commons
.httpclient
.HttpStatus
;
13 import android
.util
.Log
;
15 import com
.owncloud
.android
.network
.CertificateCombinedException
;
18 public class RemoteOperationResult
{
20 public enum ResultCode
{ // TODO leave alone our own errors
26 INSTANCE_NOT_CONFIGURED
,
32 NO_NETWORK_CONNECTION
,
37 private static final String TAG
= null
;
39 private boolean mSuccess
= false
;
40 private int mHttpCode
= -1;
41 private Exception mException
= null
;
42 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
44 public RemoteOperationResult(ResultCode code
) {
46 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
49 public RemoteOperationResult(boolean success
, int httpCode
) {
54 mCode
= ResultCode
.OK
;
56 } else if (httpCode
> 0) {
58 case HttpStatus
.SC_NOT_FOUND
:
59 mCode
= ResultCode
.FILE_NOT_FOUND
;
61 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
62 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
65 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
70 public RemoteOperationResult(Exception e
) {
73 if (e
instanceof SocketException
) {
74 mCode
= ResultCode
.WRONG_CONNECTION
;
75 Log
.e(TAG
, "Socket exception", e
);
77 } else if (e
instanceof SocketTimeoutException
) {
78 mCode
= ResultCode
.TIMEOUT
;
79 Log
.e(TAG
, "Socket timeout exception", e
);
81 } else if (e
instanceof ConnectTimeoutException
) {
82 mCode
= ResultCode
.TIMEOUT
;
83 Log
.e(TAG
, "Connect timeout exception", e
);
85 } else if (e
instanceof MalformedURLException
) {
86 mCode
= ResultCode
.INCORRECT_ADDRESS
;
87 Log
.e(TAG
, "Malformed URL exception", e
);
89 } else if (e
instanceof UnknownHostException
) {
90 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
91 Log
.e(TAG
, "Unknown host exception", e
);
93 } else if (e
instanceof SSLException
) {
94 mCode
= ResultCode
.SSL_ERROR
;
95 Log
.e(TAG
, "SSL exception", e
);
98 mCode
= ResultCode
.UNKNOWN_ERROR
;
99 Log
.e(TAG
, "Unknown exception", e
);
102 /* } catch (HttpException e) { // other specific exceptions from org.apache.commons.httpclient
103 Log.e(TAG, "HTTP exception while trying connection", e);
104 } catch (IOException e) { // UnkownsServiceException, and any other transport exceptions that could occur
105 Log.e(TAG, "I/O exception while trying connection", e);
106 } catch (Exception e) {
107 Log.e(TAG, "Unexpected exception while trying connection", e);
112 public boolean isSuccess() {
116 public int getHttpCode() {
120 public ResultCode
getCode() {
124 public Exception
getException() {
128 public boolean isSslRecoverableException() {
129 return (getSslRecoverableException() != null
);
132 public CertificateCombinedException
getSslRecoverableException() {
133 CertificateCombinedException result
= null
;
134 if (mCode
== ResultCode
.SSL_ERROR
) {
135 if (mException
instanceof CertificateCombinedException
)
136 result
= (CertificateCombinedException
)mException
;
137 Throwable cause
= mException
.getCause();
138 Throwable previousCause
= null
;
139 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
140 previousCause
= cause
;
141 cause
= cause
.getCause();
143 if (cause
!= null
&& cause
instanceof CertificateCombinedException
)
144 result
= (CertificateCombinedException
)cause
;
146 if (result
!= null
&& result
.isRecoverable())