Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-73 Add context modifier to CoapRequest #74

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions coap-core/src/main/java/com/mbed/coap/client/CoapClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public static CoapClient create(InetSocketAddress target, CoapServer server, Fun
}

public CompletableFuture<CoapResponse> send(CoapRequest request) {
return clientService.apply(request.withAddress(destination));
return send(request.modify());
}

public CompletableFuture<CoapResponse> send(CoapRequest.Builder request) {
return send(request.build());
return clientService.apply(request.address(destination).build());
}

public CoapResponse sendSync(CoapRequest request) throws CoapException {
Expand Down
17 changes: 17 additions & 0 deletions coap-core/src/main/java/com/mbed/coap/packet/CoapRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoapOptionsBuilder> 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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,7 +40,7 @@ public CompletableFuture<CoapResponse> apply(CoapRequest req, Service<CoapReques

CoapRequest obsReq;
if (req.getToken().isEmpty()) {
obsReq = req.withToken(Opaque.variableUInt(nextToken.incrementAndGet()));
obsReq = req.modify().token(nextToken.incrementAndGet()).build();
} else {
obsReq = req;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRe
removeBlockRequest(blockRequestId);

//last block received
final CoapRequest coapRequest = request.withPayload(blockRequest.getCombinedPayload());
final CoapRequest coapRequest = request.modify().payload(blockRequest.getCombinedPayload()).build();
return service
.apply(coapRequest)
.thenApply(resp -> adjustPayloadSize(coapRequest, resp));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRe
if (resp.getCode() == Code.C401_UNAUTHORIZED && resp.options().getEcho() != null) {
// server required freshness verification, retry with echo

CoapRequest requestWithEcho = request.withOptions(it -> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TokenGeneratorFilter(Supplier<Opaque> tokenGenerator) {
@Override
public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRequest, CoapResponse> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +43,7 @@
import org.junit.jupiter.api.Test;

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

@Test
void shouldCreatePing() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down