Example #1
0
        public static void SerializeResourceInCoral(IResource resource, CoralBody coral,
                                                    Dictionary <string, CBORObject> dictionary,
                                                    ResourceAttributes otherAttributes = null, Uri uriRelative = null,
                                                    bool isEndPoint = false)
        {
            CBORObject obj = CBORObject.NewArray();
            CBORObject href;

            if (uriRelative == null)
            {
                href = Ciri.ToCbor(resource.Path + resource.Name);
            }
            else
            {
                href = Ciri.ToCbor(new Uri(uriRelative, resource.Path + resource.Name));
            }

            CoralBody body = new CoralBody();

            SerializeAttributesInCoral(resource.Attributes, body, dictionary, uriRelative);
            if (otherAttributes != null)
            {
                SerializeAttributesInCoral(otherAttributes, body, dictionary, uriRelative);
            }

            if (body.Length == 0)
            {
                body = null;
            }

            CoralItem item = new CoralLink(isEndPoint ? "http://coreapps.org/ref#rd-unit" : "http://coreapps.org/reef#rd-item", href, body);

            coral.Add(item);
        }
Example #2
0
        public void TestConstructors()
        {
            Cori cori = new Cori("coap://host:99");

            Assert.ThrowsException <ArgumentException>(() => new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("01")), cori, null));
            Assert.ThrowsException <ArgumentException>(() => new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("830202820500")), cori, null));

            CoralBody body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("81830202820500")), cori, _testDictionary);

            Assert.AreEqual(1, body.Length);
            Assert.AreEqual("http://www.iana.org/assignments/relation/collection", ((CoralLink)body[0]).RelationTypeText);

            body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("828302028205008302006377766F")), cori, _testDictionary);
            Assert.AreEqual(2, body.Length);
            Assert.AreEqual("http://www.iana.org/assignments/relation/collection", ((CoralLink)body[0]).RelationTypeText);
            Assert.AreEqual("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", ((CoralLink)body[1]).RelationTypeText);

            // [
            // [2
            // [0, "http", 1, "apps.augustcellars.com", 5, "rel1"],
            // [5, "target1"],
            // [
            // [2,
            // [0, "http", 1, "apps.augustcellars.com", 5, "rel2"],
            // [4, 1, 5, "target2"]], [1, [0, "coap", 1, "host3", 5, "link1"]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel2"], [4, 1, 5, "target2"]]]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel3"], [5, "target2"]], [1, [0, "http", 1, "host", 5, "link2", 5, "link3"]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel4"], [4, 1, 5, "target2"]], [3, [0, "http", 1, "apps.augustcellars.com", 5, "op-type"], [4, 1, 5, "form"]]]

            body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("858402860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C3182056774617267657431838302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C328404010567746172676574328201860064636F61700165686F73743305656C696E6B318302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C328404010567746172676574328302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C33820567746172676574328201880064687474700164686F737405656C696E6B3205656C696E6B338302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C348404010567746172676574328303860064687474700176617070732E61756775737463656C6C6172732E636F6D05676F702D747970658404010564666F726D")), cori, _testDictionary);
            Assert.AreEqual(5, body.Length);

            Assert.IsTrue(body[0] is CoralLink);
            Assert.IsTrue(body[1] is CoralLink);
            Assert.IsTrue(body[2] is CoralBaseDirective);
            Assert.IsTrue(body[3] is CoralLink);
            Assert.IsTrue(body[4] is CoralForm);

            CoralLink link = (CoralLink)body[0];

            Assert.AreEqual("coap://host:99/target1", link.Target.ToString());
            Assert.AreEqual(3, link.Body.Length);

            Assert.IsTrue(link.Body[0] is CoralLink);
            Assert.IsTrue(link.Body[1] is CoralBaseDirective);
            Assert.IsTrue(link.Body[2] is CoralLink);

            CoralLink link2 = (CoralLink)link.Body[0];

            Assert.AreEqual("coap://host:99/target1/target2", link2.Target.ToString());
            link2 = (CoralLink)link.Body[2];
            Assert.AreEqual("coap://host3/link1/target2", link2.Target.ToString());

            link = (CoralLink)body[1];
            Assert.AreEqual("coap://host:99/target2", link.Target.ToString());

            link = (CoralLink)body[3];
            Assert.AreEqual("http://host/link2/link3/target2", link.Target.ToString());

            Assert.AreEqual("http://host/link2/link3/form", ((CoralForm)body[4]).Target.ToString());
        }
        public void ExampleTest()
        {
            CoralDocument document = new CoralDocument();

            CoralBody body = new CoralBody();

            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#content-type", CBORObject.FromObject(99599)));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#security", "OSCORE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority-type", "ACE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority", new Cori("coap://ace.example.org/token")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-register", new Cori("coap://jimsch.example.org/rd/endpoints"), body));

            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-endpointSearch", new Cori("coap://jimsch.example.org/rd/endpoints")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-resourceSearch", new Cori("coap://jimsch.example.org/rd/resources")));

            document.Add(new CoralBaseDirective(new Cori("coaps://jimsch.example.org/rd")));

            body = new CoralBody();
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#content-type", CBORObject.FromObject(99599)));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#security", "OSCORE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority-type", "ACE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority", new Cori("coaps://ace.example.org/token")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-register", new Cori("coaps://jimsch.example.org/rd/endpoints"), body));

            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-endpointSearch", new Cori("coaps://jimsch.example.org/rd/endpoints")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-resourceSearch", new Cori("coaps://jimsch.example.org/rd/resources")));

            CBORObject result = document.EncodeToCBORObject(new Cori("coap://jimsch.example.org/rd"), _Dictionary);

            Assert.AreEqual("[[2, 5, [4, 2, 5, \"endpoints\"], [[2, 1, 99599], [2, 2, \"OSCORE\"], [2, 3, \"ACE\"], [2, 4, [1, \"ace.example.org\", 5, \"token\"]]]], [2, 6, [4, 2, 5, \"endpoints\"]], [2, 7, [4, 2, 5, \"resources\"]], [1, [0, \"coaps\", 1, \"jimsch.example.org\", 5, \"rd\"]], [2, 5, [4, 2, 5, \"endpoints\"], [[2, 1, 99599], [2, 2, \"OSCORE\"], [2, 3, \"ACE\"], [2, 4, [1, \"ace.example.org\", 5, \"token\"]]]], [2, 6, [4, 2, 5, \"endpoints\"]], [2, 7, [4, 2, 5, \"resources\"]]]", result.ToString());

            CoralDocument document2 = CoralDocument.DecodeFromBytes(document.EncodeToBytes(new Cori("coap://jimsch.example.org/rd"), _Dictionary), new Cori("coap://jimsch.example.org/rd"), _Dictionary);

            //Assert.AreEqual(document, document2);

            CoralUsing dict = new CoralUsing()
            {
                { "reef", "coap://jimsch.example.com/coreapp/reef#" }
            };

            string resultOut =
                "reef:rd-register <./endpoints> [\n  reef:content-type 99599\n  reef:security \"OSCORE\"\n  reef:authority-type \"ACE\"\n  reef:authority <//ace.example.org/token>\n]\nreef:rd-endpointSearch <./endpoints>\nreef:rd-resourceSearch <./resources>\n#base <coaps://jimsch.example.org/rd>\nreef:rd-register <./endpoints> [\n  reef:content-type 99599\n  reef:security \"OSCORE\"\n  reef:authority-type \"ACE\"\n  reef:authority <//ace.example.org/token>\n]\nreef:rd-endpointSearch <./endpoints>\nreef:rd-resourceSearch <./resources>\n";

            Assert.AreEqual(resultOut, document.EncodeToString(new Cori("coap://jimsch.example.org/rd"), dict));

            resultOut = "<coap://jimsch.example.com/coreapp/reef#rd-register> <coap://jimsch.example.org/rd/endpoints> [\n  <coap://jimsch.example.com/coreapp/reef#content-type> 99599\n  <coap://jimsch.example.com/coreapp/reef#security> \"OSCORE\"\n  <coap://jimsch.example.com/coreapp/reef#authority-type> \"ACE\"\n  <coap://jimsch.example.com/coreapp/reef#authority> <coap://ace.example.org/token>\n]\n<coap://jimsch.example.com/coreapp/reef#rd-endpointSearch> <coap://jimsch.example.org/rd/endpoints>\n<coap://jimsch.example.com/coreapp/reef#rd-resourceSearch> <coap://jimsch.example.org/rd/resources>\n#base <coaps://jimsch.example.org/rd>\n<coap://jimsch.example.com/coreapp/reef#rd-register> <./endpoints> [\n  <coap://jimsch.example.com/coreapp/reef#content-type> 99599\n  <coap://jimsch.example.com/coreapp/reef#security> \"OSCORE\"\n  <coap://jimsch.example.com/coreapp/reef#authority-type> \"ACE\"\n  <coap://jimsch.example.com/coreapp/reef#authority> <//ace.example.org/token>\n]\n<coap://jimsch.example.com/coreapp/reef#rd-endpointSearch> <./endpoints>\n<coap://jimsch.example.com/coreapp/reef#rd-resourceSearch> <./resources>\n";
            Assert.AreEqual(resultOut, document.ToString());
        }
Example #4
0
        public static byte[] SerializeCoral(IResource root, IEnumerable <string> queries)
        {
            CoralBody nodeRoot = new CoralBody();


            List <string> queryList = null;

            if (queries != null)
            {
                queryList = queries.ToList();
            }

            foreach (IResource child in root.Children)
            {
                SerializeTreeInCoral(child, queryList, nodeRoot, CborAttributeKeys);
            }

            return(nodeRoot.EncodeToBytes(ReefDictionary));
        }
Example #5
0
        public void TestEncode()
        {
            CoralBody body = new CoralBody {
                new CoralLink("http://coreapps.org/collections#create", "yes"),
                new CoralLink("http://coreapps.org/coap#type", CBORObject.False)
            };
            CBORObject obj = body.EncodeToCBORObject(null);

            Assert.AreEqual("[[2, 3, \"yes\"], [2, 8, false]]", obj.ToString());

            //

            CoralBody body1 = new CoralBody()
            {
                new CoralLink("http://apps.augustcellars.com/link1", new Cori("coap://host/path1/path2/path3")),
                new CoralBaseDirective(new Cori("coaps://host/path1")),
                new CoralLink("http://apps.augustcellars.com/link1", new Cori("coap://host/path1/path2/path3")),
                new CoralLink("http://apps.augustcellars.com/link2", new Cori("coaps://host/path1/path2/path3"))
            };

            CoralForm form = new CoralForm("http://apps.augustcellars.com/form1", new Cori("coap://host/path1/path4"));

            form.FormFields.Add(new CoralFormField("http://apps.augustcellars.com/field1", new Cori("coap://host/path2")));
            form.FormFields.Add(new CoralFormField("http://apps.augustcellars.com/field2", new Cori("coaps://host/path2")));

            body = new CoralBody()
            {
                new CoralLink("http://apps.augustcellars.com/link1", new Cori("coap://host/path"), body1),
                new CoralBaseDirective(new Cori("http://host")),
                new CoralLink("http://apps.augustcellars.com/link1", new Cori("coap://host/path"), body1),
                new CoralLink("http://apps.augustcellars.com/link2", new Cori("http://host/path1"), body1),
            };

            obj = body.EncodeToCBORObject(new Cori("coap://host"), _testDictionary);
            Assert.AreEqual("[[2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [5, \"path\"], [[2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [5, \"path1\", 5, \"path2\", 5, \"path3\"]], [1, [0, \"coaps\", 1, \"host\", 5, \"path1\"]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [0, \"coap\", 1, \"host\", 5, \"path1\", 5, \"path2\", 5, \"path3\"]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link2\"], [4, 2, 5, \"path2\", 5, \"path3\"]]]], [1, [0, \"http\", 1, \"host\"]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [0, \"coap\", 1, \"host\", 5, \"path\"], [[2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [0, \"coap\", 1, \"host\", 5, \"path1\", 5, \"path2\", 5, \"path3\"]], [1, [0, \"coaps\", 1, \"host\", 5, \"path1\"]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [0, \"coap\", 1, \"host\", 5, \"path1\", 5, \"path2\", 5, \"path3\"]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link2\"], [4, 2, 5, \"path2\", 5, \"path3\"]]]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link2\"], [5, \"path1\"], [[2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [0, \"coap\", 1, \"host\", 5, \"path1\", 5, \"path2\", 5, \"path3\"]], [1, [0, \"coaps\", 1, \"host\", 5, \"path1\"]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link1\"], [0, \"coap\", 1, \"host\", 5, \"path1\", 5, \"path2\", 5, \"path3\"]], [2, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"link2\"], [4, 2, 5, \"path2\", 5, \"path3\"]]]]]", obj.ToString());
        }
Example #6
0
        private static void SerializeAttributeInCoral(string name, List <string> values, CoralBody coral,
                                                      Dictionary <string, CBORObject> dictionary)
        {
            bool       useSpace = SpaceSeparatedValueAttributes.Contains(name);
            CBORObject result;

            string nameX = CoralsKeys[name];

            if (useSpace && values.Count > 1)
            {
                StringBuilder sb = new StringBuilder();

                foreach (string value in values)
                {
                    sb.Append(value);
                    sb.Append(" ");
                }

                sb.Length = sb.Length - 1;

                result = CBORObject.FromObject(sb.ToString());
            }
            else if (values.Count == 1)
            {
                string value = values.First();
                result = string.IsNullOrEmpty(value) ? CBORObject.True : CBORObject.FromObject(values.First());
            }
            else
            {
                result = CBORObject.NewArray();
                foreach (string value in values)
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        result.Add(CBORObject.True);
                    }
                    else
                    {
                        result.Add(value);
                    }
                }
            }

            CoralLink link = new CoralLink(nameX, result);

            coral.Add(link);
        }
Example #7
0
        private static void SerializeAttributesInCoral(ResourceAttributes attributes, CoralBody coral, Dictionary <string, CBORObject> dictionary, Uri uriRelative)
        {
            List <string> keys = new List <string>(attributes.Keys);

            keys.Sort();
            foreach (string name in keys)
            {
                if (!CoralsKeys.ContainsKey(name))
                {
                    continue;
                }

                List <string> values = new List <string>(attributes.GetValues(name));
                if (values.Count == 0)
                {
                    continue;
                }

                if (uriRelative != null && name == "anchor")
                {
                    List <string> newValues = new List <string>();
                    foreach (string val in values)
                    {
                        newValues.Add(new Uri(uriRelative, val).ToString());
                    }

                    values = newValues;
                }

                SerializeAttributeInCoral(name, values, coral, null);
            }
        }
Example #8
0
        private static void SerializeTreeInCoral(IResource resource, List <string> queries, CoralBody coral,
                                                 Dictionary <string, CBORObject> dictionary)
        {
            if (resource.Visible && Matches(resource, queries))
            {
                SerializeResourceInCoral(resource, coral, dictionary);
            }

            if (resource.Children == null)
            {
                return;
            }

            //  sort by resource name
            List <IResource> children = new List <IResource>(resource.Children);

            children.Sort((r1, r2) => string.CompareOrdinal(r1.Name, r2.Name));

            foreach (IResource child in children)
            {
                SerializeTreeInCoral(child, queries, coral, dictionary);
            }
        }