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