03afda55331f40f10cc9d43fee7e13fde428eb01
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoteOperationResult.java
1 package com.owncloud.android.operations;
2
3 import java.net.MalformedURLException;
4 import java.net.SocketException;
5 import java.net.SocketTimeoutException;
6 import java.net.UnknownHostException;
7
8 import javax.net.ssl.SSLException;
9
10 import org.apache.commons.httpclient.ConnectTimeoutException;
11 import org.apache.commons.httpclient.HttpStatus;
12
13 import android.util.Log;
14
15 import com.owncloud.android.network.CertificateCombinedException;
16
17
18 public class RemoteOperationResult {
19
20 public enum ResultCode { // TODO leave alone our own errors
21 OK,
22 OK_SSL,
23 OK_NO_SSL,
24 UNHANDLED_HTTP_CODE,
25 FILE_NOT_FOUND,
26 INSTANCE_NOT_CONFIGURED,
27 UNKNOWN_ERROR,
28 WRONG_CONNECTION,
29 TIMEOUT,
30 INCORRECT_ADDRESS,
31 HOST_NOT_AVAILABLE,
32 NO_NETWORK_CONNECTION,
33 SSL_ERROR,
34 BAD_OC_VERSION,
35 }
36
37 private static final String TAG = null;
38
39 private boolean mSuccess = false;
40 private int mHttpCode = -1;
41 private Exception mException = null;
42 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
43
44 public RemoteOperationResult(ResultCode code) {
45 mCode = code;
46 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
47 }
48
49 public RemoteOperationResult(boolean success, int httpCode) {
50 mSuccess = success;
51 mHttpCode = httpCode;
52
53 if (success) {
54 mCode = ResultCode.OK;
55
56 } else if (httpCode > 0) {
57 switch (httpCode) {
58 case HttpStatus.SC_NOT_FOUND:
59 mCode = ResultCode.FILE_NOT_FOUND;
60 break;
61 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
62 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
63 break;
64 default:
65 mCode = ResultCode.UNHANDLED_HTTP_CODE;
66 }
67 }
68 }
69
70 public RemoteOperationResult(Exception e) {
71 mException = e;
72
73 if (e instanceof SocketException) {
74 mCode = ResultCode.WRONG_CONNECTION;
75 Log.e(TAG, "Socket exception", e);
76
77 } else if (e instanceof SocketTimeoutException) {
78 mCode = ResultCode.TIMEOUT;
79 Log.e(TAG, "Socket timeout exception", e);
80
81 } else if (e instanceof ConnectTimeoutException) {
82 mCode = ResultCode.TIMEOUT;
83 Log.e(TAG, "Connect timeout exception", e);
84
85 } else if (e instanceof MalformedURLException) {
86 mCode = ResultCode.INCORRECT_ADDRESS;
87 Log.e(TAG, "Malformed URL exception", e);
88
89 } else if (e instanceof UnknownHostException) {
90 mCode = ResultCode.HOST_NOT_AVAILABLE;
91 Log.e(TAG, "Unknown host exception", e);
92
93 } else if (e instanceof SSLException) {
94 mCode = ResultCode.SSL_ERROR;
95 Log.e(TAG, "SSL exception", e);
96
97 } else {
98 mCode = ResultCode.UNKNOWN_ERROR;
99 Log.e(TAG, "Unknown exception", e);
100 }
101
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);
108 */
109 }
110
111
112 public boolean isSuccess() {
113 return mSuccess;
114 }
115
116 public int getHttpCode() {
117 return mHttpCode;
118 }
119
120 public ResultCode getCode() {
121 return mCode;
122 }
123
124 public Exception getException() {
125 return mException;
126 }
127
128 public boolean isSslRecoverableException() {
129 return (getSslRecoverableException() != null);
130 }
131
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();
142 }
143 if (cause != null && cause instanceof CertificateCombinedException)
144 result = (CertificateCombinedException)cause;
145 }
146 if (result != null && result.isRecoverable())
147 return result;
148 else
149 return null;
150 }
151
152 }