Skip to content

Commit

Permalink
Clean CoAP root resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 20, 2019
1 parent 91f4a83 commit 43c3167
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public LeshanClient(String endpoint, InetSocketAddress localAddress,
@Override
protected Resource createRoot() {
// Use to handle Delete on "/"
return new org.eclipse.leshan.client.californium.impl.RootResource(bootstrapHandler);
return new org.eclipse.leshan.client.californium.impl.RootResource(bootstrapHandler, this);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toCoapResponseCode;

import java.util.List;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.leshan.client.request.ServerIdentity;
import org.eclipse.leshan.client.servers.BootstrapHandler;
Expand All @@ -28,13 +32,21 @@
import org.eclipse.leshan.util.StringUtils;

/**
* A {@link CoapResource} resource in charge of handling Bootstrap Delete requests targeting the "/" URI.
* A root {@link CoapResource} resource in charge of handling Bootstrap Delete requests targeting the "/" URI.
*/
public class RootResource extends LwM2mClientCoapResource {

public RootResource(BootstrapHandler bootstrapHandler) {
private CoapServer coapServer;

public RootResource(BootstrapHandler bootstrapHandler, CoapServer coapServer) {
super("", bootstrapHandler);
setVisible(false);
this.coapServer = coapServer;
}

@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(ResponseCode.NOT_FOUND);
}

@Override
Expand All @@ -48,4 +60,9 @@ public void handleDELETE(CoapExchange exchange) {
BootstrapDeleteResponse response = bootstrapHandler.delete(identity, new BootstrapDeleteRequest());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}

@Override
public List<Endpoint> getEndpoints() {
return coapServer.getEndpoints();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.core.server.resources.Resource;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.BootstrapHandler;
import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory;
import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapRequestSender;
import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapServer;
import org.eclipse.leshan.server.security.BootstrapSecurityStore;
Expand All @@ -53,10 +54,10 @@ public class LeshanBootstrapServer implements LwM2mBootstrapServer {
private final BootstrapConfigStore bsStore;
private final BootstrapSecurityStore bsSecurityStore;

public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint, BootstrapConfigStore bsStore,
BootstrapSecurityStore bsSecurityStore, BootstrapSessionManager bsSessionManager,
BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model, NetworkConfig coapConfig,
LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {
public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint,
BootstrapConfigStore bsStore, BootstrapSecurityStore bsSecurityStore,
BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model,
NetworkConfig coapConfig, LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {

Validate.notNull(bsStore, "bootstrap store must not be null");
Validate.notNull(bsSessionManager, "session manager must not be null");
Expand Down Expand Up @@ -90,7 +91,12 @@ public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint secure
}

protected CoapServer createCoapServer(NetworkConfig coapConfig) {
return new CoapServer(coapConfig);
return new CoapServer(coapConfig) {
@Override
protected Resource createRoot() {
return new RootResource(this);
}
};
}

protected LwM2mBootstrapRequestSender createRequestSender(Endpoint securedEndpoint, Endpoint unsecuredEndpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.core.server.resources.Resource;
import org.eclipse.leshan.core.californium.CoapResponseCallback;
import org.eclipse.leshan.core.node.codec.CodecException;
Expand Down Expand Up @@ -179,7 +175,7 @@ public void registered(Registration registration, Registration previousReg,
coapServer = new CoapServer(coapConfig) {
@Override
protected Resource createRoot() {
return new RootResource();
return new RootResource(this);
}
};

Expand Down Expand Up @@ -502,24 +498,4 @@ public void send(Registration destination, Request request, long timeout, CoapRe
public CoapServer getCoapServer() {
return coapServer;
}

/**
* The Leshan Root Resource.
*/
private class RootResource extends CoapResource {

public RootResource() {
super("");
}

@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(ResponseCode.NOT_FOUND);
}

@Override
public List<Endpoint> getEndpoints() {
return coapServer.getEndpoints();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2019 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.californium.impl;

import java.util.List;

import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.server.resources.CoapExchange;

/**
* A default root resource.
*/
public class RootResource extends CoapResource {

private CoapServer coapServer;

public RootResource(CoapServer coapServer) {
super("");
setVisible(false);
this.coapServer = coapServer;
}

@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(ResponseCode.NOT_FOUND);
}

@Override
public List<Endpoint> getEndpoints() {
return coapServer.getEndpoints();
}
}

0 comments on commit 43c3167

Please sign in to comment.