Пример #1
0
        private Identifier getSenderID(OiosiMessage message, DocumentTypeConfig docTypeConfig)
        {
            string senderID = DocumentXPathResolver.GetElementValueByXPathNavigator(
                message.MessageXml,
                docTypeConfig.EndpointType.SenderKey.XPath,
                docTypeConfig.Namespaces);
            var        senderKeyType = Utilities.GetSenderKeyTypeCode(message.MessageXml, docTypeConfig);
            Identifier id            = IdentifierUtility.GetIdentifierFromKeyType(senderID, senderKeyType);

            if (!id.IsAllowedInPublic)
            {
                string cvrNumberString;
                string endpointType;
                if (Credentials.ClientCertificate.TryGetCvrNumberString(out endpointType, out cvrNumberString))
                {
                    id = new Identifier(endpointType, cvrNumberString);
                }
                else
                {
                    throw new Exception("Sender ID is not allowed to be added to header. It was not possible to retrieve ID from certificate");
                }
            }

            return(id);
        }
Пример #2
0
        private static string GetProfileName(OiosiMessage message, DocumentTypeConfig docTypeConfig)
        {
            string profileName = string.Empty;

            // If doctype does't have a XPath expression to extract the document Profile
            // then we assume that the current document type does operate with OIOUBL profiles

            if (docTypeConfig.ProfileIdXPath == null)
            {
                // no profile
            }
            else if (docTypeConfig.ProfileIdXPath.XPath == null)
            {
                // no profile
            }
            else if (string.IsNullOrEmpty(docTypeConfig.ProfileIdXPath.XPath))
            {
                // no profile
            }
            else
            {
                // Fetch the OIOUBL profile name
                string xPath = docTypeConfig.ProfileIdXPath.XPath;
                PrefixedNamespace[] nameSpaces = docTypeConfig.Namespaces;

                profileName = DocumentXPathResolver.GetElementValueByXPathNavigator(message.MessageXml, xPath, nameSpaces);
            }

            return(profileName);
        }
Пример #3
0
        /// <summary>
        /// Discriminates the xml document from a single configuration
        /// element
        /// </summary>
        /// <param name="config"></param>
        /// <param name="xmlDocument"></param>
        /// <param name="namespaces"></param>
        /// <returns></returns>
        public static bool Discriminate(XPathDiscriminatorConfig config, XmlDocument xmlDocument, PrefixedNamespace[] namespaces)
        {
            string xpathExpression = config.XPathExpression;
            string xpathResult     = config.XPathExpectedResult;
            string result          = DocumentXPathResolver.GetElementValueByXPathNavigator(xmlDocument, xpathExpression, namespaces);
            Regex  regex           = new Regex(xpathResult);

            return(regex.IsMatch(result));
        }
Пример #4
0
        /// <summary>
        /// From an xpath expression, gets the endpoint key
        /// </summary>
        /// <param name="xmlDoc">The xml document to search</param>
        /// <param name="xpath">The xpath expression to apply</param>
        /// <param name="keyType">The type of key to return</param>
        /// <param name="prefixedNamespaces">The list of namespaces-prefix pairs used in the xpath expression</param>
        /// <returns>Returns the endpoint key</returns>
        public static Identifier GetEndpointKeyByXpath(
            XmlDocument xmlDoc,
            string xpath,
            PrefixedNamespace[] prefixedNamespaces,
            string keyType
            )
        {
            // 1. Get the endpoint as string:
            string endpointKeyString = DocumentXPathResolver.GetElementValueByXPathNavigator(xmlDoc, xpath, prefixedNamespaces);

            // 2. Convert to the correct IIdentifierType:
            Identifier id = IdentifierUtility.GetIdentifierFromKeyType(endpointKeyString, keyType);

            return(id);
        }
Пример #5
0
        /// <summary>
        /// Gets the endpoint key type code from a Message and a
        /// DocumentTypeConfig.
        /// </summary>
        public static string GetSenderKeyTypeCode(XmlDocument xmlDocument, DocumentTypeConfig documentType)
        {
            //Finds all mapping expressions with the name "EndpointKeyType"
            DocumentEndpointInformation endpointType      = documentType.EndpointType;
            KeyTypeMappingExpression    mappingExpression = endpointType.SenderKey.GetMappingExpression("EndpointKeyType");
            //Finds the endpoint key type value from the given xpath
            string xpathExpression      = mappingExpression.XPathExpression;
            string endPointKeyTypeValue =
                DocumentXPathResolver.GetElementValueByXPathNavigator(
                    xmlDocument,
                    xpathExpression,
                    documentType.Namespaces);
            KeyTypeMapping mapping             = mappingExpression.GetMapping(endPointKeyTypeValue);
            string         endpointKeyTypeCode = mapping.MapsTo;

            return(endpointKeyTypeCode);
        }
Пример #6
0
        protected UddiId GetProfileTModelId(OiosiMessage message, DocumentTypeConfig docTypeConfig)
        {
            UddiId uddiId;

            // If doctype does't have a XPath expression to extract the document Profile
            // then we assume that the current document type does operate with OIOUBL profiles
            if (docTypeConfig.ProfileIdXPath == null)
            {
                uddiId = null;
            }
            else if (docTypeConfig.ProfileIdXPath.XPath == null)
            {
                uddiId = null;
            }
            else if (docTypeConfig.ProfileIdXPath.XPath.Equals(""))
            {
                uddiId = null;
            }
            else
            {
                // Fetch the OIOUBL profile name
                string profileName = DocumentXPathResolver.GetElementValueByXPathNavigator(
                    message.MessageXml,
                    docTypeConfig.ProfileIdXPath.XPath,
                    docTypeConfig.Namespaces);

                ProfileMappingCollectionConfig config = ConfigurationHandler.GetConfigurationSection <ProfileMappingCollectionConfig>();
                if (config.ContainsProfileMappingByName(profileName))
                {
                    ProfileMapping profileMapping    = config.GetMapping(profileName);
                    string         profileTModelGuid = profileMapping.TModelGuid;
                    uddiId = IdentifierUtility.GetUddiIDFromString(profileTModelGuid);
                }
                else
                {
                    throw new Exception("GetProfileTModelId failed for : " + profileName);
                }
            }

            return(uddiId);
        }
Пример #7
0
        /// <summary>
        /// Adds custom RASP headers to a given OiosiMessage
        /// </summary>
        /// <param name="message">The message on which the headers should be added</param>
        /// <param name="documentId">The document Id used in the MessageIdentifier header</param>
        private void AddCustomHeaders(OiosiMessage message, string documentId)
        {
            var typeSearcher = new DocumentTypeConfigSearcher();
            DocumentTypeConfig docTypeConfig = typeSearcher.FindUniqueDocumentType(message.MessageXml);

            // Add custom headers to the message
            Identifier senderIdentifier   = getSenderID(message, docTypeConfig);
            Identifier receiverIdentifier = GetReceiverID(message, docTypeConfig);

            string key = PartyIdentifierHeaderSettings.MessagePropertyKey;
            var    partyIdentifierSetting = new PartyIdentifierHeaderSettings(senderIdentifier, receiverIdentifier);

            message.UbiquitousProperties[key] = partyIdentifierSetting;

            // Adds custom headers by xpaths
            foreach (CustomHeaderXPathConfiguration xpath in docTypeConfig.CustomHeaderConfiguration.XPaths)
            {
                var value         = DocumentXPathResolver.GetElementValueByXPathNavigator(message.MessageXml, xpath.XPath, docTypeConfig.Namespaces);
                var qualifiedName = new XmlQualifiedName(xpath.Name, Definitions.DefaultOiosiNamespace2007);
                message.MessageHeaders[qualifiedName] = MessageHeader.CreateHeader(qualifiedName.Name, qualifiedName.Namespace, value);
            }

            // Add the MessageIdentifier header
            var messageIdentifierHeaderName = new XmlQualifiedName("MessageIdentifier", Definitions.DefaultOiosiNamespace2007);

            message.MessageHeaders[messageIdentifierHeaderName] = MessageHeader.CreateHeader(messageIdentifierHeaderName.Name, messageIdentifierHeaderName.Namespace, documentId);

            // Change the SOAP actions
            if (!string.IsNullOrEmpty(docTypeConfig.EndpointType.RequestAction))
            {
                message.RequestAction = docTypeConfig.EndpointType.RequestAction;
            }
            if (!string.IsNullOrEmpty(docTypeConfig.EndpointType.ReplyAction))
            {
                message.ReplyAction = docTypeConfig.EndpointType.ReplyAction;
            }
        }
Пример #8
0
        /////// <summary>
        /////// Schematron validates a document.
        ///////
        /////// If the validation process fails it throws a SchematronValidationFailedException If the
        /////// document has any schematron errors it throws a SchematronErrorException
        /////// </summary>
        /////// <param name="document">The document to be validated</param>
        /////// <param name="schematronStylesheet"></param>
        ////public void SchematronValidateXmlDocument(XmlDocument document, CompiledXslt compiledXsltEntry)
        ////{
        ////    if (this.errorXPath == null)
        ////    {
        ////        throw new Exception("No error XPath is set");
        ////    }
        ////    if (this.errorMessageXPath == null)
        ////    {
        ////        throw new Exception("No error message XPath is set");
        ////    }

        ////    XmlDocument result = null;
        ////    PrefixedNamespace[] prefixedNamespaces;
        ////    bool hasAnyErrors;
        ////    try
        ////    {
        ////        // this is fast and ugly...
        ////        result = xlstUtil.TransformXml(document, schematronStylesheet);
        ////        byte[] schematronResultBytes = Encoding.Default.GetBytes(result.OuterXml);
        ////        using (MemoryStream schematronResultMemoryStream = new MemoryStream(schematronResultBytes))
        ////        {
        ////            XmlTextReader schematronResultXmlTextReader = new XmlTextReader(schematronResultMemoryStream);
        ////            prefixedNamespaces = this.CreateDefaultNamespaceManager(schematronResultXmlTextReader);
        ////            hasAnyErrors = DocumentXPathResolver.HasAnyElementsByXpath(result, this.errorXPath, prefixedNamespaces);
        ////        }
        ////    }
        ////    catch (Exception ex)
        ////    {
        ////        throw new SchematronValidationFailedException(document, ex);
        ////    }

        ////    if (hasAnyErrors)
        ////    {
        ////        string firstErrorMessage = DocumentXPathResolver.GetFirstElementValueByXPath(result, this.errorMessageXPath, prefixedNamespaces);
        ////        throw new SchematronErrorException(result, firstErrorMessage);
        ////    }
        ////    else
        ////    {
        ////        // no schematron error
        ////    }
        ////}

        /// <summary>
        /// Schematron validates a document.
        ///
        /// If the validation process fails it throws a SchematronValidationFailedException If the
        /// document has any schematron errors it throws a SchematronErrorException
        /// </summary>
        /// <param name="documentAsString">The document to be validated</param>
        /// <param name="compiledXslt"></param>
        public void SchematronValidateXmlDocument(string documentAsString, CompiledXslt compiledXslt)
        {
            if (this.errorXPath == null)
            {
                throw new Exception("No error XPath is set");
            }
            if (this.errorMessageXPath == null)
            {
                throw new Exception("No error message XPath is set");
            }

            XmlDocument schematronResultXmlDocument = null;

            PrefixedNamespace[] prefixedNamespaces = null;
            bool hasAnyErrors      = false;
            bool documentValidated = false;

            //// try
            ////{
            ////    result = xlstUtil.TransformXml(documentAsString, schematronStylesheet);
            ////    hasAnyErrors = DocumentXPathResolver.HasAnyElementsByXpath(result, this.errorXPath, prefixedNamespaces);
            ////}
            ////catch (Exception ex)
            ////{
            ////    XmlDocument xmlDoc = new XmlDocument();
            ////    xmlDoc.LoadXml(documentAsString);
            ////    throw new SchematronValidationFailedException(xmlDoc, ex);
            ////}

            ////if (hasAnyErrors)
            ////{
            ////    string firstErrorMessage = DocumentXPathResolver.GetFirstElementValueByXPath(result, this.errorMessageXPath, prefixedNamespaces);
            ////    throw new SchematronErrorException(result, firstErrorMessage);
            ////}
            ////else
            ////{
            ////    // no schematron error
            ////}
            // ---------
            try
            {
                // .Net build in xslt pserser - only xslt 1.0
                if (compiledXslt != null && compiledXslt.XslCompiledTransform != null)
                {
                    schematronResultXmlDocument = xlstUtil.TransformXml(documentAsString, compiledXslt.XslCompiledTransform);
                    byte[] schematronResultBytes = Encoding.Default.GetBytes(schematronResultXmlDocument.OuterXml);
                    using (MemoryStream schematronResultMemoryStream = new MemoryStream(schematronResultBytes))
                    {
                        XmlTextReader schematronResultXmlTextReader = new XmlTextReader(schematronResultMemoryStream);
                        prefixedNamespaces = this.CreateDefaultNamespaceManager(schematronResultXmlTextReader);

                        hasAnyErrors      = DocumentXPathResolver.HasAnyElementsByXpath(schematronResultXmlDocument, this.errorXPath, prefixedNamespaces);
                        documentValidated = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.Message);
            }

            if (documentValidated == false)
            {
                // Not xslt 1.0, or complex xslt 1.0  - so try Saxon xslt
                try
                {
                    using (MemoryStream schematronResultMemoryStream = new MemoryStream())
                    {
                        using (MemoryStream xmlSchematronStylesheetMemoryStream = compiledXslt.Stream)
                        {
                            xmlSchematronStylesheetMemoryStream.Flush();//Adjust this if you want read your data
                            xmlSchematronStylesheetMemoryStream.Position = 0;

                            Processor    processor = new Processor();
                            XsltCompiler compiler  = processor.NewXsltCompiler();
                            Uri          uri       = new Uri("file://" + compiledXslt.FileInfo.Directory.FullName);
                            compiler.ErrorList = new List <object>();
                            compiler.BaseUri   = uri;
                            Serializer serializer = new Serializer();

                            try
                            {
                                XsltTransformer saxonTransformer = compiler.Compile(xmlSchematronStylesheetMemoryStream).Load();

                                // Load the XML document. Input to the build method is the document.
                                DocumentBuilder docBuilder = processor.NewDocumentBuilder();
                                if (docBuilder.BaseUri == null)
                                {
                                    docBuilder.BaseUri = uri;
                                }

                                XdmNode docXdmNode = docBuilder.Build(documentAsString.ToStream());

                                // Set the root node of the source document to be the initial
                                // context node
                                saxonTransformer.InitialContextNode = docXdmNode;

                                // Init. the result object
                                serializer.SetOutputProperty(Serializer.INDENT, "yes");
                                serializer.SetOutputProperty(Serializer.ENCODING, Encoding.UTF8.BodyName);

                                serializer.SetOutputStream(schematronResultMemoryStream);

                                // Run the transformation with result object as input param.
                                saxonTransformer.Run(serializer);
                            }
                            catch (Exception)
                            {
                                // easy debugging
                                throw;
                            }
                            finally
                            {
                                // close/dispose
                                serializer.Close();
                            }
                        }

                        // convert the schematronResultMemoryStream, into a xmlDocument
                        schematronResultMemoryStream.Position = 0;
                        schematronResultXmlDocument           = new XmlDocument();
                        schematronResultXmlDocument.Load(schematronResultMemoryStream);

                        schematronResultMemoryStream.Position = 0;
                        XmlTextReader schematronResultXmlTextReader = new XmlTextReader(schematronResultMemoryStream);
                        prefixedNamespaces = this.CreateDefaultNamespaceManager(schematronResultXmlTextReader);

                        hasAnyErrors      = DocumentXPathResolver.HasAnyElementsByXpath(schematronResultXmlDocument, this.errorXPath, prefixedNamespaces);
                        documentValidated = true;
                    }
                }
                catch (Exception ex)
                {
                    Debug.Fail(ex.Message);
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.Load(documentAsString.ToStream());
                    throw new SchematronValidationFailedException(xmlDocument, ex);
                }
            }

            if (documentValidated == false)
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(documentAsString.ToStream());
                throw new SchematronValidationFailedException(xmlDocument, new Exception("Failed to validate the document."));
            }

            if (hasAnyErrors)
            {
                string firstErrorMessage = DocumentXPathResolver.GetFirstElementValueByXPath(schematronResultXmlDocument, this.errorMessageXPath, prefixedNamespaces);
                throw new SchematronErrorException(schematronResultXmlDocument, firstErrorMessage);
            }
            else
            {
                // no schematron error
            }
        }
Пример #9
0
        /// <summary>
        /// Validates a document
        /// </summary>
        /// <param name="xmlDocument">document to validate</param>
        /// <param name="compiledXslt">stylesheet to use</param>
        public void SchematronValidateXmlDocument(XmlDocument xmlDocument, CompiledXslt compiledXslt)
        {
            if (this.errorXPath == null)
            {
                throw new Exception("No error XPath is set");
            }
            if (this.errorMessageXPath == null)
            {
                throw new Exception("No error message XPath is set");
            }

            bool documentValidated = false;

            PrefixedNamespace[] prefixedNamespaces = null;// = this.CreateDefaultNamespaceManager(xmlTextReader);
            bool        hasAnyErrors = false;
            XmlDocument schematronResultXmlDocument = null;

            // First we try windows build in transformer. It handle xslt 1.0, but not xslt 2.0. Any
            // stylesheet in xslt 2.0 will fail, and some xslt 1.0, as they is too complex for the parser.
            try
            {
                // .Net build in xslt parser - only xslt 1.0

                if (compiledXslt != null && compiledXslt.XslCompiledTransform != null)
                {
                    schematronResultXmlDocument = xlstUtil.TransformXml(xmlDocument, compiledXslt.XslCompiledTransform);
                    byte[] schematronResultBytes = Encoding.Default.GetBytes(schematronResultXmlDocument.OuterXml);
                    using (MemoryStream schematronResultMemoryStream = new MemoryStream(schematronResultBytes))
                    {
                        XmlTextReader schematronResultXmlTextReader = new XmlTextReader(schematronResultMemoryStream);
                        prefixedNamespaces = this.CreateDefaultNamespaceManager(schematronResultXmlTextReader);

                        hasAnyErrors      = DocumentXPathResolver.HasAnyElementsByXpath(schematronResultXmlDocument, this.errorXPath, prefixedNamespaces);
                        documentValidated = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.Message);
            }

            if (documentValidated == false)
            {
                // Not xslt 1.0, or complex xslt 1.0  - so try Saxon xslt
                try
                {
                    using (MemoryStream schematronResultMemoryStream = new MemoryStream())
                    {
                        using (MemoryStream xmlSchematronStylesheetMemoryStream = compiledXslt.Stream)
                        {
                            xmlSchematronStylesheetMemoryStream.Flush();//Adjust this if you want read your data
                            xmlSchematronStylesheetMemoryStream.Position = 0;

                            Processor    processor  = new Processor();
                            XsltCompiler compiler   = processor.NewXsltCompiler();
                            Serializer   serializer = new Serializer();

                            try
                            {
                                XsltTransformer saxonTransformer = compiler.Compile(xmlSchematronStylesheetMemoryStream).Load();

                                // Load the XML document. Input to the build method is the document.
                                XdmNode docXdmNode = processor.NewDocumentBuilder().Build(xmlDocument);

                                // Set the root node of the source document to be the initial
                                // context node
                                saxonTransformer.InitialContextNode = docXdmNode;

                                // Init. the result object
                                serializer.SetOutputProperty(Serializer.INDENT, "yes");
                                serializer.SetOutputProperty(Serializer.ENCODING, Encoding.UTF8.BodyName);

                                serializer.SetOutputStream(schematronResultMemoryStream);

                                // Run the transformation with result object as input param.
                                saxonTransformer.Run(serializer);
                            }
                            catch (Exception)
                            {
                                // easy debugging
                                throw;
                            }
                            finally
                            {
                                // close/dispose
                                serializer.Close();
                            }
                        }

                        // convert the schematronResultMemoryStream, into a xmlDocument
                        schematronResultMemoryStream.Position = 0;
                        schematronResultXmlDocument           = new XmlDocument();
                        schematronResultXmlDocument.Load(schematronResultMemoryStream);

                        schematronResultMemoryStream.Position = 0;
                        XmlTextReader schematronResultXmlTextReader = new XmlTextReader(schematronResultMemoryStream);
                        prefixedNamespaces = this.CreateDefaultNamespaceManager(schematronResultXmlTextReader);

                        hasAnyErrors      = DocumentXPathResolver.HasAnyElementsByXpath(schematronResultXmlDocument, this.errorXPath, prefixedNamespaces);
                        documentValidated = true;
                    }
                }
                catch (Exception ex)
                {
                    Debug.Fail(ex.Message);
                    throw new SchematronValidationFailedException(xmlDocument, ex);
                }
            }

            if (documentValidated == false)
            {
                throw new SchematronValidationFailedException(xmlDocument, new Exception("Failed to validate the document."));
            }

            if (hasAnyErrors)
            {
                string firstErrorMessage = DocumentXPathResolver.GetFirstElementValueByXPath(schematronResultXmlDocument, this.errorMessageXPath, prefixedNamespaces);
                throw new SchematronErrorException(schematronResultXmlDocument, firstErrorMessage);
            }
            else
            {
                // no schematron error
            }
        }