Skip to content

Commit

Permalink
[Java] Added Play! WS filters support for retrofit2 client (swagger-a…
Browse files Browse the repository at this point in the history
…pi#6499)

* added play! ws filters support

* samples updated
  • Loading branch information
lukoyanov authored and wing328 committed Sep 20, 2017
1 parent 594b390 commit 7f6bccb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import okio.BufferedSource;
import play.libs.ws.WSClient;
import play.libs.ws.WSRequest;
import play.libs.ws.WSResponse;
import play.libs.ws.WSRequestFilter;

import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -31,11 +32,19 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
/** Extra query parameters to add to request */
private List<Pair> extraQueryParams = new ArrayList<>();
/** Filters (interceptors) */
private List<WSRequestFilter> filters = new ArrayList<>();
public Play25CallFactory(WSClient wsClient) {
this.wsClient = wsClient;
}

public Play25CallFactory(WSClient wsClient, List<WSRequestFilter> filters) {
this.wsClient = wsClient;
this.filters.addAll(filters);
}

public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
List<Pair> extraQueryParams) {
this.wsClient = wsClient;
Expand Down Expand Up @@ -74,7 +83,7 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
}
}

return new PlayWSCall(wsClient, rb.build());
return new PlayWSCall(wsClient, this.filters, rb.build());
}

/**
Expand All @@ -84,12 +93,14 @@ public class Play25CallFactory implements okhttp3.Call.Factory {
private final WSClient wsClient;
private WSRequest wsRequest;
private List<WSRequestFilter> filters;
private final Request request;
public PlayWSCall(WSClient wsClient, Request request) {
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
this.wsClient = wsClient;
this.request = request;
this.filters = filters;
}

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

return wsRequest.execute(request.method());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import play.libs.ws.WSClient;
import play.libs.ws.WSRequest;
import play.libs.ws.WSResponse;
import play.libs.ws.WSRequestFilter;

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

/** Extra query parameters to add to request */
private List<Pair> extraQueryParams = new ArrayList<>();

/** Filters (interceptors) */
private List<WSRequestFilter> filters = new ArrayList<>();

public Play25CallFactory(WSClient wsClient) {
this.wsClient = wsClient;
}

public Play25CallFactory(WSClient wsClient, List<WSRequestFilter> filters) {
this.wsClient = wsClient;
this.filters.addAll(filters);
}

public Play25CallFactory(WSClient wsClient, Map<String, String> extraHeaders,
List<Pair> extraQueryParams) {
this.wsClient = wsClient;
Expand Down Expand Up @@ -74,7 +83,7 @@ public Call newCall(Request request) {
}
}

return new PlayWSCall(wsClient, rb.build());
return new PlayWSCall(wsClient, this.filters, rb.build());
}

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

private final WSClient wsClient;
private WSRequest wsRequest;
private List<WSRequestFilter> filters;

private final Request request;

public PlayWSCall(WSClient wsClient, Request request) {
public PlayWSCall(WSClient wsClient, List<WSRequestFilter> filters, Request request) {
this.wsClient = wsClient;
this.request = request;
this.filters = filters;
}

@Override
Expand Down Expand Up @@ -126,6 +137,7 @@ CompletionStage<WSResponse> executeAsync() {
if (request.body() != null) {
addBody(wsRequest);
}
filters.stream().forEach(f -> wsRequest.withRequestFilter(f));

return wsRequest.execute(request.method());
} catch (Exception e) {
Expand Down

0 comments on commit 7f6bccb

Please sign in to comment.