0618b86d5c0fe1219b4f67689f319798fd196968
[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.net.MalformedURLException;
23 import java.net.SocketException;
24 import java.net.SocketTimeoutException;
25 import java.net.UnknownHostException;
26
27 import javax.net.ssl.SSLException;
28
29 import org.apache.commons.httpclient.ConnectTimeoutException;
30 import org.apache.commons.httpclient.HttpException;
31 import org.apache.commons.httpclient.HttpStatus;
32
33 import com.owncloud.android.network.CertificateCombinedException;
34
35
36 /**
37 * The result of a remote operation required to an ownCloud server.
38 *
39 * Provides a common classification of remote operation results for all the application.
40 *
41 * @author David A. Velasco
42 */
43 public class RemoteOperationResult {
44
45 public enum ResultCode {
46 OK,
47 OK_SSL,
48 OK_NO_SSL,
49 UNHANDLED_HTTP_CODE,
50 FILE_NOT_FOUND,
51 INSTANCE_NOT_CONFIGURED,
52 UNKNOWN_ERROR,
53 WRONG_CONNECTION,
54 TIMEOUT,
55 INCORRECT_ADDRESS,
56 HOST_NOT_AVAILABLE,
57 NO_NETWORK_CONNECTION,
58 SSL_ERROR,
59 SSL_RECOVERABLE_PEER_UNVERIFIED,
60 BAD_OC_VERSION,
61 STORAGE_ERROR_MOVING_FROM_TMP,
62 CANCELLED
63 }
64
65 private boolean mSuccess = false;
66 private int mHttpCode = -1;
67 private Exception mException = null;
68 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
69
70 public RemoteOperationResult(ResultCode code) {
71 mCode = code;
72 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
73 }
74
75 public RemoteOperationResult(boolean success, int httpCode) {
76 mSuccess = success;
77 mHttpCode = httpCode;
78
79 if (success) {
80 mCode = ResultCode.OK;
81
82 } else if (httpCode > 0) {
83 switch (httpCode) {
84 case HttpStatus.SC_NOT_FOUND:
85 mCode = ResultCode.FILE_NOT_FOUND;
86 break;
87 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
88 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
89 break;
90 default:
91 mCode = ResultCode.UNHANDLED_HTTP_CODE;
92 }
93 }
94 }
95
96 public RemoteOperationResult(Exception e) {
97 mException = e;
98
99 if (e instanceof OperationCancelledException) {
100 mCode = ResultCode.CANCELLED;
101
102 } else if (e instanceof SocketException) {
103 mCode = ResultCode.WRONG_CONNECTION;
104
105 } else if (e instanceof SocketTimeoutException) {
106 mCode = ResultCode.TIMEOUT;
107
108 } else if (e instanceof ConnectTimeoutException) {
109 mCode = ResultCode.TIMEOUT;
110
111 } else if (e instanceof MalformedURLException) {
112 mCode = ResultCode.INCORRECT_ADDRESS;
113
114 } else if (e instanceof UnknownHostException) {
115 mCode = ResultCode.HOST_NOT_AVAILABLE;
116
117 } else if (e instanceof SSLException || e instanceof RuntimeException) {
118 CertificateCombinedException se = getCertificateCombinedException(e);
119 if (se != null) {
120 mException = se;
121 if (se.isRecoverable()) {
122 mCode = ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
123 }
124
125 } else {
126 mCode = ResultCode.SSL_ERROR;
127 }
128
129 } else {
130 mCode = ResultCode.UNKNOWN_ERROR;
131 }
132
133 }
134
135
136 public boolean isSuccess() {
137 return mSuccess;
138 }
139
140 public boolean isCancelled() {
141 return mCode == ResultCode.CANCELLED;
142 }
143
144 public int getHttpCode() {
145 return mHttpCode;
146 }
147
148 public ResultCode getCode() {
149 return mCode;
150 }
151
152 public Exception getException() {
153 return mException;
154 }
155
156 public boolean isSslRecoverableException() {
157 return mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED;
158 }
159
160 private CertificateCombinedException getCertificateCombinedException(Exception e) {
161 CertificateCombinedException result = null;
162 if (e instanceof CertificateCombinedException) {
163 return (CertificateCombinedException)e;
164 }
165 Throwable cause = mException.getCause();
166 Throwable previousCause = null;
167 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
168 previousCause = cause;
169 cause = cause.getCause();
170 }
171 if (cause != null && cause instanceof CertificateCombinedException) {
172 result = (CertificateCombinedException)cause;
173 }
174 return result;
175 }
176
177
178 public String getLogMessage() {
179
180 if (mException != null) {
181 if (mException instanceof OperationCancelledException) {
182 return "Operation cancelled by the caller";
183
184 } else if (mException instanceof SocketException) {
185 return "Socket exception";
186
187 } else if (mException instanceof SocketTimeoutException) {
188 return "Socket timeout exception";
189
190 } else if (mException instanceof ConnectTimeoutException) {
191 return "Connect timeout exception";
192
193 } else if (mException instanceof MalformedURLException) {
194 return "Malformed URL exception";
195
196 } else if (mException instanceof UnknownHostException) {
197 return "Unknown host exception";
198
199 } else if (mException instanceof SSLException) {
200 if (mCode == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)
201 return "SSL recoverable exception";
202 else
203 return "SSL exception";
204
205 } else if (mException instanceof HttpException) {
206 return "HTTP violation";
207
208 } else if (mException instanceof IOException) {
209 return "Unrecovered transport exception";
210
211 } else {
212 return "Unexpected exception";
213 }
214 }
215
216 if (mCode == ResultCode.INSTANCE_NOT_CONFIGURED) {
217 return "The ownCloud server is not configured!";
218
219 } else if (mCode == ResultCode.NO_NETWORK_CONNECTION) {
220 return "No network connection";
221
222 } else if (mCode == ResultCode.BAD_OC_VERSION) {
223 return "No valid ownCloud version was found at the server";
224
225 } else if (mCode == ResultCode.STORAGE_ERROR_MOVING_FROM_TMP) {
226 return "Error while moving file from temporal to final directory";
227 }
228
229 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")";
230
231 }
232
233 }