Пример #1
0
        /// <summary>
        /// Invoked during execution of the query, with the
        /// pre populated expression tree.
        /// </summary>
        /// <param name="expression">Target expression block</param>
        /// <returns>Expected result</returns>
        public IEnumerable <T> Execute(Ast.Expression expression)
        {
            Visit(expression);

            var optionsIn     = string.Join(";", Options.Select(x => $"{x.Key}={x.Value}"));
            var objectType    = ObjectTypes.GetObjectType <T>();
            var xmlIn         = WitsmlParser.ToXml(Query, true);
            var originalXmlIn = xmlIn;

            if (Context.Connection.CompressRequests)
            {
                ClientCompression.Compress(ref xmlIn, ref optionsIn);
            }

            Context.LogQuery(Functions.GetFromStore, objectType, originalXmlIn, optionsIn);

            using (var client = Context.Connection.CreateClientProxy().WithUserAgent())
            {
                var    wmls = (IWitsmlClient)client;
                string suppMsgOut, xmlOut = string.Empty;
                var    result = Enumerable.Empty <T>();
                short  returnCode;

                try
                {
                    returnCode = wmls.WMLS_GetFromStore(objectType, xmlIn, optionsIn, null, out xmlOut, out suppMsgOut);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("Error querying store: {0}", ex);
                    returnCode = -1;
                    suppMsgOut = "Error querying store:" + ex.GetBaseException().Message;
                }

                try
                {
                    // Handle servers that compress the response to a compressed request.
                    if (Context.Connection.CompressRequests)
                    {
                        xmlOut = ClientCompression.SafeDecompress(xmlOut);
                    }

                    if (returnCode > 0)
                    {
                        var document = WitsmlParser.Parse(xmlOut);
                        var response = WitsmlParser.Parse <TList>(document.Root);
                        result = (IEnumerable <T>)response.Items;
                    }
                }
                catch (WitsmlException ex)
                {
                    Logger.ErrorFormat("Error parsing query response: {0}{2}{2}{1}", xmlOut, ex, Environment.NewLine);
                    returnCode = (short)ex.ErrorCode;
                    suppMsgOut = ex.Message + " " + ex.GetBaseException().Message;
                }

                Context.LogResponse(Functions.GetFromStore, objectType, originalXmlIn, optionsIn, xmlOut, returnCode, suppMsgOut);
                return(result);
            }
        }
Пример #2
0
        private IEnergisticsCollection ExecuteGetFromStoreQuery(string objectType, string xmlIn, string optionsIn, bool logQuery = true, bool logResponse = true)
        {
            IEnergisticsCollection result = null;
            string suppMsgOut, xmlOut;
            var    originalXmlIn = xmlIn;

            if (Connection.CompressRequests)
            {
                ClientCompression.Compress(ref xmlIn, ref optionsIn);
            }

            if (logQuery)
            {
                LogQuery(Functions.GetFromStore, objectType, originalXmlIn, optionsIn);
            }

            var returnCode = ExecuteQuery(Functions.GetFromStore, objectType, xmlIn, optionsIn, out xmlOut, out suppMsgOut);

            if (returnCode < 1)
            {
                if (logResponse)
                {
                    LogResponse(Functions.GetFromStore, objectType, xmlIn, optionsIn, null, returnCode, suppMsgOut);
                }

                return(null);
            }

            try
            {
                // Handle servers that compress the response to a compressed request.
                if (Connection.CompressRequests)
                {
                    xmlOut = ClientCompression.SafeDecompress(xmlOut);
                }

                if (returnCode > 0)
                {
                    var document = WitsmlParser.Parse(xmlOut);
                    var family   = ObjectTypes.GetFamily(document.Root);
                    var listType = ObjectTypes.GetObjectGroupType(objectType, family, DataSchemaVersion);

                    result = WitsmlParser.Parse(listType, document.Root) as IEnergisticsCollection;
                }
            }
            catch (WitsmlException ex)
            {
                _log.ErrorFormat("Error parsing query response: {0}{2}{2}{1}", xmlOut, ex, Environment.NewLine);
                returnCode = (short)ex.ErrorCode;
                suppMsgOut = ex.Message + " " + ex.GetBaseException().Message;
            }

            if (logResponse)
            {
                LogResponse(Functions.GetFromStore, objectType, originalXmlIn, optionsIn, xmlOut, returnCode, suppMsgOut);
            }

            return(result);
        }
Пример #3
0
 /// <summary>
 /// Decompresses the input request
 /// </summary>
 /// <param name="request">The request to decompress.</param>
 /// <returns>The decompressed request.</returns>
 /// <exception cref="WitsmlException">If the request cannot be decompressed.</exception>
 public static string DecompressRequest(string request)
 {
     try
     {
         return(ClientCompression.Base64DecodeAndGZipDecompress(request));
     }
     catch { throw new WitsmlException(ErrorCodes.CannotDecompressQuery); }
 }
Пример #4
0
        public void Target141Validator_AddToStore_Error_479_Target_Cannot_Decompress_XmlIn()
        {
            AddParents();

            var uncompressedXml = "abcd1234";
            var compressedXml = uncompressedXml;

            var optionsIn = string.Empty;
            ClientCompression.Compress(ref compressedXml, ref optionsIn);

            var response = DevKit.AddToStore(ObjectTypes.Target, uncompressedXml, null, optionsIn);
            Assert.AreEqual((short)ErrorCodes.CannotDecompressQuery, response.Result);
        }
Пример #5
0
        public void Target141Validator_AddToStore_Error_426_Target_Compressed_XmlIn_Must_Conform_To_Schema()
        {
            AddParents();

            var nonConformingXml = string.Format(BasicXMLTemplate, Target.UidWell, Target.UidWellbore, Target.Uid,
                $"<name>{Target.Name}</name><name>{Target.Name}</name>");

            var optionsIn = string.Empty;
            ClientCompression.Compress(ref nonConformingXml, ref optionsIn);

            var response = DevKit.AddToStore(ObjectTypes.Target, nonConformingXml, null, optionsIn);
            Assert.AreEqual((short)ErrorCodes.CompressedInputNonConforming, response.Result);
        }
        public void SurveyProgram141Validator_UpdateInStore_Error_479_SurveyProgram_Cannot_Decompress_XmlIn()
        {
            AddParents();
            DevKit.AddAndAssert <SurveyProgramList, SurveyProgram>(SurveyProgram);

            var uncompressedXml = "abcd1234";
            var compressedXml   = uncompressedXml;

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref compressedXml, ref optionsIn);

            var response = DevKit.UpdateInStore(ObjectTypes.SurveyProgram, uncompressedXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CannotDecompressQuery, response.Result);
        }
Пример #7
0
        public void SurveyProgram131Validator_UpdateInStore_Error_426_SurveyProgram_Compressed_XmlIn_Must_Conform_To_Schema()
        {
            AddParents();
            DevKit.AddAndAssert <SurveyProgramList, SurveyProgram>(SurveyProgram);

            var nonConformingXml = string.Format(BasicXMLTemplate, SurveyProgram.UidWell, SurveyProgram.UidWellbore, SurveyProgram.Uid,
                                                 $"<name>{SurveyProgram.Name}</name><name>{SurveyProgram.Name}</name>");

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref nonConformingXml, ref optionsIn);

            var response = DevKit.UpdateInStore(ObjectTypes.SurveyProgram, nonConformingXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CompressedInputNonConforming, response.Result);
        }
        public void FormationMarker141Validator_GetFromStore_Error_479_FormationMarker_Cannot_Decompress_XmlIn()
        {
            AddParents();
            DevKit.AddAndAssert <FormationMarkerList, FormationMarker>(FormationMarker);

            var uncompressedXml = "abcd1234";
            var compressedXml   = uncompressedXml;

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref compressedXml, ref optionsIn);

            var response = DevKit.GetFromStore(ObjectTypes.FormationMarker, uncompressedXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CannotDecompressQuery, response.Result);
        }
        public void ToolErrorModel141Validator_GetFromStore_Error_426_ToolErrorModel_Compressed_QueryIn_Must_Conform_To_Schema()
        {
            AddParents();
            DevKit.AddAndAssert <ToolErrorModelList, ToolErrorModel>(ToolErrorModel);

            var nonConformingXml = string.Format(BasicXMLTemplate, ToolErrorModel.Uid,
                                                 $"<name>{ToolErrorModel.Name}</name><name>{ToolErrorModel.Name}</name>");

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref nonConformingXml, ref optionsIn);

            var response = DevKit.GetFromStore(ObjectTypes.ToolErrorModel, nonConformingXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CompressedInputNonConforming, response.Result);
        }
        public void FormationMarker141Validator_GetFromStore_Error_426_FormationMarker_Compressed_QueryIn_Must_Conform_To_Schema()
        {
            AddParents();
            DevKit.AddAndAssert <FormationMarkerList, FormationMarker>(FormationMarker);

            var nonConformingXml = string.Format(BasicXMLTemplate, FormationMarker.UidWell, FormationMarker.UidWellbore, FormationMarker.Uid,
                                                 $"<name>{FormationMarker.Name}</name><name>{FormationMarker.Name}</name>");

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref nonConformingXml, ref optionsIn);

            var response = DevKit.GetFromStore(ObjectTypes.FormationMarker, nonConformingXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CompressedInputNonConforming, response.Result);
        }
        public void DrillReport141Validator_UpdateInStore_Error_426_DrillReport_Compressed_XmlIn_Must_Conform_To_Schema()
        {
            AddParents();
            DevKit.AddAndAssert <DrillReportList, DrillReport>(DrillReport);

            var nonConformingXml = string.Format(BasicXMLTemplate, DrillReport.UidWell, DrillReport.UidWellbore, DrillReport.Uid,
                                                 $"<name>{DrillReport.Name}</name><name>{DrillReport.Name}</name>");

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref nonConformingXml, ref optionsIn);

            var response = DevKit.UpdateInStore(ObjectTypes.DrillReport, nonConformingXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CompressedInputNonConforming, response.Result);
        }
        public void DrillReport141Validator_GetFromStore_Error_479_DrillReport_Cannot_Decompress_XmlIn()
        {
            AddParents();
            DevKit.AddAndAssert <DrillReportList, DrillReport>(DrillReport);

            var uncompressedXml = "abcd1234";
            var compressedXml   = uncompressedXml;

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref compressedXml, ref optionsIn);

            var response = DevKit.GetFromStore(ObjectTypes.DrillReport, uncompressedXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CannotDecompressQuery, response.Result);
        }
Пример #13
0
        public void Rig131Validator_UpdateInStore_Error_479_Rig_Cannot_Decompress_XmlIn()
        {
            AddParents();
            DevKit.AddAndAssert <RigList, Rig>(Rig);

            var uncompressedXml = "abcd1234";
            var compressedXml   = uncompressedXml;

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref compressedXml, ref optionsIn);

            var response = DevKit.UpdateInStore(ObjectTypes.Rig, uncompressedXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CannotDecompressQuery, response.Result);
        }
        public void SidewallCore131Validator_GetFromStore_Error_479_SidewallCore_Cannot_Decompress_XmlIn()
        {
            AddParents();
            DevKit.AddAndAssert <SidewallCoreList, SidewallCore>(SidewallCore);

            var uncompressedXml = "abcd1234";
            var compressedXml   = uncompressedXml;

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref compressedXml, ref optionsIn);

            var response = DevKit.GetFromStore(ObjectTypes.SidewallCore, uncompressedXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CannotDecompressQuery, response.Result);
        }
        public void SidewallCore131Validator_GetFromStore_Error_426_SidewallCore_Compressed_QueryIn_Must_Conform_To_Schema()
        {
            AddParents();
            DevKit.AddAndAssert <SidewallCoreList, SidewallCore>(SidewallCore);

            var nonConformingXml = string.Format(BasicXMLTemplate, SidewallCore.UidWell, SidewallCore.UidWellbore, SidewallCore.Uid,
                                                 $"<name>{SidewallCore.Name}</name><name>{SidewallCore.Name}</name>");

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref nonConformingXml, ref optionsIn);

            var response = DevKit.GetFromStore(ObjectTypes.SidewallCore, nonConformingXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CompressedInputNonConforming, response.Result);
        }
Пример #16
0
        public void DownholeComponent141Validator_GetFromStore_Error_426_DownholeComponent_Compressed_QueryIn_Must_Conform_To_Schema()
        {
            AddParents();
            DevKit.AddAndAssert <DownholeComponentList, DownholeComponent>(DownholeComponent);

            var nonConformingXml = string.Format(BasicXMLTemplate, DownholeComponent.UidWell, DownholeComponent.Uid,
                                                 $"<name>{DownholeComponent.Name}</name><name>{DownholeComponent.Name}</name>");

            var optionsIn = string.Empty;

            ClientCompression.Compress(ref nonConformingXml, ref optionsIn);

            var response = DevKit.GetFromStore(ObjectTypes.DownholeComponent, nonConformingXml, null, optionsIn);

            Assert.AreEqual((short)ErrorCodes.CompressedInputNonConforming, response.Result);
        }
Пример #17
0
        /// <summary>
        /// Validates compression options and input.
        /// </summary>
        /// <param name="function">The WITSML API function being called.</param>
        /// <param name="xml">The XML input to validate.</param>
        /// <param name="options">The options in to validate.</param>
        /// <returns><c>true</c> if the input is compressed; <c>false</c> otherwise.</returns>
        public static bool ValidateCompressedInput(Functions function, string xml, Dictionary <string, string> options)
        {
            string optionValue;

            options.TryGetValue(OptionsIn.CompressionMethod.Keyword, out optionValue);

            if (optionValue != null && !function.SupportsRequestCompression())
            {
                // A compression method was provided for a function that does not support it.

                throw new WitsmlException(ErrorCodes.KeywordNotSupportedByFunction);
            }

            if (string.IsNullOrEmpty(optionValue) || OptionsIn.CompressionMethod.None.Equals(optionValue))
            {
                // No compression method is specified

                return(false);
            }
            else if (OptionsIn.CompressionMethod.Gzip.Equals(optionValue))
            {
                // GZip compression is specified

                if (!WitsmlSettings.IsRequestCompressionEnabled)
                {
                    throw new WitsmlException(ErrorCodes.KeywordNotSupportedByServer);
                }

                if (!ClientCompression.IsBase64EncodedAndGZipCompressed(xml))
                {
                    throw new WitsmlException(ErrorCodes.CannotDecompressQuery);
                }

                return(true);
            }
            else
            {
                // An invalid compression type is specified.

                throw new WitsmlException(ErrorCodes.InvalidKeywordValue);
            }
        }
Пример #18
0
        /// <summary>
        /// Submits the query to the WITSML server for the given function type and input XML.
        /// </summary>
        /// <param name="functionType">Type of the function to execute.</param>
        /// <param name="xmlIn">The XML in.</param>
        /// <param name="optionsIn">The options in.</param>
        /// <returns>
        /// A tuple of four result values in the following order: xmlOut, suppMsgOut, optionsIn and returnCode.
        /// </returns>
        internal async Task <WitsmlResult> SubmitQuery(Functions functionType, string xmlIn, string optionsIn)
        {
            string objectType = null;
            string xmlOut     = null;
            short  returnCode = 0;

            try
            {
                // Compute the object type of the incoming xml.
                if (functionType.RequiresObjectType() && !string.IsNullOrWhiteSpace(xmlIn))
                {
                    try
                    {
                        var document = WitsmlParser.Parse(xmlIn);
                        objectType = ObjectTypes.GetObjectTypeFromGroup(document.Root);
                    }
                    catch (WitsmlException ex)
                    {
                        returnCode = (short)ex.ErrorCode;
                        throw;
                    }
                }

                using (var client = Proxy.CreateClientProxy().WithUserAgent())
                {
                    var    wmls = (IWitsmlClient)client;
                    string suppMsgOut;

                    if (wmls.CompressRequests && functionType.SupportsRequestCompression())
                    {
                        xmlIn = ClientCompression.GZipCompressAndBase64Encode(xmlIn);
                    }

                    // Execute the WITSML server function for the given functionType
                    switch (functionType)
                    {
                    case Functions.GetCap:
                        returnCode = wmls.WMLS_GetCap(optionsIn, out xmlOut, out suppMsgOut);
                        ProcessCapServer(xmlOut);
                        break;

                    case Functions.GetBaseMsg:
                        returnCode = Model.ErrorCode.GetValueOrDefault();
                        suppMsgOut = wmls.WMLS_GetBaseMsg(returnCode);
                        break;

                    case Functions.AddToStore:
                        returnCode = wmls.WMLS_AddToStore(objectType, xmlIn, optionsIn, null, out suppMsgOut);
                        break;

                    case Functions.UpdateInStore:
                        returnCode = wmls.WMLS_UpdateInStore(objectType, xmlIn, optionsIn, null, out suppMsgOut);
                        break;

                    case Functions.DeleteFromStore:
                        returnCode = wmls.WMLS_DeleteFromStore(objectType, xmlIn, optionsIn, null, out suppMsgOut);
                        break;

                    default:
                        returnCode = wmls.WMLS_GetFromStore(objectType, xmlIn, optionsIn, null, out xmlOut, out suppMsgOut);
                        break;
                    }

                    if (returnCode > 0)
                    {
                        // Handle servers that compress the response to a compressed request.
                        if (wmls.CompressRequests)
                        {
                            xmlOut = ClientCompression.SafeDecompress(xmlOut);
                        }
                    }

                    return(await Task.FromResult(new WitsmlResult(objectType, xmlIn, optionsIn, null, xmlOut, suppMsgOut, returnCode)));
                }
            }
            catch (Exception ex)
            {
                var message = string.Format("Error calling WITSML Store API method '{0}'{3}{3}Error Message: {1}{3}{3}Stack Trace:{3}{2}{3}",
                                            functionType, ex.Message, ex.StackTrace, Environment.NewLine);

                // Log the error message
                _log.Error(message);

                // Return the error to the caller so message and call stack can be displayed to the user
                return(await Task.FromResult(new WitsmlResult(objectType, xmlIn, optionsIn, null, xmlOut, message, returnCode)));
            }
        }