· 8 years ago · Dec 18, 2016, 02:22 PM
1CompletionException: java.lang.RuntimeException: java.lang.IllegalArgumentException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: play.libs.ws.ahc.AhcWSResponse["underlying"]->org.asynchttpclient.netty.NettyResponse["responseBodyAsStream"])
2
3public class OauthValidator extends Controller {
4WSClient ws;
5
6@Inject
7public OauthValidator(WSClient ws) {
8 this.ws = ws;
9}
10
11@Inject
12HttpExecutionContext ec;
13
14public CompletionStage<Result> index() {
15 Http.Request req = Http.Context.current().request();
16 String tokenCarrier = req.getHeader("AUTHORIZATION");
17 if(StringUtils.isEmpty(tokenCarrier)) {
18 Map<String, String> errorResponse = new HashMap<>();
19 errorResponse.put("value", "Unauthorized access");
20 errorResponse.put("status", "401");
21 return CompletableFuture.completedFuture(unauthorized(Json.toJson(errorResponse)));
22 }
23 String userAccessToken = tokenCarrier.replace("Bearer", "").trim();
24
25 Oauth oauth = new Oauth(this.ws);
26 CompletionStage<JsonNode> userDetails = oauth.getUserDetails(userAccessToken);
27 return userDetails.thenApply(Results:: ok);
28}}
29
30public class Oauth {
31
32private final WSClient ws;
33
34@Inject
35public Oauth(WSClient ws) {
36 this.ws = ws;
37}
38
39public CompletionStage<JsonNode> getUserDetails(String userAccessToken) {
40 String verification_type = "oauth_token";
41 String oauthUrl = Constants.oauth + Constants.userDetailsUrl;
42 String secretKey = Constants.secretKey;
43 String clientId = Constants.clientId;
44 String basicAuthentication = new String(Base64.encodeBase64((clientId + ":" + secretKey).getBytes()));
45
46 WSRequest oauthCall = ws.url(oauthUrl);
47 CompletionStage<JsonNode> jsonNodeCompletionStage = oauthCall
48 .setHeader("authorization", "BASIC" + basicAuthentication)
49 .setHeader("Content-Type", "application/json")
50 .setHeader("data", userAccessToken)
51 .setHeader("verification_type", verification_type)
52 .get()
53 .thenApply(Json::toJson);
54 return jsonNodeCompletionStage;
55}}
56
57{"value": "Unauthorized access",
58"status": "401"}