Merge remote-tracking branch 'origin/master' into long_press
[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 INVALID_LOCAL_FILE_NAME,
71 INVALID_OVERWRITE,
72 CONFLICT
73 }
74
75 private boolean mSuccess = false;
76 private int mHttpCode = -1;
77 private Exception mException = null;
78 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
79 private Object mExtraData = null;
80
81 public RemoteOperationResult(ResultCode code) {
82 mCode = code;
83 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
84 }
85
86 public RemoteOperationResult(boolean success, int httpCode) {
87 mSuccess = success;
88 mHttpCode = httpCode;
89
90 if (success) {
91 mCode = ResultCode.OK;
92
93 } else if (httpCode > 0) {
94 switch (httpCode) {
95 case HttpStatus.SC_UNAUTHORIZED:
96 mCode = ResultCode.UNAUTHORIZED;
97 break;
98 case HttpStatus.SC_NOT_FOUND:
99 mCode = ResultCode.FILE_NOT_FOUND;
100 break;
101 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
102 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
103 break;
104 case HttpStatus.SC_CONFLICT:
105 mCode = ResultCode.CONFLICT;
106 break;
107 default:
108 mCode = ResultCode.UNHANDLED_HTTP_CODE;
109 }
110 }
111 }
112
113 public RemoteOperationResult(Exception e) {
114 mException = e;
115
116 if (e instanceof OperationCancelledException) {
117 mCode = ResultCode.CANCELLED;
118
119 } else if (e instanceof SocketException) {
120 mCode = ResultCode.WRONG_CONNECTION;
121
122 } else if (e instanceof SocketTimeoutException) {
123 mCode = ResultCode.TIMEOUT;
124
125 } else if (e instanceof ConnectTimeoutException) {
126 mCode = ResultCode.TIMEOUT;
127
128 } else if (e instanceof MalformedURLException) {
129 mCode = ResultCode.INCORRECT_ADDRESS;
130
131 } else if (e instanceof UnknownHostException) {
132 mCode = ResultCode.HOST_NOT_AVAILABLE;
133
134 } else if (e instanceof SSLException || e instanceof RuntimeException) {
135 CertificateCombinedException se = getCertificateCombinedException(e);
136 if (se != null) {
137 mException = se;
138 if (se.isRecoverable()) {
139 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
140 }
141
142 } else {
143 mCode = ResultCode.SSL_ERROR;
144 }
145
146 } else {
147 mCode = ResultCode.UNKNOWN_ERROR;
148 }
149
150 }
151
152
153 public boolean isSuccess() {
154 return mSuccess;
155 }
156
157 public boolean isCancelled() {
158 return mCode == ResultCode.CANCELLED;
159 }
160
161 public int getHttpCode() {
162 return mHttpCode;
163 }
164
165 public ResultCode getCode() {
166 return mCode;
167 }
168
169 public Exception getException() {
170 return mException;
171 }
172
173 public boolean isSslRecoverableException() {
174 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
175 }
176
177 public void setExtraData(Object data) {
178 mExtraData = data;
179 }
180
181 public Object getExtraData() {
182 return mExtraData;
183 }
184
185 private CertificateCombinedException getCertificateCombinedException(Exception e) {
186 CertificateCombinedException result = null;
187 if (e instanceof CertificateCombinedException) {
188 return (CertificateCombinedException)e;
189 }
190 Throwable cause = mException.getCause();
191 Throwable previousCause = null;
192 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
193 previousCause = cause;
194 cause = cause.getCause();
195 }
196 if (cause != null && cause instanceof CertificateCombinedException) {
197 result = (CertificateCombinedException)cause;
198 }
199 return result;
200 }
201
202
203 public String getLogMessage() {
204
205 if (mException != null) {
206 if (mException instanceof OperationCancelledException) {
207 return "Operation cancelled by the caller";
208
209 } else if (mException instanceof SocketException) {
210 return "Socket exception";
211
212 } else if (mException instanceof SocketTimeoutException) {
213 return "Socket timeout exception";
214
215 } else if (mException instanceof ConnectTimeoutException) {
216 return "Connect timeout exception";
217
218 } else if (mException instanceof MalformedURLException) {
219 return "Malformed URL exception";
220
221 } else if (mException instanceof UnknownHostException) {
222 return "Unknown host exception";
223
224 } else if (mException instanceof CertificateCombinedException) {
225 if (((CertificateCombinedException) mException).isRecoverable())
226 return "SSL recoverable exception";
227 else
228 return "SSL exception";
229
230 } else if (mException instanceof SSLException) {
231 return "SSL exception";
232
233 } else if (mException instanceof DavException) {
234 return "Unexpected WebDAV exception";
235
236 } else if (mException instanceof HttpException) {
237 return "HTTP violation";
238
239 } else if (mException instanceof IOException) {
240 return "Unrecovered transport exception";
241
242 } else {
243 return "Unexpected exception";
244 }
245 }
246
247 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
248 return "The ownCloud server is not configured!";
249
250 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
251 return "No network connection";
252
253 } else if (mCode == ResultCode.BAD_OC_VERSION) {
254 return "No valid ownCloud version was found at the server";
255
256 } else if (mCode == ResultCode.STORAGE_ERROR_MOVING_FROM_TMP) {
257 return "Error while moving file from temporal to final directory";
258 }
259
260 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")";
261
262 }
263
264 }