From b8e9ecf4d9412420c9471e34be2e0aa64ba07d44 Mon Sep 17 00:00:00 2001 From: Mahdi Ben Alaya Date: Sat, 30 Jul 2016 10:08:20 -0400 Subject: Fix Bug 498911 - Error in Access Right verification for PoA notification --- .../src/main/java/org/eclipse/om2m/core/notifier/Notifier.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java index 3744af3e..4ea0d702 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java @@ -230,7 +230,8 @@ public class Notifier { // Set request parameters request.setOperation(Operation.NOTIFY); - request.setFrom("/" + Constants.CSE_ID); + //request.setFrom("/" + Constants.CSE_ID); + request.setFrom(Constants.ADMIN_REQUESTING_ENTITY); if(resourceStatus == ResourceStatus.DELETED){ notification.setSubscriptionDeletion(true); -- cgit v1.2.3 From eb99f4149131577317ce989d4e9fbdbc324a9183 Mon Sep 17 00:00:00 2001 From: BOLLE Sebastien Date: Wed, 13 Jul 2016 11:12:44 +0200 Subject: commons: add method to retrieve the AccessControlPolicys from a Label. Conflicts: org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java Signed-off-by: BOLLE Sebastien Signed-off-by: BONNARDEL Gregory --- .../org/eclipse/om2m/commons/entities/LabelEntity.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java index 24438268..1ed10b16 100644 --- a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java +++ b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java @@ -67,6 +67,9 @@ public class LabelEntity { @ManyToMany(targetEntity = PollingChannelEntity.class, mappedBy = "labelsEntities") protected List linkedPch; + @ManyToMany(targetEntity = AccessControlPolicyEntity.class, mappedBy = "labelsEntities") + protected List linkedACP; + @ManyToMany(targetEntity = NodeEntity.class, mappedBy = "labelsEntities") protected List linkedNodes; @@ -150,6 +153,17 @@ public class LabelEntity { return linkedAe; } + public void setLinkedACP(List pLinkedACPs) { + this.linkedACP = pLinkedACPs; + } + + public List getLinkedACP() { + if (this.linkedACP == null) { + this.linkedACP = new ArrayList<>(); + } + return this.linkedACP; + } + /** * @param linkedAe the linkedAe to set */ -- cgit v1.2.3 From 2148f78c70400509a3b746f2232818abdfe2c01d Mon Sep 17 00:00:00 2001 From: Francois Aissaoui Date: Mon, 3 Oct 2016 16:18:52 +0200 Subject: Implementation of MQTT Protocol binding TS-0010 This implements features from the MQTT protocol binding TS-0010 from oneM2M. It does NOT include the MQTT Broker. --- org.eclipse.om2m.binding.mqtt/.classpath | 8 + org.eclipse.om2m.binding.mqtt/.project | 34 +++ org.eclipse.om2m.binding.mqtt/META-INF/MANIFEST.MF | 18 ++ org.eclipse.om2m.binding.mqtt/README | 0 org.eclipse.om2m.binding.mqtt/build.properties | 4 + .../libs/org.eclipse.paho.client.mqttv3-1.0.2.jar | Bin 0 -> 171372 bytes org.eclipse.om2m.binding.mqtt/pom.xml | 11 + .../org/eclipse/om2m/binding/mqtt/Activator.java | 151 ++++++++++++ .../om2m/binding/mqtt/MqttRequestHandler.java | 255 +++++++++++++++++++++ .../eclipse/om2m/binding/mqtt/MqttRestClient.java | 155 +++++++++++++ .../om2m/binding/mqtt/util/DataMapperRegistry.java | 75 ++++++ .../om2m/binding/mqtt/util/MqttConstants.java | 48 ++++ .../om2m/binding/mqtt/util/QueueSender.java | 60 +++++ .../om2m/binding/mqtt/util/ResponseRegistry.java | 99 ++++++++ .../om2m/binding/mqtt/util/ResponseSemaphore.java | 32 +++ .../org/eclipse/om2m/binding/mqtt/util/Utils.java | 19 ++ .../om2m/commons/resource/RequestPrimitive.java | 48 +++- .../om2m/commons/resource/ResponsePrimitive.java | 2 +- .../core/controller/FanOutPointController.java | 2 +- .../org/eclipse/om2m/core/notifier/Notifier.java | 23 +- .../eclipse/om2m/core/redirector/Redirector.java | 52 ++++- .../java/org/eclipse/om2m/core/router/Router.java | 4 + .../src/main/resources/json-binding.xml | 13 ++ org.eclipse.om2m.site.in-cse/om2m.product | 1 + pom.xml | 1 + 25 files changed, 1102 insertions(+), 13 deletions(-) create mode 100644 org.eclipse.om2m.binding.mqtt/.classpath create mode 100644 org.eclipse.om2m.binding.mqtt/.project create mode 100644 org.eclipse.om2m.binding.mqtt/META-INF/MANIFEST.MF create mode 100644 org.eclipse.om2m.binding.mqtt/README create mode 100644 org.eclipse.om2m.binding.mqtt/build.properties create mode 100644 org.eclipse.om2m.binding.mqtt/libs/org.eclipse.paho.client.mqttv3-1.0.2.jar create mode 100644 org.eclipse.om2m.binding.mqtt/pom.xml create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java create mode 100644 org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java diff --git a/org.eclipse.om2m.binding.mqtt/.classpath b/org.eclipse.om2m.binding.mqtt/.classpath new file mode 100644 index 00000000..f7edce6b --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/org.eclipse.om2m.binding.mqtt/.project b/org.eclipse.om2m.binding.mqtt/.project new file mode 100644 index 00000000..fed26a33 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/.project @@ -0,0 +1,34 @@ + + + org.eclipse.om2m.binding.mqtt + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/org.eclipse.om2m.binding.mqtt/META-INF/MANIFEST.MF b/org.eclipse.om2m.binding.mqtt/META-INF/MANIFEST.MF new file mode 100644 index 00000000..a09741c6 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/META-INF/MANIFEST.MF @@ -0,0 +1,18 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: MQTT Binding +Bundle-SymbolicName: org.eclipse.om2m.binding.mqtt +Bundle-Version: 1.0.0.qualifier +Bundle-Activator: org.eclipse.om2m.binding.mqtt.Activator +Bundle-RequiredExecutionEnvironment: JavaSE-1.7 +Import-Package: org.apache.commons.logging, + org.eclipse.om2m.binding.service, + org.eclipse.om2m.commons.constants, + org.eclipse.om2m.commons.resource, + org.eclipse.om2m.core.service, + org.eclipse.om2m.datamapping.service, + org.osgi.framework;version="1.3.0", + org.osgi.util.tracker;version="1.5.1" +Bundle-ActivationPolicy: lazy +Bundle-ClassPath: libs/org.eclipse.paho.client.mqttv3-1.0.2.jar, + . diff --git a/org.eclipse.om2m.binding.mqtt/README b/org.eclipse.om2m.binding.mqtt/README new file mode 100644 index 00000000..e69de29b diff --git a/org.eclipse.om2m.binding.mqtt/build.properties b/org.eclipse.om2m.binding.mqtt/build.properties new file mode 100644 index 00000000..65cee246 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/build.properties @@ -0,0 +1,4 @@ +source.. = src/main/java/ +bin.includes = META-INF/,\ + .,\ + libs/org.eclipse.paho.client.mqttv3-1.0.2.jar diff --git a/org.eclipse.om2m.binding.mqtt/libs/org.eclipse.paho.client.mqttv3-1.0.2.jar b/org.eclipse.om2m.binding.mqtt/libs/org.eclipse.paho.client.mqttv3-1.0.2.jar new file mode 100644 index 00000000..9a881627 Binary files /dev/null and b/org.eclipse.om2m.binding.mqtt/libs/org.eclipse.paho.client.mqttv3-1.0.2.jar differ diff --git a/org.eclipse.om2m.binding.mqtt/pom.xml b/org.eclipse.om2m.binding.mqtt/pom.xml new file mode 100644 index 00000000..254787e9 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + org.eclipse.om2m.binding.mqtt + eclipse-plugin + + org.eclipse.om2m + org.eclipse.om2m + 1.0.0-SNAPSHOT + + \ No newline at end of file diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java new file mode 100644 index 00000000..0a74e784 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java @@ -0,0 +1,151 @@ +package org.eclipse.om2m.binding.mqtt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.om2m.binding.mqtt.util.DataMapperRegistry; +import org.eclipse.om2m.binding.service.RestClientService; +import org.eclipse.om2m.core.service.CseService; +import org.eclipse.om2m.datamapping.service.DataMapperService; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; + +public class Activator implements BundleActivator { + + private static BundleContext context; + + static BundleContext getContext() { + return context; + } + + private static final Log LOGGER = LogFactory.getLog(Activator.class); + + /** {@link DataMapperServiceTracker} reference */ + private static ServiceTracker dataMapperServiceTracker; + /** {@link CseService} reference */ + private static ServiceTracker cseServiceTracker; + + /** MQTT Request Handler that connects to the MQTT Broker */ + private static MqttRequestHandler mqttRequestHandler; + + public void start(BundleContext bundleContext) throws Exception { + Activator.context = bundleContext; + + // Listening on Cse Service + cseServiceTracker = new ServiceTracker( + bundleContext, CseService.class, + new CseServiceTrackerCustomizer()); + cseServiceTracker.open(); + + // Listening on DataMapper Service + dataMapperServiceTracker = new ServiceTracker( + bundleContext, DataMapperService.class, + new DataMapperServiceTracker()); + dataMapperServiceTracker.open(); + + // Registering RestClientService of MQTT + getContext().registerService(RestClientService.class, new MqttRestClient(), null); + } + + public void stop(BundleContext bundleContext) throws Exception { + Activator.context = null; + if (cseServiceTracker != null) { + cseServiceTracker.close(); + cseServiceTracker = null; + } + if (dataMapperServiceTracker != null) { + dataMapperServiceTracker.close(); + dataMapperServiceTracker = null; + } + if (mqttRequestHandler != null){ + mqttRequestHandler.close(); + mqttRequestHandler = null; + } + } + + private static class CseServiceTrackerCustomizer implements + ServiceTrackerCustomizer { + + @Override + public CseService addingService(ServiceReference reference) { + if (reference == null) { + return null; + } + Object service = Activator.getContext().getService(reference); + if (service != null && service instanceof CseService) { + LOGGER.debug("New CseService discovered"); + CseService cse = (CseService) service; + MqttRequestHandler.setCseService(cse); + if (mqttRequestHandler == null) { + new Thread() { + public void run() { + LOGGER.info("Creating MQTT Request Handler"); + mqttRequestHandler = new MqttRequestHandler(); + }; + }.start(); + } + return cse; + } + return null; + } + + @Override + public void modifiedService(ServiceReference reference, + CseService service) { + if (service != null) { + LOGGER.info("CseService modified"); + MqttRequestHandler.setCseService(service); + } + } + + @Override + public void removedService(ServiceReference reference, + CseService service) { + MqttRequestHandler.setCseService(null); + } + + } + + private static class DataMapperServiceTracker implements + ServiceTrackerCustomizer { + + @Override + public DataMapperService addingService( + ServiceReference reference) { + if (reference == null) { + return null; + } + Object service = Activator.getContext().getService(reference); + if (service != null && service instanceof DataMapperService) { + DataMapperService dms = (DataMapperService) service; + LOGGER.debug("New DataMapper Service discovered: " + + dms.getServiceDataType()); + DataMapperRegistry.register(dms); + return dms; + } + return null; + } + + @Override + public void modifiedService( + ServiceReference reference, + DataMapperService service) { + if (service != null) { + DataMapperRegistry.register(service); + } + } + + @Override + public void removedService( + ServiceReference reference, + DataMapperService service) { + if (service != null) { + DataMapperRegistry.remove(service); + } + } + + } + +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java new file mode 100644 index 00000000..1105a89c --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java @@ -0,0 +1,255 @@ +package org.eclipse.om2m.binding.mqtt; + +import java.math.BigInteger; +import java.util.regex.Matcher; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.om2m.binding.mqtt.util.DataMapperRegistry; +import org.eclipse.om2m.binding.mqtt.util.MqttConstants; +import org.eclipse.om2m.binding.mqtt.util.QueueSender; +import org.eclipse.om2m.commons.constants.Constants; +import org.eclipse.om2m.commons.constants.MimeMediaType; +import org.eclipse.om2m.commons.constants.ResponseStatusCode; +import org.eclipse.om2m.commons.resource.PrimitiveContent; +import org.eclipse.om2m.commons.resource.RequestPrimitive; +import org.eclipse.om2m.commons.resource.ResponsePrimitive; +import org.eclipse.om2m.core.service.CseService; +import org.eclipse.om2m.datamapping.service.DataMapperService; +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.MqttCallback; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; + +/** + * MQTT Request Handler class that subscribe to oneM2M request topic. + * When a request is received in the request topic, it is de-serialized and send + * to the CseService implementation available. Then the response from the service + * is serialized and sent to the oneM2M response topic. + */ +public class MqttRequestHandler implements MqttCallback { + + // Static attributes of the class + + /** MQTT Client ID */ + private static final String CLIENT_ID = Constants.CSE_ID; + /** Logger reference */ + private static final Log LOGGER = LogFactory.getLog(MqttRequestHandler.class); + /** MQTT Request Topic */ + private static final String REQUEST_TOPIC = "/oneM2M/req/+/" + Constants.CSE_ID + "/+"; + + + /** Reference to the current cseService implementation*/ + private static CseService cseService; + + /** + * Set the current CseService used when a request is + * received on the oneM2M request topic. + * @param cse the CseService implementation to use + */ + public static void setCseService(CseService cse) { + cseService = cse; + } + + // Private attributes + + /** MQTT Client from the Paho library */ + private MqttClient mainMqttClient; + + /** The MQTT connection options to use */ + private MqttConnectOptions connOpts; + + /** Tell the thread to keep retrying or not */ + private boolean retry = true; + /** Connection retry thread */ + private Thread retryThread; + + /** + * Default constructor of the Request Handler + */ + public MqttRequestHandler() { + MemoryPersistence persistence = new MemoryPersistence(); + String url = "tcp://" + MqttConstants.MQTT_BROKER_HOSTNAME + ":" + + MqttConstants.MQTT_BROKER_PORT; + this.connOpts = new MqttConnectOptions(); + connOpts.setCleanSession(true); + if(MqttConstants.MQTT_BROKER_USERNAME != null && MqttConstants.MQTT_BROKER_PASSWORD != null){ + connOpts.setUserName(MqttConstants.MQTT_BROKER_USERNAME); + connOpts.setPassword(MqttConstants.MQTT_BROKER_PASSWORD.toCharArray()); + } + try { + LOGGER.debug("Connecting MQTT client to: " + url); + this.mainMqttClient = new MqttClient(url, CLIENT_ID, persistence); + this.mainMqttClient.setCallback(MqttRequestHandler.this); + this.connect(this.connOpts); + } catch (MqttException e) { + LOGGER.error("Error in MQTT Client creation", e); + } + } + + /** + * Connect and retry if the connection fails + * @param connOpts + */ + private void connect(final MqttConnectOptions connOpts){ + if(retry && retryThread == null){ + retryThread = new Thread("mqtt-connection-retrier"){ + public void run() { + while(retry && !MqttRequestHandler.this.mainMqttClient.isConnected()){ + try { + MqttRequestHandler.this.mainMqttClient.connect(connOpts); + + LOGGER.info("Subscribing on MQTT topic: " + REQUEST_TOPIC); + MqttRequestHandler.this.mainMqttClient.subscribe(REQUEST_TOPIC, 2); + } catch (MqttException e) { + LOGGER.warn("Cannot connect to MQTT Broker, retrying in 10s. Cause: " + e.getMessage()); + } + if(!MqttRequestHandler.this.mainMqttClient.isConnected()){ + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + // Ignore + } + } + } + MqttRequestHandler.this.retryThread = null; + }; + }; + } + retryThread.start(); + } + + @Override + public void connectionLost(Throwable throwable) { + LOGGER.warn("Connection lost on MQTT Broker at " + MqttConstants.MQTT_BROKER_HOSTNAME + ":" + MqttConstants.MQTT_BROKER_PORT); + this.connect(this.connOpts); + } + + @Override + public void deliveryComplete(IMqttDeliveryToken token) { + // Empty + } + + @Override + public void messageArrived(String topic, MqttMessage message) + throws Exception { + Matcher matcher = MqttConstants.REQUEST_PATTERN_IN.matcher(topic); + if (matcher.matches()) { + String aeId = matcher.group(1); + String format = matcher.group(2); + String responseTopic = "/oneM2M/resp/" + Constants.CSE_ID + "/" + aeId + "/" + format; + + if (message.getPayload() == null) { + LOGGER.info("Null message received on " + topic); + sendErrorResponse("The message is null", responseTopic, aeId, format); + return; + } + String payload = new String(message.getPayload()); + LOGGER.debug("(" + topic + ") Message received (qos: " + message.getQos() + "):\n" + payload); + DataMapperService dms = DataMapperRegistry.getFromMqttFormat(format); + + if (dms == null) { + LOGGER.warn("MQTT Request received with unhandled content type: " + format); + sendErrorResponse("The format type is not handled", + responseTopic.replace("/" + format, "/" + MqttConstants.MQTT_XML), + aeId, MqttConstants.MQTT_XML); + return; + } + Object objectPayload = dms.stringToObj(payload); + if(objectPayload == null || !(objectPayload instanceof RequestPrimitive)){ + LOGGER.info("Invalid content provided in MQTT request"); + sendErrorResponse("Invalid content provided in request primitive", responseTopic, aeId, format); + return; + } + RequestPrimitive requestPrimitive = (RequestPrimitive) objectPayload; + requestPrimitive.setRequestContentType(MimeMediaType.OBJ); + requestPrimitive.setReturnContentType(MimeMediaType.OBJ); + // Primitive content handling + if(requestPrimitive.getPrimitiveContent() != null && + !requestPrimitive.getPrimitiveContent().getAny().isEmpty() && + requestPrimitive.getContent() == null){ + requestPrimitive.setContent(requestPrimitive.getPrimitiveContent().getAny().get(0)); + } + + ResponsePrimitive responsePrimitive = null; + + if(cseService != null){ + // Sending the request to the CSE + responsePrimitive = cseService.doRequest(requestPrimitive); + + // Handling the custom "content" field and map it to PrimitiveContent for serialization + if(responsePrimitive.getContent() != null && + responsePrimitive.getPrimitiveContent() == null){ + PrimitiveContent pc = new PrimitiveContent(); + pc.getAny().add(responsePrimitive.getContent()); + responsePrimitive.setPrimitiveContent(pc); + } + + // Building and sending response + final String responsePayload = dms.objToString(responsePrimitive); + LOGGER.debug("Response to be sent on topic: " + responseTopic + ". Payload:\n" + responsePayload); + + // Sending the request in another thread otherwise it blocks the reception thread of Paho + QueueSender.queue(mainMqttClient, responseTopic, responsePayload.getBytes()); + } else { + sendErrorResponse("/" + Constants.CSE_ID + " is not available", responseTopic, aeId, format, ResponseStatusCode.SERVICE_UNAVAILABLE); + } + + } else { + LOGGER.debug("The topic is not well formed. (" + topic + ")"); + } + } + + /** + * Util method that send an error message to the client + * @param message the message to send + * @param responseTopic the response topic to reply on + * @param aeId the id of the client + * @param format the format of exchange + */ + private void sendErrorResponse(String message, String responseTopic, + String aeId, String format, BigInteger responseStatusCode) { + ResponsePrimitive responsePrimitive = new ResponsePrimitive(); + responsePrimitive.setTo(aeId); + responsePrimitive.setFrom("/" + Constants.CSE_ID); + responsePrimitive.setResponseStatusCode(responseStatusCode); + responsePrimitive.setPrimitiveContent(new PrimitiveContent()); + responsePrimitive.getPrimitiveContent().getAny().add(message); + DataMapperService dms = DataMapperRegistry.getFromMqttFormat(format); + byte[] errorPayload = dms.objToString(responsePrimitive).getBytes(); + QueueSender.queue(mainMqttClient, responseTopic, errorPayload); + } + + /** + * Send a bad request response to the client + * @param message the message + * @param responseTopic the response topic + * @param aeId the id of the client + * @param format the format of exchange + */ + private void sendErrorResponse(String message, String responseTopic, + String aeId, String format){ + sendErrorResponse(message, responseTopic, aeId, format, ResponseStatusCode.BAD_REQUEST); + } + + /** + * Disconnecting and closing the MQTT Client. + */ + public void close(){ + // Disconnect the MQTT Client + try { + this.mainMqttClient.disconnect(); + } catch (MqttException e) { + LOGGER.debug("Error disconnecting the MQTT Client", e); + } + // Prevent on any reconnection retry + retry = false; + // Wake up the retry thread after the false on "retry" has been set + if(retryThread != null && retryThread.isAlive()){ + retryThread.interrupt(); + } + } +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java new file mode 100644 index 00000000..7936749b --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java @@ -0,0 +1,155 @@ +package org.eclipse.om2m.binding.mqtt; + +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.om2m.binding.mqtt.util.DataMapperRegistry; +import org.eclipse.om2m.binding.mqtt.util.MqttConstants; +import org.eclipse.om2m.binding.mqtt.util.QueueSender; +import org.eclipse.om2m.binding.mqtt.util.ResponseRegistry; +import org.eclipse.om2m.binding.mqtt.util.ResponseSemaphore; +import org.eclipse.om2m.binding.service.RestClientService; +import org.eclipse.om2m.commons.constants.Constants; +import org.eclipse.om2m.commons.constants.MimeMediaType; +import org.eclipse.om2m.commons.constants.ResponseStatusCode; +import org.eclipse.om2m.commons.resource.PrimitiveContent; +import org.eclipse.om2m.commons.resource.RequestPrimitive; +import org.eclipse.om2m.commons.resource.ResponsePrimitive; +import org.eclipse.om2m.datamapping.service.DataMapperService; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; + +public class MqttRestClient implements RestClientService { + + private static final Log LOGGER = LogFactory.getLog(MqttRestClient.class); + + @Override + public ResponsePrimitive sendRequest(RequestPrimitive requestPrimitive) { + if(requestPrimitive.getContent() != null){ + PrimitiveContent pc = new PrimitiveContent(); + switch(requestPrimitive.getRequestContentType()){ + case MimeMediaType.XML: + pc.getAny().add(DataMapperRegistry.get(MimeMediaType.XML).stringToObj((String)requestPrimitive.getContent())); + break; + case MimeMediaType.JSON: + pc.getAny().add(DataMapperRegistry.get(MimeMediaType.JSON).stringToObj((String)requestPrimitive.getContent())); + break; + case MimeMediaType.OBJ: case MimeMediaType.TEXT_PLAIN: + pc.getAny().add(requestPrimitive.getContent()); + break; + default: + break; + } + if(!pc.getAny().isEmpty()){ + requestPrimitive.setPrimitiveContent(pc); + } + } + + ResponsePrimitive responsePrimitive = new ResponsePrimitive(requestPrimitive); + + if(requestPrimitive.getMqttTopic() == null || requestPrimitive.getMqttUri() == null){ + responsePrimitive.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST); + return responsePrimitive; + } + + if(requestPrimitive.getRequestIdentifier() == null){ + requestPrimitive.setRequestIdentifier(UUID.randomUUID().toString()); + } + + String uri = requestPrimitive.getMqttUri(); + if(uri.startsWith("mqtt://")){ + uri = uri.replaceFirst("mqtt://", "tcp://"); + } + + if(requestPrimitive.getTo().startsWith("mqtt://")){ + Pattern mqttUriPatter = Pattern.compile("(mqtt://[^:/]*(:[0-9]{1,5})?)(/.*)"); + Matcher matcher = mqttUriPatter.matcher(requestPrimitive.getTo()); + if(matcher.matches()){ + requestPrimitive.setTo(matcher.group(3)); + } + } + + String topic = requestPrimitive.getMqttTopic(); + String payload = null; + String format = null; + if (topic.endsWith("/json")){ + payload = DataMapperRegistry.get(MimeMediaType.JSON).objToString(requestPrimitive); + format = "json"; + } else { + // Case of XML and default + payload = DataMapperRegistry.get(MimeMediaType.XML).objToString(requestPrimitive); + format = "xml"; + } + + try { + MqttClient mqttClient = new MqttClient(uri, requestPrimitive.getRequestIdentifier(), new MemoryPersistence()); + mqttClient.connect(); + LOGGER.debug("Sending request on topic: " + topic + " with payload:\n" + payload); + ResponseSemaphore responseSemaphore = null; + if(requestPrimitive.isMqttResponseExpected()){ + Matcher matcher = MqttConstants.REQUEST_PATTERN_OUT.matcher(topic); + if(matcher.matches()){ + String responseTopic = "/oneM2M/resp/" + matcher.group(1) + "/"+ Constants.CSE_ID + "/" + format; + responseSemaphore = ResponseRegistry.createSemaphore(requestPrimitive.getRequestIdentifier(), mqttClient, responseTopic); + } else { + responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); + } + } else { + mqttClient.publish(topic, new MqttMessage(payload.getBytes())); + responsePrimitive.setResponseStatusCode(ResponseStatusCode.OK); + } + if(responseSemaphore != null){ + QueueSender.queue(mqttClient, topic, payload.getBytes()); + LOGGER.debug("Waiting for response... (" + MqttConstants.TIME_OUT_DURATION + "s)"); + boolean released = responseSemaphore.getSemaphore().tryAcquire(1, MqttConstants.TIME_OUT_DURATION, TimeUnit.SECONDS); + if(released){ + responsePrimitive = responseSemaphore.getResponsePrimitive(); + fillAndConvertContent(requestPrimitive, responsePrimitive); + LOGGER.debug("Response received: " + responsePrimitive); + } else { + responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); + } + } + mqttClient.disconnect(); + mqttClient.close(); + } catch (MqttException e) { + LOGGER.warn("Cannot connect to: " + requestPrimitive.getMqttUri()); + responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); + return responsePrimitive; + } catch (InterruptedException e) { + LOGGER.error("Interrupted exception caught in MqttRestClient: " + e.getMessage()); + responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); + return responsePrimitive; + } + + return responsePrimitive; + } + + private void fillAndConvertContent(RequestPrimitive requestPrimitive, + ResponsePrimitive responsePrimitive) { + if(responsePrimitive.getPrimitiveContent() != null && + !responsePrimitive.getPrimitiveContent().getAny().isEmpty() && + responsePrimitive.getContent() == null){ + if(requestPrimitive.getReturnContentType().equals(MimeMediaType.OBJ)){ + responsePrimitive.setContent(responsePrimitive.getPrimitiveContent().getAny().get(0)); + } else { + DataMapperService dms = DataMapperRegistry.get(requestPrimitive.getReturnContentType()); + String content = dms.objToString(responsePrimitive.getPrimitiveContent().getAny().get(0)); + responsePrimitive.setContent(content); + responsePrimitive.setContentType(requestPrimitive.getReturnContentType()); + } + } + } + + @Override + public String getProtocol() { + return MqttConstants.PROTOCOL; + } + +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java new file mode 100644 index 00000000..18127ab3 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java @@ -0,0 +1,75 @@ +package org.eclipse.om2m.binding.mqtt.util; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.om2m.commons.constants.MimeMediaType; +import org.eclipse.om2m.datamapping.service.DataMapperService; + +/** + * This class is used to store instances of {@link DataMapperService} classes. + * + */ +public class DataMapperRegistry { + + /** Private constructor to avoid creation */ + private DataMapperRegistry(){} + + /** + * Service registry classified by data type handled. + */ + private static Map serviceRegistery = new HashMap(); + + /** + * Add a new {@link DataMapperService} to the registery. + * @param dms the service to register + */ + public static void register(DataMapperService dms){ + if(dms != null && dms.getServiceDataType() != null){ + serviceRegistery.put(dms.getServiceDataType(), dms); + } + } + + /** + * Retrieve a {@link DataMapperService} from a data type. + * @param dataType the + * @return the {@link DataMapperService} that handle the data type or null if none + */ + public static DataMapperService get(String dataType){ + return serviceRegistery.get(dataType); + } + + /** + * Remove the {@link DataMapperService} from the registry + * @param dataType the data type of the service to remove + */ + public static void remove(String dataType){ + serviceRegistery.remove(dataType); + } + + /** + * Remove the {@link DataMapperService} from the registry + * @param dms the service to remove from the registry + */ + public static void remove(DataMapperService dms){ + remove(dms.getServiceDataType()); + } + + /** + * Retrieve the {@link DataMapperService} from the registry + * from the MQTT format String + * @param format the format of the DMS + * @return the DMS with the specified format or null + */ + public static DataMapperService getFromMqttFormat(String format){ + switch (format) { + case "xml": + return DataMapperRegistry.get(MimeMediaType.XML); + case "json": + return DataMapperRegistry.get(MimeMediaType.JSON); + default: + return null; + } + } + +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java new file mode 100644 index 00000000..24fceff6 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java @@ -0,0 +1,48 @@ +package org.eclipse.om2m.binding.mqtt.util; + +import java.util.regex.Pattern; + +import org.eclipse.om2m.commons.constants.Constants; + +/** + * A set of MQTT constants retrieved from the System.getProperty method. + * + */ +public final class MqttConstants { + + private MqttConstants(){} + + /** Hostname of the main broker */ + public static final String MQTT_BROKER_HOSTNAME = System.getProperty("org.eclipse.om2m.mqtt.ip", "127.0.0.1"); + + /** IP of the main broker */ + public static final int MQTT_BROKER_PORT = Integer.valueOf(System.getProperty("org.eclipse.om2m.mqtt.port", "1883")); + + /** Username to connect to broker */ + public static final String MQTT_BROKER_USERNAME = System.getProperty("org.eclipse.om2m.mqtt.username"); + + /** Password to connect to broker */ + public static final String MQTT_BROKER_PASSWORD = System.getProperty("org.eclipse.om2m.mqtt.password"); + + /** MQTT Protocol prefix */ + public static final String PROTOCOL = "mqtt"; + + /** Size of the request sender queue */ + public static final int MQTT_QUEUE_SENDER_SIZE = Integer.valueOf(System.getProperty("org.eclipse.om2m.mqtt.queue.size", "8")); + + /** Request pattern to parse the request topic on message reception */ + public static final Pattern REQUEST_PATTERN_IN = Pattern.compile("/oneM2M/req/([^/]+)/" + Constants.CSE_ID + "/(.*)"); + + /** Request pattern when sending a message. */ + public static final Pattern REQUEST_PATTERN_OUT = Pattern.compile("/oneM2M/req/" + Constants.CSE_ID+ "/([^/]+)+/(.*)"); + + /** Time out duration when waiting for a response. Unit in second. */ + public static final long TIME_OUT_DURATION = Long.valueOf(System.getProperty("org.eclipse.om2m.mqtt.timeout", "20")); + + /** MQTT format for XML in topic */ + public static final String MQTT_XML = "xml"; + + /** MQTT format for JSON in topic */ + public static final String MQTT_JSON = "json"; + +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java new file mode 100644 index 00000000..43be0ecb --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java @@ -0,0 +1,60 @@ +package org.eclipse.om2m.binding.mqtt.util; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttException; + +public final class QueueSender { + + private static final Log LOGGER = LogFactory.getLog(QueueSender.class); + private static ExecutorService threadPool; + + static { + int queueSize = MqttConstants.MQTT_QUEUE_SENDER_SIZE <= 2 ? 2 + : MqttConstants.MQTT_QUEUE_SENDER_SIZE; + threadPool = new ThreadPoolExecutor(2, queueSize, 1, TimeUnit.MINUTES, + new SynchronousQueue()); + } + + public static void queue(MqttClient mqttClient, String topic, byte[] payload){ + LOGGER.debug("Sending MQTT message to " + mqttClient.getServerURI() + " topic: " + topic); + threadPool.execute(new MqttSender(mqttClient, topic, payload)); + } + + private static class MqttSender implements Runnable { + + private MqttClient mqttClient; + private String topic; + private byte[] payload; + + public MqttSender(MqttClient mqttClient, String topic, byte[] payload) { + super(); + this.mqttClient = mqttClient; + this.topic = topic; + this.payload = payload; + } + + @Override + public void run() { + try { + this.mqttClient.publish(topic, payload, 1, false); + } catch (MqttException e) { + LOGGER.warn("Error publishing on topic: " + this.topic + + " of broker " + this.mqttClient.getServerURI() + + ". Error: " + e.getMessage()); + } + } + + } + + private QueueSender(){ + // Empty and private constructor to avoid class creation + } + +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java new file mode 100644 index 00000000..20c7f246 --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java @@ -0,0 +1,99 @@ +package org.eclipse.om2m.binding.mqtt.util; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Semaphore; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.om2m.commons.constants.Constants; +import org.eclipse.om2m.commons.constants.MimeMediaType; +import org.eclipse.om2m.commons.resource.ResponsePrimitive; +import org.eclipse.om2m.datamapping.service.DataMapperService; +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.MqttCallback; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; + +public final class ResponseRegistry { + + private ResponseRegistry() { + // Empty and private constructor to avoid instantiation of this class + } + + private static final Map responseMap = new HashMap(); + + public static ResponseSemaphore createSemaphore(String requestIdentifier, MqttClient mqttClient, + String responseTopic) throws MqttException{ + synchronized (responseMap) { + mqttClient.setCallback(new ResponseCallback()); + mqttClient.subscribe(responseTopic, 1); + if(!responseMap.containsKey(requestIdentifier)){ + ResponseSemaphore respSemaphore = new ResponseSemaphore(new Semaphore(0)); + responseMap.put(requestIdentifier, respSemaphore); + return respSemaphore; + } + return null; + } + } + + private static void responseReceived(ResponsePrimitive responsePrimitive){ + synchronized (responseMap) { + if(responseMap.containsKey(responsePrimitive.getRequestIdentifier())){ + ResponseSemaphore responseSemanphore = responseMap.get(responsePrimitive.getRequestIdentifier()); + responseSemanphore.setResponsePrimitive(responsePrimitive); + responseSemanphore.getSemaphore().release(); + responseMap.remove(responsePrimitive.getRequestIdentifier()); + } + } + } + + private static class ResponseCallback implements MqttCallback { + + private static Pattern responsePattern = Pattern.compile("/oneM2M/resp/([^/]+)/" + Constants.CSE_ID + "/(.*)"); + + @Override + public void connectionLost(Throwable cause) { + // Ignore + } + + @Override + public void deliveryComplete(IMqttDeliveryToken token) { + // Ignore + } + + @Override + public void messageArrived(String topic, MqttMessage message) + throws Exception { + Matcher matcher = responsePattern.matcher(topic); + if(!matcher.matches()){ + return; + } + String format = matcher.group(2); + DataMapperService dms = null; + switch (format) { + case "xml": + dms = DataMapperRegistry.get(MimeMediaType.XML); + break; + case "json": + dms = DataMapperRegistry.get(MimeMediaType.JSON); + break; + default: + break; + } + if(dms == null){ + // The format is not handled here + return; + } + String payload = new String(message.getPayload()); + ResponsePrimitive resp = (ResponsePrimitive) dms.stringToObj(payload); + if(resp == null || resp.getRequestIdentifier() == null){ + return; + } + responseReceived(resp); + } + + } + +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java new file mode 100644 index 00000000..7588712d --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java @@ -0,0 +1,32 @@ +package org.eclipse.om2m.binding.mqtt.util; + +import java.util.concurrent.Semaphore; + +import org.eclipse.om2m.commons.resource.ResponsePrimitive; + +public class ResponseSemaphore { + + private Semaphore semaphore; + private ResponsePrimitive responsePrimitive; + + public ResponseSemaphore(Semaphore semaphore) { + this.semaphore = semaphore; + } + + public Semaphore getSemaphore() { + return semaphore; + } + + public void setSemaphore(Semaphore semaphore) { + this.semaphore = semaphore; + } + + public ResponsePrimitive getResponsePrimitive() { + return responsePrimitive; + } + + public void setResponsePrimitive(ResponsePrimitive responsePrimitive) { + this.responsePrimitive = responsePrimitive; + } + +} diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java new file mode 100644 index 00000000..219fa66b --- /dev/null +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java @@ -0,0 +1,19 @@ +package org.eclipse.om2m.binding.mqtt.util; + +import org.eclipse.om2m.commons.resource.ResponsePrimitive; + +public final class Utils { + + public static void fillPrimitiveContent(){ + + } + + public static void fillContent(ResponsePrimitive requestPrimitive){ + if(requestPrimitive.getPrimitiveContent() != null && + !requestPrimitive.getPrimitiveContent().getAny().isEmpty() && + requestPrimitive.getContent() == null){ + requestPrimitive.setContent(requestPrimitive.getPrimitiveContent().getAny().get(0)); + } + } + +} diff --git a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/RequestPrimitive.java b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/RequestPrimitive.java index 7bb68b11..9e351993 100644 --- a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/RequestPrimitive.java +++ b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/RequestPrimitive.java @@ -135,7 +135,15 @@ public class RequestPrimitive { protected String requestContentType; @XmlTransient protected Map> queryStrings; - + @XmlTransient + protected String targetId; + @XmlTransient + protected String mqttTopic; + @XmlTransient + protected String mqttUri; + @XmlTransient + protected boolean mqttResponseExpected = true; + /** * @return the queryStrings */ @@ -558,7 +566,7 @@ public class RequestPrimitive { * @return the targetId */ public String getTargetId() { - return this.getTo(); + return this.targetId; } /** @@ -566,7 +574,7 @@ public class RequestPrimitive { * the targetId to set */ public void setTargetId(String targetId) { - this.setTo(targetId); + this.targetId= targetId; } /** @@ -597,6 +605,38 @@ public class RequestPrimitive { this.requestContentType = requestContentType; } + public PrimitiveContent getPrimitiveContent() { + return primitiveContent; + } + + public void setPrimitiveContent(PrimitiveContent primitiveContent) { + this.primitiveContent = primitiveContent; + } + + public String getMqttTopic() { + return mqttTopic; + } + + public void setMqttTopic(String mqttTopic) { + this.mqttTopic = mqttTopic; + } + + public String getMqttUri() { + return mqttUri; + } + + public void setMqttUri(String mqttUri) { + this.mqttUri = mqttUri; + } + + public boolean isMqttResponseExpected() { + return mqttResponseExpected; + } + + public void setMqttResponseExpected(boolean mqttResponseExpected) { + this.mqttResponseExpected = mqttResponseExpected; + } + /* (non-Javadoc) * @see java.lang.Object#toString() */ @@ -675,5 +715,5 @@ public class RequestPrimitive { result.to = this.to; return result; } - + } diff --git a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/ResponsePrimitive.java b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/ResponsePrimitive.java index 93b6b1b0..d6ad846b 100644 --- a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/ResponsePrimitive.java +++ b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/resource/ResponsePrimitive.java @@ -331,7 +331,7 @@ public class ResponsePrimitive { this.content = content; } - public PrimitiveContent getPritimitiveContent(){ + public PrimitiveContent getPrimitiveContent(){ return content; } diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FanOutPointController.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FanOutPointController.java index 1fc142a4..424ea3fb 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FanOutPointController.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/FanOutPointController.java @@ -158,7 +158,7 @@ public class FanOutPointController extends Controller { public ResponsePrimitive call() throws Exception { ResponsePrimitive resp = new Router().doRequest(request); resp.setPrimitiveContent(new PrimitiveContent()); - resp.getPritimitiveContent().getAny().add(resp.getContent()); + resp.getPrimitiveContent().getAny().add(resp.getContent()); return resp; } diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java index 4ea0d702..191f26cb 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java @@ -22,6 +22,8 @@ package org.eclipse.om2m.core.notifier; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -133,6 +135,25 @@ public class Notifier { LOGGER.info("Sending notify request to: " + contact); if(contact.matches(".*://.*")){ // Contact = protocol-dependent -> direct notification using the rest client. + // In case of MQTT, the URI of the broker and the Topic has to be handled separatly + if(contact.startsWith("mqtt://")){ + Pattern mqttUriPattern = Pattern.compile("(mqtt://[^:/]*(:[0-9]{1,5})?)(/.*)"); + Matcher matcher = mqttUriPattern.matcher(contact); + if(matcher.matches()){ + String uri = matcher.group(1); + String topic = matcher.group(3) == null ? "" : matcher.group(3).substring(1); + request.setMqttTopic(topic); + request.setMqttUri(uri); + // We do not want to wait for a response on AE topic + request.setMqttResponseExpected(false); + } else { + ResponsePrimitive resp = new ResponsePrimitive(request); + resp.setResponseStatusCode(ResponseStatusCode.BAD_REQUEST); + resp.setContent("Error in mqtt URI"); + resp.setContentType(MimeMediaType.TEXT_PLAIN); + return resp; + } + } request.setTo(contact); return RestClient.sendRequest(request); }else{ @@ -267,7 +288,7 @@ public class Notifier { for(final String uri : sub.getNotificationURI()){ CoreExecutor.postThread(new Runnable(){ public void run() { - Notifier.notify(request, uri); + Notifier.notify(request, uri); }; }); } diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java index fd20c05b..b5132356 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java @@ -19,6 +19,9 @@ *******************************************************************************/ package org.eclipse.om2m.core.redirector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.om2m.commons.constants.CSEType; @@ -114,15 +117,32 @@ public class Redirector { LOGGER.debug("Removing / at the end of poa: " + url); url = url.substring(0, url.length() - 1); } + - if(request.getTo().startsWith("//")){ - url += request.getTo().replaceFirst("//", "/_/"); - } else if(request.getTo().startsWith("/")){ - url += request.getTo().replaceFirst("/", "/~/"); + if(url.startsWith("mqtt://")){ + url += request.getTo(); + Pattern mqttUriPattern = Pattern.compile("(mqtt://[^:/]*(:[0-9]{1,5})?)(/.*)"); + Matcher matcher = mqttUriPattern.matcher(url); + if(matcher.matches()){ + // FIXME we need a response but not yet implemented in MQTT binding + request.setMqttResponseExpected(true); + // TODO Format type can be enhanced + request.setMqttTopic("/oneM2M/req/" + Constants.CSE_ID + "/" + csrEntity.getRemoteCseId().replaceAll("/", "") + "/xml"); + request.setMqttUri(matcher.group(1)); + } else { + LOGGER.warn("Incorrect MQTT URI specified in remoteCSE: " + url); + i++; + continue; + } } else { - url+= "/" + request.getTo(); + if(request.getTo().startsWith("//")){ + url += request.getTo().replaceFirst("//", "/_/"); + } else if(request.getTo().startsWith("/")){ + url += request.getTo().replaceFirst("/", "/~/"); + } else { + url+= "/" + request.getTo(); + } } - request.setTo(url); ResponsePrimitive response = RestClient.sendRequest(request); if(!(response.getResponseStatusCode() @@ -192,6 +212,26 @@ public class Redirector { done = true; } else { request.setTo(poa); + if(poa.startsWith("mqtt://")){ + Pattern mqttUriPattern = Pattern.compile("(mqtt://[^:/]*(:[0-9]{1,5})?)(/.*)?"); + Matcher matcher = mqttUriPattern.matcher(poa); + if(matcher.matches()){ + String topic = matcher.group(3); + String aeId = ae.getAeid(); + if(topic != null){ + request.setMqttTopic(topic); + request.setMqttResponseExpected(false); + } else { + request.setMqttTopic("/oneM2M/req/" + Constants.CSE_ID + "/" + aeId + "/xml"); + request.setMqttResponseExpected(true); + } + request.setMqttUri(matcher.group(1)); + } else { + LOGGER.warn("POA is incorrect for MQTT: " + poa); + i++; + continue; + } + } response = RestClient.sendRequest(request); if(!response.getResponseStatusCode().equals(ResponseStatusCode.TARGET_NOT_REACHABLE)){ done = true; diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java index 44f4f5c2..44b27cb3 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java @@ -126,6 +126,10 @@ public class Router implements CseService { } // URI Handling + if(request.getTo() == null && request.getTargetId() == null){ + throw new BadRequestException("No To parameter provided provided"); + } + if(request.getTargetId() == null){ request.setTargetId(request.getTo()); } diff --git a/org.eclipse.om2m.datamapping.jaxb/src/main/resources/json-binding.xml b/org.eclipse.om2m.datamapping.jaxb/src/main/resources/json-binding.xml index 74b8d2ab..583a3ee1 100644 --- a/org.eclipse.om2m.datamapping.jaxb/src/main/resources/json-binding.xml +++ b/org.eclipse.om2m.datamapping.jaxb/src/main/resources/json-binding.xml @@ -19,6 +19,19 @@ + + + + + + + + + + + + + diff --git a/org.eclipse.om2m.site.in-cse/om2m.product b/org.eclipse.om2m.site.in-cse/om2m.product index 517b1d71..47305cb7 100644 --- a/org.eclipse.om2m.site.in-cse/om2m.product +++ b/org.eclipse.om2m.site.in-cse/om2m.product @@ -52,6 +52,7 @@ + diff --git a/pom.xml b/pom.xml index 9ee48c01..e365d1c3 100644 --- a/pom.xml +++ b/pom.xml @@ -122,6 +122,7 @@ org.eclipse.om2m.commons.logging org.eclipse.om2m.binding.http org.eclipse.om2m.binding.coap + org.eclipse.om2m.binding.mqtt org.eclipse.om2m.binding.service org.eclipse.om2m.core.service org.eclipse.om2m.datamapping.jaxb -- cgit v1.2.3 From f113b0cf0394310a918b5a4c79f3bc6ef7a34182 Mon Sep 17 00:00:00 2001 From: Francois Aissaoui Date: Mon, 3 Oct 2016 17:01:21 +0200 Subject: Fix the MQTT README which was empty Change-Id: I3e5960a7ad0ed2c3b70186585afaa4b71b8cd4f4 Signed-off-by: Francois Aissaoui --- org.eclipse.om2m.binding.mqtt/README | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/org.eclipse.om2m.binding.mqtt/README b/org.eclipse.om2m.binding.mqtt/README index e69de29b..13e6cde9 100644 --- a/org.eclipse.om2m.binding.mqtt/README +++ b/org.eclipse.om2m.binding.mqtt/README @@ -0,0 +1,9 @@ +This binding makes the integration of the MQTT Protocol into the OM2M platform following +the oneM2M document TS-0010 v2.4. + +This does NOT include a MQTT Broker. It has to be deployed seperatly. +The link to the broker is set via the constants: + org.eclipse.om2m.mqtt.ip + org.eclipse.om2m.mqtt.port + +For other constants, see java class: org.eclipse.om2m.binding.mqtt.util.MqttConstants.java -- cgit v1.2.3 From 8eff8ba84776f83dd53ff22251cdef2345dbd67e Mon Sep 17 00:00:00 2001 From: Guillaume Garzone Date: Mon, 3 Oct 2016 17:02:58 +0200 Subject: Fix missing Copyrights Change-Id: Ica3a5d829cbb8a402be125a8d38f48a785811491 Signed-off-by: Guillaume Garzone --- org.eclipse.om2m.binding.mqtt/pom.xml | 19 +++++++++++++++++++ .../java/org/eclipse/om2m/binding/mqtt/Activator.java | 19 +++++++++++++++++++ .../eclipse/om2m/binding/mqtt/MqttRequestHandler.java | 19 +++++++++++++++++++ .../org/eclipse/om2m/binding/mqtt/MqttRestClient.java | 19 +++++++++++++++++++ .../om2m/binding/mqtt/util/DataMapperRegistry.java | 19 +++++++++++++++++++ .../eclipse/om2m/binding/mqtt/util/MqttConstants.java | 19 +++++++++++++++++++ .../eclipse/om2m/binding/mqtt/util/QueueSender.java | 19 +++++++++++++++++++ .../om2m/binding/mqtt/util/ResponseRegistry.java | 19 +++++++++++++++++++ .../om2m/binding/mqtt/util/ResponseSemaphore.java | 19 +++++++++++++++++++ .../org/eclipse/om2m/binding/mqtt/util/Utils.java | 19 +++++++++++++++++++ 10 files changed, 190 insertions(+) diff --git a/org.eclipse.om2m.binding.mqtt/pom.xml b/org.eclipse.om2m.binding.mqtt/pom.xml index 254787e9..46e30e61 100644 --- a/org.eclipse.om2m.binding.mqtt/pom.xml +++ b/org.eclipse.om2m.binding.mqtt/pom.xml @@ -1,3 +1,22 @@ + 4.0.0 diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java index 0a74e784..c8f126f0 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt; import org.apache.commons.logging.Log; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java index 1105a89c..779e4dee 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt; import java.math.BigInteger; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java index 7936749b..95ba01ce 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt; import java.util.UUID; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java index 18127ab3..d4e4741a 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.HashMap; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java index 24fceff6..5f0f546c 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.regex.Pattern; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java index 43be0ecb..d6db3593 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.concurrent.ExecutorService; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java index 20c7f246..b77bf73b 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.HashMap; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java index 7588712d..037c3360 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.concurrent.Semaphore; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java index 219fa66b..56766fd7 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import org.eclipse.om2m.commons.resource.ResponsePrimitive; -- cgit v1.2.3 From 93fbfb5e0edfd76f502d85da182c6603cc9feda7 Mon Sep 17 00:00:00 2001 From: Francois Aissaoui Date: Mon, 3 Oct 2016 17:21:38 +0200 Subject: Fix copyrights --- org.eclipse.om2m.binding.mqtt/pom.xml | 19 +++++++++++++++++++ .../java/org/eclipse/om2m/binding/mqtt/Activator.java | 19 +++++++++++++++++++ .../eclipse/om2m/binding/mqtt/MqttRequestHandler.java | 19 +++++++++++++++++++ .../org/eclipse/om2m/binding/mqtt/MqttRestClient.java | 19 +++++++++++++++++++ .../om2m/binding/mqtt/util/DataMapperRegistry.java | 19 +++++++++++++++++++ .../eclipse/om2m/binding/mqtt/util/MqttConstants.java | 19 +++++++++++++++++++ .../eclipse/om2m/binding/mqtt/util/QueueSender.java | 19 +++++++++++++++++++ .../om2m/binding/mqtt/util/ResponseRegistry.java | 19 +++++++++++++++++++ .../om2m/binding/mqtt/util/ResponseSemaphore.java | 19 +++++++++++++++++++ .../org/eclipse/om2m/binding/mqtt/util/Utils.java | 19 +++++++++++++++++++ 10 files changed, 190 insertions(+) diff --git a/org.eclipse.om2m.binding.mqtt/pom.xml b/org.eclipse.om2m.binding.mqtt/pom.xml index 254787e9..46e30e61 100644 --- a/org.eclipse.om2m.binding.mqtt/pom.xml +++ b/org.eclipse.om2m.binding.mqtt/pom.xml @@ -1,3 +1,22 @@ + 4.0.0 diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java index 0a74e784..c8f126f0 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/Activator.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt; import org.apache.commons.logging.Log; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java index 1105a89c..779e4dee 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRequestHandler.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt; import java.math.BigInteger; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java index 7936749b..95ba01ce 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/MqttRestClient.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt; import java.util.UUID; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java index 18127ab3..d4e4741a 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/DataMapperRegistry.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.HashMap; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java index 24fceff6..5f0f546c 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/MqttConstants.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.regex.Pattern; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java index 43be0ecb..d6db3593 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/QueueSender.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.concurrent.ExecutorService; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java index 20c7f246..b77bf73b 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseRegistry.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.HashMap; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java index 7588712d..037c3360 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/ResponseSemaphore.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import java.util.concurrent.Semaphore; diff --git a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java index 219fa66b..56766fd7 100644 --- a/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java +++ b/org.eclipse.om2m.binding.mqtt/src/main/java/org/eclipse/om2m/binding/mqtt/util/Utils.java @@ -1,3 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr) + * 7 Colonel Roche 31077 Toulouse - France + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Initial Contributors: + * Thierry Monteil : Project manager, technical co-manager + * Mahdi Ben Alaya : Technical co-manager + * Samir Medjiah : Technical co-manager + * Khalil Drira : Strategy expert + * Guillaume Garzone : Developer + * François Aïssaoui : Developer + * + * New contributors : + *******************************************************************************/ package org.eclipse.om2m.binding.mqtt.util; import org.eclipse.om2m.commons.resource.ResponsePrimitive; -- cgit v1.2.3 From 24b27ad9b1f71f6db093d116ebdfcc0a25aea930 Mon Sep 17 00:00:00 2001 From: Mahdi Ben Alaya Date: Mon, 14 Nov 2016 01:07:11 +0100 Subject: Fix Bug 507443 - Use JSON format by default for notifications Signed-off-by: Mahdi Ben Alaya --- .../main/java/org/eclipse/om2m/core/notifier/Notifier.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java index 4ea0d702..c80439af 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java @@ -109,12 +109,12 @@ public class Notifier { notification.setSubscriptionReference(subscriptionEntity.getHierarchicalURI()); notification.setSubscriptionDeletion(false); RequestPrimitive notifRequest = new RequestPrimitive(); - notifRequest.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.XML).objToString(notification)); + notifRequest.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.JSON).objToString(notification)); notifRequest.setFrom("/" + Constants.CSE_ID); notifRequest.setTo(uri); notifRequest.setOperation(Operation.NOTIFY); - notifRequest.setRequestContentType(MimeMediaType.XML); - notifRequest.setReturnContentType(MimeMediaType.XML); + notifRequest.setRequestContentType(MimeMediaType.JSON); + notifRequest.setReturnContentType(MimeMediaType.JSON); ResponsePrimitive resp = notify(notifRequest, uri); if(resp.getResponseStatusCode().equals(ResponseStatusCode.TARGET_NOT_REACHABLE)){ throw new Om2mException("Error during the verification request", @@ -254,15 +254,15 @@ public class Notifier { if(sub.getNotificationContentType().equals(NotificationContentType.MODIFIED_ATTRIBUTES)){ serializableResource = (Resource)mapper.mapEntityToResource(resource, ResultContent.ATTRIBUTES); notification.getNotificationEvent().setRepresentation(serializableResource); - request.setRequestContentType(MimeMediaType.XML); + request.setRequestContentType(MimeMediaType.JSON); } else if(sub.getNotificationContentType().equals(NotificationContentType.WHOLE_RESOURCE)){ serializableResource = (Resource) mapper.mapEntityToResource(resource, ResultContent.ATTRIBUTES); notification.getNotificationEvent().setRepresentation(serializableResource); - request.setRequestContentType(MimeMediaType.XML); + request.setRequestContentType(MimeMediaType.JSON); } } // Set the content - request.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.XML).objToString(notification)); + request.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.JSON).objToString(notification)); // For each notification URI: send the notify request for(final String uri : sub.getNotificationURI()){ CoreExecutor.postThread(new Runnable(){ -- cgit v1.2.3 From 117eafe2fe316490af97f8e1c58632811dadb38f Mon Sep 17 00:00:00 2001 From: Francois Aissaoui Date: Thu, 17 Nov 2016 10:13:53 +0100 Subject: [Bug 507561] Fix closable HTTP client Change-Id: I69abdc09134873db5d3dd223aff69efad362005f Signed-off-by: Francois Aissaoui --- .../org/eclipse/om2m/binding/http/RestHttpClient.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java b/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java index bf31768b..8ed1e279 100644 --- a/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java +++ b/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java @@ -39,7 +39,7 @@ import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.HttpClientBuilder; import org.eclipse.om2m.binding.http.constants.HttpHeaders; import org.eclipse.om2m.binding.http.constants.HttpParameters; import org.eclipse.om2m.binding.service.RestClientService; @@ -79,11 +79,12 @@ public class RestHttpClient implements RestClientService { */ public ResponsePrimitive sendRequest(RequestPrimitive requestPrimitive) { LOGGER.info("Sending request: " + requestPrimitive); - CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpClient httpClient = HttpClientBuilder.create() + .disableAutomaticRetries() + .build(); ResponsePrimitive responsePrimitive = new ResponsePrimitive(requestPrimitive); HttpUriRequest method = null; - // Retrieve the url String url = requestPrimitive.getTo(); if(!url.startsWith(protocol+"://")){ @@ -212,7 +213,6 @@ public class RestHttpClient implements RestClientService { responsePrimitive.setLocation(contentHeader); } LOGGER.info("Http Client response: " + responsePrimitive); - httpClient.close(); } catch(HttpHostConnectException e){ LOGGER.info("Target is not reachable: " + requestPrimitive.getTo()); responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); @@ -221,7 +221,14 @@ public class RestHttpClient implements RestClientService { } catch (IOException e){ LOGGER.error(url + " not found", e); responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); - } + } finally { + try { + httpClient.close(); + } catch (IOException e) { + // Silently fail + LOGGER.trace("Fail closing the http client", e); + } + } return responsePrimitive; } -- cgit v1.2.3 From 80da928e4ed79e30efb3b961caae5d5a5e0e80a2 Mon Sep 17 00:00:00 2001 From: benalaya Date: Wed, 30 Nov 2016 07:04:58 +0900 Subject: Revert "[Bug 507561] Fix closable HTTP client. Regression detected in Notification procedure " This reverts commit 117eafe2fe316490af97f8e1c58632811dadb38f. Signed-off-by: Mahdi Ben Alaya --- .../org/eclipse/om2m/binding/http/RestHttpClient.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java b/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java index 8ed1e279..bf31768b 100644 --- a/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java +++ b/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java @@ -39,7 +39,7 @@ import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; import org.eclipse.om2m.binding.http.constants.HttpHeaders; import org.eclipse.om2m.binding.http.constants.HttpParameters; import org.eclipse.om2m.binding.service.RestClientService; @@ -79,12 +79,11 @@ public class RestHttpClient implements RestClientService { */ public ResponsePrimitive sendRequest(RequestPrimitive requestPrimitive) { LOGGER.info("Sending request: " + requestPrimitive); - CloseableHttpClient httpClient = HttpClientBuilder.create() - .disableAutomaticRetries() - .build(); + CloseableHttpClient httpClient = HttpClients.createDefault(); ResponsePrimitive responsePrimitive = new ResponsePrimitive(requestPrimitive); HttpUriRequest method = null; + // Retrieve the url String url = requestPrimitive.getTo(); if(!url.startsWith(protocol+"://")){ @@ -213,6 +212,7 @@ public class RestHttpClient implements RestClientService { responsePrimitive.setLocation(contentHeader); } LOGGER.info("Http Client response: " + responsePrimitive); + httpClient.close(); } catch(HttpHostConnectException e){ LOGGER.info("Target is not reachable: " + requestPrimitive.getTo()); responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); @@ -221,14 +221,7 @@ public class RestHttpClient implements RestClientService { } catch (IOException e){ LOGGER.error(url + " not found", e); responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); - } finally { - try { - httpClient.close(); - } catch (IOException e) { - // Silently fail - LOGGER.trace("Fail closing the http client", e); - } - } + } return responsePrimitive; } -- cgit v1.2.3 From 20539b288a97ac6bf618d6be6c130ce97cbfeae8 Mon Sep 17 00:00:00 2001 From: benalaya Date: Wed, 30 Nov 2016 07:14:15 +0900 Subject: Revert "Fix Bug 507443 - Use JSON format by default for notifications" Regression detected in Notification procedure. This reverts commit 24b27ad9b1f71f6db093d116ebdfcc0a25aea930. Signed-off-by: Mahdi Ben Alaya --- .../main/java/org/eclipse/om2m/core/notifier/Notifier.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java index 69b86a2b..191f26cb 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java @@ -111,12 +111,12 @@ public class Notifier { notification.setSubscriptionReference(subscriptionEntity.getHierarchicalURI()); notification.setSubscriptionDeletion(false); RequestPrimitive notifRequest = new RequestPrimitive(); - notifRequest.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.JSON).objToString(notification)); + notifRequest.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.XML).objToString(notification)); notifRequest.setFrom("/" + Constants.CSE_ID); notifRequest.setTo(uri); notifRequest.setOperation(Operation.NOTIFY); - notifRequest.setRequestContentType(MimeMediaType.JSON); - notifRequest.setReturnContentType(MimeMediaType.JSON); + notifRequest.setRequestContentType(MimeMediaType.XML); + notifRequest.setReturnContentType(MimeMediaType.XML); ResponsePrimitive resp = notify(notifRequest, uri); if(resp.getResponseStatusCode().equals(ResponseStatusCode.TARGET_NOT_REACHABLE)){ throw new Om2mException("Error during the verification request", @@ -275,15 +275,15 @@ public class Notifier { if(sub.getNotificationContentType().equals(NotificationContentType.MODIFIED_ATTRIBUTES)){ serializableResource = (Resource)mapper.mapEntityToResource(resource, ResultContent.ATTRIBUTES); notification.getNotificationEvent().setRepresentation(serializableResource); - request.setRequestContentType(MimeMediaType.JSON); + request.setRequestContentType(MimeMediaType.XML); } else if(sub.getNotificationContentType().equals(NotificationContentType.WHOLE_RESOURCE)){ serializableResource = (Resource) mapper.mapEntityToResource(resource, ResultContent.ATTRIBUTES); notification.getNotificationEvent().setRepresentation(serializableResource); - request.setRequestContentType(MimeMediaType.JSON); + request.setRequestContentType(MimeMediaType.XML); } } // Set the content - request.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.JSON).objToString(notification)); + request.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.XML).objToString(notification)); // For each notification URI: send the notify request for(final String uri : sub.getNotificationURI()){ CoreExecutor.postThread(new Runnable(){ -- cgit v1.2.3 From efd4474b1f0a0deda2c6e92c0b6d164b24db1bde Mon Sep 17 00:00:00 2001 From: Francois Aissaoui Date: Thu, 1 Dec 2016 16:00:52 +0100 Subject: Update Bug 507443: Add parameter for notif format A new parameter has been added to select the content format type for the notification serialization. The name of the parameter is: org.eclipse.om2m.notification.mmt and can be found in the Constants class in commons plugin. Signed-off-by: Francois Aissaoui --- .../java/org/eclipse/om2m/commons/constants/Constants.java | 3 +++ .../main/java/org/eclipse/om2m/core/notifier/Notifier.java | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/constants/Constants.java b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/constants/Constants.java index 183c960e..ec12efde 100644 --- a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/constants/Constants.java +++ b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/constants/Constants.java @@ -93,5 +93,8 @@ public class Constants { public static final String ABSOLUTE_URI_SEPARATOR = "_"; public static final Integer MAX_THREAD_POOL_SIZE = Integer.valueOf(System.getProperty("org.eclipse.om2m.maxThreadPoolSize", "50")); + + // The MimeMediaType to use to serialize a notification + public static final String NOTIFICATION_MMT = System.getProperty("org.eclipse.om2m.notification.mmt", MimeMediaType.XML); } \ No newline at end of file diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java index 69b86a2b..e67a0da4 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java @@ -111,12 +111,12 @@ public class Notifier { notification.setSubscriptionReference(subscriptionEntity.getHierarchicalURI()); notification.setSubscriptionDeletion(false); RequestPrimitive notifRequest = new RequestPrimitive(); - notifRequest.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.JSON).objToString(notification)); + notifRequest.setContent(DataMapperSelector.getDataMapperList().get(Constants.NOTIFICATION_MMT).objToString(notification)); notifRequest.setFrom("/" + Constants.CSE_ID); notifRequest.setTo(uri); notifRequest.setOperation(Operation.NOTIFY); - notifRequest.setRequestContentType(MimeMediaType.JSON); - notifRequest.setReturnContentType(MimeMediaType.JSON); + notifRequest.setRequestContentType(Constants.NOTIFICATION_MMT); + notifRequest.setReturnContentType(Constants.NOTIFICATION_MMT); ResponsePrimitive resp = notify(notifRequest, uri); if(resp.getResponseStatusCode().equals(ResponseStatusCode.TARGET_NOT_REACHABLE)){ throw new Om2mException("Error during the verification request", @@ -275,15 +275,15 @@ public class Notifier { if(sub.getNotificationContentType().equals(NotificationContentType.MODIFIED_ATTRIBUTES)){ serializableResource = (Resource)mapper.mapEntityToResource(resource, ResultContent.ATTRIBUTES); notification.getNotificationEvent().setRepresentation(serializableResource); - request.setRequestContentType(MimeMediaType.JSON); + request.setRequestContentType(Constants.NOTIFICATION_MMT); } else if(sub.getNotificationContentType().equals(NotificationContentType.WHOLE_RESOURCE)){ serializableResource = (Resource) mapper.mapEntityToResource(resource, ResultContent.ATTRIBUTES); notification.getNotificationEvent().setRepresentation(serializableResource); - request.setRequestContentType(MimeMediaType.JSON); + request.setRequestContentType(Constants.NOTIFICATION_MMT); } } // Set the content - request.setContent(DataMapperSelector.getDataMapperList().get(MimeMediaType.JSON).objToString(notification)); + request.setContent(DataMapperSelector.getDataMapperList().get(Constants.NOTIFICATION_MMT).objToString(notification)); // For each notification URI: send the notify request for(final String uri : sub.getNotificationURI()){ CoreExecutor.postThread(new Runnable(){ -- cgit v1.2.3 From 0ad4f9cafdfce99bf647d88812232532d077236b Mon Sep 17 00:00:00 2001 From: Guillaume Garzone Date: Sun, 4 Dec 2016 22:57:24 +0100 Subject: Recover important bug fix for Http client. Revert "Revert "[Bug 507561] Fix closable HTTP client. Regression detected in Notification procedure "" This reverts commit 80da928e4ed79e30efb3b961caae5d5a5e0e80a2. Signed-off-by: Guillaume Garzone --- .../org/eclipse/om2m/binding/http/RestHttpClient.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java b/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java index bf31768b..8ed1e279 100644 --- a/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java +++ b/org.eclipse.om2m.binding.http/src/main/java/org/eclipse/om2m/binding/http/RestHttpClient.java @@ -39,7 +39,7 @@ import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.HttpClientBuilder; import org.eclipse.om2m.binding.http.constants.HttpHeaders; import org.eclipse.om2m.binding.http.constants.HttpParameters; import org.eclipse.om2m.binding.service.RestClientService; @@ -79,11 +79,12 @@ public class RestHttpClient implements RestClientService { */ public ResponsePrimitive sendRequest(RequestPrimitive requestPrimitive) { LOGGER.info("Sending request: " + requestPrimitive); - CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpClient httpClient = HttpClientBuilder.create() + .disableAutomaticRetries() + .build(); ResponsePrimitive responsePrimitive = new ResponsePrimitive(requestPrimitive); HttpUriRequest method = null; - // Retrieve the url String url = requestPrimitive.getTo(); if(!url.startsWith(protocol+"://")){ @@ -212,7 +213,6 @@ public class RestHttpClient implements RestClientService { responsePrimitive.setLocation(contentHeader); } LOGGER.info("Http Client response: " + responsePrimitive); - httpClient.close(); } catch(HttpHostConnectException e){ LOGGER.info("Target is not reachable: " + requestPrimitive.getTo()); responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); @@ -221,7 +221,14 @@ public class RestHttpClient implements RestClientService { } catch (IOException e){ LOGGER.error(url + " not found", e); responsePrimitive.setResponseStatusCode(ResponseStatusCode.TARGET_NOT_REACHABLE); - } + } finally { + try { + httpClient.close(); + } catch (IOException e) { + // Silently fail + LOGGER.trace("Fail closing the http client", e); + } + } return responsePrimitive; } -- cgit v1.2.3 From 3a7d04f035752c683cc8f7727466d6e97f708889 Mon Sep 17 00:00:00 2001 From: Guillaume Garzone Date: Sun, 4 Dec 2016 23:02:31 +0100 Subject: Partial fix of bug 508414 Missing "to" parameter when sending notification with redirection. Signed-off-by: Guillaume Garzone --- .../src/main/java/org/eclipse/om2m/core/notifier/Notifier.java | 1 + 1 file changed, 1 insertion(+) diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java index debdf7b9..46cbf6f2 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/notifier/Notifier.java @@ -158,6 +158,7 @@ public class Notifier { request.setTo(contact); return RestClient.sendRequest(request); }else{ + request.setTo(contact); request.setTargetId(contact); LOGGER.info("Sending notify request..."); return new Router().doRequest(request); -- cgit v1.2.3 From e421bcb33e4979220d9b82617d5d1761e5077ea1 Mon Sep 17 00:00:00 2001 From: Francois Aissaoui Date: Wed, 11 Jan 2017 11:43:47 +0100 Subject: [Bug 510246] Fix discovery inconsistency The contentInstance was not removed from the URI Mapper table when it is deleted due to a full container. This fix removes the entry on this specific execution. Signed-off-by: Francois Aissaoui --- .../org/eclipse/om2m/core/controller/ContentInstanceController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/ContentInstanceController.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/ContentInstanceController.java index 5f7bc2a4..45fb3bf0 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/ContentInstanceController.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/controller/ContentInstanceController.java @@ -222,9 +222,11 @@ public class ContentInstanceController extends Controller { if (container.getMaxNrOfInstances() != null && (cinList.size() == container.getMaxNrOfInstances().intValue())) { LOGGER.info("Deleting oldest content instance due to container size limit: " + container.getChildContentInstances().get(0).getHierarchicalURI()); + String hierarchicalUriToDelete = container.getChildContentInstances().get(0).getHierarchicalURI(); dbs.getDAOFactory().getContentInstanceDAO().delete(transaction, container.getChildContentInstances().get(0)); transaction.commit(); transaction.close(); + UriMapper.deleteUri(hierarchicalUriToDelete); transaction = dbs.getDbTransaction(); transaction.open(); container = (ContainerEntity)dao.find(transaction, request.getTargetId()); -- cgit v1.2.3 From 93b4efc9c30ea265d1e8cad3194792109c652a23 Mon Sep 17 00:00:00 2001 From: Guillaume Garzone Date: Fri, 24 Mar 2017 14:06:24 +0100 Subject: [Bug 507560] Fix query strings duplication bug when redirection Change-Id: I63befacfe385f26c29adb111edc20f08c21e57e8 Signed-off-by: Guillaume Garzone --- .../src/main/java/org/eclipse/om2m/core/router/Router.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java index 44b27cb3..ad0ada8a 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/router/Router.java @@ -372,8 +372,10 @@ public class Router implements CseService { request.getQueryStrings().putAll(parameters); if(request.getTo() == null){ request.setTo(request.getTargetId().split("\\?")[0]); - request.setTargetId(request.getTo()); + } else { + request.setTo(request.getTo().split("\\?")[0]); } + request.setTargetId(request.getTo()); } } -- cgit v1.2.3 From 09c418edf5af1edac63a72ba2ad06c30782e5c25 Mon Sep 17 00:00:00 2001 From: Mahdi Ben Alaya Date: Mon, 24 Jul 2017 17:27:14 +0200 Subject: Add missing mqtt check to Redirector --- .../eclipse/om2m/core/redirector/Redirector.java | 34 ++++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java index c161bee2..c05dbcdd 100644 --- a/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java +++ b/org.eclipse.om2m.core/src/main/java/org/eclipse/om2m/core/redirector/Redirector.java @@ -125,16 +125,32 @@ public class Redirector { url = url.substring(0, url.length() - 1); } - if (request.getTo().startsWith("//")) { - url += request.getTo().replaceFirst("//", "/_/"); - } else if (request.getTo().startsWith("/")) { - url += request.getTo().replaceFirst("/", "/~/"); - - } else { - url += "/" + request.getTo(); - + if(url.startsWith("mqtt://")){ + url += request.getTo(); + Pattern mqttUriPattern = Pattern.compile("(mqtt://[^:/]*(:[0-9]{1,5})?)(/.*)"); + Matcher matcher = mqttUriPattern.matcher(url); + if(matcher.matches()){ + // FIXME we need a response but not yet implemented in MQTT binding + request.setMqttResponseExpected(true); + // TODO Format type can be enhanced + request.setMqttTopic("/oneM2M/req/" + Constants.CSE_ID + "/" + csrEntity.getRemoteCseId().replaceAll("/", "") + "/xml"); + request.setMqttUri(matcher.group(1)); + } else { + LOGGER.warn("Incorrect MQTT URI specified in remoteCSE: " + url); + i++; + continue; + } + } else { + if(request.getTo().startsWith("//")){ + url += request.getTo().replaceFirst("//", "/_/"); + } else if(request.getTo().startsWith("/")){ + url += request.getTo().replaceFirst("/", "/~/"); + } else { + url+= "/" + request.getTo(); + } } - + + request.setTo(url); // modify the request if content type is OBJ. -- cgit v1.2.3 From 3154453965712aabad6fe1f8aa3432068fa31927 Mon Sep 17 00:00:00 2001 From: Mahdi Ben Alaya Date: Mon, 24 Jul 2017 19:29:59 +0200 Subject: Add missing AccessControlPolicyTest class to flexcontainer testsuite --- .../flexcontainer/AccessControlPolicyTest.java | 263 +++++++++++++++++++++ .../om2m/testsuite/flexcontainer/Activator.java | 1 + .../testsuite/flexcontainer/SubscriptionTest.java | 3 +- 3 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/AccessControlPolicyTest.java diff --git a/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/AccessControlPolicyTest.java b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/AccessControlPolicyTest.java new file mode 100644 index 00000000..33a4099b --- /dev/null +++ b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/AccessControlPolicyTest.java @@ -0,0 +1,263 @@ +/******************************************************************************* + * Copyright (c) 2014, 2016 Orange. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.om2m.testsuite.flexcontainer; + +import org.eclipse.om2m.commons.constants.AccessControl; +import org.eclipse.om2m.commons.constants.Constants; +import org.eclipse.om2m.commons.constants.ResponseStatusCode; +import org.eclipse.om2m.commons.resource.AccessControlPolicy; +import org.eclipse.om2m.commons.resource.AccessControlRule; +import org.eclipse.om2m.commons.resource.CustomAttribute; +import org.eclipse.om2m.commons.resource.FlexContainer; +import org.eclipse.om2m.commons.resource.ResponsePrimitive; +import org.eclipse.om2m.commons.resource.SetOfAcrs; +import org.eclipse.om2m.core.service.CseService; +import org.eclipse.om2m.testsuite.flexcontainer.TestReport.Status; + +public class AccessControlPolicyTest extends FlexContainerTestSuite { + + public AccessControlPolicyTest(final CseService pCseService) { + super(pCseService); + } + + @Override + protected String getTestSuiteName() { + return "AccessControlPolicyTest"; + } + + public void testCreateAccessControlPolicy() { + + AccessControlPolicy acp = new AccessControlPolicy(); + SetOfAcrs privileges = new SetOfAcrs(); + AccessControlRule accessControlRule = new AccessControlRule(); + accessControlRule.getAccessControlOriginators().add("greg:greg"); + // accessControlRule.getAccessControlOriginators().add("admin:admin"); + accessControlRule.setAccessControlOperations(AccessControl.ALL); + privileges.getAccessControlRule().add(accessControlRule); + acp.setPrivileges(privileges); + + SetOfAcrs selfPrivileges = new SetOfAcrs(); + AccessControlRule selfAccessControlRule = new AccessControlRule(); + selfAccessControlRule.getAccessControlOriginators().add("admin:admin"); + selfAccessControlRule.setAccessControlOperations(AccessControl.ALL); + selfPrivileges.getAccessControlRule().add(selfAccessControlRule); + acp.setSelfPrivileges(selfPrivileges); + + String baseLocation = "/" + Constants.CSE_ID + "/" + Constants.CSE_NAME; + String acpName = "acp_" + System.currentTimeMillis(); + String acpLocation = baseLocation + "/" + acpName; + + ResponsePrimitive response = sendCreateAccessControlPolicyRequest(acp, baseLocation, acpName); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)) { + // KO + createTestReport("testAccessControlPolicy", Status.KO, "unable to create a new acp", null); + return; + } + // here are sure the ACP has been created + AccessControlPolicy returnedAcp = (AccessControlPolicy) response.getContent(); + + // init a new FlexContainer + FlexContainer flexContainer = new FlexContainer(); + flexContainer.setContainerDefinition("org.onem2m.home.moduleclass.binaryswitch"); + CustomAttribute ca = new CustomAttribute(); + ca.setCustomAttributeName("powerState"); + ca.setCustomAttributeType("xs:boolean"); + ca.setCustomAttributeValue("false"); + flexContainer.getCustomAttributes().add(ca); + String flexContainerName = "flexContainerACPTest_" + System.currentTimeMillis(); + String flexContainerLocation = baseLocation + "/" + flexContainerName; + + // set acp + flexContainer.getAccessControlPolicyIDs().add(returnedAcp.getResourceID()); + + // send create FlexContainer request + response = sendCreateFlexContainerRequest(flexContainer, baseLocation, flexContainerName); + FlexContainer createdFlexContainer = null; + if (!response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)) { + // KO + createTestReport("testAccessControlPolicy", Status.KO, "unable to create a FlexContainer", null); + return; + } else { + createdFlexContainer = (FlexContainer) response.getContent(); + } + + // retrieve the flexContainer with greg:greg credentials + response = sendRetrieveRequest(flexContainerLocation, "greg:greg"); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.OK)) { + // KO + createTestReport("testAccessControlPolicy", Status.KO, "unable to retrieve the FlexContainer", null); + return; + } else { + FlexContainer toBeRetrieved = (FlexContainer) response.getContent(); + try { + checkFlexContainer(createdFlexContainer, toBeRetrieved); + } catch (Exception e) { + // KO + createTestReport("testAccessControlPolicy", Status.KO, "flexContainer are differents", null); + return; + } + } + + // retrieve the flexContainer with admin:admin, expected FORBIDDEN + response = sendRetrieveRequest(flexContainerLocation, Constants.ADMIN_REQUESTING_ENTITY); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.ACCESS_DENIED)) { + // KO + createTestReport("testAccessControlPolicy", Status.KO, + "expected " + ResponseStatusCode.ACCESS_DENIED + ", received:" + response.getResponseStatusCode(), + null); + return; + } + + createTestReport("testAccessControlPolicy", Status.OK, null, null); + + } + + public void testCreateFlexContainerWithNoRight() { + // create an ACP for greg:greg with RETRIEVE rights + AccessControlPolicy acp = new AccessControlPolicy(); + SetOfAcrs privileges = new SetOfAcrs(); + AccessControlRule accessControlRule = new AccessControlRule(); + accessControlRule.getAccessControlOriginators().add("greg:greg"); + // accessControlRule.getAccessControlOriginators().add("admin:admin"); + accessControlRule.setAccessControlOperations(AccessControl.RETRIEVE); + privileges.getAccessControlRule().add(accessControlRule); + acp.setPrivileges(privileges); + + SetOfAcrs selfPrivileges = new SetOfAcrs(); + AccessControlRule selfAccessControlRule = new AccessControlRule(); + selfAccessControlRule.getAccessControlOriginators().add("admin:admin"); + selfAccessControlRule.setAccessControlOperations(AccessControl.ALL); + selfPrivileges.getAccessControlRule().add(selfAccessControlRule); + acp.setSelfPrivileges(selfPrivileges); + + String baseLocation = "/" + Constants.CSE_ID + "/" + Constants.CSE_NAME; + String acpName = "acp_" + System.currentTimeMillis(); + String acpLocation = baseLocation + "/" + acpName; + + ResponsePrimitive response = sendCreateAccessControlPolicyRequest(acp, baseLocation, acpName); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)) { + // KO + createTestReport("testCreateFlexContainerWithNoRight", Status.KO, "unable to create a new acp", null); + return; + } + // here are sure the ACP has been created + AccessControlPolicy returnedAcp = (AccessControlPolicy) response.getContent(); + + // init a new FlexContainer + FlexContainer flexContainer = new FlexContainer(); + flexContainer.setContainerDefinition("org.onem2m.home.moduleclass.binaryswitch"); + CustomAttribute ca = new CustomAttribute(); + ca.setCustomAttributeName("powerState"); + ca.setCustomAttributeType("xs:boolean"); + ca.setCustomAttributeValue("false"); + flexContainer.getCustomAttributes().add(ca); + String flexContainerName = "flexContainerACPTest_" + System.currentTimeMillis(); + String flexContainerLocation = baseLocation + "/" + flexContainerName; + + // try to create a FlexContainer using greg:greg credentials => expect + // ACCESS DENIED + response = sendCreateFlexContainerRequest(flexContainer, baseLocation, flexContainerName, "greg:greg"); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.ACCESS_DENIED)) { + // KO + createTestReport( + "testCreateFlexContainerWithNoRight", Status.KO, "error : expected " + + ResponseStatusCode.ACCESS_DENIED + ", received:" + response.getResponseStatusCode(), + null); + return; + } + + createTestReport("testCreateFlexContainerWithNoRight", Status.OK, null, null); + } + + public void testDeleteFlexContainerWithNoRight() { + // create an ACP for greg:greg with RETRIEVE rights + AccessControlPolicy acp = new AccessControlPolicy(); + SetOfAcrs privileges = new SetOfAcrs(); + AccessControlRule accessControlRule = new AccessControlRule(); + accessControlRule.getAccessControlOriginators().add("greg:greg"); + // accessControlRule.getAccessControlOriginators().add("admin:admin"); + accessControlRule.setAccessControlOperations(AccessControl.RETRIEVE); + privileges.getAccessControlRule().add(accessControlRule); + acp.setPrivileges(privileges); + + SetOfAcrs selfPrivileges = new SetOfAcrs(); + AccessControlRule selfAccessControlRule = new AccessControlRule(); + selfAccessControlRule.getAccessControlOriginators().add("admin:admin"); + selfAccessControlRule.setAccessControlOperations(AccessControl.ALL); + selfPrivileges.getAccessControlRule().add(selfAccessControlRule); + acp.setSelfPrivileges(selfPrivileges); + + String baseLocation = "/" + Constants.CSE_ID + "/" + Constants.CSE_NAME; + String acpName = "acp_" + System.currentTimeMillis(); + String acpLocation = baseLocation + "/" + acpName; + + ResponsePrimitive response = sendCreateAccessControlPolicyRequest(acp, baseLocation, acpName); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)) { + // KO + createTestReport("testDeleteFlexContainerWithNoRight", Status.KO, "unable to create a new acp", null); + return; + } + // here are sure the ACP has been created + AccessControlPolicy returnedAcp = (AccessControlPolicy) response.getContent(); + + // init a new FlexContainer + FlexContainer flexContainer = new FlexContainer(); + flexContainer.setContainerDefinition("org.onem2m.home.moduleclass.binaryswitch"); + CustomAttribute ca = new CustomAttribute(); + ca.setCustomAttributeName("powerState"); + ca.setCustomAttributeType("xs:boolean"); + ca.setCustomAttributeValue("false"); + flexContainer.getCustomAttributes().add(ca); + String flexContainerName = "flexContainerACPTest_" + System.currentTimeMillis(); + String flexContainerLocation = baseLocation + "/" + flexContainerName; + + // try to create a FlexContainer using admin:admin credentials + FlexContainer createdFlexContainer = null; + response = sendCreateFlexContainerRequest(flexContainer, baseLocation, flexContainerName, + Constants.ADMIN_REQUESTING_ENTITY); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.CREATED)) { + // KO + createTestReport("testDeleteFlexContainerWithNoRight", Status.KO, + "error : expected " + ResponseStatusCode.CREATED + ", received:" + response.getResponseStatusCode(), + null); + return; + } else { + createdFlexContainer = (FlexContainer) response.getContent(); + } + + // try to delete the flexContainer with greg:greg ==> expect + // ACCESS_DENIED + response = sendDeleteRequest(flexContainerLocation, "greg:greg"); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.ACCESS_DENIED)) { + createTestReport( + "testDeleteFlexContainerWithNoRight", Status.KO, "error : expected " + + ResponseStatusCode.ACCESS_DENIED + ", received:" + response.getResponseStatusCode(), + null); + return; + } + + // try to retrieve the FlexContainer using greg:greg credentials ==> ok + response = sendRetrieveRequest(flexContainerLocation, "greg:greg"); + if (!response.getResponseStatusCode().equals(ResponseStatusCode.OK)) { + createTestReport("testDeleteFlexContainerWithNoRight", Status.KO, + "error : expected " + ResponseStatusCode.OK + ", received:" + response.getResponseStatusCode(), + null); + return; + } else { + try { + checkFlexContainer(createdFlexContainer, (FlexContainer) response.getContent()); + } catch (Exception e) { + // KO + createTestReport("testDeleteFlexContainerWithNoRight", Status.KO, + "flexContainers are differents:" + e.getMessage(), e); + } + } + + createTestReport("testDeleteFlexContainerWithNoRight", Status.OK, null, null); + } +} \ No newline at end of file diff --git a/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/Activator.java b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/Activator.java index 8c19f827..d2df6992 100644 --- a/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/Activator.java +++ b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/Activator.java @@ -162,6 +162,7 @@ public class Activator implements BundleActivator { subscriptionTest.executeTestsAndPrintReports(); } + AccessControlPolicyTest acpTest = new AccessControlPolicyTest(currentCseService); acpTest.executeTestsAndPrintReports(); diff --git a/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/SubscriptionTest.java b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/SubscriptionTest.java index 11b878ee..743997e6 100644 --- a/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/SubscriptionTest.java +++ b/org.eclipse.om2m.testsuite.flexcontainer/src/main/java/org/eclipse/om2m/testsuite/flexcontainer/SubscriptionTest.java @@ -9,7 +9,6 @@ package org.eclipse.om2m.testsuite.flexcontainer; import java.util.List; -import javax.servlet.ServletException; import org.eclipse.om2m.commons.constants.Constants; import org.eclipse.om2m.commons.constants.NotificationContentType; @@ -29,6 +28,8 @@ import org.eclipse.om2m.datamapping.service.DataMapperService; import org.eclipse.om2m.testsuite.flexcontainer.TestReport.Status; import org.osgi.service.http.HttpService; import org.osgi.service.http.NamespaceException; +import javax.servlet.ServletException; + public class SubscriptionTest extends FlexContainerTestSuite { -- cgit v1.2.3 From c9f2d761ab522aaadba1796bdb06d726847ef55e Mon Sep 17 00:00:00 2001 From: Mahdi Ben Alaya Date: Tue, 25 Jul 2017 18:50:08 +0200 Subject: clean Label and ACP entities --- .../commons/entities/AccessControlPolicyEntity.java | 19 +++++++++++-------- .../eclipse/om2m/commons/entities/LabelEntity.java | 13 ------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/AccessControlPolicyEntity.java b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/AccessControlPolicyEntity.java index 084e24bf..1c24f5d2 100644 --- a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/AccessControlPolicyEntity.java +++ b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/AccessControlPolicyEntity.java @@ -499,14 +499,7 @@ public class AccessControlPolicyEntity extends AnnounceableSubordinateEntity { this.linkedDynamicAuthorizationConsultation = linkedDynamicAuthorizationConsultation; } - @Override - public List getAccessControlPolicies() { - return new ArrayList<>(); - } - - @Override - public void setAccessControlPolicies(List accessControlPolicies) { - } + @Override public List getDynamicAuthorizationConsultations() { @@ -520,6 +513,16 @@ public class AccessControlPolicyEntity extends AnnounceableSubordinateEntity { public void setDynamicAuthorizationConsultations(List list) { dynamicConsultationAuthorizations = list; } + + @Override + public List getAccessControlPolicies() { + return null; + } + + @Override + public void setAccessControlPolicies( + List accessControlPolicies) { + } diff --git a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java index b0ac942d..6cee0367 100644 --- a/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java +++ b/org.eclipse.om2m.commons/src/main/java/org/eclipse/om2m/commons/entities/LabelEntity.java @@ -79,9 +79,6 @@ public class LabelEntity { @ManyToMany(targetEntity = AccessControlPolicyEntity.class, mappedBy = "labelsEntities") protected List linkedACP; - @ManyToMany(targetEntity = AccessControlPolicyEntity.class, mappedBy = "labelsEntities") - protected List linkedACP; - @ManyToMany(targetEntity = NodeEntity.class, mappedBy = "labelsEntities") protected List linkedNodes; @@ -194,16 +191,6 @@ public class LabelEntity { return linkedAe; } - public void setLinkedACP(List pLinkedACPs) { - this.linkedACP = pLinkedACPs; - } - - public List getLinkedACP() { - if (this.linkedACP == null) { - this.linkedACP = new ArrayList<>(); - } - return this.linkedACP; - } /** * @param linkedAe the linkedAe to set -- cgit v1.2.3 From 9a3283bdaa8f8abf9576d29230e5bd641fd79543 Mon Sep 17 00:00:00 2001 From: Mahdi Ben Alaya Date: Tue, 25 Jul 2017 18:55:48 +0200 Subject: Add ADN project to easily test the API using HTTP, JSON, and Junit --- .../test/java/org/eclipse/om2m/adn/tests/ACP.java | 167 ++++++++++++++++ .../test/java/org/eclipse/om2m/adn/tests/AE.java | 77 ++++++++ .../java/org/eclipse/om2m/adn/tests/CSEBase.java | 17 ++ .../java/org/eclipse/om2m/adn/tests/Container.java | 81 ++++++++ .../eclipse/om2m/adn/tests/ContentInstance.java | 107 +++++++++++ .../java/org/eclipse/om2m/adn/tests/Discovery.java | 86 +++++++++ .../org/eclipse/om2m/adn/tests/FanoutPoint.java | 144 ++++++++++++++ .../java/org/eclipse/om2m/adn/tests/Group.java | 92 +++++++++ .../org/eclipse/om2m/adn/tests/LongPolling.java | 80 ++++++++ .../test/java/org/eclipse/om2m/adn/tests/Node.java | 74 +++++++ .../java/org/eclipse/om2m/adn/tests/RemoteCSE.java | 93 +++++++++ .../org/eclipse/om2m/adn/tests/Subscription.java | 161 ++++++++++++++++ .../org/eclipse/om2m/adn/tests/TestConfig.java | 22 +++ .../org/eclipse/om2m/adn/tools/HttpResponse.java | 22 +++ .../org/eclipse/om2m/adn/tools/RestHttpClient.java | 214 +++++++++++++++++++++ 15 files changed, 1437 insertions(+) create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ACP.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/AE.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/CSEBase.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Container.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ContentInstance.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Discovery.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/FanoutPoint.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Group.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/LongPolling.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Node.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/RemoteCSE.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Subscription.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/TestConfig.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/HttpResponse.java create mode 100644 org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/RestHttpClient.java diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ACP.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ACP.java new file mode 100644 index 00000000..367b959d --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ACP.java @@ -0,0 +1,167 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ACP extends TestConfig{ + private static String acpTestRU = "acpTestRU"; + private static String acpTestD = "aeTestD"; + private static String acpTestC = "acpTestC"; + private static String aeName = "aeTestRU"; + private static String cntName = "cntTest"; + private static String badOriginator = "guest:guest"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("rn", aeName); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + + JSONArray acor = new JSONArray(); + acor.put("admin:admin"); + //acor.put("cae-admin"); + JSONObject item = new JSONObject(); + item.put("acor", acor); + item.put("acop", 63); + JSONArray acr1 = new JSONArray(); + acr1.put(item); + + acor = new JSONArray(); + acor.put("guest:guest"); + acor.put("admin:admin"); + //acor.put("cae-admin"); + item = new JSONObject(); + item.put("acor", acor); + item.put("acop", 34); + JSONArray acr2 = new JSONArray(); + acr2.put(item); + + JSONObject pv = new JSONObject(); + pv.put("acr",acr1); + pv.put("acr",acr2); + JSONObject pvs = new JSONObject(); + pvs.put("acr", acr1); + obj = new JSONObject(); + obj.put("rn", acpTestRU); + obj.put("pv", pv); + obj.put("pvs", pvs); + resource = new JSONObject(); + resource.put("m2m:acp", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 1); + + obj = new JSONObject(); + obj.put("rn", acpTestD); + obj.put("pv", pv); + obj.put("pvs", pvs); + resource = new JSONObject(); + resource.put("m2m:acp", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 1); + + JSONArray acpi = new JSONArray(); + acpi.put("/"+cseId+"/"+cseName+"/"+aeName+"/"+acpTestRU); + obj = new JSONObject(); + obj.put("acpi",acpi); + obj.put("rn", cntName); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 3); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName); + } + + @Test + public void testCreate() { + JSONArray acor = new JSONArray(); + acor.put("admin:admin"); + //acor.put("cae-admin"); + + JSONObject item = new JSONObject(); + item.put("acor", acor); + item.put("acop", 63); + + JSONArray acr = new JSONArray(); + acr.put(item); + + JSONObject pv = new JSONObject(); + pv.put("acr",acr); + + JSONObject pvs = new JSONObject(); + pvs.put("acr", acr); + + JSONObject obj = new JSONObject(); + obj.put("rn", acpTestC); + obj.put("pv", pv); + obj.put("pvs", pv); + + JSONObject resource = new JSONObject(); + resource.put("m2m:acp", obj); + + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 1); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+acpTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate() { + JSONArray acor = new JSONArray(); + //acor.put("cae-admin"); + acor.put("*"); + + JSONObject item = new JSONObject(); + item.put("acor", acor); + item.put("acop", 63); + + JSONArray acr = new JSONArray(); + acr.put(item); + + JSONObject pv = new JSONObject(); + pv.put("acr",acr); + + JSONObject pvs = new JSONObject(); + pvs.put("acr", acr); + + + JSONObject obj = new JSONObject(); + obj.put("pv", pv); + obj.put("pvs", pv); + + JSONObject resource = new JSONObject(); + resource.put("m2m:acp", obj); + + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+acpTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+acpTestD); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testNonAuthorizedOperation() { + HttpResponse httpResponse = RestHttpClient.delete(badOriginator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName); + assertEquals(403, httpResponse.getStatusCode()); + } + +} + diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/AE.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/AE.java new file mode 100644 index 00000000..718c346b --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/AE.java @@ -0,0 +1,77 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class AE extends TestConfig{ + private static String aeTestRU = "aeTestRU"; + private static String aeTestD = "aeTestD"; + private static String aeTestC = "aeTestC"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("rn", aeTestRU); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + + obj = new JSONObject(); + obj.put("rn", aeTestD); + obj.put("api", 12345); + obj.put("rr", false); + resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeTestRU); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeTestD); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeTestC); + } + + @Test + public void testCreate() { + JSONObject obj = new JSONObject(); + obj.put("rn", aeTestC); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate() { + JSONObject obj = new JSONObject(); + obj.put("rr", true); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeTestD); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/CSEBase.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/CSEBase.java new file mode 100644 index 00000000..34fc2b1e --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/CSEBase.java @@ -0,0 +1,17 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.junit.Test; + +public class CSEBase extends TestConfig { + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Container.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Container.java new file mode 100644 index 00000000..be210298 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Container.java @@ -0,0 +1,81 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class Container extends TestConfig { + + private static String aeC = "aeTestRU"; + private static String cnTestRU = "cnTestRU"; + private static String cnTestD = "cnTestD"; + private static String cnTestC = "cnTestC"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + JSONObject obj = new JSONObject(); + obj.put("rn", aeC); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + + + obj = new JSONObject(); + obj.put("rn", cnTestRU); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeC, resource.toString(), 3); + + + obj = new JSONObject(); + obj.put("rn", cnTestD); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeC, resource.toString(), 3); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeC); + } + + @Test + public void testCreate() { + JSONObject obj = new JSONObject(); + obj.put("rn", cnTestC); + JSONObject resource = new JSONObject(); + resource.put("m2m:cnt", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeC, resource.toString(), 3); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeC+"/"+cnTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate() { + JSONObject obj = new JSONObject(); + obj.put("et", "20181228T164835"); + JSONObject resource = new JSONObject(); + resource.put("m2m:cnt", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeC+"/"+cnTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeC+"/"+cnTestD); + assertEquals(200, httpResponse.getStatusCode()); + } +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ContentInstance.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ContentInstance.java new file mode 100644 index 00000000..2f161be4 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/ContentInstance.java @@ -0,0 +1,107 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ContentInstance extends TestConfig{ + + private static String aeName = "aeTestRU"; + private static String cntName = "cntTest"; + private static String cinTestRU = "cinTestRU"; + private static String cinTestD = "cinTestD"; + private static String cinTestC = "cinTestC"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + JSONObject obj = new JSONObject(); + obj.put("rn", aeName); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + + + obj = new JSONObject(); + obj.put("rn", cntName); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 3); + + + + obj = new JSONObject(); + obj.put("rn", cinTestRU); + obj.put("cnf", "application/text"); + obj.put("con", 25); + resource = new JSONObject(); + resource.put("m2m:cin", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 4); + + + obj = new JSONObject(); + obj.put("rn", cinTestD); + obj.put("cnf", "application/text"); + obj.put("con", 35); + resource = new JSONObject(); + resource.put("m2m:cin", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 4); + } + + //@AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName); + } + + @Test + public void testCreate() { + JSONObject obj = new JSONObject(); + obj.put("rn", cinTestC); + obj.put("cnf", "application/text"); + obj.put("con", 30); + JSONObject resource = new JSONObject(); + resource.put("m2m:cin", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 4); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName+"/"+cinTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testRetreiveLA() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName+"/la"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testRetreiveOL() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName+"/ol"); + assertEquals(200, httpResponse.getStatusCode()); + } + + /*public void testUpdate() { + JSONObject obj = new JSONObject(); + obj.put("et", "20161228T164835"); + JSONObject resource = new JSONObject(); + resource.put("m2m:cin", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeCi+"/"+cnCi+"/"+cinTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + }*/ + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName+"/"+cinTestD); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Discovery.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Discovery.java new file mode 100644 index 00000000..d83b0cbb --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Discovery.java @@ -0,0 +1,86 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class Discovery extends TestConfig { + + private static String aeName = "aeTest"; + + //@BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("rn", aeName); + obj.put("api", 12345); + obj.put("rr", false); + JSONArray array = new JSONArray(); + array.put("Type/Sensor"); + array.put("Category/Luminosity"); + array.put("Manufactor/Philips"); + obj.put("lbl", array); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName); + } + + @Test + public void testAllResources() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void TestWithFilterCriteria() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&ty=2"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void TestWithLimitFilterCriteria() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&lim=1"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void TestWithLabelFilterCriteria() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&lbl=Manufactor/Philips"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void TestWithMultipleFilterCriteria() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&ty=1&lbl=Type/Sensor"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void TestWithLevelFilterCriteria() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&lvl=1"); + assertEquals(200, httpResponse.getStatusCode()); + httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&lvl=2"); + assertEquals(200, httpResponse.getStatusCode()); + httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&lvl=3"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void TestWithOffsetFilterCriteria() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&ofst=3&lim=1"); + assertEquals(200, httpResponse.getStatusCode()); + httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"?fu=1&ofst=3"); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/FanoutPoint.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/FanoutPoint.java new file mode 100644 index 00000000..4ec53160 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/FanoutPoint.java @@ -0,0 +1,144 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class FanoutPoint extends TestConfig { + + private static String groupC = "groupTest"; + private static String groupRU = "groupRU"; + private static String groupD = "groupD"; + + private static String cnTestC1 = "cnTestC1"; + private static String cnTestC2 = "cnTestC2"; + + private static String cnTestRU1 = "cnTestRU1"; + private static String cnTestRU2 = "cnTestRU2"; + + private static String cnTestD1 = "cnTestD1"; + private static String cnTestD2 = "cnTestD1"; + + private static String cinTest = "cinTest"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("rn", cnTestRU1); + JSONObject resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 3); + + obj = new JSONObject(); + obj.put("rn", cnTestRU2); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+remoteCseId, resource.toString(), 3); + + JSONArray array = new JSONArray(); + array.put("/"+cseId+"/"+cseName+"/"+cnTestRU1); + array.put("/"+remoteCseId+"/"+remoteCseName+"/"+cnTestRU2); + obj = new JSONObject(); + obj.put("mid", array); + obj.put("rn", groupRU); + obj.put("mnm", 3); + resource = new JSONObject(); + resource.put("m2m:grp", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 9); + + obj = new JSONObject(); + obj.put("rn", cnTestD1); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 3); + + obj = new JSONObject(); + obj.put("rn", cnTestD2); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+remoteCseId, resource.toString(), 3); + + array = new JSONArray(); + array.put("/"+cseId+"/"+cseName+"/"+cnTestD1); + array.put("/"+remoteCseId+"/"+remoteCseName+"/"+cnTestD2); + obj = new JSONObject(); + obj.put("mid", array); + obj.put("rn", groupD); + obj.put("mnm", 3); + resource = new JSONObject(); + resource.put("m2m:grp", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 9); + + + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupC); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupRU); + } + + @Test + public void testCreate() { + JSONObject obj = new JSONObject(); + obj.put("rn", cnTestC1); + JSONObject resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 3); + + obj = new JSONObject(); + obj.put("rn", cnTestC2); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+remoteCseId, resource.toString(), 3); + + JSONArray array = new JSONArray(); + array.put("/"+cseId+"/"+cseName+"/"+cnTestC1); + array.put("/"+remoteCseId+"/"+remoteCseName+"/"+cnTestC2); + obj = new JSONObject(); + obj.put("mid", array); + obj.put("rn", groupC); + obj.put("mnm", 3); + resource = new JSONObject(); + resource.put("m2m:grp", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 9); + + obj = new JSONObject(); + obj.put("rn", cinTest); + obj.put("cnf", "application/text"); + obj.put("con", 25); + resource = new JSONObject(); + resource.put("m2m:cin", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupC+"/fopt", resource.toString(), 4); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupRU+"/fopt"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupD+"/fopt"); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate() { + JSONObject obj = new JSONObject(); + obj.put("et", "20181228T164835"); + JSONObject resource = new JSONObject(); + resource.put("m2m:cnt", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupRU+"/fopt", resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Group.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Group.java new file mode 100644 index 00000000..2e5b2a18 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Group.java @@ -0,0 +1,92 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class Group extends TestConfig { + + private static String groupTestRU = "groupTestRU"; + private static String groupTestD = "groupTestD"; + private static String groupTestC = "groupTestC"; + private static String aeName = "aeTest"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("rn", aeName); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + + JSONArray array = new JSONArray(); + array.put("/"+cseId+"/"+cseName+"/"+aeName); + obj = new JSONObject(); + obj.put("mid", array); + obj.put("rn", groupTestRU); + obj.put("mnm", 3); + resource = new JSONObject(); + resource.put("m2m:grp", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 9); + + obj = new JSONObject(); + obj.put("mid", array); + obj.put("rn", groupTestD); + obj.put("mnm", 3); + resource = new JSONObject(); + resource.put("m2m:grp", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 9); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupTestC); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupTestRU); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName); + } + + @Test + public void testCreate() { + JSONArray array = new JSONArray(); + array.put("/"+cseId+"/"+cseName+"/"+aeName); + JSONObject obj = new JSONObject(); + obj.put("mid", array); + obj.put("rn", groupTestC); + obj.put("mnm", 3); + JSONObject resource = new JSONObject(); + resource.put("m2m:grp", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 9); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive(){ + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate(){ + JSONObject obj = new JSONObject(); + obj.put("et", "20171228T164835"); + JSONObject resource = new JSONObject(); + resource.put("m2m:grp", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+groupTestD); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/LongPolling.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/LongPolling.java new file mode 100644 index 00000000..902dc46d --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/LongPolling.java @@ -0,0 +1,80 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LongPolling extends TestConfig { + + private static String lpTestRU = "lpRU"; + private static String lpTestD = "lpD"; + private static String lpTestC = "lpC"; + private static String aeName = "ae_test"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("rn", aeName); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + + obj = new JSONObject(); + obj.put("rn", lpTestRU); + resource = new JSONObject(); + resource.put("m2m:pch", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 15); + + obj = new JSONObject(); + obj.put("rn", lpTestD); + resource = new JSONObject(); + resource.put("m2m:pch", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 15); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+lpTestC); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+lpTestRU); + } + + @Test + public void testCreate() { + JSONObject obj = new JSONObject(); + obj.put("rn", lpTestC); + JSONObject resource = new JSONObject(); + resource.put("m2m:pch", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 15); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+lpTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate(){ + JSONObject obj = new JSONObject(); + obj.put("et", "20181228T164835"); + JSONObject resource = new JSONObject(); + resource.put("m2m:pch", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+lpTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+lpTestD); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Node.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Node.java new file mode 100644 index 00000000..d309f7c2 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Node.java @@ -0,0 +1,74 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class Node extends TestConfig { + + private static String nodeTestRU = "nodeRU"; + private static String nodeTestD = "nodeD"; + private static String nodeTestC = "nodeC"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("rn", nodeTestRU); + obj.put("ni", "node-test"); + JSONObject resource = new JSONObject(); + resource.put("m2m:nod", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 14); + + obj = new JSONObject(); + obj.put("rn", nodeTestD); + obj.put("ni", "node-test2"); + resource = new JSONObject(); + resource.put("m2m:nod", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 14); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+nodeTestC); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+nodeTestRU); + } + + @Test + public void testCreate() { + JSONObject obj = new JSONObject(); + obj.put("rn", nodeTestC); + obj.put("ni", "node-test3"); + JSONObject resource = new JSONObject(); + resource.put("m2m:nod", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 14); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+nodeTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate(){ + JSONObject obj = new JSONObject(); + obj.put("et", "20181228T164835"); + JSONObject resource = new JSONObject(); + resource.put("m2m:nod", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+nodeTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+nodeTestD); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/RemoteCSE.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/RemoteCSE.java new file mode 100644 index 00000000..523a966e --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/RemoteCSE.java @@ -0,0 +1,93 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RemoteCSE extends TestConfig { + + private static String remoteCSETestRU = "remoteCSETestRU"; + private static String remoteCSETestD = "remoteCSETestD"; + private static String remoteCSETestC = "remoteCSETestC"; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + JSONObject obj = new JSONObject(); + obj.put("cb", remoteCSETestRU); + obj.put("rn", remoteCSETestRU); + obj.put("csi", remoteCSETestRU); + obj.put("rr", true); + JSONArray array = new JSONArray(); + array.put(remoteCsePoa+"/"); + obj.put("poa", array); + JSONObject resource = new JSONObject(); + resource.put("m2m:csr", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 16); + + obj = new JSONObject(); + obj.put("cb", remoteCSETestD); + obj.put("rn", remoteCSETestD); + obj.put("csi", remoteCSETestD); + obj.put("rr", true); + array = new JSONArray(); + array.put(remoteCsePoa+"/"); + obj.put("poa", array); + resource = new JSONObject(); + resource.put("m2m:csr", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 16); + + + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+remoteCSETestRU); + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+remoteCSETestC); + } + + @Test + public void testCreate() { + JSONObject obj = new JSONObject(); + obj.put("cb", remoteCSETestC); + obj.put("rn", remoteCSETestC); + obj.put("csi", remoteCSETestC); + obj.put("rr", true); + JSONArray array = new JSONArray(); + array.put(remoteCsePoa+"/"); + obj.put("poa", array); + JSONObject resource = new JSONObject(); + resource.put("m2m:csr", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 16); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+remoteCSETestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+remoteCSETestD); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate(){ + JSONObject obj = new JSONObject(); + JSONArray array = new JSONArray(); + array.put("http://127.0.0.1:8585/"); + obj.put("poa", array); + JSONObject resource = new JSONObject(); + resource.put("m2m:csr", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+remoteCSETestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Subscription.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Subscription.java new file mode 100644 index 00000000..c1b384d7 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/Subscription.java @@ -0,0 +1,161 @@ +package org.eclipse.om2m.adn.tests; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.util.concurrent.Executors; + +import org.eclipse.om2m.adn.tools.HttpResponse; +import org.eclipse.om2m.adn.tools.RestHttpClient; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; + +public class Subscription extends TestConfig { + + private static String aeName = "aeTest"; + private static String cntName = "cntTest"; + private static String subTestRU = "subTestRU"; + private static String subTestD = "subTestD"; + private static String subTestC = "subTestC"; + + private static String aeProtocol="http"; + private static String aeIp = "127.0.0.1"; + private static int aePort = 1401; + private static String notificationURI = aeProtocol+"://"+aeIp+":"+aePort; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + //Starting Server + HttpServer server = null; + try { + server = HttpServer.create(new InetSocketAddress(aePort), 0); + } catch (IOException e) { + e.printStackTrace(); + } + server.createContext("/", new MyHandler()); + server.setExecutor(Executors.newCachedThreadPool()); + server.start(); + + + JSONObject obj = new JSONObject(); + obj.put("rn", aeName); + obj.put("api", 12345); + obj.put("rr", false); + JSONObject resource = new JSONObject(); + resource.put("m2m:ae", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName, resource.toString(), 2); + + + obj = new JSONObject(); + obj.put("rn", cntName); + resource = new JSONObject(); + resource.put("m2m:cnt", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName, resource.toString(), 3); + + + + JSONArray array = new JSONArray(); + array.put(notificationURI); + obj = new JSONObject(); + obj.put("nu", array); + obj.put("rn", subTestRU); + obj.put("nct", 2); + resource = new JSONObject(); + resource.put("m2m:sub", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 23); + + + array = new JSONArray(); + array.put(notificationURI); + obj = new JSONObject(); + obj.put("nu", array); + obj.put("rn", subTestD); + obj.put("nct", 2); + resource = new JSONObject(); + resource.put("m2m:sub", obj); + RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 23); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName); + } + + @Test + public void testCreate() { + JSONArray array = new JSONArray(); + array.put(notificationURI); + JSONObject obj = new JSONObject(); + obj.put("nu", array); + obj.put("rn", subTestC); + obj.put("nct", 2); + JSONObject resource = new JSONObject(); + resource.put("m2m:sub", obj); + HttpResponse httpResponse = RestHttpClient.post(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName, resource.toString(), 23); + assertEquals(201, httpResponse.getStatusCode()); + } + + @Test + public void testRetreive() { + HttpResponse httpResponse = RestHttpClient.get(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName+"/"+subTestRU); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void testUpdate() { + JSONObject obj = new JSONObject(); + obj.put("et", "20181228T164835"); + JSONObject resource = new JSONObject(); + resource.put("m2m:sub", obj); + HttpResponse httpResponse = RestHttpClient.put(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName+"/"+subTestRU, resource.toString()); + assertEquals(200, httpResponse.getStatusCode()); + } + + @Test + public void TestDelete() { + HttpResponse httpResponse = RestHttpClient.delete(originator, csePoa+"/~/"+cseId+"/"+cseName+"/"+aeName+"/"+cntName+"/"+subTestD); + assertEquals(200, httpResponse.getStatusCode()); + } + + + static class MyHandler implements HttpHandler { + + public void handle(HttpExchange httpExchange) { + System.out.println("Event Recieved!"); + + try{ + InputStream in = httpExchange.getRequestBody(); + + String requestBody = ""; + int i;char c; + while ((i = in.read()) != -1) { + c = (char) i; + requestBody = (String) (requestBody+c); + } + + System.out.println(requestBody); + + String responseBudy =""; + byte[] out = responseBudy.getBytes("UTF-8"); + httpExchange.sendResponseHeaders(200, out.length); + OutputStream os = httpExchange.getResponseBody(); + os.write(out); + os.close(); + + } catch(Exception e){ + e.printStackTrace(); + } + } + } + +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/TestConfig.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/TestConfig.java new file mode 100644 index 00000000..adf80070 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tests/TestConfig.java @@ -0,0 +1,22 @@ +package org.eclipse.om2m.adn.tests; + +public class TestConfig { + + public final static String originator="admin:admin"; + public final static String cseProtocol="http"; + + public final static String cseIp = "127.0.0.1"; + public final static int csePort = 8080; + + public final static String cseId = "in-cse"; + public final static String cseName = "in-name"; + + public final static String remoteCseIp = "127.0.0.1"; + public final static int remoteCSEPort = 8282; + + public final static String remoteCseId = "mn-cse"; + public final static String remoteCseName = "mn-name"; + + public final static String csePoa = cseProtocol+"://"+cseIp+":"+csePort; + public final static String remoteCsePoa = cseProtocol+"://"+remoteCseIp+":"+remoteCSEPort; +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/HttpResponse.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/HttpResponse.java new file mode 100644 index 00000000..d9289d24 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/HttpResponse.java @@ -0,0 +1,22 @@ +package org.eclipse.om2m.adn.tools; + +public class HttpResponse { + private int statusCode; + private String body; + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } +} diff --git a/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/RestHttpClient.java b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/RestHttpClient.java new file mode 100644 index 00000000..081e3717 --- /dev/null +++ b/org.eclipse.om2m.adntest/src/test/java/org/eclipse/om2m/adn/tools/RestHttpClient.java @@ -0,0 +1,214 @@ +/***************************************************************************************** +* SENSINOV ("COMPANY") CONFIDENTIAL +* Copyright (c) 2016 SENSINOV, All Rights Reserved. +* +* NOTICE: All information contained herein is, and remains the property of COMPANY. +* The intellectual and technical concepts contained herein are proprietary to COMPANY +* and may be covered by France and Foreign Patents, patents in process, and are protected +* by trade secret or copyright law. Dissemination of this information or reproduction +* of this material is strictly forbidden unless prior written permission is obtained +* from COMPANY. Access to the source code contained herein is hereby forbidden to +* anyone except current COMPANY employees, managers or contractors who have executed +* Confidentiality and Non-disclosure agreements explicitly covering such access. +* +* The copyright notice above does not evidence any actual or intended publication or +* disclosure of this source code, which includes * information that is confidential +* and/or proprietary, and is a trade secret, of COMPANY. ANY REPRODUCTION, MODIFICATION, +* DISTRIBUTION, PUBLIC PERFORMANCE, OR PUBLIC DISPLAY OF OR THROUGH USE OF THIS SOURCE +* CODE WITHOUT THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED, AND IN +* VIOLATION OF APPLICABLE * LAWS AND INTERNATIONAL TREATIES. THE RECEIPT OR POSSESSION OF +* THIS SOURCE CODE AND/OR RELATED INFORMATION DOES NOT CONVEY OR IMPLY ANY RIGHTS TO +* REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR TO MANUFACTURE, USE, OR SELL ANYTHING +* THAT IT MAY DESCRIBE, IN WHOLE OR IN PART. +*******************************************************************************************/ + +package org.eclipse.om2m.adn.tools; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import javax.net.ssl.HostnameVerifier; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.*; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.ssl.SSLSocketFactory; +import org.apache.http.conn.ssl.X509HostnameVerifier; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.eclipse.om2m.adn.tests.TestConfig; + + +public class RestHttpClient { + static DefaultHttpClient httpclient=null; + + public static void init(){ + + httpclient = new DefaultHttpClient(); + + if(TestConfig.cseProtocol.equals("https")){ + try{ + KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); + FileInputStream instream = new FileInputStream(new File("keystore.jks")); + try { + trustStore.load(instream, "keystore".toCharArray()); + }catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + }catch (CertificateException e) { + e.printStackTrace(); + }finally { + try { instream.close(); } catch (Exception ignore) {} + } + SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore, "keystore",trustStore); + socketFactory.setHostnameVerifier((X509HostnameVerifier) SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + Scheme sch = new Scheme("https", 8443, socketFactory); + httpclient.getConnectionManager().getSchemeRegistry().register(sch); + }catch (Exception e){ + e.printStackTrace(); + } + } + + } + + + public static HttpResponse get(String originator, String uri) { + if(httpclient==null){ + init(); + } + System.out.println("HTTP GET "+uri); + + HttpGet httpGet= new HttpGet(uri); + + httpGet.addHeader("x-m2m-ri",generateRI()); + httpGet.addHeader("x-m2m-origin",originator); + httpGet.addHeader("Accept","application/json"); + + HttpResponse httpResponse = new HttpResponse(); + + try { + CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpGet); + try{ + httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode()); + httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity())); + }finally{ + closeableHttpResponse.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody()); + return httpResponse; + } + public static HttpResponse put(String originator, String uri, String body) { + if(httpclient==null){ + init(); + } + System.out.println("HTTP PUT "+uri+"\n"+body); + + HttpPut httpPut= new HttpPut(uri); + + httpPut.addHeader("X-M2M-RI",generateRI()); + httpPut.addHeader("X-M2M-Origin",originator); + httpPut.addHeader("Content-Type","application/json"); + httpPut.addHeader("Accept","application/json"); + + HttpResponse httpResponse = new HttpResponse(); + try { + CloseableHttpResponse closeableHttpResponse =null; + try { + httpPut.setEntity(new StringEntity(body)); + closeableHttpResponse= httpclient.execute(httpPut); + httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode()); + httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity())); + + }finally{ + closeableHttpResponse.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody()); + + return httpResponse ; + } + + public static HttpResponse post(String originator, String uri, String body, int ty) { + if(httpclient==null){ + init(); + } + System.out.println("HTTP POST "+uri+"\n"+body); + + + final HttpPost httpPost = new HttpPost(uri); + + httpPost.addHeader("X-M2M-RI",generateRI()); + httpPost.addHeader("X-M2M-Origin",originator); + httpPost.addHeader("Accept","application/json"); + + httpPost.addHeader("Content-Type","application/json;ty="+ty); + + final HttpResponse httpResponse = new HttpResponse(); + + try{ + httpPost.setEntity(new StringEntity(body)); + try { + CloseableHttpResponse closeableHttpResponse=null; + closeableHttpResponse = httpclient.execute(httpPost); + httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode()); + httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity())); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody()); + return httpResponse ; + } + + public static HttpResponse delete(String originator, String uri) { + if(httpclient==null){ + init(); + } + System.out.println("HTTP DELETE "+uri); + + HttpDelete httpDelete = new HttpDelete(uri); + + httpDelete.addHeader("X-M2M-RI",generateRI()); + httpDelete.addHeader("X-M2M-Origin",originator); + httpDelete.addHeader("Accept","application/json"); + + + HttpResponse httpResponse = new HttpResponse(); + try { + CloseableHttpResponse closeableHttpResponse = httpclient.execute(httpDelete); + try { + httpResponse.setStatusCode(closeableHttpResponse.getStatusLine().getStatusCode()); + httpResponse.setBody(EntityUtils.toString(closeableHttpResponse.getEntity())); + }finally{ + closeableHttpResponse.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("HTTP Response "+httpResponse.getStatusCode()+"\n"+httpResponse.getBody()); + return httpResponse ; + } + + public static String generateRI(){ + Integer random = (int)(Math.random()*1000)+100000; + return random.toString(); + } +} \ No newline at end of file -- cgit v1.2.3