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