/
RootResource.java
68 lines (58 loc) · 2.63 KB
/
RootResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
* Copyright (c) 2015 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
* Achim Kraus (Bosch Software Innovations GmbH) - use ServerIdentity
*******************************************************************************/
package org.eclipse.leshan.client.californium.impl;
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;
import org.eclipse.leshan.core.request.BootstrapDeleteRequest;
import org.eclipse.leshan.core.response.BootstrapDeleteResponse;
import org.eclipse.leshan.util.StringUtils;
/**
* A root {@link CoapResource} resource in charge of handling Bootstrap Delete requests targeting the "/" URI.
*/
public class RootResource extends LwM2mClientCoapResource {
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
public void handleDELETE(CoapExchange exchange) {
if (!StringUtils.isEmpty(exchange.getRequestOptions().getUriPathString())) {
exchange.respond(ResponseCode.METHOD_NOT_ALLOWED);
return;
}
ServerIdentity identity = extractServerIdentity(exchange);
BootstrapDeleteResponse response = bootstrapHandler.delete(identity, new BootstrapDeleteRequest());
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}
@Override
public List<Endpoint> getEndpoints() {
return coapServer.getEndpoints();
}
}