Skip to content

Commit

Permalink
Extract registration rootpath in builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 3, 2021
1 parent f042517 commit 63683cb
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private Registration newRegistration() throws UnknownHostException {
private Registration newRegistration(String rootpath) throws UnknownHostException {
Builder b = new Registration.Builder("regid", "endpoint",
Identity.unsecure(Inet4Address.getLoopbackAddress(), 12354));
b.extractDataFromObjectLink(true);
if (rootpath != null) {
Map<String, String> attr = new HashMap<>();
attr.put("rt", "\"oma.lwm2m\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class Registration implements Serializable {
private final Date lastUpdate;

protected Registration(String id, String endpoint, Identity identity, Version lwM2mVersion, Long lifetimeInSec,
String smsNumber, EnumSet<BindingMode> bindingMode, Boolean queueMode, Link[] objectLinks,
String smsNumber, EnumSet<BindingMode> bindingMode, Boolean queueMode, Link[] objectLinks, String rootPath,
Date registrationDate, Date lastUpdate, Map<String, String> additionalRegistrationAttributes,
Map<Integer, String> supportedObjects) {

Expand All @@ -97,19 +97,7 @@ protected Registration(String id, String endpoint, Identity identity, Version lw
this.smsNumber = smsNumber;

this.objectLinks = objectLinks;
// Parse object link to extract root path.
String rootPath = "/";
if (objectLinks != null) {
for (Link link : objectLinks) {
if (link != null && "oma.lwm2m".equals(Link.unquote(link.getAttributes().get("rt")))) {
rootPath = link.getUrl();
break;
}
}
}
if (!rootPath.endsWith("/"))
rootPath = rootPath + "/";
this.rootPath = rootPath;
this.rootPath = rootPath == null ? "/" : rootPath;
this.supportedObjects = new AtomicReference<Map<Integer, String>>(supportedObjects);
this.lifeTimeInSec = lifetimeInSec == null ? DEFAULT_LIFETIME_IN_SEC : lifetimeInSec;
this.lwM2mVersion = lwM2mVersion == null ? Version.getDefault() : lwM2mVersion;
Expand Down Expand Up @@ -491,9 +479,13 @@ public static class Builder {
private Boolean queueMode;
private Version lwM2mVersion;
private Link[] objectLinks;
private String rootPath;
private Map<Integer, String> supportedObjects;
private Map<String, String> additionalRegistrationAttributes;

// builder setting
private boolean extractData; // if true extract data from objectLinks

public Builder(String registrationId, String endpoint, Identity identity) {

Validate.notNull(registrationId);
Expand All @@ -504,6 +496,11 @@ public Builder(String registrationId, String endpoint, Identity identity) {
this.identity = identity;
}

public Builder extractDataFromObjectLink(boolean extract) {
this.extractData = extract;
return this;
}

public Builder registrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
return this;
Expand Down Expand Up @@ -544,8 +541,8 @@ public Builder objectLinks(Link[] objectLinks) {
return this;
}

public Builder supportedObjects(Map<Integer, String> supportedObjects) {
this.supportedObjects = Collections.unmodifiableMap(supportedObjects);
public Builder rootPath(String rootPath) {
this.rootPath = rootPath;
return this;
}

Expand All @@ -554,12 +551,38 @@ public Builder additionalRegistrationAttributes(Map<String, String> additionalRe
return this;
}

public Registration build() {
return new Registration(Builder.this.registrationId, Builder.this.endpoint, Builder.this.identity,
Builder.this.lwM2mVersion, Builder.this.lifeTimeInSec, Builder.this.smsNumber, this.bindingMode,
this.queueMode, this.objectLinks, this.registrationDate, this.lastUpdate,
this.additionalRegistrationAttributes, this.supportedObjects);
private void extractDataFromObjectLinks() {
if (objectLinks != null) {
// Define default RootPath;
rootPath = "/";

// Parse object link to extract root path
for (Link link : objectLinks) {
if (link != null && "oma.lwm2m".equals(Link.unquote(link.getAttributes().get("rt")))) {
rootPath = link.getUrl();
if (!rootPath.endsWith("/")) {
rootPath = rootPath + "/";
}
break;
}
}

// TODO extract supported Content format
// TODO extract object supported
// TODO extract available instances
}
}

public Registration build() {
// Extract data from object links if wanted
if (extractData) {
extractDataFromObjectLinks();
}

// Create Registration
return new Registration(registrationId, endpoint, identity, lwM2mVersion, lifeTimeInSec, smsNumber,
bindingMode, queueMode, objectLinks, rootPath, registrationDate, lastUpdate,
additionalRegistrationAttributes, supportedObjects);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public SendableResponse<RegisterResponse> register(Identity sender, RegisterRequ

Registration.Builder builder = new Registration.Builder(
registrationIdProvider.getRegistrationId(registerRequest), registerRequest.getEndpointName(), sender);
builder.extractDataFromObjectLink(true);

builder.lwM2mVersion(Version.get(registerRequest.getLwVersion())).lifeTimeInSec(registerRequest.getLifetime())
.bindingMode(registerRequest.getBindingMode()).queueMode(registerRequest.getQueueMode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ public Registration update(Registration registration) {

Registration.Builder builder = new Registration.Builder(registration.getId(), registration.getEndpoint(),
identity);
builder.extractDataFromObjectLink(this.objectLinks != null); // we parse object link only if there was updated.

builder.lwM2mVersion(registration.getLwM2mVersion()).lifeTimeInSec(lifeTimeInSec).smsNumber(smsNumber)
.bindingMode(bindingMode).queueMode(registration.getQueueMode()).objectLinks(linkObject)
.registrationDate(registration.getRegistrationDate()).lastUpdate(lastUpdate)
.additionalRegistrationAttributes(additionalAttributes);
.additionalRegistrationAttributes(additionalAttributes).rootPath(registration.getRootPath());

return builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void test_object_links_with_text_in_lwm2m_path() {
private Registration given_a_registration_with_object_link_like(String objectLinks) {
Builder builder = new Registration.Builder("id", "endpoint",
Identity.unsecure(InetSocketAddress.createUnresolved("localhost", 0)));

builder.extractDataFromObjectLink(true);
builder.objectLinks(Link.parse(objectLinks.getBytes()));
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public static Registration deserialize(JsonObject jObj) {
b.smsNumber(jObj.getString("sms", ""));
}

b.rootPath(jObj.getString("root", "/"));

JsonArray links = (JsonArray) jObj.get("objLink");
Link[] linkObjs = new Link[links.size()];
for (int i = 0; i < links.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void ser_and_des_are_equals() throws Exception {
objs[1] = new Link("/0/2");

Registration.Builder builder = new Registration.Builder("registrationId", "endpoint",
Identity.unsecure(Inet4Address.getLoopbackAddress(), 1)).objectLinks(objs);
Identity.unsecure(Inet4Address.getLoopbackAddress(), 1)).objectLinks(objs).rootPath("/");

builder.registrationDate(new Date(100L));
builder.lastUpdate(new Date(101L));
Expand Down

0 comments on commit 63683cb

Please sign in to comment.