Merge pull request #186 from owncloud/fixed_contradicted_messages_in_login_view
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RemoteOperationResult.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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.Log_OC;
36 import com.owncloud.android.network.CertificateCombinedException;
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
42 * application.
43 *
44 * @author David A. Velasco
45 */
46 public class RemoteOperationResult implements Serializable {
47
48 /** Generated - should be refreshed every time the class changes!! */
49 private static final long serialVersionUID = -7805531062432602444L;
50
51 private static final String TAG = "RemoteOperationResult";
52
53 public enum ResultCode {
54 OK,
55 OK_SSL,
56 OK_NO_SSL,
57 UNHANDLED_HTTP_CODE,
58 UNAUTHORIZED,
59 FILE_NOT_FOUND,
60 INSTANCE_NOT_CONFIGURED,
61 UNKNOWN_ERROR,
62 WRONG_CONNECTION,
63 TIMEOUT,
64 INCORRECT_ADDRESS,
65 HOST_NOT_AVAILABLE,
66 NO_NETWORK_CONNECTION,
67 SSL_ERROR,
68 SSL_RECOVERABLE_PEER_UNVERIFIED,
69 BAD_OC_VERSION,
70 CANCELLED,
71 INVALID_LOCAL_FILE_NAME,
72 INVALID_OVERWRITE,
73 CONFLICT,
74 OAUTH2_ERROR,
75 SYNC_CONFLICT,
76 LOCAL_STORAGE_FULL,
77 LOCAL_STORAGE_NOT_MOVED,
78 LOCAL_STORAGE_NOT_COPIED,
79 OAUTH2_ERROR_ACCESS_DENIED,
80 QUOTA_EXCEEDED
81 }
82
83 private boolean mSuccess = false;
84 private int mHttpCode = -1;
85 private Exception mException = null;
86 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
87
88 public RemoteOperationResult(ResultCode code) {
89 mCode = code;
90 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
91 }
92
93 public RemoteOperationResult(boolean success, int httpCode) {
94 mSuccess = success;
95 mHttpCode = httpCode;
96
97 if (success) {
98 mCode = ResultCode.OK;
99
100 } else if (httpCode > 0) {
101 switch (httpCode) {
102 case HttpStatus.SC_UNAUTHORIZED:
103 mCode = ResultCode.UNAUTHORIZED;
104 break;
105 case HttpStatus.SC_NOT_FOUND:
106 mCode = ResultCode.FILE_NOT_FOUND;
107 break;
108 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
109 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
110 break;
111 case HttpStatus.SC_CONFLICT:
112 mCode = ResultCode.CONFLICT;
113 break;
114 case HttpStatus.SC_INSUFFICIENT_STORAGE:
115 mCode = ResultCode.QUOTA_EXCEEDED;
116 break;
117 default:
118 mCode = ResultCode.UNHANDLED_HTTP_CODE;
119 Log_OC.d(TAG, "RemoteOperationResult has prcessed UNHANDLED_HTTP_CODE: " + httpCode);
120 }
121 }
122 }
123
124 public RemoteOperationResult(Exception e) {
125 mException = e;
126
127 if (e instanceof OperationCancelledException) {
128 mCode = ResultCode.CANCELLED;
129
130 } else if (e instanceof SocketException) {
131 mCode = ResultCode.WRONG_CONNECTION;
132
133 } else if (e instanceof SocketTimeoutException) {
134 mCode = ResultCode.TIMEOUT;
135
136 } else if (e instanceof ConnectTimeoutException) {
137 mCode = ResultCode.TIMEOUT;
138
139 } else if (e instanceof MalformedURLException) {
140 mCode = ResultCode.INCORRECT_ADDRESS;
141
142 } else if (e instanceof UnknownHostException) {
143 mCode = ResultCode.HOST_NOT_AVAILABLE;
144
145 } else if (e instanceof SSLException || e instanceof RuntimeException) {
146 CertificateCombinedException se = getCertificateCombinedException(e);
147 if (se != null) {
148 mException = se;
149 if (se.isRecoverable()) {
150 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
151 }
152 } else if (e instanceof RuntimeException) {
153 mCode = ResultCode.HOST_NOT_AVAILABLE;
154
155 } else {
156 mCode = ResultCode.SSL_ERROR;
157 }
158
159 } else {
160 mCode = ResultCode.UNKNOWN_ERROR;
161 }
162
163 }
164
165 public boolean isSuccess() {
166 return mSuccess;
167 }
168
169 public boolean isCancelled() {
170 return mCode == ResultCode.CANCELLED;
171 }
172
173 public int getHttpCode() {
174 return mHttpCode;
175 }
176
177 public ResultCode getCode() {
178 return mCode;
179 }
180
181 public Exception getException() {
182 return mException;
183 }
184
185 public boolean isSslRecoverableException() {
186 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
187 }
188
189 private CertificateCombinedException getCertificateCombinedException(Exception e) {
190 CertificateCombinedException result = null;
191 if (e instanceof CertificateCombinedException) {
192 return (CertificateCombinedException) e;
193 }
194 Throwable cause = mException.getCause();
195 Throwable previousCause = null;
196 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
197 previousCause = cause;
198 cause = cause.getCause();
199 }
200 if (cause != null && cause instanceof CertificateCombinedException) {
201 result = (CertificateCombinedException) cause;
202 }
203 return result;
204 }
205
206 public String getLogMessage() {
207
208 if (mException != null) {
209 if (mException instanceof OperationCancelledException) {
210 return "Operation cancelled by the caller";
211
212 } else if (mException instanceof SocketException) {
213 return "Socket exception";
214
215 } else if (mException instanceof SocketTimeoutException) {
216 return "Socket timeout exception";
217
218 } else if (mException instanceof ConnectTimeoutException) {
219 return "Connect timeout exception";
220
221 } else if (mException instanceof MalformedURLException) {
222 return "Malformed URL exception";
223
224 } else if (mException instanceof UnknownHostException) {
225 return "Unknown host exception";
226
227 } else if (mException instanceof CertificateCombinedException) {
228 if (((CertificateCombinedException) mException).isRecoverable())
229 return "SSL recoverable exception";
230 else
231 return "SSL exception";
232
233 } else if (mException instanceof SSLException) {
234 return "SSL exception";
235
236 } else if (mException instanceof DavException) {
237 return "Unexpected WebDAV exception";
238
239 } else if (mException instanceof HttpException) {
240 return "HTTP violation";
241
242 } else if (mException instanceof IOException) {
243 return "Unrecovered transport exception";
244
245 } else {
246 return "Unexpected exception";
247 }
248 }
249
250 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
251 return "The ownCloud server is not configured!";
252
253 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
254 return "No network connection";
255
256 } else if (mCode == ResultCode.BAD_OC_VERSION) {
257 return "No valid ownCloud version was found at the server";
258
259 } else if (mCode == ResultCode.LOCAL_STORAGE_FULL) {
260 return "Local storage full";
261
262 } else if (mCode == ResultCode.LOCAL_STORAGE_NOT_MOVED) {
263 return "Error while moving file to final directory";
264 }
265
266 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess() ? "success" : "fail") + ")";
267
268 }
269
270 public boolean isServerFail() {
271 return (mHttpCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR);
272 }
273
274 public boolean isException() {
275 return (mException != null);
276 }
277
278 }