Search in sources :

Example 1 with BootstrapWriteRequest

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

the class ObjectResource method handlePUT.

@Override
public void handlePUT(CoapExchange coapExchange) {
    ServerIdentity identity = extractServerIdentity(coapExchange, bootstrapHandler);
    String URI = coapExchange.getRequestOptions().getUriPathString();
    // get Observe Spec
    ObserveSpec spec = null;
    if (coapExchange.advanced().getRequest().getOptions().getURIQueryCount() != 0) {
        List<String> uriQueries = coapExchange.advanced().getRequest().getOptions().getUriQuery();
        spec = ObserveSpec.parse(uriQueries);
    }
    // Manage Write Attributes Request
    if (spec != null) {
        WriteAttributesResponse response = nodeEnabler.writeAttributes(identity, new WriteAttributesRequest(URI, spec));
        if (response.getCode().isError()) {
            coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
        } else {
            coapExchange.respond(toCoapResponseCode(response.getCode()));
        }
        return;
    } else // Manage Write and Bootstrap Write Request (replace)
    {
        LwM2mPath path = new LwM2mPath(URI);
        if (!coapExchange.getRequestOptions().hasContentFormat()) {
            coapExchange.respond(ResponseCode.BAD_REQUEST, "Content Format is mandatory");
            return;
        }
        ContentFormat contentFormat = ContentFormat.fromCode(coapExchange.getRequestOptions().getContentFormat());
        if (!decoder.isSupported(contentFormat)) {
            coapExchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
            return;
        }
        LwM2mNode lwM2mNode;
        try {
            LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
            lwM2mNode = decoder.decode(coapExchange.getRequestPayload(), contentFormat, path, model);
            if (identity.isLwm2mBootstrapServer()) {
                BootstrapWriteResponse response = nodeEnabler.write(identity, new BootstrapWriteRequest(path, lwM2mNode, contentFormat));
                if (response.getCode().isError()) {
                    coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                } else {
                    coapExchange.respond(toCoapResponseCode(response.getCode()));
                }
            } else {
                WriteResponse response = nodeEnabler.write(identity, new WriteRequest(Mode.REPLACE, contentFormat, URI, lwM2mNode));
                if (response.getCode().isError()) {
                    coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                } else {
                    coapExchange.respond(toCoapResponseCode(response.getCode()));
                }
            }
            return;
        } catch (CodecException e) {
            LOG.warn("Unable to decode payload to write", e);
            coapExchange.respond(ResponseCode.BAD_REQUEST);
            return;
        }
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) ObserveSpec(org.eclipse.leshan.ObserveSpec) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) ResourceUtil.extractServerIdentity(org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity) ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CodecException(org.eclipse.leshan.core.node.codec.CodecException) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest)

Example 2 with BootstrapWriteRequest

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

the class ObjectEnabler method doWrite.

@Override
protected BootstrapWriteResponse doWrite(ServerIdentity identity, BootstrapWriteRequest request) {
    LwM2mPath path = request.getPath();
    // Manage Object case
    if (path.isObject()) {
        for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) {
            LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId());
            if (instanceEnabler == null) {
                doCreate(new CreateRequest(path.getObjectId(), instanceNode));
            } else {
                doWrite(identity, new WriteRequest(Mode.REPLACE, path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
            }
        }
        return BootstrapWriteResponse.success();
    }
    // Manage Instance case
    if (path.isObjectInstance()) {
        LwM2mObjectInstance instanceNode = (LwM2mObjectInstance) request.getNode();
        LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
        if (instanceEnabler == null) {
            doCreate(new CreateRequest(path.getObjectId(), instanceNode));
        } else {
            doWrite(identity, new WriteRequest(Mode.REPLACE, request.getContentFormat(), path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values()));
        }
        return BootstrapWriteResponse.success();
    }
    // Manage resource case
    LwM2mResource resource = (LwM2mResource) request.getNode();
    LwM2mInstanceEnabler instanceEnabler = instances.get(path.getObjectInstanceId());
    if (instanceEnabler == null) {
        doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource)));
    } else {
        instanceEnabler.write(path.getResourceId(), resource);
    }
    return BootstrapWriteResponse.success();
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Example 3 with BootstrapWriteRequest

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

the class BootstrapHandler method sendServers.

private void sendServers(final BootstrapSession session, final BootstrapConfig cfg, final List<Integer> toSend) {
    if (!toSend.isEmpty()) {
        // get next config
        Integer key = toSend.remove(0);
        ServerConfig serverConfig = cfg.servers.get(key);
        // extract write request parameters
        LwM2mPath path = new LwM2mPath(1, key);
        final LwM2mNode serverInstance = convertToServerInstance(key, serverConfig);
        final BootstrapWriteRequest writeServerRequest = new BootstrapWriteRequest(path, serverInstance, session.getContentFormat());
        send(session, writeServerRequest, new ResponseCallback<BootstrapWriteResponse>() {

            @Override
            public void onResponse(BootstrapWriteResponse response) {
                LOG.trace("Bootstrap write {} return code {}", session.getEndpoint(), response.getCode());
                // recursive call until toSend is empty
                sendServers(session, cfg, toSend);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.warn(String.format("Error during bootstrap write of server instance %s on %s", serverInstance, session.getEndpoint()), e);
                sessionManager.failed(session, WRITE_SERVER_FAILED, writeServerRequest);
            }
        });
    } else {
        final BootstrapFinishRequest finishBootstrapRequest = new BootstrapFinishRequest();
        send(session, finishBootstrapRequest, new ResponseCallback<BootstrapFinishResponse>() {

            @Override
            public void onResponse(BootstrapFinishResponse response) {
                LOG.trace("Bootstrap Finished {} return code {}", session.getEndpoint(), response.getCode());
                if (response.isSuccess()) {
                    sessionManager.end(session);
                } else {
                    sessionManager.failed(session, FINISHED_WITH_ERROR, finishBootstrapRequest);
                }
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.debug(String.format("Error during bootstrap finished on %s", session.getEndpoint()), e);
                sessionManager.failed(session, SEND_FINISH_FAILED, finishBootstrapRequest);
            }
        });
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapFinishRequest(org.eclipse.leshan.core.request.BootstrapFinishRequest) ServerConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) BootstrapFinishResponse(org.eclipse.leshan.core.response.BootstrapFinishResponse) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest)

Example 4 with BootstrapWriteRequest

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

the class BootstrapHandler method sendBootstrap.

private void sendBootstrap(final BootstrapSession session, final BootstrapConfig cfg, final List<Integer> toSend) {
    if (!toSend.isEmpty()) {
        // 1st encode them into a juicy TLV binary
        Integer key = toSend.remove(0);
        ServerSecurity securityConfig = cfg.security.get(key);
        // extract write request parameters
        LwM2mPath path = new LwM2mPath(0, key);
        final LwM2mNode securityInstance = convertToSecurityInstance(key, securityConfig);
        final BootstrapWriteRequest writeBootstrapRequest = new BootstrapWriteRequest(path, securityInstance, session.getContentFormat());
        send(session, writeBootstrapRequest, new ResponseCallback<BootstrapWriteResponse>() {

            @Override
            public void onResponse(BootstrapWriteResponse response) {
                LOG.trace("Bootstrap write {} return code {}", session.getEndpoint(), response.getCode());
                // recursive call until toSend is empty
                sendBootstrap(session, cfg, toSend);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.debug(String.format("Error during bootstrap write of security instance %s on %s", securityInstance, session.getEndpoint()), e);
                sessionManager.failed(session, WRITE_SECURITY_FAILED, writeBootstrapRequest);
            }
        });
    } else {
        // we are done, send the servers
        List<Integer> serversToSend = new ArrayList<>(cfg.servers.keySet());
        sendServers(session, cfg, serversToSend);
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) ArrayList(java.util.ArrayList) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) ServerSecurity(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)

Aggregations

LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)4 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)4 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 BootstrapWriteResponse (org.eclipse.leshan.core.response.BootstrapWriteResponse)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)2 ErrorCallback (org.eclipse.leshan.core.response.ErrorCallback)2 ArrayList (java.util.ArrayList)1 ObserveSpec (org.eclipse.leshan.ObserveSpec)1 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)1 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)1 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)1 CodecException (org.eclipse.leshan.core.node.codec.CodecException)1 BootstrapFinishRequest (org.eclipse.leshan.core.request.BootstrapFinishRequest)1 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)1 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)1 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)1 BootstrapFinishResponse (org.eclipse.leshan.core.response.BootstrapFinishResponse)1 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)1