Skip to content

Commit 7f6bccb

Browse files
lukoyanovwing328
authored andcommitted
[Java] Added Play! WS filters support for retrofit2 client (#6499)
* added play! ws filters support * samples updated
1 parent 594b390 commit 7f6bccb

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/play25/Play25CallFactory.mustache

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import okio.BufferedSource;
66
import play.libs.ws.WSClient;
77
import play.libs.ws.WSRequest;
88
import play.libs.ws.WSResponse;
9+
import play.libs.ws.WSRequestFilter;
910

1011
import java.io.IOException;
1112
import java.net.MalformedURLException;
@@ -31,11 +32,19 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
3132
3233
/** Extra query parameters to add to request */
3334
private List<Pair> extraQueryParams = new ArrayList<>();
35+
36+
/** Filters (interceptors) */
37+
private List<WSRequestFilter> filters = new ArrayList<>();
3438
3539
public Play25CallFactory(WSClient wsClient) {
3640
this.wsClient = wsClient;
3741
}
3842

43+
public Play25CallFactory(WSClient wsClient, List<WSRequestFilter> filters) {
44+
this.wsClient = wsClient;
45+
this.filters.addAll(filters);
46+
}
47+
3948
public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
4049
List<Pair> extraQueryParams) {
4150
this.wsClient = wsClient;
@@ -74,7 +83,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
7483
}
7584
}
7685

77-
return new PlayWSCall(wsClient, rb.build());
86+
return new PlayWSCall(wsClient, this.filters, rb.build());
7887
}
7988

8089
/**
@@ -84,12 +93,14 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
8493
8594
private final WSClient wsClient;
8695
private WSRequest wsRequest;
96+
private List<WSRequestFilter> filters;
8797
8898
private final Request request;
8999
90-
public PlayWSCall(WSClient wsClient, Request request) {
100+
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
91101
this.wsClient = wsClient;
92102
this.request = request;
103+
this.filters = filters;
93104
}
94105

95106
@Override
@@ -126,6 +137,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
126137
if (request.body() != null) {
127138
addBody(wsRequest);
128139
}
140+
filters.stream().forEach(f -> wsRequest.withRequestFilter(f));
129141

130142
return wsRequest.execute(request.method());
131143
} catch (Exception e) {

samples/client/petstore/java/retrofit2-play25/src/main/java/io/swagger/client/Play25CallFactory.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import play.libs.ws.WSClient;
77
import play.libs.ws.WSRequest;
88
import play.libs.ws.WSResponse;
9+
import play.libs.ws.WSRequestFilter;
910

1011
import java.io.IOException;
1112
import java.net.MalformedURLException;
@@ -31,11 +32,19 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
3132

3233
/** Extra query parameters to add to request */
3334
private List<Pair> extraQueryParams = new ArrayList<>();
35+
36+
/** Filters (interceptors) */
37+
private List<WSRequestFilter> filters = new ArrayList<>();
3438

3539
public Play25CallFactory(WSClient wsClient) {
3640
this.wsClient = wsClient;
3741
}
3842

43+
public Play25CallFactory(WSClient wsClient, List<WSRequestFilter> filters) {
44+
this.wsClient = wsClient;
45+
this.filters.addAll(filters);
46+
}
47+
3948
public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
4049
List<Pair> extraQueryParams) {
4150
this.wsClient = wsClient;
@@ -74,7 +83,7 @@ public Call newCall(Request request) {
7483
}
7584
}
7685

77-
return new PlayWSCall(wsClient, rb.build());
86+
return new PlayWSCall(wsClient, this.filters, rb.build());
7887
}
7988

8089
/**
@@ -84,12 +93,14 @@ static class PlayWSCall implements Call {
8493

8594
private final WSClient wsClient;
8695
private WSRequest wsRequest;
96+
private List<WSRequestFilter> filters;
8797

8898
private final Request request;
8999

90-
public PlayWSCall(WSClient wsClient, Request request) {
100+
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
91101
this.wsClient = wsClient;
92102
this.request = request;
103+
this.filters = filters;
93104
}
94105

95106
@Override
@@ -126,6 +137,7 @@ CompletionStage<WSResponse> executeAsync() {
126137
if (request.body() != null) {
127138
addBody(wsRequest);
128139
}
140+
filters.stream().forEach(f -> wsRequest.withRequestFilter(f));
129141

130142
return wsRequest.execute(request.method());
131143
} catch (Exception e) {

0 commit comments

Comments
 (0)