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