Skip to content

Commit 3dae7f5

Browse files
authored
Merge pull request eugenp#721 from ivanp81/master
BAEL-342: OKHttp Example
2 parents 3613428 + f63ebcf commit 3dae7f5

11 files changed

Lines changed: 629 additions & 16 deletions

File tree

‎okhttp/pom.xml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.baeldung.okhttp</groupId>
4+
<artifactId>okhttp</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
7+
<dependencies>
8+
9+
<!-- okhttp -->
10+
<dependency>
11+
<groupId>com.squareup.okhttp3</groupId>
12+
<artifactId>okhttp</artifactId>
13+
<version>${com.squareup.okhttp3.version}</version>
14+
</dependency>
15+
16+
<!-- test scoped -->
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>${junit.version}</version>
21+
<scope>test</scope>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.hamcrest</groupId>
26+
<artifactId>hamcrest-library</artifactId>
27+
<version>${org.hamcrest.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
31+
</dependencies>
32+
33+
<properties>
34+
35+
<!-- okhttp -->
36+
<com.squareup.okhttp3.version>3.4.1</com.squareup.okhttp3.version>
37+
38+
<!-- testing -->
39+
<org.hamcrest.version>1.3</org.hamcrest.version>
40+
<junit.version>4.12</junit.version>
41+
</properties>
42+
43+
44+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.okhttp;
2+
3+
import java.io.IOException;
4+
5+
import okhttp3.Interceptor;
6+
import okhttp3.Request;
7+
import okhttp3.Response;
8+
9+
public class DefaultContentTypeInterceptor implements Interceptor {
10+
11+
private final String contentType;
12+
13+
public DefaultContentTypeInterceptor(String contentType) {
14+
this.contentType = contentType;
15+
}
16+
17+
public Response intercept(Interceptor.Chain chain) throws IOException {
18+
19+
Request originalRequest = chain.request();
20+
Request requestWithUserAgent = originalRequest.newBuilder()
21+
.header("Content-Type", contentType)
22+
.build();
23+
24+
return chain.proceed(requestWithUserAgent);
25+
}
26+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.baeldung.okhttp;
2+
3+
import okhttp3.RequestBody;
4+
import okhttp3.MediaType;
5+
6+
import java.io.IOException;
7+
8+
import okio.Buffer;
9+
import okio.BufferedSink;
10+
import okio.ForwardingSink;
11+
import okio.Okio;
12+
import okio.Sink;
13+
14+
public class ProgressRequestWrapper extends RequestBody {
15+
16+
protected RequestBody delegate;
17+
protected ProgressListener listener;
18+
19+
protected CountingSink countingSink;
20+
21+
public ProgressRequestWrapper(RequestBody delegate, ProgressListener listener) {
22+
this.delegate = delegate;
23+
this.listener = listener;
24+
}
25+
26+
@Override
27+
public MediaType contentType() {
28+
return delegate.contentType();
29+
}
30+
31+
@Override
32+
public long contentLength() throws IOException {
33+
return delegate.contentLength();
34+
}
35+
36+
@Override
37+
public void writeTo(BufferedSink sink) throws IOException {
38+
39+
BufferedSink bufferedSink;
40+
41+
countingSink = new CountingSink(sink);
42+
bufferedSink = Okio.buffer(countingSink);
43+
44+
delegate.writeTo(bufferedSink);
45+
46+
bufferedSink.flush();
47+
}
48+
49+
protected final class CountingSink extends ForwardingSink {
50+
51+
private long bytesWritten = 0;
52+
53+
public CountingSink(Sink delegate) {
54+
super(delegate);
55+
}
56+
57+
@Override
58+
public void write(Buffer source, long byteCount) throws IOException {
59+
60+
super.write(source, byteCount);
61+
62+
bytesWritten += byteCount;
63+
listener.onRequestProgress(bytesWritten, contentLength());
64+
}
65+
66+
}
67+
68+
public static interface ProgressListener {
69+
70+
public void onRequestProgress(long bytesWritten, long contentLength);
71+
72+
}
73+
}
74+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.baeldung.okhttp;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
import org.baeldung.okhttp.ProgressRequestWrapper;
11+
import org.junit.Test;
12+
13+
import okhttp3.Call;
14+
import okhttp3.MediaType;
15+
import okhttp3.MultipartBody;
16+
import okhttp3.OkHttpClient;
17+
import okhttp3.Request;
18+
import okhttp3.RequestBody;
19+
import okhttp3.Response;
20+
21+
public class OkHttpFileUploadingTest {
22+
23+
private static final String BASE_URL = "http://localhost:8080/spring-rest";
24+
25+
@Test
26+
public void whenUploadFileUsingOkHttp_thenCorrect() throws IOException {
27+
28+
OkHttpClient client = new OkHttpClient();
29+
30+
RequestBody requestBody = new MultipartBody.Builder()
31+
.setType(MultipartBody.FORM)
32+
.addFormDataPart("file", "file.txt",
33+
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
34+
.build();
35+
36+
Request request = new Request.Builder()
37+
.url(BASE_URL + "/users/upload")
38+
.post(requestBody)
39+
.build();
40+
41+
Call call = client.newCall(request);
42+
Response response = call.execute();
43+
44+
assertThat(response.code(), equalTo(200));
45+
}
46+
47+
@Test
48+
public void whenGetUploadFileProgressUsingOkHttp_thenCorrect() throws IOException {
49+
50+
OkHttpClient client = new OkHttpClient();
51+
52+
RequestBody requestBody = new MultipartBody.Builder()
53+
.setType(MultipartBody.FORM)
54+
.addFormDataPart("file", "file.txt",
55+
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
56+
.build();
57+
58+
59+
ProgressRequestWrapper.ProgressListener listener = new ProgressRequestWrapper.ProgressListener() {
60+
61+
public void onRequestProgress(long bytesWritten, long contentLength) {
62+
63+
float percentage = 100f * bytesWritten / contentLength;
64+
assertFalse(Float.compare(percentage, 100) > 0);
65+
}
66+
};
67+
68+
ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, listener);
69+
70+
Request request = new Request.Builder()
71+
.url(BASE_URL + "/users/upload")
72+
.post(countingBody)
73+
.build();
74+
75+
Call call = client.newCall(request);
76+
Response response = call.execute();
77+
78+
assertThat(response.code(), equalTo(200));
79+
80+
}
81+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.baeldung.okhttp;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.io.IOException;
7+
8+
import org.junit.Test;
9+
10+
import okhttp3.Call;
11+
import okhttp3.Callback;
12+
import okhttp3.HttpUrl;
13+
import okhttp3.OkHttpClient;
14+
import okhttp3.Request;
15+
import okhttp3.Response;
16+
17+
public class OkHttpGetTest {
18+
19+
private static final String BASE_URL = "http://localhost:8080/spring-rest";
20+
21+
@Test
22+
public void whenGetRequestUsingOkHttp_thenCorrect() throws IOException {
23+
24+
OkHttpClient client = new OkHttpClient();
25+
26+
Request request = new Request.Builder()
27+
.url(BASE_URL + "/date")
28+
.build();
29+
30+
Call call = client.newCall(request);
31+
Response response = call.execute();
32+
33+
assertThat(response.code(), equalTo(200));
34+
}
35+
36+
@Test
37+
public void whenGetRequestWithQueryParameterUsingOkHttp_thenCorrect() throws IOException {
38+
39+
OkHttpClient client = new OkHttpClient();
40+
41+
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder();
42+
urlBuilder.addQueryParameter("id", "1");
43+
44+
String url = urlBuilder.build().toString();
45+
46+
Request request = new Request.Builder()
47+
.url(url)
48+
.build();
49+
50+
Call call = client.newCall(request);
51+
Response response = call.execute();
52+
53+
assertThat(response.code(), equalTo(200));
54+
}
55+
56+
@Test
57+
public void whenAsynchronousGetRequestUsingOkHttp_thenCorrect() {
58+
59+
OkHttpClient client = new OkHttpClient();
60+
61+
Request request = new Request.Builder()
62+
.url(BASE_URL + "/date")
63+
.build();
64+
65+
Call call = client.newCall(request);
66+
67+
call.enqueue(new Callback() {
68+
69+
public void onResponse(Call call, Response response) throws IOException {
70+
assertThat(response.code(), equalTo(200));
71+
}
72+
73+
public void onFailure(Call call, IOException e) {
74+
75+
}
76+
});
77+
}
78+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.okhttp;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Test;
6+
7+
import okhttp3.Call;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
11+
12+
public class OkHttpHeaderTest {
13+
14+
private static final String SAMPLE_URL = "http://www.github.com";
15+
16+
@Test
17+
public void whenSetHeaderUsingOkHttp_thenCorrect() throws IOException {
18+
19+
OkHttpClient client = new OkHttpClient();
20+
21+
Request request = new Request.Builder()
22+
.url(SAMPLE_URL)
23+
.addHeader("Content-Type", "application/json")
24+
.build();
25+
26+
Call call = client.newCall(request);
27+
Response response = call.execute();
28+
response.close();
29+
}
30+
31+
@Test
32+
public void whenSetDefaultHeaderUsingOkHttp_thenCorrect() throws IOException {
33+
34+
OkHttpClient client = new OkHttpClient.Builder()
35+
.addInterceptor(new DefaultContentTypeInterceptor("application/json"))
36+
.build();
37+
38+
Request request = new Request.Builder()
39+
.url(SAMPLE_URL)
40+
.build();
41+
42+
Call call = client.newCall(request);
43+
Response response = call.execute();
44+
response.close();
45+
}
46+
47+
48+
}

0 commit comments

Comments
 (0)