080e0b6454279f798f963c94f5364d2956600593
[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 android.util.Log;
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 resulst for all the application.
42 *
43 * @author David A. Velasco
44 */
45 public class RemoteOperationResult {
46
47 public enum ResultCode {
48 OK,
49 OK_SSL,
50 OK_NO_SSL,
51 UNHANDLED_HTTP_CODE,
52 FILE_NOT_FOUND,
53 INSTANCE_NOT_CONFIGURED,
54 UNKNOWN_ERROR,
55 WRONG_CONNECTION,
56 TIMEOUT,
57 INCORRECT_ADDRESS,
58 HOST_NOT_AVAILABLE,
59 NO_NETWORK_CONNECTION,
60 SSL_ERROR,
61 BAD_OC_VERSION,
62 }
63
64 private static final String TAG = null;
65
66 private boolean mSuccess = false;
67 private int mHttpCode = -1;
68 private Exception mException = null;
69 private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
70
71 public RemoteOperationResult(ResultCode code) {
72 mCode = code;
73 mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL);
74 }
75
76 public RemoteOperationResult(boolean success, int httpCode) {
77 mSuccess = success;
78 mHttpCode = httpCode;
79
80 if (success) {
81 mCode = ResultCode.OK;
82
83 } else if (httpCode > 0) {
84 switch (httpCode) {
85 case HttpStatus.SC_NOT_FOUND:
86 mCode = ResultCode.FILE_NOT_FOUND;
87 break;
88 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
89 mCode = ResultCode.INSTANCE_NOT_CONFIGURED;
90 break;
91 default:
92 mCode = ResultCode.UNHANDLED_HTTP_CODE;
93 }
94 }
95 }
96
97 public RemoteOperationResult(Exception e) {
98 mException = e;
99
100 if (e instanceof SocketException) {
101 mCode = ResultCode.WRONG_CONNECTION;
102
103 } else if (e instanceof SocketTimeoutException) {
104 mCode = ResultCode.TIMEOUT;
105
106 } else if (e instanceof ConnectTimeoutException) {
107 mCode = ResultCode.TIMEOUT;
108
109 } else if (e instanceof MalformedURLException) {
110 mCode = ResultCode.INCORRECT_ADDRESS;
111
112 } else if (e instanceof UnknownHostException) {
113 mCode = ResultCode.HOST_NOT_AVAILABLE;
114
115 } else if (e instanceof SSLException) {
116 mCode = ResultCode.SSL_ERROR;
117
118 } else {
119 mCode = ResultCode.UNKNOWN_ERROR;
120 }
121
122 }
123
124
125 public boolean isSuccess() {
126 return mSuccess;
127 }
128
129 public int getHttpCode() {
130 return mHttpCode;
131 }
132
133 public ResultCode getCode() {
134 return mCode;
135 }
136
137 public Exception getException() {
138 return mException;
139 }
140
141 public boolean isSslRecoverableException() {
142 return (getSslRecoverableException() != null);
143 }
144
145 public CertificateCombinedException getSslRecoverableException() {
146 CertificateCombinedException result = null;
147 if (mCode == ResultCode.SSL_ERROR) {
148 if (mException instanceof CertificateCombinedException)
149 result = (CertificateCombinedException)mException;
150 Throwable cause = mException.getCause();
151 Throwable previousCause = null;
152 while (cause != null && cause != previousCause && !(cause instanceof CertificateCombinedException)) {
153 previousCause = cause;
154 cause = cause.getCause();
155 }
156 if (cause != null && cause instanceof CertificateCombinedException)
157 result = (CertificateCombinedException)cause;
158 }
159 if (result != null && result.isRecoverable())
160 return result;
161 else
162 return null;
163 }
164
165
166 public String getLogMessage() {
167
168 if (mException != null) {
169 if (mException instanceof SocketException) {
170 return "Socket exception";
171
172 } else if (mException instanceof SocketTimeoutException) {
173 return "Socket timeout exception";
174
175 } else if (mException instanceof ConnectTimeoutException) {
176 return "Connect timeout exception";
177
178 } else if (mException instanceof MalformedURLException) {
179 return "Malformed URL exception";
180
181 } else if (mException instanceof UnknownHostException) {
182 return "Unknown host exception";
183
184 } else if (mException instanceof SSLException) {
185 return "SSL exception";
186
187 } else if (mException instanceof HttpException) {
188 return "HTTP violation";
189
190 } else if (mException instanceof IOException) {
191 return "Unrecovered transport exception";
192
193 } else {
194 return "Unexpected exception";
195 }
196 }
197
198 return "Operation finished with HTTP status code " + mHttpCode + " (" + (isSuccess()?"success":"fail") + ")";
199
200 }
201
202 }