/** * Converts a JSON request to a given class. Conversion is performed * with [[Json.fromJson(JsonNode,Class)]]. * * Will return Optional.empty() if the request body is not an instance of JsonNode. * If the JsonNode simply has missing fields, a valid reference with null fields is returne. * * @param <A> The type to convert the JSON value to. * @param clazz The class to convert the JSON value to. * @return The converted value if the request has a JSON body or an empty value if the request has an empty body or a body of a different type. */ public <A> Optional<A> parseJson(Class<A> clazz) { return (body instanceof JsonNode) ? Optional.of(Json.fromJson(asJson(), clazz)) : Optional.empty(); }
/** * Converts a JSON request to a given class. Conversion is performed * with [[Json.fromJson(JsonNode,Class)]]. * * Will return Optional.empty() if the request body is not an instance of JsonNode. * If the JsonNode simply has missing fields, a valid reference with null fields is returne. * * @param <A> The type to convert the JSON value to. * @param clazz The class to convert the JSON value to. * @return The converted value if the request has a JSON body or an empty value if the request has an empty body or a body of a different type. */ public <A> Optional<A> parseJson(Class<A> clazz) { return (body instanceof JsonNode) ? Optional.of(Json.fromJson(asJson(), clazz)) : Optional.empty(); }
/** * Converts a JSON request to a given class. Conversion is performed * with [[Json.fromJson(JsonNode,Class)]]. * * Will return Optional.empty() if the request body is not an instance of JsonNode. * If the JsonNode simply has missing fields, a valid reference with null fields is returne. * * @param <A> The type to convert the JSON value to. * @param clazz The class to convert the JSON value to. * @return The converted value if the request has a JSON body or an empty value if the request has an empty body or a body of a different type. */ public <A> Optional<A> parseJson(Class<A> clazz) { return (body instanceof JsonNode) ? Optional.of(Json.fromJson(asJson(), clazz)) : Optional.empty(); }
final JsonNode webhookJson = request().body().asJson(); final String eventOwnerAddress = webhookJson.findValue("user").findValue("emailAddress").asText(); final String webhookEvent = webhookJson.get("webhookEvent").asText();
JsonNode jn = request().body().asJson(); JsonNode type = jn.get("type"); JsonNode dataClass = jn.get("class");
when(mockedBody.asJson()).thenReturn(json);
JsonNode jn = request().body().asJson(); JsonNode type = jn.get("type"); JsonNode dataClass = jn.get("class");
JsonNode jn = request().body().asJson(); JsonNode type = jn.get("type"); JsonNode dataClass = jn.get("class");
protected Map<String,String> requestData(Http.Request request) { Map<String,String[]> urlFormEncoded = new HashMap<>(); if (request.body().asFormUrlEncoded() != null) { urlFormEncoded = request.body().asFormUrlEncoded(); } Map<String,String[]> multipartFormData = new HashMap<>(); if (request.body().asMultipartFormData() != null) { multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded(); } Map<String,String> jsonData = new HashMap<>(); if (request.body().asJson() != null) { jsonData = play.libs.Scala.asJava( play.api.data.FormUtils.fromJson("", play.api.libs.json.Json.parse( play.libs.Json.stringify(request.body().asJson()) ) ) ); } Map<String,String> data = new HashMap<>(); fillDataWith(data, urlFormEncoded); fillDataWith(data, multipartFormData); jsonData.forEach(data::put); if(!request.method().equalsIgnoreCase(HttpVerbs.POST) && !request.method().equalsIgnoreCase(HttpVerbs.PUT) && !request.method().equalsIgnoreCase(HttpVerbs.PATCH)) { fillDataWith(data, request.queryString()); } return data; }
protected Map<String,String> requestData(Http.Request request) { Map<String,String[]> urlFormEncoded = new HashMap<>(); if (request.body().asFormUrlEncoded() != null) { urlFormEncoded = request.body().asFormUrlEncoded(); } Map<String,String[]> multipartFormData = new HashMap<>(); if (request.body().asMultipartFormData() != null) { multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded(); } Map<String,String> jsonData = new HashMap<>(); if (request.body().asJson() != null) { jsonData = play.libs.Scala.asJava( play.api.data.FormUtils.fromJson("", play.api.libs.json.Json.parse( play.libs.Json.stringify(request.body().asJson()) ) ) ); } Map<String,String> data = new HashMap<>(); fillDataWith(data, urlFormEncoded); fillDataWith(data, multipartFormData); jsonData.forEach(data::put); if(!request.method().equalsIgnoreCase(HttpVerbs.POST) && !request.method().equalsIgnoreCase(HttpVerbs.PUT) && !request.method().equalsIgnoreCase(HttpVerbs.PATCH)) { fillDataWith(data, request.queryString()); } return data; }
protected Map<String,String> requestData(Http.Request request) { Map<String,String[]> urlFormEncoded = new HashMap<>(); if (request.body().asFormUrlEncoded() != null) { urlFormEncoded = request.body().asFormUrlEncoded(); } Map<String,String[]> multipartFormData = new HashMap<>(); if (request.body().asMultipartFormData() != null) { multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded(); } Map<String,String> jsonData = new HashMap<>(); if (request.body().asJson() != null) { jsonData = play.libs.Scala.asJava( play.api.data.FormUtils.fromJson("", play.api.libs.json.Json.parse( play.libs.Json.stringify(request.body().asJson()) ) ) ); } Map<String,String> data = new HashMap<>(); fillDataWith(data, urlFormEncoded); fillDataWith(data, multipartFormData); jsonData.forEach(data::put); if(!request.method().equalsIgnoreCase(HttpVerbs.POST) && !request.method().equalsIgnoreCase(HttpVerbs.PUT) && !request.method().equalsIgnoreCase(HttpVerbs.PATCH)) { fillDataWith(data, request.queryString()); } return data; }
protected <T> F.Promise<Result> withJsonBody(Class<T> klass, ToPromiseResult<T> f) { JsonNode jsonNode = request().body().asJson(); if (jsonNode == null) { return pure(badRequest("could not parse body to JSON")); } else { try { T obj = play.libs.Json.fromJson(jsonNode, klass); F.Tuple<Integer, Object> r = f.apply(obj); return pure(Results.status(r._1, toJson(r._2))); } catch (ConstraintViolationException ex) { return pure((badRequest(createJsonResponse(ex)))); } catch (RuntimeException e) { if (e.getCause() instanceof JsonProcessingException) { return pure(badRequest(handleProcessingException((JsonProcessingException) e.getCause()))); } else { throw e; } } } }
protected String getJsonText() { JsonNode tryJson = request().body().asJson(); if (tryJson != null) return tryJson.toString(); else return request().body().asText(); } }