public override SchemaReference ResolveSchemaReference(ResolveSchemaContext context)
        {
            SchemaReference reference = base.ResolveSchemaReference(context);

            if (PreloadedUris.Any(i => i == reference.BaseUri))
            {
                return(reference);
            }

            foreach (Uri preloadedUri in PreloadedUris)
            {
                if (preloadedUri.IsBaseOf(reference.BaseUri))
                {
                    Uri relativeUri = preloadedUri.MakeRelativeUri(reference.BaseUri);

                    string uriText = relativeUri.OriginalString;
                    if (reference.SubschemaId != null)
                    {
                        uriText += reference.SubschemaId.OriginalString;
                    }

                    reference.BaseUri     = preloadedUri;
                    reference.SubschemaId = new Uri(uriText, UriKind.RelativeOrAbsolute);

                    return(reference);
                }
            }

            return(reference);
        }
Ejemplo n.º 2
0
        public override Stream GetSchemaResource(ResolveSchemaContext context, SchemaReference reference)
        {
            var contextFilename = Path.GetFileName(context.ResolvedSchemaId.LocalPath);
            var reducedUri      = new UriBuilder(context.ResolvedSchemaId)
            {
                Fragment = null,
                Path     = context.ResolvedSchemaId.LocalPath.Substring(0, context.ResolvedSchemaId.LocalPath.Length - contextFilename.Length)
            }.Uri;

            if (!_baseUris.Any(b => Uri.Compare(b, reducedUri, UriComponents.Scheme | UriComponents.HostAndPort | UriComponents.Path, UriFormat.UriEscaped, StringComparison.OrdinalIgnoreCase) == 0))
            {
                Debug.WriteLine($"Failed to find matching baseuri for {reducedUri}");
            }
            else if (_schemaCache.TryGetValue(contextFilename, out var schemaData))
            {
                return(new MemoryStream(Encoding.UTF8.GetBytes(schemaData), false));
            }

            foreach (var searchPath in _schemaSearchDir)
            {
                var file = new FileInfo(Path.Combine(searchPath, contextFilename));
                if (!file.Exists)
                {
                    continue;
                }

                return(file.Open(FileMode.Open));
            }

            Debug.WriteLine($"Failed to find schema for {contextFilename}");
            return(null);
        }
Ejemplo n.º 3
0
        public static void AvroWithReferences(Config config)
        {
            var srClient = new CachedSchemaRegistryClient(new SchemaRegistryConfig {
                Url = config.Server
            });
            Schema schema1 = new Schema(@"{""type"":""record"",""name"":""EventA"",""namespace"":""Kafka.Avro.Examples"",""fields"":[{""name"":""EventType"",""type"":""string""},{""name"":""EventId"",""type"":""string""},{""name"":""OccuredOn"",""type"":""long""},{""name"":""A"",""type"":""string""}]}", SchemaType.Avro);
            Schema schema2 = new Schema(@"{""type"":""record"",""name"":""EventB"",""namespace"":""Kafka.Avro.Examples"",""fields"":[{""name"":""EventType"",""type"":""string""},{""name"":""EventId"",""type"":""string""},{""name"":""OccuredOn"",""type"":""long""},{""name"":""B"",""type"":""long""}]}", SchemaType.Avro);
            var    id1     = srClient.RegisterSchemaAsync("events-a", schema1).Result;
            var    id2     = srClient.RegisterSchemaAsync("events-b", schema2).Result;

            var             avroUnion   = @"[""Kafka.Avro.Examples.EventA"",""Kafka.Avro.Examples.EventB""]";
            Schema          unionSchema = new Schema(avroUnion, SchemaType.Avro);
            SchemaReference reference   = new SchemaReference(
                "Kafka.Avro.Examples.EventA",
                "events-a",
                1);

            unionSchema.References.Add(reference);
            reference = new SchemaReference(
                "Kafka.Avro.Examples.EventB",
                "events-b",
                1);
            unionSchema.References.Add(reference);

            var id3 = srClient.RegisterSchemaAsync("events-value", unionSchema).Result;

            Assert.NotEqual(0, id3);
        }
Ejemplo n.º 4
0
    public static void Main()
    {
        try
        {
// <Snippet2>

            // Reference the schema document.
            string    myStringUrl = "c:\\Inetpub\\wwwroot\\dataservice.xsd";
            XmlSchema myXmlSchema;

            // Create the client protocol.
            DiscoveryClientProtocol myDiscoveryClientProtocol =
                new DiscoveryClientProtocol();
            myDiscoveryClientProtocol.Credentials =
                CredentialCache.DefaultCredentials;

            //  Create a schema reference.
            SchemaReference mySchemaReferenceNoParam = new SchemaReference();

            SchemaReference mySchemaReference = new SchemaReference(myStringUrl);

            // Set the client protocol.
            mySchemaReference.ClientProtocol = myDiscoveryClientProtocol;

            // Access the default file name associated with the schema reference.
            Console.WriteLine("Default filename is : " +
                              mySchemaReference.DefaultFilename);

            // Access the namespace associated with schema reference class.
            Console.WriteLine("Namespace is : " + SchemaReference.Namespace);

            FileStream myStream =
                new FileStream(myStringUrl, FileMode.OpenOrCreate);

            // Read the document in a stream.
            mySchemaReference.ReadDocument(myStream);

            // Get the schema of referenced document.
            myXmlSchema = mySchemaReference.Schema;

            Console.WriteLine("Reference is : " + mySchemaReference.Ref);

            Console.WriteLine("Target namespace (default empty) is : " +
                              mySchemaReference.TargetNamespace);

            Console.WriteLine("URL is : " + mySchemaReference.Url);

            // Write the document in the stream.
            mySchemaReference.WriteDocument(myXmlSchema, myStream);

            myStream.Close();
            mySchemaReference = null;

// </Snippet2>
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
		public override DiscoveryReference GetDiscoveryReference (string filename)
		{
			SchemaReference refe = new SchemaReference ();
			refe.Url = filename;
			refe.Ref = filename;
			return refe;
		}
        private JSchema ResolvedSchema(Uri schemaId, Uri resolvedSchemaId)
        {
            ResolveSchemaContext context = new ResolveSchemaContext
            {
                ResolverBaseUri  = _baseUri,
                SchemaId         = schemaId,
                ResolvedSchemaId = resolvedSchemaId
            };

            SchemaReference schemaReference = _resolver.ResolveSchemaReference(context);

            if (schemaReference.BaseUri == _baseUri)
            {
                // reference is to inside the current schema
                // use normal schema resolution
                return(null);
            }

            if (Cache.ContainsKey(schemaReference.BaseUri))
            {
                // base URI has already been resolved
                // use previously retrieved schema
                JSchema cachedSchema = Cache[schemaReference.BaseUri];
                return(_resolver.GetSubschema(schemaReference, cachedSchema));
            }

            Stream schemaData = _resolver.GetSchemaResource(context, schemaReference);

            if (schemaData == null)
            {
                // resolver returned no data
                return(null);
            }

            JSchemaReaderSettings settings = new JSchemaReaderSettings
            {
                BaseUri  = schemaReference.BaseUri,
                Resolver = _resolver
            };
            JSchemaReader schemaReader = new JSchemaReader(settings);

            schemaReader.Cache = Cache;

            JSchema rootSchema;

            using (StreamReader streamReader = new StreamReader(schemaData))
                using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                {
                    rootSchema = schemaReader.ReadRoot(jsonReader, false);
                }

            Cache[schemaReference.BaseUri] = rootSchema;

            // resolve defered schemas after it has been cached to avoid
            // stackoverflow on circular references
            schemaReader.ResolveDeferedSchemas();

            return(_resolver.GetSubschema(schemaReference, rootSchema));
        }
Ejemplo n.º 7
0
        public override DiscoveryReference GetDiscoveryReference(string filename)
        {
            SchemaReference refe = new SchemaReference();

            refe.Url = filename;
            refe.Ref = filename;
            return(refe);
        }
            public override Stream GetSchemaResource(ResolveSchemaContext context, SchemaReference reference)
            {
                if (reference.BaseUri.Host == "localhost")
                {
                    return(File.OpenRead(Path.Combine(baseDir, @"JSON-Schema-Test-Suite", "remotes", reference.BaseUri.LocalPath.Trim('/').Replace('/', '\\'))));
                }

                return(nested.GetSchemaResource(context, reference));
            }
Ejemplo n.º 9
0
        XmlSchemas GetXmlSchemas(DiscoveryClientProtocol protocol)
        {
            XmlSchemas schemas = new XmlSchemas();

            foreach (DictionaryEntry entry in protocol.References)
            {
                SchemaReference schemaRef = entry.Value as SchemaReference;
                if (schemaRef != null)
                {
                    schemas.Add(schemaRef.Schema);
                }
            }
            return(schemas);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Generates the content of the custom XML properties part.
        /// </summary>
        /// <param name="customXmlPropertiesPart">The custom XML properties part1.</param>
        private void GenerateCustomXmlPropertiesPartContent(CustomXmlPropertiesPart customXmlPropertiesPart)
        {
            DataStoreItem dataStoreItem = new DataStoreItem()
            {
                ItemId = "{" + Guid.NewGuid().ToString().ToUpper() + "}"
            };

            dataStoreItem.AddNamespaceDeclaration("ds", "http://schemas.openxmlformats.org/officeDocument/2006/customXml");
            SchemaReferences schemaReferences = new SchemaReferences();
            SchemaReference  schemaReference  = new SchemaReference()
            {
                Uri = namespaceUri
            };

            schemaReferences.Append(schemaReference);
            dataStoreItem.Append(schemaReferences);
            customXmlPropertiesPart.DataStoreItem = dataStoreItem;
        }
Ejemplo n.º 11
0
        private SchemaObjectOrReference BuildSchemaForType(Type type)
        {
            if (type == typeof(void))
            {
                return(null);
            }
            if (type == typeof(IActionResult))
            {
                var s1 = new SchemaObject();
                s1.Type        = "object";
                s1.Description = "No enought information provided to constrain the type.";
                return(s1);
            }

            var primitiveType = GetPrimitiveType(type);

            if (IsEnumerable(type))
            {
                var itemType = GetItemTypeInArray(type);
                var sArray   = new SchemaObject();
                sArray.Type  = "array";
                sArray.Items = BuildSchemaForType(itemType);
                return(sArray);
            }
            else if (primitiveType != null)
            {
                return(primitiveType);
            }
            else
            {
                var sr = new SchemaReference();
                sr.Ref = BuildOpenApiReference(type.Name);

                // enroll for later type-description
                DefineSchemasFor[type.Name] = type;
                return(sr);
            }
        }
Ejemplo n.º 12
0
        public override WebServiceDiscoveryResult Load(WebReferenceItem item)
        {
            FilePath       basePath = item.MapFile.FilePath.ParentDirectory;
            ReferenceGroup resfile  = ReferenceGroup.Read(item.MapFile.FilePath);

            // TODO: Read as MetadataSet

            var protocol = new DiscoveryClientProtocol();

            foreach (MetadataFile dcr in resfile.Metadata)
            {
                DiscoveryReference dr;
                switch (dcr.MetadataType)
                {
                case "Wsdl":
                    dr = new ContractReference();
                    break;

                case "Disco":
                    dr = new DiscoveryDocumentReference();
                    break;

                case "Schema":
                    dr = new SchemaReference();
                    break;

                default:
                    continue;
                }

                dr.Url = dcr.SourceUrl;
                var fs = new FileStream(basePath.Combine(dcr.FileName), FileMode.Open, FileAccess.Read);
                protocol.Documents.Add(dr.Url, dr.ReadDocument(fs));
                fs.Close();
                protocol.References.Add(dr.Url, dr);
            }
            return(new WebServiceDiscoveryResultWCF(protocol, null, item, resfile, DefaultClientOptions));
        }
Ejemplo n.º 13
0
 public override Stream GetSchemaResource(ResolveSchemaContext context, SchemaReference reference)
 {
     return(GetStreamFromUrl(reference.BaseUri));
 }
 public override Stream GetSchemaResource(ResolveSchemaContext context, SchemaReference reference)
 {
     return null;
 }
Ejemplo n.º 15
0
        public static void InsertCustomXmlForPpt(string pptFileName, string customXML)
        {
            using (PresentationDocument openXmlDoc = PresentationDocument.Open(pptFileName, true))
            {
                // Create a new custom XML part
                int           count         = openXmlDoc.PresentationPart.Parts.Count();
                string        customXmlId   = string.Format("rId{0}", count + 1);
                CustomXmlPart customXmlPart = openXmlDoc.PresentationPart.AddCustomXmlPart(CustomXmlPartType.CustomXml, customXmlId);
                using (Stream outputStream = customXmlPart.GetStream())
                {
                    using (StreamWriter ts = new StreamWriter(outputStream))
                    {
                        ts.Write(customXML);
                    }
                }

                // Add datastore so that the xml will persist after the document is modified
                count = customXmlPart.Parts.Count();
                CustomXmlPropertiesPart customXmlPropertiesPart = customXmlPart.AddNewPart <CustomXmlPropertiesPart>(string.Format("rId{0}", count + 1));
                DataStoreItem           dataStoreItem           = new DataStoreItem()
                {
                    ItemId = string.Format("{0}", Guid.NewGuid())
                };
                dataStoreItem.AddNamespaceDeclaration("ds", "http://schemas.openxmlformats.org/officeDocument/2006/customXml");

                SchemaReferences schemaReferences = new SchemaReferences();
                SchemaReference  schemaReference  = new SchemaReference()
                {
                    Uri = "http://www.w3.org/2001/XMLSchema"
                };

                schemaReferences.Append(schemaReference);
                dataStoreItem.Append(schemaReferences);
                customXmlPropertiesPart.DataStoreItem = dataStoreItem;

                // Add the xml to the customer data section of the document
                CustomerData customerData = new CustomerData()
                {
                    Id = customXmlId
                };
                if (openXmlDoc.PresentationPart.Presentation.CustomerDataList != null)
                {
                    // Sequence matters: http://www.schemacentral.com/sc/ooxml/e-p_custDataLst-1.html
                    if (openXmlDoc.PresentationPart.Presentation.CustomerDataList.Count() > 0)
                    {
                        openXmlDoc.PresentationPart.Presentation.CustomerDataList.InsertBefore(customerData, openXmlDoc.PresentationPart.Presentation.CustomerDataList.FirstChild);
                    }
                    else
                    {
                        openXmlDoc.PresentationPart.Presentation.CustomerDataList.Append(customerData);
                    }
                }
                else
                {
                    CustomerDataList customerDataList = new CustomerDataList();
                    customerDataList.Append(customerData);

                    // Sequence matters: http://www.schemacentral.com/sc/ooxml/e-p_presentation.html
                    if (openXmlDoc.PresentationPart.Presentation.Kinsoku != null)
                    {
                        openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.Kinsoku);
                    }
                    else if (openXmlDoc.PresentationPart.Presentation.DefaultTextStyle != null)
                    {
                        openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.DefaultTextStyle);
                    }
                    else if (openXmlDoc.PresentationPart.Presentation.ModificationVerifier != null)
                    {
                        openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.ModificationVerifier);
                    }
                    else if (openXmlDoc.PresentationPart.Presentation.PresentationExtensionList != null)
                    {
                        openXmlDoc.PresentationPart.Presentation.InsertBefore(customerDataList, openXmlDoc.PresentationPart.Presentation.PresentationExtensionList);
                    }
                    else
                    {
                        openXmlDoc.PresentationPart.Presentation.Append(customerDataList);
                    }
                }
            }
        }
Ejemplo n.º 16
0
 public override void VisitSchemaReference(SchemaReference schemaReference)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 17
0
 public override Stream GetSchemaResource(ResolveSchemaContext context, SchemaReference reference)
 {
     return(new FileStream(Path.Combine(_baseDir, Path.GetFileName(reference.BaseUri.ToString())), FileMode.Open));
 }
 public override Stream GetSchemaResource(ResolveSchemaContext context, SchemaReference reference)
 {
     return(null);
 }
Ejemplo n.º 19
0
        private static SchemaReference GetSchemaReference(this string schema)
        {
            var extension = ".schema.json";

            if (schema.ToLower() == @"http://json-schema.org/draft-04/schema#")
            {
                return new SchemaReference()
                       {
                           Kind = KindSchemaEnum.Schema, Schema = schema
                       }
            }
            ;

            if (schema.ToLower().EndsWith((nameof(MetaDefinitions) + extension).ToLower()))
            {
                return new SchemaReference()
                       {
                           Kind = KindSchemaEnum.SchemaDefinitions, Schema = schema
                       }
            }
            ;

            if (schema.ToLower().EndsWith((nameof(LayersDefinition) + extension).ToLower()))
            {
                return new SchemaReference()
                       {
                           Kind = KindSchemaEnum.SchemaLayerDefinitions, Schema = schema
                       }
            }
            ;

            if (schema.ToLower().EndsWith((nameof(CooperationViewpoint) + extension).ToLower()))
            {
                return new SchemaReference()
                       {
                           Kind = KindSchemaEnum.CooperationViewpoint, Schema = schema
                       }
            }
            ;

            foreach (Match match in _regex.Matches(schema))
            {
                if (match.Success)
                {
                    var p = match.Value;
                    if (p.ToLower().EndsWith(extension))
                    {
                        p = p.Substring(0, p.Length - extension.Length).Trim('.');
                    }

                    var i = p.Split('.');


                    var r = new SchemaReference()
                    {
                        Type   = i[0],
                        Kind   = ResolveKind(i[1]),
                        Schema = schema,
                    };

                    return(r);
                }
            }

            return(new SchemaReference()
            {
                Kind = KindSchemaEnum.Undefined,
                Schema = schema,
            });
        }
Ejemplo n.º 20
0
        private static void BMSpecificProduceConsume(string bootstrapServers, string schemaRegistryServers)
        {
            var producerConfig = new ProducerConfig {
                BootstrapServers = bootstrapServers
            };

            var schemaRegistryConfig = new SchemaRegistryConfig
            {
                Url = schemaRegistryServers
            };

            var adminClientConfig = new AdminClientConfig
            {
                BootstrapServers = bootstrapServers
            };

            var topic = Guid.NewGuid().ToString();

            Console.Error.WriteLine($"topic: {topic}");

            using (var adminClient = new AdminClientBuilder(adminClientConfig).Build())
            {
                adminClient.CreateTopicsAsync(
                    new List <TopicSpecification> {
                    new TopicSpecification {
                        Name = topic, NumPartitions = 1, ReplicationFactor = 1
                    }
                }).Wait();
            }

            var    srClient = new CachedSchemaRegistryClient(schemaRegistryConfig);
            Schema schema1  = new Schema(EventA._SCHEMA.ToString(), SchemaType.Avro);
            Schema schema2  = new Schema(EventB._SCHEMA.ToString(), SchemaType.Avro);
            var    id1      = srClient.RegisterSchemaAsync("events-a", schema1).Result;
            var    id2      = srClient.RegisterSchemaAsync("events-b", schema2).Result;

            var             avroUnion   = @"[""Confluent.Kafka.Examples.AvroSpecific.EventA"",""Confluent.Kafka.Examples.AvroSpecific.EventB""]";
            Schema          unionSchema = new Schema(avroUnion, SchemaType.Avro);
            SchemaReference reference   = new SchemaReference(
                "Confluent.Kafka.Examples.AvroSpecific.EventA",
                "events-a",
                srClient.GetLatestSchemaAsync("events-a").Result.Version);

            unionSchema.References.Add(reference);
            reference = new SchemaReference(
                "Confluent.Kafka.Examples.AvroSpecific.EventB",
                "events-b",
                srClient.GetLatestSchemaAsync("events-b").Result.Version);
            unionSchema.References.Add(reference);

            var id3 = srClient.RegisterSchemaAsync($"{topic}-value", unionSchema).Result;

            AvroSerializerConfig avroSerializerConfig = new AvroSerializerConfig {
                AutoRegisterSchemas = false, UseLatestSchemaVersion = true
            };

            using (var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig))
                using (var producer =
                           new ProducerBuilder <string, ISpecificRecord>(producerConfig)
                           .SetKeySerializer(new AvroSerializer <string>(schemaRegistry))
                           .SetValueSerializer(new BmSpecificSerializerImpl <ISpecificRecord>(
                                                   schemaRegistry,
                                                   false,
                                                   1024,
                                                   SubjectNameStrategy.Topic.ToDelegate(),
                                                   true))
                           .Build())
                {
                    for (int i = 0; i < 3; ++i)
                    {
                        var eventA = new EventA
                        {
                            A         = "I'm event A",
                            EventId   = Guid.NewGuid().ToString(),
                            EventType = "EventType-A",
                            OccuredOn = DateTime.UtcNow.Ticks,
                        };

                        producer.ProduceAsync(topic, new Message <string, ISpecificRecord> {
                            Key = "DomainEvent", Value = eventA
                        }).Wait();
                    }

                    for (int i = 0; i < 3; ++i)
                    {
                        var eventB = new EventB
                        {
                            B         = 123456987,
                            EventId   = Guid.NewGuid().ToString(),
                            EventType = "EventType-B",
                            OccuredOn = DateTime.UtcNow.Ticks,
                        };

                        producer.ProduceAsync(topic, new Message <string, ISpecificRecord> {
                            Key = "DomainEvent", Value = eventB
                        }).Wait();
                    }

                    Assert.Equal(0, producer.Flush(TimeSpan.FromSeconds(10)));
                }

            var consumerConfig = new ConsumerConfig
            {
                BootstrapServers   = bootstrapServers,
                GroupId            = Guid.NewGuid().ToString(),
                SessionTimeoutMs   = 6000,
                AutoOffsetReset    = AutoOffsetReset.Earliest,
                EnablePartitionEof = true
            };

            using (var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig))
                using (var consumer =
                           new ConsumerBuilder <string, GenericRecord>(consumerConfig)
                           .SetKeyDeserializer(new AvroDeserializer <string>(schemaRegistry).AsSyncOverAsync())
                           .SetValueDeserializer(new BmGenericDeserializerImpl(schemaRegistry).AsSyncOverAsync())
                           .SetErrorHandler((_, e) => Assert.True(false, e.Reason))
                           .Build())
                {
                    consumer.Subscribe(topic);

                    int i = 0;
                    while (true)
                    {
                        var record = consumer.Consume(TimeSpan.FromMilliseconds(100));
                        if (record == null)
                        {
                            continue;
                        }
                        if (record.IsPartitionEOF)
                        {
                            break;
                        }

                        Console.WriteLine(record.Message.Value["EventType"]);
                        i += 1;
                    }

                    Assert.Equal(6, i);

                    consumer.Close();
                }
        }
Ejemplo n.º 21
0
 public virtual void VisitSchemaReference(SchemaReference schemaReference)
 {
     DefaultVisit(schemaReference);
 }