Skip to content

Commit ef8809f

Browse files
committed
GH-73 Add modify method to CoapRequest, depricated CoapRequest.with* methods
1 parent fa79e86 commit ef8809f

File tree

8 files changed

+53
-12
lines changed

8 files changed

+53
-12
lines changed

coap-core/src/main/java/com/mbed/coap/client/CoapClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public static CoapClient create(InetSocketAddress target, CoapServer server, Fun
6060
}
6161

6262
public CompletableFuture<CoapResponse> send(CoapRequest request) {
63-
return clientService.apply(request.withAddress(destination));
63+
return send(request.modify());
6464
}
6565

6666
public CompletableFuture<CoapResponse> send(CoapRequest.Builder request) {
67-
return send(request.build());
67+
return clientService.apply(request.address(destination).build());
6868
}
6969

7070
public CoapResponse sendSync(CoapRequest request) throws CoapException {

coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,32 @@ public String toString() {
169169

170170

171171
// --- MODIFIERS ---
172+
@Deprecated
172173
public CoapRequest withToken(Opaque newToken) {
173174
return new CoapRequest(method, newToken, options, payload, peerAddress, transContext);
174175
}
175176

177+
@Deprecated
176178
public CoapRequest withOptions(Consumer<CoapOptionsBuilder> optionsFunc) {
177179
CoapOptionsBuilder optionsBuilder = CoapOptionsBuilder.from(options);
178180
optionsFunc.accept(optionsBuilder);
179181
return new CoapRequest(method, token, optionsBuilder.build(), payload, peerAddress, transContext);
180182
}
181183

184+
@Deprecated
182185
public CoapRequest withPayload(Opaque newPayload) {
183186
return new CoapRequest(method, token, options, newPayload, peerAddress, transContext);
184187
}
185188

189+
@Deprecated
186190
public CoapRequest withAddress(InetSocketAddress newPeerAddress) {
187191
return new CoapRequest(method, token, options, payload, newPeerAddress, transContext);
188192
}
189193

194+
public Builder modify() {
195+
return new Builder(method, token, CoapOptionsBuilder.from(options), payload, peerAddress, transContext);
196+
}
197+
190198
public static class Builder {
191199
private final Method method;
192200
private Opaque token = Opaque.EMPTY;
@@ -201,6 +209,15 @@ private Builder(Method method, String uriPath) {
201209
this.options.uriPath(uriPath);
202210
}
203211

212+
private Builder(Method method, Opaque token, CoapOptionsBuilder options, Opaque payload, InetSocketAddress peerAddress, TransportContext transContext) {
213+
this.method = method;
214+
this.token = token;
215+
this.options = options;
216+
this.payload = payload;
217+
this.peerAddress = peerAddress;
218+
this.transContext = transContext;
219+
}
220+
204221
public CoapRequest build() {
205222
return new CoapRequest(method, token, options.build(), payload, peerAddress, transContext);
206223
}

coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.mbed.coap.packet.CoapRequest;
1919
import com.mbed.coap.packet.CoapResponse;
20-
import com.mbed.coap.packet.Opaque;
2120
import com.mbed.coap.utils.Filter;
2221
import com.mbed.coap.utils.Service;
2322
import java.util.concurrent.CompletableFuture;
@@ -41,7 +40,7 @@ public CompletableFuture<CoapResponse> apply(CoapRequest req, Service<CoapReques
4140

4241
CoapRequest obsReq;
4342
if (req.getToken().isEmpty()) {
44-
obsReq = req.withToken(Opaque.variableUInt(nextToken.incrementAndGet()));
43+
obsReq = req.modify().token(nextToken.incrementAndGet()).build();
4544
} else {
4645
obsReq = req;
4746
}

coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseIncomingFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRe
8585
removeBlockRequest(blockRequestId);
8686

8787
//last block received
88-
final CoapRequest coapRequest = request.withPayload(blockRequest.getCombinedPayload());
88+
final CoapRequest coapRequest = request.modify().payload(blockRequest.getCombinedPayload()).build();
8989
return service
9090
.apply(coapRequest)
9191
.thenApply(resp -> adjustPayloadSize(coapRequest, resp));

coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseTransfer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ static CoapRequest createFirstBlock(CoapRequest request, Capabilities csm) {
4242
BlockOption blockOption = new BlockOption(0, csm.getBlockSize(), true);
4343
int maxBlockPayload = csm.getMaxOutboundPayloadSize();
4444

45-
return request
46-
.withOptions(o -> o
45+
return request.modify()
46+
.options(o -> o
4747
.block1Req(blockOption)
4848
.unsetBlock2Res()
4949
.size1(payloadSize)
@@ -52,9 +52,10 @@ static CoapRequest createFirstBlock(CoapRequest request, Capabilities csm) {
5252
o.requestTag(csm.nextRequestTag())
5353
)
5454
)
55-
.withPayload(
55+
.payload(
5656
createBlockPart(blockOption, request.getPayload(), maxBlockPayload)
57-
);
57+
)
58+
.build();
5859
}
5960

6061
static Opaque createBlockPart(BlockOption blockOption, Opaque fullPayload, int maxPayloadSizePerBlock) {

coap-core/src/main/java/com/mbed/coap/server/filter/EchoFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRe
3333
if (resp.getCode() == Code.C401_UNAUTHORIZED && resp.options().getEcho() != null) {
3434
// server required freshness verification, retry with echo
3535

36-
CoapRequest requestWithEcho = request.withOptions(it -> it.echo(resp.options().getEcho()));
36+
CoapRequest requestWithEcho = request.modify().options(it -> it.echo(resp.options().getEcho())).build();
3737
return service.apply(requestWithEcho);
3838
} else {
3939
return completedFuture(resp);

coap-core/src/main/java/com/mbed/coap/server/filter/TokenGeneratorFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TokenGeneratorFilter(Supplier<Opaque> tokenGenerator) {
4848
@Override
4949
public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRequest, CoapResponse> service) {
5050
if (!request.isPing() && request.getToken().isEmpty()) {
51-
return service.apply(request.withToken(tokenGenerator.get()));
51+
return service.apply(request.modify().token(tokenGenerator.get()).build());
5252
}
5353

5454
return service.apply(request);

coap-core/src/test/java/com/mbed/coap/packet/CoapRequestTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import static com.mbed.coap.packet.MediaTypes.CT_APPLICATION_JSON;
2727
import static com.mbed.coap.packet.Opaque.EMPTY;
2828
import static com.mbed.coap.packet.Opaque.decodeHex;
29+
import static com.mbed.coap.transport.TransportContext.RESPONSE_TIMEOUT;
30+
import static java.time.Duration.ofSeconds;
2931
import static org.junit.jupiter.api.Assertions.assertEquals;
3032
import static org.junit.jupiter.api.Assertions.assertFalse;
3133
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -41,6 +43,7 @@
4143
import org.junit.jupiter.api.Test;
4244

4345
class CoapRequestTest {
46+
private static final TransportContext.Key<String> DUMMY_KEY = new TransportContext.Key<>(null);
4447

4548
@Test
4649
void shouldCreatePing() {
@@ -87,6 +90,19 @@ void testToString() {
8790
assertEquals("CoapRequest[PING]", CoapRequest.ping(LOCAL_5683, TransportContext.EMPTY).toString());
8891
}
8992

93+
@Test
94+
void shouldModifyTransportContext() {
95+
CoapRequest request = CoapRequest.delete("/test").token(1023).build();
96+
97+
// when
98+
CoapRequest request2 = request.modify()
99+
.context(DUMMY_KEY, "test")
100+
.build();
101+
102+
// then
103+
assertEquals("test", request2.getTransContext(DUMMY_KEY));
104+
}
105+
90106
@Test
91107
public void equalsAndHashTest() {
92108
EqualsVerifier.forClass(CoapRequest.class).suppress(Warning.NONFINAL_FIELDS)
@@ -115,9 +131,17 @@ public void buildWithAllPossibleFields() {
115131
.size1(342)
116132
.observe()
117133
.payload("perse", MediaTypes.CT_TEXT_PLAIN)
134+
.context(RESPONSE_TIMEOUT, ofSeconds(12))
135+
.context(DUMMY_KEY, "test")
118136
.from(LOCAL_5683);
119137

120-
CoapRequest expected = new CoapRequest(Method.GET, Opaque.ofBytes(0xB1, 0x97), new HeaderOptions(), Opaque.of("perse"), LOCAL_5683, TransportContext.EMPTY);
138+
CoapRequest expected = new CoapRequest(
139+
Method.GET,
140+
Opaque.ofBytes(0xB1, 0x97),
141+
new HeaderOptions(), Opaque.of("perse"),
142+
LOCAL_5683,
143+
TransportContext.of(RESPONSE_TIMEOUT, ofSeconds(12)).with(DUMMY_KEY, "test")
144+
);
121145
expected.options().setUriPath("/0/1/2");
122146
expected.options().setAccept(CT_APPLICATION_JSON);
123147
expected.options().setObserve(0);

0 commit comments

Comments
 (0)