1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
20 package com
.owncloud
.android
.operations
;
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
;
29 import javax
.net
.ssl
.SSLException
;
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
;
36 import com
.owncloud
.android
.network
.CertificateCombinedException
;
40 * The result of a remote operation required to an ownCloud server.
42 * Provides a common classification of remote operation results for all the application.
44 * @author David A. Velasco
46 public class RemoteOperationResult
implements Serializable
{
48 /** Generated - should be refreshed every time the class changes!! */
49 private static final long serialVersionUID
= -7805531062432602444L;
52 public enum ResultCode
{
59 INSTANCE_NOT_CONFIGURED
,
65 NO_NETWORK_CONNECTION
,
67 SSL_RECOVERABLE_PEER_UNVERIFIED
,
70 INVALID_LOCAL_FILE_NAME
,
75 LOCAL_STORAGE_NOT_MOVED
,
76 LOCAL_STORAGE_NOT_COPIED
79 private boolean mSuccess
= false
;
80 private int mHttpCode
= -1;
81 private Exception mException
= null
;
82 private ResultCode mCode
= ResultCode
.UNKNOWN_ERROR
;
84 public RemoteOperationResult(ResultCode code
) {
86 mSuccess
= (code
== ResultCode
.OK
|| code
== ResultCode
.OK_SSL
|| code
== ResultCode
.OK_NO_SSL
);
89 public RemoteOperationResult(boolean success
, int httpCode
) {
94 mCode
= ResultCode
.OK
;
96 } else if (httpCode
> 0) {
98 case HttpStatus
.SC_UNAUTHORIZED
:
99 mCode
= ResultCode
.UNAUTHORIZED
;
101 case HttpStatus
.SC_NOT_FOUND
:
102 mCode
= ResultCode
.FILE_NOT_FOUND
;
104 case HttpStatus
.SC_INTERNAL_SERVER_ERROR
:
105 mCode
= ResultCode
.INSTANCE_NOT_CONFIGURED
;
107 case HttpStatus
.SC_CONFLICT
:
108 mCode
= ResultCode
.CONFLICT
;
111 mCode
= ResultCode
.UNHANDLED_HTTP_CODE
;
116 public RemoteOperationResult(Exception e
) {
119 if (e
instanceof OperationCancelledException
) {
120 mCode
= ResultCode
.CANCELLED
;
122 } else if (e
instanceof SocketException
) {
123 mCode
= ResultCode
.WRONG_CONNECTION
;
125 } else if (e
instanceof SocketTimeoutException
) {
126 mCode
= ResultCode
.TIMEOUT
;
128 } else if (e
instanceof ConnectTimeoutException
) {
129 mCode
= ResultCode
.TIMEOUT
;
131 } else if (e
instanceof MalformedURLException
) {
132 mCode
= ResultCode
.INCORRECT_ADDRESS
;
134 } else if (e
instanceof UnknownHostException
) {
135 mCode
= ResultCode
.HOST_NOT_AVAILABLE
;
137 } else if (e
instanceof SSLException
|| e
instanceof RuntimeException
) {
138 CertificateCombinedException se
= getCertificateCombinedException(e
);
141 if (se
.isRecoverable()) {
142 mCode
= ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
146 mCode
= ResultCode
.SSL_ERROR
;
150 mCode
= ResultCode
.UNKNOWN_ERROR
;
156 public boolean isSuccess() {
160 public boolean isCancelled() {
161 return mCode
== ResultCode
.CANCELLED
;
164 public int getHttpCode() {
168 public ResultCode
getCode() {
172 public Exception
getException() {
176 public boolean isSslRecoverableException() {
177 return mCode
== ResultCode
.SSL_RECOVERABLE_PEER_UNVERIFIED
;
180 private CertificateCombinedException
getCertificateCombinedException(Exception e
) {
181 CertificateCombinedException result
= null
;
182 if (e
instanceof CertificateCombinedException
) {
183 return (CertificateCombinedException
)e
;
185 Throwable cause
= mException
.getCause();
186 Throwable previousCause
= null
;
187 while (cause
!= null
&& cause
!= previousCause
&& !(cause
instanceof CertificateCombinedException
)) {
188 previousCause
= cause
;
189 cause
= cause
.getCause();
191 if (cause
!= null
&& cause
instanceof CertificateCombinedException
) {
192 result
= (CertificateCombinedException
)cause
;
198 public String
getLogMessage() {
200 if (mException
!= null
) {
201 if (mException
instanceof OperationCancelledException
) {
202 return "Operation cancelled by the caller";
204 } else if (mException
instanceof SocketException
) {
205 return "Socket exception";
207 } else if (mException
instanceof SocketTimeoutException
) {
208 return "Socket timeout exception";
210 } else if (mException
instanceof ConnectTimeoutException
) {
211 return "Connect timeout exception";
213 } else if (mException
instanceof MalformedURLException
) {
214 return "Malformed URL exception";
216 } else if (mException
instanceof UnknownHostException
) {
217 return "Unknown host exception";
219 } else if (mException
instanceof CertificateCombinedException
) {
220 if (((CertificateCombinedException
) mException
).isRecoverable())
221 return "SSL recoverable exception";
223 return "SSL exception";
225 } else if (mException
instanceof SSLException
) {
226 return "SSL exception";
228 } else if (mException
instanceof DavException
) {
229 return "Unexpected WebDAV exception";
231 } else if (mException
instanceof HttpException
) {
232 return "HTTP violation";
234 } else if (mException
instanceof IOException
) {
235 return "Unrecovered transport exception";
238 return "Unexpected exception";
242 if (mCode
== ResultCode
.INSTANCE_NOT_CONFIGURED
) {
243 return "The ownCloud server is not configured!";
245 } else if (mCode
== ResultCode
.NO_NETWORK_CONNECTION
) {
246 return "No network connection";
248 } else if (mCode
== ResultCode
.BAD_OC_VERSION
) {
249 return "No valid ownCloud version was found at the server";
251 } else if (mCode
== ResultCode
.LOCAL_STORAGE_FULL
) {
252 return "Local storage full";
254 } else if (mCode
== ResultCode
.LOCAL_STORAGE_NOT_MOVED
) {
255 return "Error while moving file to final directory";
258 return "Operation finished with HTTP status code " + mHttpCode
+ " (" + (isSuccess()?
"success":"fail") + ")";