Repository: eclipse/leshan
Pull request #248 (2017-01-17 16:10:56)

Move SecurityMode from leshan-server-core to leshan-core
I move SecurityMode from leshan-server-core to leshan-core to be able to reuse it in leshan-client.

sbernard31: It's fixed.

Patch:
diff --git a/leshan-bsserver-demo/src/main/java/org/eclipse/leshan/server/bootstrap/demo/BootstrapSecurityStoreImpl.java b/leshan-bsserver-demo/src/main/java/org/eclipse/leshan/server/bootstrap/demo/BootstrapSecurityStoreImpl.java
index 10be177e..9310c968 100644
--- a/leshan-bsserver-demo/src/main/java/org/eclipse/leshan/server/bootstrap/demo/BootstrapSecurityStoreImpl.java
+++ b/leshan-bsserver-demo/src/main/java/org/eclipse/leshan/server/bootstrap/demo/BootstrapSecurityStoreImpl.java
@@ -20,9 +20,9 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.io.Charsets;
+import org.eclipse.leshan.SecurityMode;
 import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
 import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity;
-import org.eclipse.leshan.server.bootstrap.SecurityMode;
 import org.eclipse.leshan.server.security.BootstrapSecurityStore;
 import org.eclipse.leshan.server.security.SecurityInfo;
 
diff --git a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/SecurityObjectPskStore.java b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/SecurityObjectPskStore.java
index dcab075e..7f95a734 100644
--- a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/SecurityObjectPskStore.java
+++ b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/SecurityObjectPskStore.java
@@ -16,11 +16,7 @@
  *******************************************************************************/
 package org.eclipse.leshan.client.californium.impl;
 
-import static org.eclipse.leshan.LwM2mId.SECURITY;
-import static org.eclipse.leshan.LwM2mId.SEC_PUBKEY_IDENTITY;
-import static org.eclipse.leshan.LwM2mId.SEC_SECRET_KEY;
-import static org.eclipse.leshan.LwM2mId.SEC_SECURITY_MODE;
-import static org.eclipse.leshan.LwM2mId.SEC_SERVER_URI;
+import static org.eclipse.leshan.LwM2mId.*;
 import static org.eclipse.leshan.client.request.ServerIdentity.SYSTEM;
 
 import java.net.InetSocketAddress;
@@ -29,6 +25,7 @@ import java.net.URISyntaxException;
 import java.util.Arrays;
 
 import org.eclipse.californium.scandium.dtls.pskstore.PskStore;
+import org.eclipse.leshan.SecurityMode;
 import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
 import org.eclipse.leshan.client.servers.ServerInfo;
 import org.eclipse.leshan.core.node.LwM2mObject;
@@ -57,8 +54,7 @@ public class SecurityObjectPskStore implements PskStore {
         LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
         for (LwM2mObjectInstance security : securities.getInstances().values()) {
             long securityMode = (long) security.getResource(SEC_SECURITY_MODE).getValue();
-            // TODO use SecurityMode from serve.core ?
-            if (securityMode == 0) // psk
+            if (securityMode == SecurityMode.PSK.code) // psk
             {
                 byte[] pskIdentity = (byte[]) security.getResource(SEC_PUBKEY_IDENTITY).getValue();
                 if (Arrays.equals(identity.getBytes(), pskIdentity))
@@ -76,8 +72,7 @@ public class SecurityObjectPskStore implements PskStore {
         LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
         for (LwM2mObjectInstance security : securities.getInstances().values()) {
             long securityMode = (long) security.getResource(SEC_SECURITY_MODE).getValue();
-            // TODO use SecurityMode from server.core ?
-            if (securityMode == 0) // psk
+            if (securityMode == SecurityMode.PSK.code)
             {
                 try {
                     URI uri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java
index 6a3d55bb..fd1559c7 100644
--- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java
+++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java
@@ -290,6 +290,8 @@ public class RegistrationEngine {
                 }
             } catch (InterruptedException e) {
                 LOG.info("Registration task interrupted.");
+            } catch (RuntimeException e) {
+                LOG.error("Unexpected exception during update registration task", e);
             }
         }
     }
@@ -324,6 +326,8 @@ public class RegistrationEngine {
                 }
             } catch (InterruptedException e) {
                 LOG.info("Registration update task interrupted.");
+            } catch (RuntimeException e) {
+                LOG.error("Unexpected exception during update registration task", e);
             }
         }
     }
diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServerInfo.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServerInfo.java
index 3a3a56af..284c0314 100644
--- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServerInfo.java
+++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServerInfo.java
@@ -19,6 +19,7 @@ import java.net.InetSocketAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
 
+import org.eclipse.leshan.SecurityMode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -28,8 +29,7 @@ public class ServerInfo {
 
     public long serverId;
     public URI serverUri;
-    // TODO use SecureMode from server.core
-    public long secureMode;
+    public SecurityMode secureMode;
 
     public InetSocketAddress getAddress() {
         return getAddress(serverUri);
@@ -40,7 +40,7 @@ public class ServerInfo {
     }
 
     public boolean isSecure() {
-        return secureMode != 3;
+        return secureMode != SecurityMode.NO_SEC;
     }
 
     @Override
diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServersInfoExtractor.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServersInfoExtractor.java
index b6f29126..92c7cc92 100644
--- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServersInfoExtractor.java
+++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/ServersInfoExtractor.java
@@ -16,21 +16,14 @@
  *******************************************************************************/
 package org.eclipse.leshan.client.servers;
 
-import static org.eclipse.leshan.LwM2mId.SECURITY;
-import static org.eclipse.leshan.LwM2mId.SEC_BOOTSTRAP;
-import static org.eclipse.leshan.LwM2mId.SEC_SECURITY_MODE;
-import static org.eclipse.leshan.LwM2mId.SEC_SERVER_ID;
-import static org.eclipse.leshan.LwM2mId.SEC_SERVER_URI;
-import static org.eclipse.leshan.LwM2mId.SERVER;
-import static org.eclipse.leshan.LwM2mId.SRV_BINDING;
-import static org.eclipse.leshan.LwM2mId.SRV_LIFETIME;
-import static org.eclipse.leshan.LwM2mId.SRV_SERVER_ID;
+import static org.eclipse.leshan.LwM2mId.*;
 import static org.eclipse.leshan.client.request.ServerIdentity.SYSTEM;
 
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Map;
 
+import org.eclipse.leshan.SecurityMode;
 import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
 import org.eclipse.leshan.core.node.LwM2mObject;
 import org.eclipse.leshan.core.node.LwM2mObjectInstance;
@@ -66,7 +59,8 @@ public class ServersInfoExtractor {
                         ServerInfo info = new ServerInfo();
                         info.serverId = (long) security.getResource(SEC_SERVER_ID).getValue();
                         info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
-                        info.secureMode = (long) security.getResource(SEC_SECURITY_MODE).getValue();
+                        info.secureMode = SecurityMode
+                                .fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                         infos.bootstrap = info;
                     }
                 } else {
@@ -74,7 +68,7 @@ public class ServersInfoExtractor {
                     DmServerInfo info = new DmServerInfo();
                     info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                     info.serverId = (long) security.getResource(SEC_SERVER_ID).getValue();
-                    info.secureMode = (long) security.getResource(SEC_SECURITY_MODE).getValue();
+                    info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
 
                     // search corresponding device management server
                     for (LwM2mObjectInstance server : servers.getInstances().values()) {
diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/SecurityMode.java b/leshan-core/src/main/java/org/eclipse/leshan/SecurityMode.java
similarity index 68%
rename from leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/SecurityMode.java
rename to leshan-core/src/main/java/org/eclipse/leshan/SecurityMode.java
index 07dd4580..26b1aa04 100644
--- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/SecurityMode.java
+++ b/leshan-core/src/main/java/org/eclipse/leshan/SecurityMode.java
@@ -13,7 +13,7 @@
  * Contributors:
  *     Sierra Wireless - initial API and implementation
  *******************************************************************************/
-package org.eclipse.leshan.server.bootstrap;
+package org.eclipse.leshan;
 
 /**
  * The different DTLS security modes
@@ -26,4 +26,17 @@ public enum SecurityMode {
     private SecurityMode(int code) {
         this.code = code;
     }
+
+    public static SecurityMode fromCode(long code) {
+        return fromCode((int) code);
+    }
+
+    public static SecurityMode fromCode(int code) {
+        for (SecurityMode sm : SecurityMode.values()) {
+            if (sm.code == code) {
+                return sm;
+            }
+        }
+        throw new IllegalArgumentException(String.format("Unsupported security code : %d", code));
+    }
 }
diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java
index 4221b652..e5397be1 100644
--- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java
+++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.eclipse.leshan.SecurityMode;
 import org.eclipse.leshan.core.request.BindingMode;
 
 /**


Test patch:
diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapIntegrationTestHelper.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapIntegrationTestHelper.java
index 682493ad..f42f417c 100644
--- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapIntegrationTestHelper.java
+++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapIntegrationTestHelper.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 import java.util.List;
 
 import org.eclipse.leshan.LwM2mId;
+import org.eclipse.leshan.SecurityMode;
 import org.eclipse.leshan.client.californium.LeshanClientBuilder;
 import org.eclipse.leshan.client.object.Device;
 import org.eclipse.leshan.client.object.Security;
@@ -32,7 +33,6 @@ import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig;
 import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity;
 import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager;
 import org.eclipse.leshan.server.bootstrap.BootstrapStore;
-import org.eclipse.leshan.server.bootstrap.SecurityMode;
 import org.eclipse.leshan.server.californium.impl.LwM2mBootstrapServerImpl;
 import org.eclipse.leshan.server.impl.BootstrapSessionManagerImpl;
 import org.eclipse.leshan.server.security.BootstrapSecurityStore;