15
15
import android .net .ConnectivityManager ;
16
16
import android .net .Uri ;
17
17
18
- import com .owncloud . android . lib . common .OwnCloudClient ;
19
- import com .owncloud . android . lib . common . OwnCloudClientManagerFactory ;
18
+ import com .nextcloud . common .NextcloudClient ;
19
+ import com .nextcloud . operations . GetMethod ;
20
20
import com .owncloud .android .lib .common .accounts .AccountUtils ;
21
21
import com .owncloud .android .lib .common .operations .RemoteOperation ;
22
22
import com .owncloud .android .lib .common .operations .RemoteOperationResult ;
23
23
import com .owncloud .android .lib .common .utils .Log_OC ;
24
24
25
25
import org .apache .commons .httpclient .HttpStatus ;
26
- import org .apache .commons .httpclient .methods .GetMethod ;
27
- import org .apache .commons .httpclient .params .HttpMethodParams ;
28
- import org .apache .commons .httpclient .params .HttpParams ;
29
26
import org .json .JSONException ;
30
27
import org .json .JSONObject ;
31
28
32
- import java . util . ArrayList ;
29
+ import kotlin . Pair ;
33
30
34
31
/**
35
32
* Checks if the server is valid and if the server supports the Share API
36
33
*
37
34
* @author David A. Velasco
38
35
* @author masensio
39
36
*/
40
- public class GetStatusRemoteOperation extends RemoteOperation {
41
-
42
- /**
43
- * Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs.
44
- */
45
- private static final int TRY_CONNECTION_TIMEOUT = 50000 ;
46
-
37
+ public class GetStatusRemoteOperation extends RemoteOperation <Pair <OwnCloudVersion , Boolean >> {
47
38
private static final String TAG = GetStatusRemoteOperation .class .getSimpleName ();
48
39
49
40
private static final String NODE_INSTALLED = "installed" ;
@@ -53,28 +44,24 @@ public class GetStatusRemoteOperation extends RemoteOperation {
53
44
private static final String PROTOCOL_HTTP = "http://" ;
54
45
private static final int UNTRUSTED_DOMAIN_ERROR_CODE = 15 ;
55
46
56
- private RemoteOperationResult mLatestResult ;
57
- private Context mContext ;
47
+ private RemoteOperationResult < Pair < OwnCloudVersion , Boolean >> mLatestResult ;
48
+ private final Context mContext ;
58
49
59
50
public GetStatusRemoteOperation (Context context ) {
60
51
mContext = context ;
61
52
}
62
53
63
- private boolean tryConnection (OwnCloudClient client ) {
54
+ private boolean tryConnection (NextcloudClient client ) {
64
55
boolean retval = false ;
65
- GetMethod get = null ;
66
- String baseUrlSt = client .getBaseUri (). toString ( );
56
+ com . nextcloud . operations . GetMethod get = null ;
57
+ String baseUrlSt = String . valueOf ( client .getBaseUri ());
67
58
try {
68
- get = new GetMethod (baseUrlSt + AccountUtils .STATUS_PATH );
69
-
70
- HttpParams params = HttpMethodParams .getDefaultParams ();
71
- params .setParameter (HttpMethodParams .USER_AGENT , OwnCloudClientManagerFactory .getUserAgent ());
72
- get .getParams ().setDefaults (params );
59
+ get = new com .nextcloud .operations .GetMethod (baseUrlSt + AccountUtils .STATUS_PATH , false );
73
60
74
61
client .setFollowRedirects (false );
75
62
boolean isRedirectToNonSecureConnection = false ;
76
- int status = client .executeMethod (get , TRY_CONNECTION_TIMEOUT , TRY_CONNECTION_TIMEOUT );
77
- mLatestResult = new RemoteOperationResult ((status == HttpStatus .SC_OK ), get );
63
+ int status = client .execute (get );
64
+ mLatestResult = new RemoteOperationResult <> ((status == HttpStatus .SC_OK ), get );
78
65
79
66
String redirectedLocation = mLatestResult .getRedirectedLocation ();
80
67
while (redirectedLocation != null && redirectedLocation .length () > 0
@@ -85,9 +72,9 @@ private boolean tryConnection(OwnCloudClient client) {
85
72
redirectedLocation .startsWith (PROTOCOL_HTTP )
86
73
);
87
74
get .releaseConnection ();
88
- get = new GetMethod (redirectedLocation );
89
- status = client .executeMethod (get , TRY_CONNECTION_TIMEOUT , TRY_CONNECTION_TIMEOUT );
90
- mLatestResult = new RemoteOperationResult ((status == HttpStatus .SC_OK ), get );
75
+ get = new GetMethod (redirectedLocation , false );
76
+ status = client .execute (get );
77
+ mLatestResult = new RemoteOperationResult <> ((status == HttpStatus .SC_OK ), get );
91
78
redirectedLocation = mLatestResult .getRedirectedLocation ();
92
79
}
93
80
@@ -96,7 +83,7 @@ private boolean tryConnection(OwnCloudClient client) {
96
83
if (status == HttpStatus .SC_OK ) {
97
84
JSONObject json = new JSONObject (response );
98
85
if (!json .getBoolean (NODE_INSTALLED )) {
99
- mLatestResult = new RemoteOperationResult (
86
+ mLatestResult = new RemoteOperationResult <> (
100
87
RemoteOperationResult .ResultCode .INSTANCE_NOT_CONFIGURED );
101
88
} else {
102
89
boolean extendedSupport = false ;
@@ -108,24 +95,22 @@ private boolean tryConnection(OwnCloudClient client) {
108
95
OwnCloudVersion ocVersion = new OwnCloudVersion (version );
109
96
110
97
if (!ocVersion .isVersionValid ()) {
111
- mLatestResult = new RemoteOperationResult (RemoteOperationResult .ResultCode .BAD_OC_VERSION );
98
+ mLatestResult = new RemoteOperationResult <> (RemoteOperationResult .ResultCode .BAD_OC_VERSION );
112
99
} else {
113
100
// success
114
101
if (isRedirectToNonSecureConnection ) {
115
- mLatestResult = new RemoteOperationResult (
102
+ mLatestResult = new RemoteOperationResult <> (
116
103
RemoteOperationResult .ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION );
117
104
} else {
118
- mLatestResult = new RemoteOperationResult (
105
+ mLatestResult = new RemoteOperationResult <> (
119
106
baseUrlSt .startsWith (PROTOCOL_HTTPS ) ?
120
107
RemoteOperationResult .ResultCode .OK_SSL :
121
108
RemoteOperationResult .ResultCode .OK_NO_SSL
122
109
);
123
110
}
124
111
125
- ArrayList <Object > data = new ArrayList <>();
126
- data .add (ocVersion );
127
- data .add (extendedSupport );
128
- mLatestResult .setData (data );
112
+ Pair <OwnCloudVersion , Boolean > data = new Pair <>(ocVersion , extendedSupport );
113
+ mLatestResult .setResultData (data );
129
114
retval = true ;
130
115
}
131
116
}
@@ -134,22 +119,22 @@ private boolean tryConnection(OwnCloudClient client) {
134
119
JSONObject json = new JSONObject (response );
135
120
136
121
if (json .getInt ("code" ) == UNTRUSTED_DOMAIN_ERROR_CODE ) {
137
- mLatestResult = new RemoteOperationResult (RemoteOperationResult .ResultCode .UNTRUSTED_DOMAIN );
122
+ mLatestResult = new RemoteOperationResult <> (RemoteOperationResult .ResultCode .UNTRUSTED_DOMAIN );
138
123
} else {
139
- mLatestResult = new RemoteOperationResult (false , status , get . getResponseHeaders () );
124
+ mLatestResult = new RemoteOperationResult <> (false , get );
140
125
}
141
126
} catch (JSONException e ) {
142
- mLatestResult = new RemoteOperationResult (false , status , get . getResponseHeaders () );
127
+ mLatestResult = new RemoteOperationResult <> (false , get );
143
128
}
144
129
} else {
145
- mLatestResult = new RemoteOperationResult (false , status , get . getResponseHeaders () );
130
+ mLatestResult = new RemoteOperationResult <> (false , get );
146
131
}
147
132
148
133
} catch (JSONException e ) {
149
- mLatestResult = new RemoteOperationResult (RemoteOperationResult .ResultCode .INSTANCE_NOT_CONFIGURED );
134
+ mLatestResult = new RemoteOperationResult <> (RemoteOperationResult .ResultCode .INSTANCE_NOT_CONFIGURED );
150
135
151
136
} catch (Exception e ) {
152
- mLatestResult = new RemoteOperationResult (e );
137
+ mLatestResult = new RemoteOperationResult <> (e );
153
138
154
139
} finally {
155
140
if (get != null )
@@ -171,18 +156,16 @@ private boolean tryConnection(OwnCloudClient client) {
171
156
}
172
157
173
158
private boolean isOnline () {
174
- ConnectivityManager cm = (ConnectivityManager ) mContext
175
- .getSystemService (Context .CONNECTIVITY_SERVICE );
176
- return cm != null && cm .getActiveNetworkInfo () != null
177
- && cm .getActiveNetworkInfo ().isConnectedOrConnecting ();
159
+ ConnectivityManager cm = (ConnectivityManager ) mContext .getSystemService (Context .CONNECTIVITY_SERVICE );
160
+ return cm != null && cm .getActiveNetworkInfo () != null && cm .getActiveNetworkInfo ().isConnectedOrConnecting ();
178
161
}
179
162
180
163
@ Override
181
- protected RemoteOperationResult run (OwnCloudClient client ) {
164
+ public RemoteOperationResult < Pair < OwnCloudVersion , Boolean >> run (NextcloudClient client ) {
182
165
if (!isOnline ()) {
183
- return new RemoteOperationResult (RemoteOperationResult .ResultCode .NO_NETWORK_CONNECTION );
166
+ return new RemoteOperationResult <> (RemoteOperationResult .ResultCode .NO_NETWORK_CONNECTION );
184
167
}
185
- String baseUriStr = client .getBaseUri (). toString ( );
168
+ String baseUriStr = String . valueOf ( client .getBaseUri ());
186
169
if (baseUriStr .startsWith (PROTOCOL_HTTP ) || baseUriStr .startsWith (PROTOCOL_HTTPS )) {
187
170
tryConnection (client );
188
171
0 commit comments