Refine search
/** * 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(); }
return ByteString.empty(); } else if (body instanceof Optional) { if (!((Optional<?>) body).isPresent()) { return ByteString.empty(); return ByteString.fromArray((byte[]) body); } else if (body instanceof String) { return ByteString.fromString((String) body); return XML.toBytes((Document) body); } else { Map<String, String[]> form = asFormUrlEncoded(); if (form != null) { return ByteString.fromString( .stream() .flatMap(entry -> { String key = encode(entry.getKey()); return Arrays.stream(entry.getValue()).map( value -> key + "=" + encode(value) ); })
/** * Set the body of the request. * * @param body The body. * @return the modified builder */ protected RequestBuilder body(RequestBody body) { if (body == null || body.as(Object.class) == null) { // assume null signifies no body; RequestBody is a wrapper for the actual body content headers(getHeaders().remove(HeaderNames.CONTENT_LENGTH).remove(HeaderNames.TRANSFER_ENCODING)); } else { if (!getHeaders().get(HeaderNames.TRANSFER_ENCODING).isPresent()) { final MultipartFormData<?> multipartFormData = body.asMultipartFormData(); if (multipartFormData != null) { header(HeaderNames.CONTENT_LENGTH, Long.toString(calcMultipartFormDataBodyLength(multipartFormData))); } else { int length = body.asBytes().length(); header(HeaderNames.CONTENT_LENGTH, Integer.toString(length)); } } } req = req.withBody(body); return this; }
parseQueryString(qs, request.body().asText()); if (request.body().asText() != null) { body = request.body().asText().getBytes("utf-8"); } else if (request.body().asRaw() != null) { body = request.body().asBytes().toArray(); } else if (request.body().asJson() != null) { body = request.body().asJson().asText().getBytes("utf-8"); if (body.length == 0) { body = request.body().asJson().toString().getBytes("utf-8"); } else if (request.body().asXml() != null) { body = request.body().asXml().getTextContent().getBytes("utf-8");
/** * The request content as text. * * @return The request content as text. */ public String asText() { return as(String.class); }
/** * The request content as Array bytes. * * @return The request content as Array bytes. */ public RawBuffer asRaw() { return as(RawBuffer.class); }
/** * The request content parsed as multipart form data. * * @param <A> the file type (e.g. play.api.libs.Files.TemporaryFile) * @return the content parsed as multipart form data */ public <A> MultipartFormData<A> asMultipartFormData() { return as(MultipartFormData.class); }
/** * The request content parsed as multipart form data. * * @param <A> the file type (e.g. play.api.libs.Files.TemporaryFile) * @return the content parsed as multipart form data */ public <A> MultipartFormData<A> asMultipartFormData() { return as(MultipartFormData.class); }
/** * The request content as XML. * * @return The request content as XML. */ public Document asXml() { return as(Document.class); }
/** * The request content as Array bytes. * * @return The request content as Array bytes. */ public RawBuffer asRaw() { return as(RawBuffer.class); }
/** * The request content as XML. * * @return The request content as XML. */ public Document asXml() { return as(Document.class); }
/** * The request content as Json. * * @return The request content as Json. */ public JsonNode asJson() { return as(JsonNode.class); }
/** * The request content as XML. * * @return The request content as XML. */ public Document asXml() { return as(Document.class); }
/** * The request content as Json. * * @return The request content as Json. */ public JsonNode asJson() { return as(JsonNode.class); }
/** * The request content parsed as multipart form data. * * @param <A> the file type (e.g. play.api.libs.Files.TemporaryFile) * @return the content parsed as multipart form data */ public <A> MultipartFormData<A> asMultipartFormData() { return as(MultipartFormData.class); }
/** * The request content as text. * * @return The request content as text. */ public String asText() { return as(String.class); }
/** * The request content as Json. * * @return The request content as Json. */ public JsonNode asJson() { return as(JsonNode.class); }
/** * The request content as Array bytes. * * @return The request content as Array bytes. */ public RawBuffer asRaw() { return as(RawBuffer.class); }
/** * The request content as text. * * @return The request content as text. */ public String asText() { return as(String.class); }
/** * 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(); }