SSL connections update: notice about untrusted certificates and allow the user save...
[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
14 public class RemoteOperationResult {
15
16 public enum ResultCode { // TODO leave alone our own errors
17 OK,
18 OK_SSL,
19 OK_NO_SSL,
20 UNHANDLED_HTTP_CODE,
21 FILE_NOT_FOUND,
22 INSTANCE_NOT_CONFIGURED,
23 UNKNOWN_ERROR,
24 WRONG_CONNECTION,
25 TIMEOUT,
26 INCORRECT_ADDRESS,
27 HOST_NOT_AVAILABLE,
28 NO_NETWORK_CONNECTION,
29 SSL_ERROR,
30 BAD_OC_VERSION,
31 }
32
33 private boolean mSuccess = false;
34 private int mHttpCode = -1;
35 private Exception mException = null;
36 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
37
38 public RemoteOperationResult(ResultCode code) {
39 mCode = code;
40 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
41 }
42
43 public RemoteOperationResult(boolean success, int httpCode) {
44 mSuccess = success;
45 mHttpCode = httpCode;
46
47 if (success) {
48 mCode = ResultCode.OK;
49
50 } else if (httpCode > 0) {
51 switch (httpCode) {
52 case HttpStatus.SC_NOT_FOUND:
53 mCode = ResultCode.FILE_NOT_FOUND;
54 break;
55 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
56 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
57 break;
58 default:
59 mCode = ResultCode.UNHANDLED_HTTP_CODE;
60 }
61 }
62 }
63
64 public RemoteOperationResult(Exception e) {
65 mException = e;
66
67 if (e instanceof SocketException) {
68 mCode = ResultCode.WRONG_CONNECTION;
69 //Log.e(TAG, "Socket exception while trying connection", e);
70
71 } else if (e instanceof SocketTimeoutException) {
72 mCode = ResultCode.TIMEOUT;
73 //Log.e(TAG, "Socket timeout exception while trying connection", e);
74
75 } else if (e instanceof ConnectTimeoutException) {
76 mCode = ResultCode.TIMEOUT;
77 //Log.e(TAG, "Socket timeout exception while trying connection", e);
78
79 } else if (e instanceof MalformedURLException) {
80 mCode = ResultCode.INCORRECT_ADDRESS;
81 //Log.e(TAG, "Connect exception while trying connection", e);
82
83 } else if (e instanceof UnknownHostException) {
84 mCode = ResultCode.HOST_NOT_AVAILABLE;
85 //Log.e(TAG, "Unknown host exception while trying connection", e);
86
87 } else if (e instanceof SSLException) {
88 mCode = ResultCode.SSL_ERROR;
89 //Log.e(TAG, "SSL exception while trying connection", e);
90
91 } else {
92 mCode = ResultCode.UNKNOWN_ERROR;
93 }
94
95 /* } catch (HttpException e) { // other specific exceptions from org.apache.commons.httpclient
96 Log.e(TAG, "HTTP exception while trying connection", e);
97 } catch (IOException e) { // UnkownsServiceException, and any other transport exceptions that could occur
98 Log.e(TAG, "I/O exception while trying connection", e);
99 } catch (Exception e) {
100 Log.e(TAG, "Unexpected exception while trying connection", e);
101 */
102 }
103
104
105 public boolean isSuccess() {
106 return mSuccess;
107 }
108
109 public int getHttpCode() {
110 return mHttpCode;
111 }
112
113 public ResultCode getCode() {
114 return mCode;
115 }
116
117 public Exception getException() {
118 return mException;
119 }
120
121 }