diff --git a/coap-core/src/main/java/com/mbed/coap/client/CoapClient.java b/coap-core/src/main/java/com/mbed/coap/client/CoapClient.java index b5990492..2275f92a 100644 --- a/coap-core/src/main/java/com/mbed/coap/client/CoapClient.java +++ b/coap-core/src/main/java/com/mbed/coap/client/CoapClient.java @@ -60,11 +60,11 @@ public static CoapClient create(InetSocketAddress target, CoapServer server, Fun } public CompletableFuture send(CoapRequest request) { - return clientService.apply(request.withAddress(destination)); + return send(request.modify()); } public CompletableFuture send(CoapRequest.Builder request) { - return send(request.build()); + return clientService.apply(request.address(destination).build()); } public CoapResponse sendSync(CoapRequest request) throws CoapException { diff --git a/coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java b/coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java index 4e81d3a0..7f5d4d19 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java @@ -169,24 +169,32 @@ public String toString() { // --- MODIFIERS --- + @Deprecated public CoapRequest withToken(Opaque newToken) { return new CoapRequest(method, newToken, options, payload, peerAddress, transContext); } + @Deprecated public CoapRequest withOptions(Consumer optionsFunc) { CoapOptionsBuilder optionsBuilder = CoapOptionsBuilder.from(options); optionsFunc.accept(optionsBuilder); return new CoapRequest(method, token, optionsBuilder.build(), payload, peerAddress, transContext); } + @Deprecated public CoapRequest withPayload(Opaque newPayload) { return new CoapRequest(method, token, options, newPayload, peerAddress, transContext); } + @Deprecated public CoapRequest withAddress(InetSocketAddress newPeerAddress) { return new CoapRequest(method, token, options, payload, newPeerAddress, transContext); } + public Builder modify() { + return new Builder(method, token, CoapOptionsBuilder.from(options), payload, peerAddress, transContext); + } + public static class Builder { private final Method method; private Opaque token = Opaque.EMPTY; @@ -201,6 +209,15 @@ private Builder(Method method, String uriPath) { this.options.uriPath(uriPath); } + private Builder(Method method, Opaque token, CoapOptionsBuilder options, Opaque payload, InetSocketAddress peerAddress, TransportContext transContext) { + this.method = method; + this.token = token; + this.options = options; + this.payload = payload; + this.peerAddress = peerAddress; + this.transContext = transContext; + } + public CoapRequest build() { return new CoapRequest(method, token, options.build(), payload, peerAddress, transContext); } diff --git a/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java b/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java index a9c8b041..ed924cc3 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java +++ b/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java @@ -17,7 +17,6 @@ import com.mbed.coap.packet.CoapRequest; import com.mbed.coap.packet.CoapResponse; -import com.mbed.coap.packet.Opaque; import com.mbed.coap.utils.Filter; import com.mbed.coap.utils.Service; import java.util.concurrent.CompletableFuture; @@ -41,7 +40,7 @@ public CompletableFuture apply(CoapRequest req, Service apply(CoapRequest request, Service adjustPayloadSize(coapRequest, resp)); diff --git a/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseTransfer.java b/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseTransfer.java index 182d031e..1d580163 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseTransfer.java +++ b/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseTransfer.java @@ -42,8 +42,8 @@ static CoapRequest createFirstBlock(CoapRequest request, Capabilities csm) { BlockOption blockOption = new BlockOption(0, csm.getBlockSize(), true); int maxBlockPayload = csm.getMaxOutboundPayloadSize(); - return request - .withOptions(o -> o + return request.modify() + .options(o -> o .block1Req(blockOption) .unsetBlock2Res() .size1(payloadSize) @@ -52,9 +52,10 @@ static CoapRequest createFirstBlock(CoapRequest request, Capabilities csm) { o.requestTag(csm.nextRequestTag()) ) ) - .withPayload( + .payload( createBlockPart(blockOption, request.getPayload(), maxBlockPayload) - ); + ) + .build(); } static Opaque createBlockPart(BlockOption blockOption, Opaque fullPayload, int maxPayloadSizePerBlock) { diff --git a/coap-core/src/main/java/com/mbed/coap/server/filter/EchoFilter.java b/coap-core/src/main/java/com/mbed/coap/server/filter/EchoFilter.java index 4bdb201f..072ca735 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/filter/EchoFilter.java +++ b/coap-core/src/main/java/com/mbed/coap/server/filter/EchoFilter.java @@ -33,7 +33,7 @@ public CompletableFuture apply(CoapRequest request, Service it.echo(resp.options().getEcho())); + CoapRequest requestWithEcho = request.modify().options(it -> it.echo(resp.options().getEcho())).build(); return service.apply(requestWithEcho); } else { return completedFuture(resp); diff --git a/coap-core/src/main/java/com/mbed/coap/server/filter/TokenGeneratorFilter.java b/coap-core/src/main/java/com/mbed/coap/server/filter/TokenGeneratorFilter.java index afd55d83..96b7491a 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/filter/TokenGeneratorFilter.java +++ b/coap-core/src/main/java/com/mbed/coap/server/filter/TokenGeneratorFilter.java @@ -48,7 +48,7 @@ public TokenGeneratorFilter(Supplier tokenGenerator) { @Override public CompletableFuture apply(CoapRequest request, Service service) { if (!request.isPing() && request.getToken().isEmpty()) { - return service.apply(request.withToken(tokenGenerator.get())); + return service.apply(request.modify().token(tokenGenerator.get()).build()); } return service.apply(request); diff --git a/coap-core/src/test/java/com/mbed/coap/packet/CoapRequestTest.java b/coap-core/src/test/java/com/mbed/coap/packet/CoapRequestTest.java index 90e18069..53aa58d4 100644 --- a/coap-core/src/test/java/com/mbed/coap/packet/CoapRequestTest.java +++ b/coap-core/src/test/java/com/mbed/coap/packet/CoapRequestTest.java @@ -26,6 +26,8 @@ import static com.mbed.coap.packet.MediaTypes.CT_APPLICATION_JSON; import static com.mbed.coap.packet.Opaque.EMPTY; import static com.mbed.coap.packet.Opaque.decodeHex; +import static com.mbed.coap.transport.TransportContext.RESPONSE_TIMEOUT; +import static java.time.Duration.ofSeconds; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; @@ -41,6 +43,7 @@ import org.junit.jupiter.api.Test; class CoapRequestTest { + private static final TransportContext.Key DUMMY_KEY = new TransportContext.Key<>(null); @Test void shouldCreatePing() { @@ -87,6 +90,19 @@ void testToString() { assertEquals("CoapRequest[PING]", CoapRequest.ping(LOCAL_5683, TransportContext.EMPTY).toString()); } + @Test + void shouldModifyTransportContext() { + CoapRequest request = CoapRequest.delete("/test").token(1023).build(); + + // when + CoapRequest request2 = request.modify() + .context(DUMMY_KEY, "test") + .build(); + + // then + assertEquals("test", request2.getTransContext(DUMMY_KEY)); + } + @Test public void equalsAndHashTest() { EqualsVerifier.forClass(CoapRequest.class).suppress(Warning.NONFINAL_FIELDS) @@ -115,9 +131,17 @@ public void buildWithAllPossibleFields() { .size1(342) .observe() .payload("perse", MediaTypes.CT_TEXT_PLAIN) + .context(RESPONSE_TIMEOUT, ofSeconds(12)) + .context(DUMMY_KEY, "test") .from(LOCAL_5683); - CoapRequest expected = new CoapRequest(Method.GET, Opaque.ofBytes(0xB1, 0x97), new HeaderOptions(), Opaque.of("perse"), LOCAL_5683, TransportContext.EMPTY); + CoapRequest expected = new CoapRequest( + Method.GET, + Opaque.ofBytes(0xB1, 0x97), + new HeaderOptions(), Opaque.of("perse"), + LOCAL_5683, + TransportContext.of(RESPONSE_TIMEOUT, ofSeconds(12)).with(DUMMY_KEY, "test") + ); expected.options().setUriPath("/0/1/2"); expected.options().setAccept(CT_APPLICATION_JSON); expected.options().setObserve(0);