Search in sources :

Example 1 with BindingMode

use of org.eclipse.leshan.core.request.BindingMode 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 BindingMode

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

the class CoapRequestBuilder method visit.

@Override
public void visit(RegisterRequest request) {
    coapRequest = Request.newPost();
    buildRequestSettings();
    coapRequest.getOptions().setContentFormat(ContentFormat.LINK.getCode());
    coapRequest.getOptions().addUriPath("rd");
    HashMap<String, String> attributes = new HashMap<>();
    attributes.putAll(request.getAdditionalAttributes());
    attributes.put("ep", request.getEndpointName());
    Long lifetime = request.getLifetime();
    if (lifetime != null)
        attributes.put("lt", lifetime.toString());
    String smsNumber = request.getSmsNumber();
    if (smsNumber != null)
        attributes.put("sms", smsNumber);
    String lwVersion = request.getLwVersion();
    if (lwVersion != null)
        attributes.put("lwm2m", lwVersion);
    BindingMode bindingMode = request.getBindingMode();
    if (bindingMode != null)
        attributes.put("b", bindingMode.toString());
    for (Entry<String, String> attr : attributes.entrySet()) {
        coapRequest.getOptions().addUriQuery(attr.getKey() + "=" + attr.getValue());
    }
    Link[] objectLinks = request.getObjectLinks();
    if (objectLinks != null)
        coapRequest.setPayload(Link.serialize(objectLinks));
}
Also used : HashMap(java.util.HashMap) BindingMode(org.eclipse.leshan.core.request.BindingMode) Link(org.eclipse.leshan.Link)

Example 3 with BindingMode

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

the class CoapRequestBuilder method visit.

@Override
public void visit(UpdateRequest request) {
    coapRequest = Request.newPost();
    buildRequestSettings();
    coapRequest.getOptions().setUriPath(request.getRegistrationId());
    Long lifetime = request.getLifeTimeInSec();
    if (lifetime != null)
        coapRequest.getOptions().addUriQuery("lt=" + lifetime);
    String smsNumber = request.getSmsNumber();
    if (smsNumber != null)
        coapRequest.getOptions().addUriQuery("sms=" + smsNumber);
    BindingMode bindingMode = request.getBindingMode();
    if (bindingMode != null)
        coapRequest.getOptions().addUriQuery("b=" + bindingMode.toString());
    Link[] linkObjects = request.getObjectLinks();
    if (linkObjects != null) {
        coapRequest.getOptions().setContentFormat(ContentFormat.LINK.getCode());
        coapRequest.setPayload(Link.serialize(linkObjects));
    }
}
Also used : BindingMode(org.eclipse.leshan.core.request.BindingMode) Link(org.eclipse.leshan.Link)

Example 4 with BindingMode

use of org.eclipse.leshan.core.request.BindingMode 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 5 with BindingMode

use of org.eclipse.leshan.core.request.BindingMode 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)

Aggregations

Link (org.eclipse.leshan.Link)6 BindingMode (org.eclipse.leshan.core.request.BindingMode)6 HashMap (java.util.HashMap)4 Identity (org.eclipse.leshan.core.request.Identity)4 EndpointContextUtil.extractIdentity (org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity)2 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 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)1 UpdateRequest (org.eclipse.leshan.core.request.UpdateRequest)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