replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-container / src / Configuration.cpp
index 13bb113..3a7fd95 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdexcept>
 #include <utility>
 #include "InternalTypes.h"
+#include <string.h>
 
 #define CONTAINER_TAG "RESOURCE_CONTAINER"
 #define DISCOVER_TAG "DISCOVER_RESOURCE_UNIT"
@@ -86,8 +87,8 @@ namespace OIC
 
         void Configuration::getConfiguredBundles(configInfo *configOutput)
         {
-            rapidxml::xml_node< char > *bundle;
-            rapidxml::xml_node< char > *subItem;
+            rapidxml::xml_node< char > *bundle = nullptr;
+            rapidxml::xml_node< char > *subItem = nullptr;
 
             string strKey, strValue;
 
@@ -95,9 +96,10 @@ namespace OIC
             {
                 try
                 {
-                    if (m_xmlDoc.first_node())
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
                     {
-                        for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
                                  bundle->next_sibling())
                         {
                             std::map< std::string, std::string > bundleMap;
@@ -128,7 +130,7 @@ namespace OIC
 
         void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
         {
-            rapidxml::xml_node< char > *bundle;
+            rapidxml::xml_node< char > *bundle = nullptr;
 
             string strBundleId, strPath, strVersion;
 
@@ -139,9 +141,10 @@ namespace OIC
                     std::map< std::string, std::string > bundleConfigMap;
 
                     // <bundle>
-                    if (m_xmlDoc.first_node())
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
                     {
-                        for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
                                  bundle->next_sibling())
                         {
                             // <id>
@@ -149,7 +152,8 @@ namespace OIC
                             {
                                 strBundleId = bundle->first_node(BUNDLE_ID)->value();
                             }
-                            else{
+                            else
+                            {
                                 strBundleId = "";
                             }
 
@@ -158,19 +162,23 @@ namespace OIC
                                 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
 
                                 // <path>
-                                if (bundle->first_node(BUNDLE_PATH)){
+                                if (bundle->first_node(BUNDLE_PATH))
+                                {
                                     strPath = bundle->first_node(BUNDLE_PATH)->value();
                                 }
-                                else{
+                                else
+                                {
                                     strPath = "";
                                 }
                                 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
 
                                 // <version>
-                                if (bundle->first_node(BUNDLE_VERSION)){
+                                if (bundle->first_node(BUNDLE_VERSION))
+                                {
                                     strVersion = bundle->first_node(BUNDLE_VERSION)->value();
                                 }
-                                else{
+                                else
+                                {
                                     strVersion = "";
                                 }
                                 bundleConfigMap.insert(
@@ -191,26 +199,30 @@ namespace OIC
             }
         }
 
-        void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceName,
-                        resourceInfo *resourceInfoOut){
-            rapidxml::xml_node< char > *bundle;
-            rapidxml::xml_node< char > *resource;
-            rapidxml::xml_node< char > *item, *subItem, *subItem2;
+        void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceUri,
+                        resourceInfo *resourceInfoOut)
+        {
+            rapidxml::xml_node< char > *bundle = nullptr;
+            rapidxml::xml_node< char > *resource = nullptr;
+            rapidxml::xml_node< char > *item = nullptr;
+            rapidxml::xml_node< char > *subItem = nullptr;
+            rapidxml::xml_node< char > *subItem2 = nullptr;
 
             string strBundleId;
             string strKey, strValue;
             OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
-                    bundleId.c_str(), resourceName.c_str());
+                bundleId.c_str(), resourceUri.c_str());
 
             if (m_loaded)
             {
                 try
                 {
                     // <bundle>
-                    if (m_xmlDoc.first_node())
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
                     {
-                        for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
-                                 bundle->next_sibling())
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle;
+                            bundle = bundle->next_sibling())
                         {
                             // <id>
                             strBundleId = bundle->first_node(BUNDLE_ID)->value();
@@ -222,9 +234,10 @@ namespace OIC
                             {
                                 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
                                 // <resourceInfo>
-                                if (bundle->first_node(OUTPUT_RESOURCES_TAG)){
-                                    for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->
-                                            first_node(OUTPUT_RESOURCE_INFO);
+                                bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
+                                if (bundle)
+                                {
+                                    for (resource = bundle->first_node(OUTPUT_RESOURCE_INFO);
                                          resource; resource = resource->next_sibling())
                                     {
 
@@ -235,16 +248,29 @@ namespace OIC
                                             strValue = item->value();
 
                                             if (!strKey.compare(OUTPUT_RESOURCE_NAME))
+                                            {
+                                                
                                                 resourceInfoOut->name = trim_both(strValue);
+                                            }
 
                                             else if (!strKey.compare(OUTPUT_RESOURCE_URI))
+                                            {
+                                                if (trim_both(strValue).compare(resourceUri) != 0)
+                                                {
+                                                    break;
+                                                }
                                                 resourceInfoOut->uri = trim_both(strValue);
+                                            }
 
                                             else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
+                                            {
                                                 resourceInfoOut->address = trim_both(strValue);
+                                            }
 
                                             else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
+                                            {
                                                 resourceInfoOut->resourceType = trim_both(strValue);
+                                            }
 
                                             else
                                             {
@@ -294,7 +320,8 @@ namespace OIC
                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
                 }
             }
-            else{
+            else
+            {
                 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
             }
         }
@@ -302,9 +329,11 @@ namespace OIC
         void Configuration::getResourceConfiguration(std::string bundleId,
                 std::vector< resourceInfo > *configOutput)
         {
-            rapidxml::xml_node< char > *bundle;
-            rapidxml::xml_node< char > *resource;
-            rapidxml::xml_node< char > *item, *subItem, *subItem2;
+            rapidxml::xml_node< char > *bundle = nullptr;
+            rapidxml::xml_node< char > *resource = nullptr;
+            rapidxml::xml_node< char > *item = nullptr;
+            rapidxml::xml_node< char > *subItem = nullptr;
+            rapidxml::xml_node< char > *subItem2 = nullptr;
 
             string strBundleId;
             string strKey, strValue;
@@ -315,13 +344,17 @@ namespace OIC
                 try
                 {
                     // <bundle>
-                    if (m_xmlDoc.first_node())
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
                     {
-                        for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
                                  bundle->next_sibling())
                         {
                             // <id>
-                            strBundleId = bundle->first_node(BUNDLE_ID)->value();
+                            if (bundle->first_node(BUNDLE_ID))
+                            {
+                                strBundleId = bundle->first_node(BUNDLE_ID)->value();
+                            }
 
                             OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
                                     strBundleId.c_str(), bundleId.c_str());
@@ -330,9 +363,10 @@ namespace OIC
                             {
                                 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
                                 // <resourceInfo>
-                                if (bundle->first_node(OUTPUT_RESOURCES_TAG))
+                                bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
+                                if (bundle)
                                 {
-                                    for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->
+                                    for (resource = bundle->
                                             first_node(OUTPUT_RESOURCE_INFO);
                                          resource; resource = resource->next_sibling())
                                     {
@@ -345,16 +379,24 @@ namespace OIC
                                             strValue = item->value();
 
                                             if (!strKey.compare(OUTPUT_RESOURCE_NAME))
+                                            {
                                                 tempResourceInfo.name = trim_both(strValue);
+                                            }
 
                                             else if (!strKey.compare(OUTPUT_RESOURCE_URI))
+                                            {
                                                 tempResourceInfo.uri = trim_both(strValue);
+                                            }
 
                                             else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
+                                            {
                                                 tempResourceInfo.address = trim_both(strValue);
+                                            }
 
                                             else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
+                                            {
                                                 tempResourceInfo.resourceType = trim_both(strValue);
+                                            }
 
                                             else
                                             {
@@ -405,7 +447,8 @@ namespace OIC
                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
                 }
             }
-            else{
+            else
+            {
                 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
             }
         }