Skip to content

Commit

Permalink
CoapServerBuilder: added routeFilter method
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Jun 13, 2023
1 parent faca9d0 commit c16839e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public final class CoapServerBuilder {
private Service<CoapRequest, CoapResponse> route = RouterService.NOT_FOUND_SERVICE;
private int maxQueueSize = 100;
private Filter.SimpleFilter<CoapRequest, CoapResponse> outboundFilter = Filter.identity();
private Filter.SimpleFilter<CoapRequest, CoapResponse> routeFilter = Filter.identity();

CoapServerBuilder() {
}
Expand All @@ -97,6 +98,11 @@ public CoapServerBuilder route(RouterService.RouteBuilder routeBuilder) {
return route(routeBuilder.build());
}

public CoapServerBuilder routeFilter(Filter.SimpleFilter<CoapRequest, CoapResponse> routeFilter) {
this.routeFilter = requireNonNull(routeFilter);
return this;
}

public CoapServerBuilder outboundFilter(Filter.SimpleFilter<CoapRequest, CoapResponse> outboundFilter) {
this.outboundFilter = requireNonNull(outboundFilter);
return this;
Expand Down Expand Up @@ -227,6 +233,7 @@ public CoapServer build() {
.andThen(new CriticalOptionVerifier())
.andThen(new ObservationSenderFilter(sendNotification))
.andThen(new BlockWiseIncomingFilter(capabilities(), maxIncomingBlockTransferSize))
.andThen(routeFilter)
.then(route);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static RouteBuilder builder() {
return new RouteBuilder();
}

RouterService(Map<RequestMatcher, Service<CoapRequest, CoapResponse>> handlers) {
private RouterService(Map<RequestMatcher, Service<CoapRequest, CoapResponse>> handlers) {

this.handlers = unmodifiableMap(
handlers.entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mbed.coap.packet.CoapResponse;
import com.mbed.coap.server.CoapServer;
import com.mbed.coap.server.filter.TokenGeneratorFilter;
import com.mbed.coap.utils.Filter;
import com.mbed.coap.utils.Service;
import java.io.IOException;

Expand All @@ -38,10 +39,11 @@ protected CoapClient buildClient(int port) throws IOException {
}

@Override
protected CoapServer buildServer(int port, Service<CoapRequest, CoapResponse> route) {
protected CoapServer buildServer(int port, Filter.SimpleFilter<CoapRequest, CoapResponse> routeFilter, Service<CoapRequest, CoapResponse> route) {
return CoapServer.builder()
.blockSize(BlockSize.S_1024)
.transport(udp(port))
.routeFilter(routeFilter)
.route(route)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.mbed.coap.server.ObservableResourceService;
import com.mbed.coap.server.RouterService;
import com.mbed.coap.utils.Bytes;
import com.mbed.coap.utils.Filter;
import com.mbed.coap.utils.Service;
import java.io.IOException;
import java.text.ParseException;
Expand Down Expand Up @@ -102,15 +103,15 @@ public void setUp() throws IOException {
completedFuture(CoapResponse.ok("<test/1>,<test2>", MediaTypes.CT_APPLICATION_LINK__FORMAT))
).build();

server = buildServer(0, route).start();
server = buildServer(0, IntegrationTestBase::routeFilter, route).start();

int port = server.getLocalSocketAddress().getPort();
client = buildClient(port);
}

abstract protected CoapClient buildClient(int port) throws IOException;

abstract protected CoapServer buildServer(int port, Service<CoapRequest, CoapResponse> route) throws IOException;
abstract protected CoapServer buildServer(int port, Filter.SimpleFilter<CoapRequest, CoapResponse> routeFilter, Service<CoapRequest, CoapResponse> route) throws IOException;

@AfterEach
public void tearDown() throws IOException {
Expand Down Expand Up @@ -290,6 +291,21 @@ public void should_handle_exception() throws Exception {
assertEquals(CoapResponse.of(Code.C500_INTERNAL_SERVER_ERROR), client.sendSync(get("/exception")));
}

@Test
public void should_return_by_route_filter() throws Exception {
CoapResponse resp = client.sendSync(get("/route-filter"));

assertEquals(CoapResponse.ok("Intercepted"), resp);
}


static CompletableFuture<CoapResponse> routeFilter(CoapRequest request, Service<CoapRequest, CoapResponse> service) {
if ("/route-filter".equals(request.options().getUriPath())) {
return CompletableFuture.completedFuture(CoapResponse.ok("Intercepted"));
}
return service.apply(request);
}

private static class TestResource implements Service<CoapRequest, CoapResponse> {

private Opaque payload = of("Dziala2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void setUp() throws IOException {
dtlsServer = DtlsServerTransport.create(serverConf);
coapServer = CoapServer.builder()
.transport(new MbedtlsCoapTransport(dtlsServer))
.route(new RouterService.RouteBuilder()
.route(RouterService.builder()
.get("/test", it -> completedFuture(ok("OK!")))
.post("/send-malformed", it -> {
dtlsServer.send(new Packet<>("acghfh", it.getPeerAddress()).map(MbedtlsCoapTransportTest::toByteBuffer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class CoapServerBuilderForTcp {
private int maxQueueSize = 100;
private BlockSize blockSize;
private Filter.SimpleFilter<CoapRequest, CoapResponse> outboundFilter = Filter.identity();
private Filter.SimpleFilter<CoapRequest, CoapResponse> routeFilter = Filter.identity();

CoapServerBuilderForTcp() {
csmStorage = new CapabilitiesStorageImpl();
Expand Down Expand Up @@ -81,6 +82,11 @@ public CoapServerBuilderForTcp route(RouterService.RouteBuilder routeBuilder) {
return route(routeBuilder.build());
}

public CoapServerBuilderForTcp routeFilter(Filter.SimpleFilter<CoapRequest, CoapResponse> routeFilter) {
this.routeFilter = requireNonNull(routeFilter);
return this;
}

public CoapServerBuilderForTcp csmStorage(CapabilitiesStorage csmStorage) {
this.csmStorage = csmStorage;
return this;
Expand Down Expand Up @@ -128,6 +134,7 @@ public CoapServer build() {
.andThenIf(hasRoute(), new CriticalOptionVerifier())
.andThenIf(hasRoute(), new ObservationSenderFilter(sendNotification))
.andThenIf(hasRoute(), new BlockWiseIncomingFilter(capabilities(), maxIncomingBlockTransferSize))
.andThen(routeFilter)
.then(route);

// OUTBOUND
Expand Down
6 changes: 4 additions & 2 deletions coap-tcp/src/test/java/protocolTests/TcpIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.mbed.coap.server.TcpCoapServer;
import com.mbed.coap.transport.javassl.SingleConnectionSocketServerTransport;
import com.mbed.coap.transport.javassl.SocketClientTransport;
import com.mbed.coap.utils.Filter;
import com.mbed.coap.utils.Service;
import java.io.IOException;
import java.net.InetAddress;
Expand All @@ -38,11 +39,12 @@
public class TcpIntegrationTest extends IntegrationTestBase {

@Override
protected CoapServer buildServer(int port, Service<CoapRequest, CoapResponse> route) throws IOException {
protected CoapServer buildServer(int port, Filter.SimpleFilter<CoapRequest, CoapResponse> routeFilter, Service<CoapRequest, CoapResponse> route) throws IOException {
return TcpCoapServer.builder()
.transport(new SingleConnectionSocketServerTransport(port))
.blockSize(BlockSize.S_1024_BERT)
.maxMessageSize(100_000)
.routeFilter(routeFilter)
.route(route)
.build();
}
Expand Down Expand Up @@ -73,7 +75,7 @@ public void reconnect() throws Exception {

server.stop();

server = buildServer(port, RouterService.builder().build()).start();
server = buildServer(port, Filter.identity(), RouterService.builder().build()).start();

await().ignoreExceptions().untilAsserted(() ->
assertTrue(client.ping().get())
Expand Down

0 comments on commit c16839e

Please sign in to comment.