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