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