/** * Request to write an <b>Object Instance</b> using the TLV content format. * * @param mode the mode of the request : replace or update. * @param objectId the id of the object to write. * @param objectInstanceId the id of the object instance to write. * @param resources the list of resources to write. */ public WriteRequest(Mode mode, int objectId, int objectInstanceId, LwM2mResource... resources) { this(mode, ContentFormat.TLV, new LwM2mPath(objectId, objectInstanceId), new LwM2mObjectInstance(objectId, resources)); }
/** * Creates a request for creating an instance of a particular object.<br> * If the path is an object path, the instance id will be chosen by the client and accessible in the CreateResponse. * To choose instance id at server side, the path must be an object instance path. * * @param path the target path (object or object instance) * @param instance the object instance * @exception InvalidRequestException if the target path is not valid. */ public CreateRequest(String path, LwM2mObjectInstance instance) throws InvalidRequestException { this(null, newPath(path), instance.getId(), instance.getResources().values().toArray((new LwM2mResource[instance.getResources().size()]))); }
private static Certificate getClientCertificate(LwM2mObjectInstance securityInstance) { byte[] encodedCert = (byte[]) securityInstance.getResource(SEC_PUBKEY_IDENTITY).getValue(); try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); try (ByteArrayInputStream in = new ByteArrayInputStream(encodedCert)) { return cf.generateCertificate(in); } } catch (CertificateException | IOException e) { LOG.debug("Failed to decode X.509 certificate", e); return null; } }
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())); } else { doWrite(identity, new WriteRequest(Mode.REPLACE, request.getContentFormat(), path.getObjectId(), path.getObjectInstanceId(), instanceNode.getResources().values())); if (instanceEnabler == null) { doCreate(new CreateRequest(path.getObjectId(), new LwM2mObjectInstance(path.getObjectInstanceId(), resource))); } else { instanceEnabler.write(path.getResourceId(), resource);
@Override public void visit(LwM2mObject object) { LOG.trace("Encoding object {} into TLV", object); Tlv[] tlvs; // encoded as an array of instances tlvs = new Tlv[object.getInstances().size()]; int i = 0; for (Entry<Integer, LwM2mObjectInstance> instance : object.getInstances().entrySet()) { Tlv[] resources = encodeResources(instance.getValue().getResources().values(), new LwM2mPath(object.getId(), instance.getKey())); tlvs[i] = new Tlv(TlvType.OBJECT_INSTANCE, resources, null, instance.getKey()); i++; } try { out.write(TlvEncoder.encode(tlvs).array()); } catch (IOException e) { throw new RuntimeException(e); } }
public LwM2mObject(int id, Collection<LwM2mObjectInstance> instances) { Validate.notNull(instances); this.id = id; HashMap<Integer, LwM2mObjectInstance> instancesMap = new HashMap<>(instances.size()); for (LwM2mObjectInstance instance : instances) { instancesMap.put(instance.getId(), instance); } this.instances = Collections.unmodifiableMap(instancesMap); }
element.add("instances", context.serialize(((LwM2mObject) src).getInstances().values())); } else if (typeOfSrc == LwM2mObjectInstance.class) { element.add("resources", context.serialize(((LwM2mObjectInstance) src).getResources().values())); } else if (LwM2mResource.class.isAssignableFrom((Class<?>) typeOfSrc)) { LwM2mResource rsc = (LwM2mResource) src;
LwM2mObjectInstance previousObjectInstance = instances.put(objectInstance.getId(), objectInstance); if (previousObjectInstance != null) { throw new CodecException( "2 OBJECT_INSTANCE nodes (%s,%s) with the same identifier %d for path %s", previousObjectInstance, objectInstance, objectInstance.getId(), path);
/** * Creates a request for creating an instance of a particular object.<br> * If the path is an object path, the instance id will be chosen by the client and accessible in the CreateResponse. * To choose instance id at server side, the path must be an object instance path. * * @param contentFormat the payload format (TLV or JSON) * @param path the target path (object or object instance) * @param instance the object instance * @exception InvalidRequestException if parameters are invalid. */ public CreateRequest(ContentFormat contentFormat, String path, LwM2mObjectInstance instance) throws InvalidRequestException { this(contentFormat, newPath(path), instance.getId(), instance.getResources().values().toArray((new LwM2mResource[instance.getResources().size()]))); }
/** * Request to write an <b>Object Instance</b> using the TLV content format. * * @param mode the mode of the request : replace or update. * @param objectId the id of the object to write. * @param objectInstanceId the id of the object instance to write. * @param resources the list of resources to write. */ public WriteRequest(Mode mode, int objectId, int objectInstanceId, Collection<LwM2mResource> resources) { this(mode, ContentFormat.TLV, new LwM2mPath(objectId, objectInstanceId), new LwM2mObjectInstance(objectId, resources)); }
for (Integer writeResourceId : ((LwM2mObjectInstance) request.getNode()).getResources().keySet()) { ResourceModel resourceModel = model.resources.get(writeResourceId); if (null != resourceModel && !resourceModel.operations.isWritable()) { for (Integer writeResourceId : ((LwM2mObjectInstance) request.getNode()).getResources().keySet()) { mandatoryResources.remove(writeResourceId);
private static Certificate getServerCertificate(LwM2mObjectInstance securityInstance) { byte[] encodedCert = (byte[]) securityInstance.getResource(SEC_SERVER_PUBKEY).getValue(); try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); try (ByteArrayInputStream in = new ByteArrayInputStream(encodedCert)) { return cf.generateCertificate(in); } } catch (CertificateException | IOException e) { LOG.debug("Failed to decode X.509 certificate", e); return null; } }
/** * Creates a request for creating an instance of a particular object. * * @param contentFormat the payload format * @param objectId the object id * @param instance the object instance * @exception InvalidRequestException if bad @{link ContentFormat} format was used. */ public CreateRequest(ContentFormat contentFormat, int objectId, LwM2mObjectInstance instance) throws InvalidRequestException { this(contentFormat, new LwM2mPath(objectId), instance.getId(), instance.getResources().values().toArray(new LwM2mResource[0])); }
/** * Request to write an <b>Object Instance</b>. * * @param mode the mode of the request : replace or update. * @param contentFormat Format of the payload (TLV or JSON). * @param objectId the id of the object to write. * @param objectInstanceId the id of the object instance to write. * @param resources the list of resources to write. * @exception InvalidRequestException if bad @{link ContentFormat} format was used. */ public WriteRequest(Mode mode, ContentFormat contentFormat, int objectId, int objectInstanceId, Collection<LwM2mResource> resources) throws InvalidRequestException { this(mode, contentFormat, new LwM2mPath(objectId, objectInstanceId), new LwM2mObjectInstance(objectId, resources)); }
Map<Integer, LwM2mResource> writeResources = ((LwM2mObjectInstance) request.getNode()).getResources(); if (request.isReplaceRequest()) {
public static String getPskIdentity(LwM2mObjectInstance securityInstance) { byte[] pubKey = (byte[]) securityInstance.getResource(SEC_PUBKEY_IDENTITY).getValue(); return new String(pubKey); }
@Override public void visit(LwM2mObject object) { LOG.trace("Encoding Object {} into JSON", object); // Validate request path if (!requestPath.isObject()) { throw new CodecException("Invalid request path %s for JSON object encoding", requestPath); } // Create resources resourceList = new ArrayList<>(); for (LwM2mObjectInstance instance : object.getInstances().values()) { for (LwM2mResource resource : instance.getResources().values()) { String prefixPath = Integer.toString(instance.getId()) + "/" + Integer.toString(resource.getId()); resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } } }
/** * Request to write an <b>Object Instance</b>. * * @param mode the mode of the request : replace or update. * @param contentFormat Format of the payload (TLV or JSON). * @param objectId the id of the object to write. * @param objectInstanceId the id of the object instance to write. * @param resources the list of resources to write. * @exception InvalidRequestException if bad @{link ContentFormat} format was used. */ public WriteRequest(Mode mode, ContentFormat contentFormat, int objectId, int objectInstanceId, LwM2mResource... resources) throws InvalidRequestException { this(mode, contentFormat, new LwM2mPath(objectId, objectInstanceId), new LwM2mObjectInstance(objectId, resources)); }
private static PrivateKey getPrivateKey(LwM2mObjectInstance securityInstance) { byte[] encodedKey = (byte[]) securityInstance.getResource(SEC_SECRET_KEY).getValue(); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey); String algorithm = "EC"; try { KeyFactory kf = KeyFactory.getInstance(algorithm); return kf.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { LOG.warn("Failed to instantiate key factory for algorithm " + algorithm, e); } catch (InvalidKeySpecException e) { LOG.warn("Failed to decode RFC5958 private key with algorithm " + algorithm, e); } return null; }
@Override public void visit(LwM2mObjectInstance instance) { LOG.trace("Encoding object instance {} into TLV", instance); Tlv[] tlvs; if (path.isObjectInstance() || instance.getId() == LwM2mObjectInstance.UNDEFINED) { // the instanceId is part of the request path or is undefined // so the instance TLV layer is not needed. // encoded as an array of resource TLVs tlvs = encodeResources(instance.getResources().values(), new LwM2mPath(path.getObjectId(), instance.getId())); } else { // encoded as an instance TLV Tlv[] resources = encodeResources(instance.getResources().values(), new LwM2mPath(path.getObjectId(), instance.getId())); tlvs = new Tlv[] { new Tlv(TlvType.OBJECT_INSTANCE, resources, null, instance.getId()) }; } try { out.write(TlvEncoder.encode(tlvs).array()); } catch (IOException e) { throw new RuntimeException(e); } }