Search in sources :

Example 1 with Identity

use of org.eclipse.leshan.core.request.Identity in project leshan by eclipse.

the class RegistrationUpdateSerDes method deserialize.

public static RegistrationUpdate deserialize(byte[] data) throws UnknownHostException {
    JsonObject v = (JsonObject) Json.parse(new String(data));
    // mandatory fields
    String regId = v.getString("regId", null);
    Identity identity = IdentitySerDes.deserialize(v.get("identity").asObject());
    // optional fields
    BindingMode b = null;
    if (v.get("bnd") != null) {
        b = BindingMode.valueOf(v.getString("bnd", null));
    }
    Long lifetime = null;
    if (v.get("lt") != null) {
        lifetime = v.getLong("lt", 0);
    }
    String sms = null;
    if (v.get("sms") != null) {
        sms = v.getString("sms", "");
    }
    // parse object link
    JsonArray links = (JsonArray) v.get("objLink");
    Link[] linkObjs = null;
    if (links != null) {
        linkObjs = new Link[links.size()];
        for (int i = 0; i < links.size(); i++) {
            JsonObject ol = (JsonObject) links.get(i);
            Map<String, Object> attMap = new HashMap<>();
            JsonObject att = (JsonObject) ol.get("at");
            for (String k : att.names()) {
                JsonValue jsonValue = att.get(k);
                if (jsonValue.isNull()) {
                    attMap.put(k, null);
                } else if (jsonValue.isNumber()) {
                    attMap.put(k, jsonValue.asInt());
                } else {
                    attMap.put(k, jsonValue.asString());
                }
            }
            Link o = new Link(ol.getString("url", null), attMap);
            linkObjs[i] = o;
        }
    }
    Map<String, String> addAttr = new HashMap<>();
    JsonObject o = (JsonObject) v.get("addAttr");
    for (String k : o.names()) {
        addAttr.put(k, o.getString(k, ""));
    }
    return new RegistrationUpdate(regId, identity, lifetime, sms, b, linkObjs, addAttr);
}
Also used : HashMap(java.util.HashMap) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) BindingMode(org.eclipse.leshan.core.request.BindingMode) JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) Identity(org.eclipse.leshan.core.request.Identity) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Link(org.eclipse.leshan.Link)

Example 2 with Identity

use of org.eclipse.leshan.core.request.Identity in project leshan by eclipse.

the class RegisterResource method handleUpdate.

private void handleUpdate(CoapExchange exchange, Request request, String registrationId) {
    // Get identity
    Identity sender = extractIdentity(request.getSourceContext());
    // Create LwM2m request from CoAP request
    Long lifetime = null;
    String smsNumber = null;
    BindingMode binding = null;
    Link[] objectLinks = null;
    Map<String, String> additionalParams = new HashMap<>();
    for (String param : request.getOptions().getUriQuery()) {
        if (param.startsWith(QUERY_PARAM_LIFETIME)) {
            lifetime = Long.valueOf(param.substring(3));
        } else if (param.startsWith(QUERY_PARAM_SMS)) {
            smsNumber = param.substring(4);
        } else if (param.startsWith(QUERY_PARAM_BINDING_MODE)) {
            binding = BindingMode.valueOf(param.substring(2));
        } else {
            String[] tokens = param.split("\\=");
            if (tokens != null && tokens.length == 2) {
                additionalParams.put(tokens[0], tokens[1]);
            }
        }
    }
    if (request.getPayload() != null && request.getPayload().length > 0) {
        objectLinks = Link.parse(request.getPayload());
    }
    UpdateRequest updateRequest = new UpdateRequest(registrationId, lifetime, smsNumber, binding, objectLinks, additionalParams);
    // Handle request
    final SendableResponse<UpdateResponse> sendableResponse = registrationHandler.update(sender, updateRequest);
    UpdateResponse updateResponse = sendableResponse.getResponse();
    // Create CoAP Response from LwM2m request
    if (updateResponse.getCode().isError()) {
        exchange.respond(toCoapResponseCode(updateResponse.getCode()), updateResponse.getErrorMessage());
    } else {
        exchange.respond(toCoapResponseCode(updateResponse.getCode()));
    }
    sendableResponse.sent();
}
Also used : UpdateResponse(org.eclipse.leshan.core.response.UpdateResponse) HashMap(java.util.HashMap) UpdateRequest(org.eclipse.leshan.core.request.UpdateRequest) Identity(org.eclipse.leshan.core.request.Identity) EndpointContextUtil.extractIdentity(org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity) BindingMode(org.eclipse.leshan.core.request.BindingMode) Link(org.eclipse.leshan.Link)

Example 3 with Identity

use of org.eclipse.leshan.core.request.Identity in project leshan by eclipse.

the class BootstrapResource method handlePOST.

@Override
public void handlePOST(CoapExchange exchange) {
    Request request = exchange.advanced().getRequest();
    LOG.debug("POST received : {}", request);
    // messages
    if (!Type.CON.equals(request.getType())) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return;
    }
    // which endpoint?
    String endpoint = null;
    for (String param : request.getOptions().getUriQuery()) {
        if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
            endpoint = param.substring(QUERY_PARAM_ENDPOINT.length());
            break;
        }
    }
    // Extract client identity
    Identity clientIdentity = EndpointContextUtil.extractIdentity(request.getSourceContext());
    // handle bootstrap request
    BootstrapResponse response = bootstrapHandler.bootstrap(clientIdentity, new BootstrapRequest(endpoint));
    if (response.isSuccess()) {
        exchange.respond(toCoapResponseCode(response.getCode()));
    } else {
        exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
    }
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) Request(org.eclipse.californium.core.coap.Request) BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) BootstrapResponse(org.eclipse.leshan.core.response.BootstrapResponse) Identity(org.eclipse.leshan.core.request.Identity)

Example 4 with Identity

use of org.eclipse.leshan.core.request.Identity in project leshan by eclipse.

the class RegisterResource method handleRegister.

private void handleRegister(CoapExchange exchange, Request request) {
    // Get identity
    // --------------------------------
    Identity sender = extractIdentity(request.getSourceContext());
    // Create LwM2m request from CoAP request
    // --------------------------------
    // We don't check content media type is APPLICATION LINK FORMAT for now as this is the only format we can expect
    String endpoint = null;
    Long lifetime = null;
    String smsNumber = null;
    String lwVersion = null;
    BindingMode binding = null;
    // Get object Links
    Link[] objectLinks = Link.parse(request.getPayload());
    Map<String, String> additionalParams = new HashMap<>();
    // Get parameters
    for (String param : request.getOptions().getUriQuery()) {
        if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
            endpoint = param.substring(3);
        } else if (param.startsWith(QUERY_PARAM_LIFETIME)) {
            lifetime = Long.valueOf(param.substring(3));
        } else if (param.startsWith(QUERY_PARAM_SMS)) {
            smsNumber = param.substring(4);
        } else if (param.startsWith(QUERY_PARAM_LWM2M_VERSION)) {
            lwVersion = param.substring(6);
        } else if (param.startsWith(QUERY_PARAM_BINDING_MODE)) {
            binding = BindingMode.valueOf(param.substring(2));
        } else {
            String[] tokens = param.split("\\=");
            if (tokens != null && tokens.length == 2) {
                additionalParams.put(tokens[0], tokens[1]);
            }
        }
    }
    // Create request
    RegisterRequest registerRequest = new RegisterRequest(endpoint, lifetime, lwVersion, binding, smsNumber, objectLinks, additionalParams);
    // Handle request
    // -------------------------------
    InetSocketAddress serverEndpoint = exchange.advanced().getEndpoint().getAddress();
    final SendableResponse<RegisterResponse> sendableResponse = registrationHandler.register(sender, registerRequest, serverEndpoint);
    RegisterResponse response = sendableResponse.getResponse();
    // -------------------------------
    if (response.getCode() == org.eclipse.leshan.ResponseCode.CREATED) {
        exchange.setLocationPath(RESOURCE_NAME + "/" + response.getRegistrationID());
        exchange.respond(ResponseCode.CREATED);
    } else {
        exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
    }
    sendableResponse.sent();
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) BindingMode(org.eclipse.leshan.core.request.BindingMode) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse) Identity(org.eclipse.leshan.core.request.Identity) EndpointContextUtil.extractIdentity(org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity) Link(org.eclipse.leshan.Link)

Example 5 with Identity

use of org.eclipse.leshan.core.request.Identity in project leshan by eclipse.

the class RegisterResource method handleDeregister.

private void handleDeregister(CoapExchange exchange, String registrationId) {
    // Get identity
    Identity sender = extractIdentity(exchange.advanced().getRequest().getSourceContext());
    // Create request
    DeregisterRequest deregisterRequest = new DeregisterRequest(registrationId);
    // Handle request
    final SendableResponse<DeregisterResponse> sendableResponse = registrationHandler.deregister(sender, deregisterRequest);
    DeregisterResponse deregisterResponse = sendableResponse.getResponse();
    // Create CoAP Response from LwM2m request
    if (deregisterResponse.getCode().isError()) {
        exchange.respond(toCoapResponseCode(deregisterResponse.getCode()), deregisterResponse.getErrorMessage());
    } else {
        exchange.respond(toCoapResponseCode(deregisterResponse.getCode()));
    }
    sendableResponse.sent();
}
Also used : DeregisterResponse(org.eclipse.leshan.core.response.DeregisterResponse) Identity(org.eclipse.leshan.core.request.Identity) EndpointContextUtil.extractIdentity(org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest)

Aggregations

Identity (org.eclipse.leshan.core.request.Identity)6 Link (org.eclipse.leshan.Link)4 BindingMode (org.eclipse.leshan.core.request.BindingMode)4 HashMap (java.util.HashMap)3 EndpointContextUtil.extractIdentity (org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity)3 JsonArray (com.eclipsesource.json.JsonArray)1 JsonObject (com.eclipsesource.json.JsonObject)1 JsonValue (com.eclipsesource.json.JsonValue)1 InetSocketAddress (java.net.InetSocketAddress)1 Date (java.util.Date)1 Request (org.eclipse.californium.core.coap.Request)1 BootstrapRequest (org.eclipse.leshan.core.request.BootstrapRequest)1 DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)1 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)1 UpdateRequest (org.eclipse.leshan.core.request.UpdateRequest)1 BootstrapResponse (org.eclipse.leshan.core.response.BootstrapResponse)1 DeregisterResponse (org.eclipse.leshan.core.response.DeregisterResponse)1 RegisterResponse (org.eclipse.leshan.core.response.RegisterResponse)1 UpdateResponse (org.eclipse.leshan.core.response.UpdateResponse)1 RegistrationUpdate (org.eclipse.leshan.server.registration.RegistrationUpdate)1