Skip to content

Commit cd66f66

Browse files
committed
✨ Implement RawBodyAccessor in BsonRequest and JsonRequest for raw body retrieval
1 parent ce31acc commit cd66f66

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

commons/src/main/java/org/restheart/exchange/BsonRequest.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
*
3434
* @author Andrea Di Cesare {@literal <[email protected]>}
3535
*/
36-
public class BsonRequest extends ServiceRequest<BsonValue> {
36+
public class BsonRequest extends ServiceRequest<BsonValue> implements RawBodyAccessor<String> {
37+
38+
private String rawBody;
39+
3740
protected BsonRequest(HttpServerExchange exchange) {
3841
super(exchange);
3942
}
@@ -49,9 +52,15 @@ public static BsonRequest of(HttpServerExchange exchange) {
4952
@Override
5053
public BsonValue parseContent() throws IOException, BadRequestException {
5154
try {
52-
return BsonUtils.parse(ChannelReader.readString(wrapped));
53-
} catch(JsonParseException jpe) {
55+
rawBody = ChannelReader.readString(wrapped);
56+
return BsonUtils.parse(rawBody);
57+
} catch (JsonParseException jpe) {
5458
throw new BadRequestException(jpe.getMessage(), jpe);
5559
}
5660
}
61+
62+
@Override
63+
public final String getRawBody() {
64+
return rawBody;
65+
}
5766
}

commons/src/main/java/org/restheart/exchange/JsonRequest.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
*
3434
* @author Andrea Di Cesare {@literal <[email protected]>}
3535
*/
36-
public class JsonRequest extends ServiceRequest<JsonElement> {
36+
public class JsonRequest extends ServiceRequest<JsonElement> implements RawBodyAccessor<String> {
37+
38+
private String rawBody;
39+
3740
protected JsonRequest(HttpServerExchange exchange) {
3841
super(exchange);
3942
}
@@ -50,12 +53,18 @@ public static JsonRequest of(HttpServerExchange exchange) {
5053
public JsonElement parseContent() throws IOException, BadRequestException {
5154
if (wrapped.getRequestContentLength() > 0) {
5255
try {
53-
return JsonParser.parseString(ChannelReader.readString(wrapped));
56+
rawBody = ChannelReader.readString(wrapped);
57+
return JsonParser.parseString(rawBody);
5458
} catch(JsonSyntaxException jse) {
5559
throw new BadRequestException(jse.getMessage(), jse);
5660
}
5761
} else {
5862
return null;
5963
}
6064
}
65+
66+
@Override
67+
public String getRawBody() {
68+
return rawBody;
69+
}
6170
}

0 commit comments

Comments
 (0)