@Override public void visit(LwM2mResource resource) { if (resource.isMultiInstances()) { throw new CodecException("Multiple instances resource %s cannot be encoded in opaque format", path); } ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); if (rSpec != null && rSpec.type != Type.OPAQUE) { throw new CodecException("Only single opaque resource can be encoded in opaque format. [%s]", path); } LOG.trace("Encoding resource {} into text", resource); Object value = converter.convertValue(resource.getValue(), resource.getType(), Type.OPAQUE, path); encoded = (byte[]) value; } }
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; } }
if (value.getType() != Type.STRING) { return WriteResponse.badRequest("invalid type"); serverUri = (String) value.getValue(); return WriteResponse.success(); if (value.getType() != Type.BOOLEAN) { return WriteResponse.badRequest("invalid type"); bootstrapServer = (Boolean) value.getValue(); return WriteResponse.success(); if (value.getType() != Type.INTEGER) { return WriteResponse.badRequest("invalid type"); securityMode = ((Long) value.getValue()).intValue(); return WriteResponse.success(); if (value.getType() != Type.OPAQUE) { return WriteResponse.badRequest("invalid type"); publicKeyOrIdentity = (byte[]) value.getValue(); return WriteResponse.success(); case SEC_SERVER_PUBKEY: // server public key if (value.getType() != Type.OPAQUE) { return WriteResponse.badRequest("invalid type"); serverPublicKey = (byte[]) value.getValue(); return WriteResponse.success();
} else { LwM2mResource resource = (LwM2mResource) node; if (resource.isMultiInstances()) { throw new InvalidRequestException( "Invalid format for path %s: %s format must be used only for single resources", target, format); } else { if (resource.getType() == Type.OPAQUE && format == ContentFormat.TEXT) { throw new InvalidRequestException( "Invalid format for path %s: TEXT format must not be used for byte array single resources", target); } else if (resource.getType() != Type.OPAQUE && format == ContentFormat.OPAQUE) { throw new InvalidRequestException( "Invalid format for path %s: OPAQUE format must be used only for byte array single resources",
public LwM2mObjectInstance(int id, Collection<LwM2mResource> resources) { Validate.notNull(resources); this.id = id; Map<Integer, LwM2mResource> resourcesMap = new HashMap<>(resources.size()); for (LwM2mResource resource : resources) { resourcesMap.put(resource.getId(), resource); } this.resources = Collections.unmodifiableMap(resourcesMap); }
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; } }
if (value.getType() != Type.INTEGER) { return WriteResponse.badRequest("invalid type"); shortServerId = ((Long) value.getValue()).intValue(); return WriteResponse.success(); if (value.getType() != Type.INTEGER) { return WriteResponse.badRequest("invalid type"); lifetime = (Long) value.getValue(); return WriteResponse.success(); if (value.getType() != Type.INTEGER) { return WriteResponse.badRequest("invalid type"); defaultMinPeriod = (Long) value.getValue(); return WriteResponse.success(); if (value.getType() != Type.INTEGER) { return WriteResponse.badRequest("invalid type"); defaultMaxPeriod = (Long) value.getValue(); return WriteResponse.success(); if (value.getType() != Type.BOOLEAN) { return WriteResponse.badRequest("invalid type"); notifyWhenDisable = (boolean) value.getValue(); return WriteResponse.success();
} else { LwM2mResource resource = (LwM2mResource) node; if (resource.isMultiInstances()) { throw new InvalidRequestException( "Invalid format for path %s: format must be used only for single resources", target, format); } else { if (resource.getType() == Type.OPAQUE && format == ContentFormat.TEXT) { throw new InvalidRequestException( "Invalid format for %s: TEXT format must not be used for byte array single resources", target); } else if (resource.getType() != Type.OPAQUE && format == ContentFormat.OPAQUE) { throw new InvalidRequestException( "Invalid format for %s: OPAQUE format must be used only for byte array single resources",
public void setObjectModel(ObjectModel objectModel) { this.objectModel = objectModel; // initialize resources for (ResourceModel resourceModel : objectModel.resources.values()) { if (resourceModel.operations.isReadable()) { LwM2mResource newResource = createResource(objectModel, resourceModel); if (newResource != null) { resources.put(newResource.getId(), newResource); } } } }
private Tlv encodeResource(LwM2mResource resource, LwM2mPath resourcePath) { ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); Tlv rTlv; if (resource.isMultiInstances()) { Tlv[] instances = new Tlv[resource.getValues().size()]; int i = 0; for (Entry<Integer, ?> entry : resource.getValues().entrySet()) { LwM2mPath resourceInstancePath = resourcePath.append(entry.getKey()); Object convertedValue = converter.convertValue(entry.getValue(), resource.getType(), expectedType, resourceInstancePath); instances[i] = new Tlv(TlvType.RESOURCE_INSTANCE, null, this.encodeTlvValue(convertedValue, expectedType, resourceInstancePath), entry.getKey()); i++; } rTlv = new Tlv(TlvType.MULTIPLE_RESOURCE, instances, null, resource.getId()); } else { Object convertedValue = converter.convertValue(resource.getValue(), resource.getType(), expectedType, resourcePath); rTlv = new Tlv(TlvType.RESOURCE_VALUE, null, this.encodeTlvValue(convertedValue, expectedType, resourcePath), resource.getId()); } return rTlv; }
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; }
private Tlv[] encodeResources(Collection<LwM2mResource> resources, LwM2mPath instancePath) { Tlv[] rTlvs = new Tlv[resources.size()]; int i = 0; for (LwM2mResource resource : resources) { rTlvs[i] = encodeResource(resource, instancePath.append(resource.getId())); i++; } return rTlvs; }
} else if (LwM2mResource.class.isAssignableFrom((Class<?>) typeOfSrc)) { LwM2mResource rsc = (LwM2mResource) src; if (rsc.isMultiInstances()) { JsonObject values = new JsonObject(); for (Entry<Integer, ?> entry : rsc.getValues().entrySet()) { if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) { values.add(entry.getKey().toString(), context.serialize(Hex.encodeHex((byte[]) entry.getValue()))); if (rsc.getType() == org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE) { element.add("value", context.serialize(Hex.encodeHex((byte[]) rsc.getValue()))); } else { element.add("value", context.serialize(rsc.getValue()));
public static String getPskIdentity(LwM2mObjectInstance securityInstance) { byte[] pubKey = (byte[]) securityInstance.getResource(SEC_PUBKEY_IDENTITY).getValue(); return new String(pubKey); }
private static LwM2mObjectInstance parseObjectInstanceTlv(Tlv[] rscTlvs, int objectId, int instanceId, LwM2mModel model) throws CodecException { // read resources Map<Integer, LwM2mResource> resources = new HashMap<>(rscTlvs.length); for (Tlv rscTlv : rscTlvs) { LwM2mPath resourcePath = new LwM2mPath(objectId, instanceId, rscTlv.getIdentifier()); LwM2mResource resource = parseResourceTlv(rscTlv, resourcePath, model); LwM2mResource previousResource = resources.put(resource.getId(), resource); if (previousResource != null) { throw new CodecException("2 RESOURCE nodes (%s,%s) with the same identifier %d for path %s", previousResource, resource, resource.getId(), resourcePath); } } return new LwM2mObjectInstance(instanceId, resources.values()); }
@Override public void visit(LwM2mResource resource) { if (resource.isMultiInstances()) { throw new CodecException("Multiple instances resource %s cannot be encoded in text format", path); ResourceModel rSpec = model.getResourceModel(path.getObjectId(), resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); Object val = converter.convertValue(resource.getValue(), resource.getType(), expectedType, path);
public static byte[] getPskKey(LwM2mObjectInstance securityInstance) { return (byte[]) securityInstance.getResource(SEC_SECRET_KEY).getValue(); }
@Override public void visit(LwM2mObjectInstance instance) { LOG.trace("Encoding object instance {} into JSON", instance); resourceList = new ArrayList<>(); for (LwM2mResource resource : instance.getResources().values()) { // Validate request path & compute resource path String prefixPath; if (requestPath.isObject()) { prefixPath = instance.getId() + "/" + resource.getId(); } else if (requestPath.isObjectInstance()) { prefixPath = Integer.toString(resource.getId()); } else { throw new CodecException("Invalid request path %s for JSON instance encoding", requestPath); } // Create resources resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } }
private ArrayList<JsonArrayEntry> lwM2mResourceToJsonArrayEntry(String resourcePath, Long timestamp, LwM2mResource resource) { ResourceModel rSpec = model.getResourceModel(objectId, resource.getId()); Type expectedType = rSpec != null ? rSpec.type : resource.getType(); ArrayList<JsonArrayEntry> resourcesList = new ArrayList<>(); if (resource.isMultiInstances()) { for (Entry<Integer, ?> entry : resource.getValues().entrySet()) { Object convertedValue = converter.convertValue(entry.getValue(), resource.getType(), expectedType, lwM2mResourceInstancePath); this.setResourceValue(convertedValue, expectedType, jsonResourceElt, lwM2mResourceInstancePath); this.setResourceValue(converter.convertValue(resource.getValue(), resource.getType(), expectedType, lwM2mResourcePath), expectedType, jsonResourceElt, lwM2mResourcePath);
private static PublicKey getPublicKey(LwM2mObjectInstance securityInstance) { byte[] encodedKey = (byte[]) securityInstance.getResource(SEC_PUBKEY_IDENTITY).getValue(); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey); String algorithm = "EC"; try { KeyFactory kf = KeyFactory.getInstance(algorithm); return kf.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { LOG.debug("Failed to instantiate key factory for algorithm " + algorithm, e); } catch (InvalidKeySpecException e) { LOG.debug("Failed to decode RFC7250 public key with algorithm " + algorithm, e); } return null; }