Merge two-way synch changes with synch-service refactoring for SSL warning
[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.io.Serializable;
23 import java.net.MalformedURLException;
24 import java.net.SocketException;
25 import java.net.SocketTimeoutException;
26 import java.net.UnknownHostException;
27
28 import javax.net.ssl.SSLException;
29
30 import org.apache.commons.httpclient.ConnectTimeoutException;
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.commons.httpclient.HttpStatus;
33 import org.apache.jackrabbit.webdav.DavException;
34
35 import com.owncloud.android.network.CertificateCombinedException;
36
37
38 /**
39 * The result of a remote operation required to an ownCloud server.
40 *
41 * Provides a common classification of remote operation results for all the application.
42 *
43 * @author David A. Velasco
44 */
45 public class RemoteOperationResult implements Serializable {
46
47 /** Generated - to refresh every time the class changes */
48 private static final long serialVersionUID = -7805531062432602444L;
49
50
51 public enum ResultCode {
52 OK,
53 OK_SSL,
54 OK_NO_SSL,
55 UNHANDLED_HTTP_CODE,
56 UNAUTHORIZED,
57 FILE_NOT_FOUND,
58 INSTANCE_NOT_CONFIGURED,
59 UNKNOWN_ERROR,
60 WRONG_CONNECTION,
61 TIMEOUT,
62 INCORRECT_ADDRESS,
63 HOST_NOT_AVAILABLE,
64 NO_NETWORK_CONNECTION,
65 SSL_ERROR,
66 SSL_RECOVERABLE_PEER_UNVERIFIED,
67 BAD_OC_VERSION,
68 STORAGE_ERROR_MOVING_FROM_TMP,
69 CANCELLED
70 }
71
72 private boolean mSuccess = false;
73 private int mHttpCode = -1;
74 private Exception mException = null;
75 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
76
77 public RemoteOperationResult(ResultCode code) {
78 mCode = code;
79 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
80 }
81
82 public RemoteOperationResult(boolean success, int httpCode) {
83 mSuccess = success;
84 mHttpCode = httpCode;
85
86 if (success) {
87 mCode = ResultCode.OK;
88
89 } else if (httpCode > 0) {
90 switch (httpCode) {
91 case HttpStatus.SC_UNAUTHORIZED:
92 mCode = ResultCode.UNAUTHORIZED;
93 break;
94 case HttpStatus.SC_NOT_FOUND:
95 mCode = ResultCode.FILE_NOT_FOUND;
96 break;
97 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
98 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
99 break;
100 default:
101 mCode = ResultCode.UNHANDLED_HTTP_CODE;
102 }
103 }
104 }
105
106 public RemoteOperationResult(Exception e) {
107 mException = e;
108
109 if (e instanceof OperationCancelledException) {
110 mCode = ResultCode.CANCELLED;
111
112 } else if (e instanceof SocketException) {
113 mCode = ResultCode.WRONG_CONNECTION;
114
115 } else if (e instanceof SocketTimeoutException) {
116 mCode = ResultCode.TIMEOUT;
117
118 } else if (e instanceof ConnectTimeoutException) {
119 mCode = ResultCode.TIMEOUT;
120
121 } else if (e instanceof MalformedURLException) {
122 mCode = ResultCode.INCORRECT_ADDRESS;
123
124 } else if (e instanceof UnknownHostException) {
125 mCode = ResultCode.HOST_NOT_AVAILABLE;
126
127 } else if (e instanceof SSLException || e instanceof RuntimeException) {
128 CertificateCombinedException se = getCertificateCombinedException(e);
129 if (se != null) {
130 mException = se;
131 if (se.isRecoverable()) {
132 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
133 }
134
135 } else {
136 mCode = ResultCode.SSL_ERROR;
137 }
138
139 } else {
140 mCode = ResultCode.UNKNOWN_ERROR;
141 }
142
143 }
144
145
146 public boolean isSuccess() {
147 return mSuccess;
148 }
149
150 public boolean isCancelled() {
151 return mCode == ResultCode.CANCELLED;
152 }
153
154 public int getHttpCode() {
155 return mHttpCode;
156 }
157
158 public ResultCode getCode() {
159 return mCode;
160 }
161
162 public Exception getException() {
163 return mException;
164 }
165
166 public boolean isSslRecoverableException() {
167 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
168 }
169
170 private CertificateCombinedException getCertificateCombinedException(Exception e) {
171 CertificateCombinedException result = null;
172 if (e instanceof CertificateCombinedException) {
173 return (CertificateCombinedException)e;
174 }
175 Throwable cause = mException.getCause();
176 Throwable previousCause = null;
177 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
178 previousCause = cause;
179 cause = cause.getCause();
180 }
181 if (cause != null && cause instanceof CertificateCombinedException) {
182 result = (CertificateCombinedException)cause;
183 }
184 return result;
185 }
186
187
188 public String getLogMessage() {
189
190 if (mException != null) {
191 if (mException instanceof OperationCancelledException) {
192 return "Operation cancelled by the caller";
193
194 } else if (mException instanceof SocketException) {
195 return "Socket exception";
196
197 } else if (mException instanceof SocketTimeoutException) {
198 return "Socket timeout exception";
199
200 } else if (mException instanceof ConnectTimeoutException) {
201 return "Connect timeout exception";
202
203 } else if (mException instanceof MalformedURLException) {
204 return "Malformed URL exception";
205
206 } else if (mException instanceof UnknownHostException) {
207 return "Unknown host exception";
208
209 } else if (mException instanceof CertificateCombinedException) {
210 if (((CertificateCombinedException) mException).isRecoverable())
211 return "SSL recoverable exception";
212 else
213 return "SSL exception";
214
215 } else if (mException instanceof SSLException) {
216 return "SSL exception";
217
218 } else if (mException instanceof DavException) {
219 return "Unexpected WebDAV exception";
220
221 } else if (mException instanceof HttpException) {
222 return "HTTP violation";
223
224 } else if (mException instanceof IOException) {
225 return "Unrecovered transport exception";
226
227 } else {
228 return "Unexpected exception";
229 }
230 }
231
232 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
233 return "The ownCloud server is not configured!";
234
235 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
236 return "No network connection";
237
238 } else if (mCode == ResultCode.BAD_OC_VERSION) {
239 return "No valid ownCloud version was found at the server";
240
241 } else if (mCode == ResultCode.STORAGE_ERROR_MOVING_FROM_TMP) {
242 return "Error while moving file from temporal to final directory";
243 }
244
245 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")";
246
247 }
248
249 }