Updating translation files...
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.operations;
21
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.net.MalformedURLException;
25 import java.net.SocketException;
26 import java.net.SocketTimeoutException;
27 import java.net.UnknownHostException;
28
29 import javax.net.ssl.SSLException;
30
31 import org.apache.commons.httpclient.ConnectTimeoutException;
32 import org.apache.commons.httpclient.HttpException;
33 import org.apache.commons.httpclient.HttpStatus;
34 import org.apache.jackrabbit.webdav.DavException;
35
36 import com.owncloud.android.network.CertificateCombinedException;
37
38
39 /**
40 * The result of a remote operation required to an ownCloud server.
41 *
42 * Provides a common classification of remote operation results for all the 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
52 public enum ResultCode {
53 OK,
54 OK_SSL,
55 OK_NO_SSL,
56 UNHANDLED_HTTP_CODE,
57 UNAUTHORIZED,
58 FILE_NOT_FOUND,
59 INSTANCE_NOT_CONFIGURED,
60 UNKNOWN_ERROR,
61 WRONG_CONNECTION,
62 TIMEOUT,
63 INCORRECT_ADDRESS,
64 HOST_NOT_AVAILABLE,
65 NO_NETWORK_CONNECTION,
66 SSL_ERROR,
67 SSL_RECOVERABLE_PEER_UNVERIFIED,
68 BAD_OC_VERSION,
69 CANCELLED,
70 INVALID_LOCAL_FILE_NAME,
71 INVALID_OVERWRITE,
72 CONFLICT,
73 SYNC_CONFLICT,
74 LOCAL_STORAGE_FULL,
75 LOCAL_STORAGE_NOT_MOVED,
76 LOCAL_STORAGE_NOT_COPIED
77 }
78
79 private boolean mSuccess = false;
80 private int mHttpCode = -1;
81 private Exception mException = null;
82 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
83
84 public RemoteOperationResult(ResultCode code) {
85 mCode = code;
86 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
87 }
88
89 public RemoteOperationResult(boolean success, int httpCode) {
90 mSuccess = success;
91 mHttpCode = httpCode;
92
93 if (success) {
94 mCode = ResultCode.OK;
95
96 } else if (httpCode > 0) {
97 switch (httpCode) {
98 case HttpStatus.SC_UNAUTHORIZED:
99 mCode = ResultCode.UNAUTHORIZED;
100 break;
101 case HttpStatus.SC_NOT_FOUND:
102 mCode = ResultCode.FILE_NOT_FOUND;
103 break;
104 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
105 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
106 break;
107 case HttpStatus.SC_CONFLICT:
108 mCode = ResultCode.CONFLICT;
109 break;
110 default:
111 mCode = ResultCode.UNHANDLED_HTTP_CODE;
112 }
113 }
114 }
115
116 public RemoteOperationResult(Exception e) {
117 mException = e;
118
119 if (e instanceof OperationCancelledException) {
120 mCode = ResultCode.CANCELLED;
121
122 } else if (e instanceof SocketException) {
123 mCode = ResultCode.WRONG_CONNECTION;
124
125 } else if (e instanceof SocketTimeoutException) {
126 mCode = ResultCode.TIMEOUT;
127
128 } else if (e instanceof ConnectTimeoutException) {
129 mCode = ResultCode.TIMEOUT;
130
131 } else if (e instanceof MalformedURLException) {
132 mCode = ResultCode.INCORRECT_ADDRESS;
133
134 } else if (e instanceof UnknownHostException) {
135 mCode = ResultCode.HOST_NOT_AVAILABLE;
136
137 } else if (e instanceof SSLException || e instanceof RuntimeException) {
138 CertificateCombinedException se = getCertificateCombinedException(e);
139 if (se != null) {
140 mException = se;
141 if (se.isRecoverable()) {
142 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
143 }
144
145 } else {
146 mCode = ResultCode.SSL_ERROR;
147 }
148
149 } else {
150 mCode = ResultCode.UNKNOWN_ERROR;
151 }
152
153 }
154
155
156 public boolean isSuccess() {
157 return mSuccess;
158 }
159
160 public boolean isCancelled() {
161 return mCode == ResultCode.CANCELLED;
162 }
163
164 public int getHttpCode() {
165 return mHttpCode;
166 }
167
168 public ResultCode getCode() {
169 return mCode;
170 }
171
172 public Exception getException() {
173 return mException;
174 }
175
176 public boolean isSslRecoverableException() {
177 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
178 }
179
180 private CertificateCombinedException getCertificateCombinedException(Exception e) {
181 CertificateCombinedException result = null;
182 if (e instanceof CertificateCombinedException) {
183 return (CertificateCombinedException)e;
184 }
185 Throwable cause = mException.getCause();
186 Throwable previousCause = null;
187 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
188 previousCause = cause;
189 cause = cause.getCause();
190 }
191 if (cause != null && cause instanceof CertificateCombinedException) {
192 result = (CertificateCombinedException)cause;
193 }
194 return result;
195 }
196
197
198 public String getLogMessage() {
199
200 if (mException != null) {
201 if (mException instanceof OperationCancelledException) {
202 return "Operation cancelled by the caller";
203
204 } else if (mException instanceof SocketException) {
205 return "Socket exception";
206
207 } else if (mException instanceof SocketTimeoutException) {
208 return "Socket timeout exception";
209
210 } else if (mException instanceof ConnectTimeoutException) {
211 return "Connect timeout exception";
212
213 } else if (mException instanceof MalformedURLException) {
214 return "Malformed URL exception";
215
216 } else if (mException instanceof UnknownHostException) {
217 return "Unknown host exception";
218
219 } else if (mException instanceof CertificateCombinedException) {
220 if (((CertificateCombinedException) mException).isRecoverable())
221 return "SSL recoverable exception";
222 else
223 return "SSL exception";
224
225 } else if (mException instanceof SSLException) {
226 return "SSL exception";
227
228 } else if (mException instanceof DavException) {
229 return "Unexpected WebDAV exception";
230
231 } else if (mException instanceof HttpException) {
232 return "HTTP violation";
233
234 } else if (mException instanceof IOException) {
235 return "Unrecovered transport exception";
236
237 } else {
238 return "Unexpected exception";
239 }
240 }
241
242 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
243 return "The ownCloud server is not configured!";
244
245 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
246 return "No network connection";
247
248 } else if (mCode == ResultCode.BAD_OC_VERSION) {
249 return "No valid ownCloud version was found at the server";
250
251 } else if (mCode == ResultCode.LOCAL_STORAGE_FULL) {
252 return "Local storage full";
253
254 } else if (mCode == ResultCode.LOCAL_STORAGE_NOT_MOVED) {
255 return "Error while moving file to final directory";
256 }
257
258 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")";
259
260 }
261
262 }