Fixed crash when the device is turned while the warning dialog about server certifica...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoteOperationResult.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.operations;
20
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;
26
27 import javax.net.ssl.SSLException;
28 import javax.net.ssl.SSLPeerUnverifiedException;
29
30 import org.apache.commons.httpclient.ConnectTimeoutException;
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.commons.httpclient.HttpStatus;
33
34 import com.owncloud.android.network.CertificateCombinedException;
35
36
37 /**
38 * The result of a remote operation required to an ownCloud server.
39 *
40 * Provides a common classification of remote operation results for all the application.
41 *
42 * @author David A. Velasco
43 */
44 public class RemoteOperationResult {
45
46 public enum ResultCode {
47 OK,
48 OK_SSL,
49 OK_NO_SSL,
50 UNHANDLED_HTTP_CODE,
51 FILE_NOT_FOUND,
52 INSTANCE_NOT_CONFIGURED,
53 UNKNOWN_ERROR,
54 WRONG_CONNECTION,
55 TIMEOUT,
56 INCORRECT_ADDRESS,
57 HOST_NOT_AVAILABLE,
58 NO_NETWORK_CONNECTION,
59 SSL_ERROR,
60 SSL_RECOVERABLE_PEER_UNVERIFIED,
61 BAD_OC_VERSION
62 }
63
64 private boolean mSuccess = false;
65 private int mHttpCode = -1;
66 private Exception mException = null;
67 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
68
69 public RemoteOperationResult(ResultCode code) {
70 mCode = code;
71 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
72 }
73
74 public RemoteOperationResult(boolean success, int httpCode) {
75 mSuccess = success;
76 mHttpCode = httpCode;
77
78 if (success) {
79 mCode = ResultCode.OK;
80
81 } else if (httpCode > 0) {
82 switch (httpCode) {
83 case HttpStatus.SC_NOT_FOUND:
84 mCode = ResultCode.FILE_NOT_FOUND;
85 break;
86 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
87 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
88 break;
89 default:
90 mCode = ResultCode.UNHANDLED_HTTP_CODE;
91 }
92 }
93 }
94
95 public RemoteOperationResult(Exception e) {
96 mException = e;
97
98 if (e instanceof SocketException) {
99 mCode = ResultCode.WRONG_CONNECTION;
100
101 } else if (e instanceof SocketTimeoutException) {
102 mCode = ResultCode.TIMEOUT;
103
104 } else if (e instanceof ConnectTimeoutException) {
105 mCode = ResultCode.TIMEOUT;
106
107 } else if (e instanceof MalformedURLException) {
108 mCode = ResultCode.INCORRECT_ADDRESS;
109
110 } else if (e instanceof UnknownHostException) {
111 mCode = ResultCode.HOST_NOT_AVAILABLE;
112
113 } else if (e instanceof SSLException || e instanceof RuntimeException) {
114 CertificateCombinedException se = getCertificateCombinedException(e);
115 if (se != null) {
116 mException = se;
117 if (se.isRecoverable()) {
118 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
119 }
120
121 } else {
122 mCode = ResultCode.SSL_ERROR;
123 }
124
125 } else {
126 mCode = ResultCode.UNKNOWN_ERROR;
127 }
128
129 }
130
131
132 public boolean isSuccess() {
133 return mSuccess;
134 }
135
136 public int getHttpCode() {
137 return mHttpCode;
138 }
139
140 public ResultCode getCode() {
141 return mCode;
142 }
143
144 public Exception getException() {
145 return mException;
146 }
147
148 public boolean isSslRecoverableException() {
149 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
150 }
151
152 private CertificateCombinedException getCertificateCombinedException(Exception e) {
153 CertificateCombinedException result = null;
154 if (e instanceof CertificateCombinedException) {
155 return (CertificateCombinedException)e;
156 }
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();
162 }
163 if (cause != null && cause instanceof CertificateCombinedException) {
164 result = (CertificateCombinedException)cause;
165 }
166 return result;
167 }
168
169
170 public String getLogMessage() {
171
172 if (mException != null) {
173 if (mException instanceof SocketException) {
174 return "Socket exception";
175
176 } else if (mException instanceof SocketTimeoutException) {
177 return "Socket timeout exception";
178
179 } else if (mException instanceof ConnectTimeoutException) {
180 return "Connect timeout exception";
181
182 } else if (mException instanceof MalformedURLException) {
183 return "Malformed URL exception";
184
185 } else if (mException instanceof UnknownHostException) {
186 return "Unknown host exception";
187
188 } else if (mException instanceof SSLException) {
189 if (mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)
190 return "SSL recoverable exception";
191 else
192 return "SSL exception";
193
194 } else if (mException instanceof HttpException) {
195 return "HTTP violation";
196
197 } else if (mException instanceof IOException) {
198 return "Unrecovered transport exception";
199
200 } else {
201 return "Unexpected exception";
202 }
203 }
204
205 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
206 return "The ownCloud server is not configured!";
207
208 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
209 return "No network connection";
210
211 } else if (mCode == ResultCode.BAD_OC_VERSION) {
212 return "No valid ownCloud version was found at the server";
213 }
214
215 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")";
216
217 }
218
219 }