-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathOkHttp.java
187 lines (167 loc) · 5.36 KB
/
OkHttp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2019, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License,
* Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the
* License.
*/
package com.okta.oidc.example;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import com.okta.oidc.net.ConnectionParameters;
import com.okta.oidc.net.OktaHttpClient;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* A OktaHttpClient implementation using OkHttpClient.
*/
public class OkHttp implements OktaHttpClient {
private static final int CONNECTION_TIMEOUT_MS = 15_000;
private static final int READ_TIMEOUT_MS = 10_000;
private static final String TAG = "OkHttp";
/**
* The constant sOkHttpClient.
*/
protected static OkHttpClient sOkHttpClient;
/**
* The Call.
*/
protected volatile Call mCall;
/**
* The response.
*/
protected Response mResponse;
/**
* The exception.
*/
protected Exception mException;
/**
* Build request request.
*
* @param uri the uri
* @param param the param
* @return the request
*/
protected Request buildRequest(Uri uri, ConnectionParameters param) {
if (sOkHttpClient == null) {
sOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.build();
}
Request.Builder requestBuilder = new Request.Builder().url(uri.toString());
for (Map.Entry<String, String> headerEntry : param.requestProperties().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
if (param.requestMethod() == ConnectionParameters.RequestMethod.GET) {
requestBuilder = requestBuilder.get();
} else {
Map<String, String> postParameters = param.postParameters();
if (postParameters != null) {
FormBody.Builder formBuilder = new FormBody.Builder();
for (Map.Entry<String, String> postEntry : postParameters.entrySet()) {
String key = postEntry.getKey();
formBuilder.add(key, postEntry.getValue());
}
RequestBody formBody = formBuilder.build();
requestBuilder.post(formBody);
} else {
requestBuilder.post(RequestBody.create(null, ""));
}
}
return requestBuilder.build();
}
@Override
@WorkerThread
public InputStream connect(@NonNull Uri uri, @NonNull ConnectionParameters param)
throws Exception {
Request request = buildRequest(uri, param);
mCall = sOkHttpClient.newCall(request);
final CountDownLatch latch = new CountDownLatch(1);
mCall.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
mException = e;
latch.countDown();
}
@Override
public void onResponse(Call call, Response response) {
mResponse = response;
latch.countDown();
}
});
latch.await();
if (mException != null) {
throw mException;
}
if (mResponse != null && mResponse.body() != null) {
return mResponse.body().byteStream();
}
return null;
}
@Override
public void cleanUp() {
//NO-OP
}
@Override
public void cancel() {
if (mCall != null) {
mCall.cancel();
}
}
@Override
public Map<String, List<String>> getHeaderFields() {
if (mResponse != null) {
return mResponse.headers().toMultimap();
}
return null;
}
@Override
public String getHeader(String header) {
if (mResponse != null) {
return mResponse.header(header);
}
return null;
}
@Override
public int getResponseCode() throws IOException {
if (mResponse != null) {
return mResponse.code();
}
return -1;
}
@Override
public int getContentLength() {
if (mResponse != null && mResponse.body() != null) {
return (int) mResponse.body().contentLength();
}
return -1;
}
@Override
public String getResponseMessage() throws IOException {
if (mResponse != null) {
return mResponse.message();
}
return null;
}
}