本文整理汇总了C#中ODataValidator.RuleEngine.ExtensionRuleResultDetail类的典型用法代码示例。如果您正苦于以下问题:C# ExtensionRuleResultDetail类的具体用法?C# ExtensionRuleResultDetail怎么用?C# ExtensionRuleResultDetail使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ExtensionRuleResultDetail类属于ODataValidator.RuleEngine命名空间,在下文中一共展示了ExtensionRuleResultDetail类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
Uri metadataServiceUrl = new Uri(context.Destination.AbsoluteUri.TrimEnd('/') + @"/$metadata" + @"/");
var req = WebRequest.Create(metadataServiceUrl) as HttpWebRequest;
Response response = WebHelper.Get(req, RuleEngineSetting.Instance().DefaultMaximumPayloadSize);
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name, metadataServiceUrl.AbsoluteUri, "GET", string.Empty, response);
if (response != null && response.StatusCode == HttpStatusCode.OK && response.ResponsePayload.IsMetadata())
{
passed = true;
}
else
{
passed = false;
detail.ErrorMessage = "The response is not the metadata service document. Please refer to section 15 of [OData-CSDL].";
}
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
示例2: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
var response = WebHelper.Get(context.Destination, string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name, context.Destination.AbsoluteUri, "GET", StringHelper.MergeHeaders(string.Empty, context.RequestHeaders), response);
if (response != null && response.StatusCode != null)
{
if (context.PayloadFormat == RuleEngine.PayloadFormat.JsonLight)
{
passed = true;
}
else
{
passed = false;
detail.ErrorMessage = "Get above URI with accept header 'application/json', the response is not JSON format.";
}
}
else
{
passed = false;
detail.ErrorMessage = String.Format("No response returned from above URI with accept header 'application/json'.");
}
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
示例3: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name);
var restrictions = AnnotationsHelper.GetChangeTracking(context.MetadataDocument, context.VocCapabilities);
if (string.IsNullOrEmpty(restrictions.Item1) ||
null == restrictions.Item2 || !restrictions.Item2.Any() ||
null == restrictions.Item3 || !restrictions.Item3.Any())
{
detail.ErrorMessage = "Cannot find an entity-container or any entity-sets which support the change tracking function.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
string url = string.Format("{0}/{1}", context.ServiceBaseUri, restrictions.Item1);
List<KeyValuePair<string, string>> requestHeaders = new List<KeyValuePair<string,string>>();
foreach (KeyValuePair<string, string> kvp in context.RequestHeaders)
{
requestHeaders.Add(new KeyValuePair<string, string>(kvp.Key, kvp.Value));
}
// Add odata.track-changes preference in request header.
KeyValuePair<string, string> prefer = new KeyValuePair<string, string>("Prefer", "odata.track-changes");
requestHeaders.Add(prefer);
Response response = WebHelper.Get(new Uri(url), Constants.V4AcceptHeaderJsonFullMetadata, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, requestHeaders);
detail = new ExtensionRuleResultDetail(this.Name, url, "GET", StringHelper.MergeHeaders(Constants.V4AcceptHeaderJsonFullMetadata, requestHeaders), response);
if (response != null && !string.IsNullOrEmpty(response.ResponseHeaders))
{
string preferHeader = response.ResponseHeaders.GetHeaderValue("Preference-Applied");
if (string.IsNullOrEmpty(preferHeader))
{
passed = false;
detail.ErrorMessage = "The response header returned by the service does not contain 'odata.track-changes', when request with the header 'Prefer:odata.track-changes'.";
}
else if (preferHeader.Contains("odata.track-changes"))
{
passed = true;
}
}
else
{
passed = false;
detail.ErrorMessage = "The service does not support Delta change tracking.";
}
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
示例4: ExtensionRuleViolationInfo
public ExtensionRuleViolationInfo(Uri uri, string content, ExtensionRuleResultDetail detail)
{
this.Endpoint = uri;
this.Content = content;
this.Details = new List<ExtensionRuleResultDetail>();
if (detail != null)
{
this.Details.Add(detail.Clone());
}
}
示例5: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name);
detail.ErrorMessage = @"This negative rule is not verified.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
示例6: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
var response = WebHelper.Get(context.Destination, Constants.AcceptHeaderJson, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name, context.Destination.AbsoluteUri, "GET", StringHelper.MergeHeaders(Constants.AcceptHeaderJson, context.RequestHeaders), response);
if (response != null && response.StatusCode != null)
{
// Extracts OData-Version value from ResponseHttpHeaders.
string oDataVersion = context.ResponseHttpHeaders.GetHeaderValue(Constants.ODataVersion).TrimEnd(';');
if (!string.IsNullOrEmpty(oDataVersion))
{
float version = float.Parse(oDataVersion);
// The minimal OData version of conformance validation is 4.0.
if (version >= 4.0)
{
passed = true;
}
else
{
passed = false;
detail.ErrorMessage = string.Format(@"The OData version is {0} which should be not less than minimal version 4.0.", version);
}
}
else
{
passed = false;
detail.ErrorMessage = @"Can not get the OData-Version header from response headers.";
}
}
else
{
passed = null;
detail.ErrorMessage = "Cannot get response from above URI.";
}
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
示例7: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
string url = context.Destination.AbsoluteUri + @"/$metadata";
Response response = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name, url, "GET", StringHelper.MergeHeaders(string.Empty, context.RequestHeaders), response);
// Get EntityContainer from metadata response payload.
string xpath = @"//*[local-name()='EntityContainer']";
XElement metadata = XElement.Parse(response.ResponsePayload);
IEnumerable<XElement> entityContainer = metadata.XPathSelectElements(xpath, ODataNamespaceManager.Instance);
List<string> propNames = new List<string>();
foreach (var prop in entityContainer)
{
propNames.Add(prop.Attribute("Name").Value);
}
if (propNames.Count > 0)
{
passed = true;
}
else
{
passed = false;
detail.ErrorMessage = "There is no EntityContainer in metadat document.";
}
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
示例8: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
string url = string.Format("{0}/?$find=*", context.Destination);
var req = WebRequest.Create(url) as HttpWebRequest;
Response response = WebHelper.Get(req, RuleEngineSetting.Instance().DefaultMaximumPayloadSize);
ExtensionRuleResultDetail detail1 = new ExtensionRuleResultDetail(this.Name, url, "GET", string.Empty, response);
if (response != null && response.StatusCode != null)
{
if (response.StatusCode != HttpStatusCode.OK)
{
passed = true;
}
else
{
passed = false;
detail1.ErrorMessage = "Services SHOULD fail the above URI because it contains query options 'find' which services does not understand.";
}
}
else
{
passed = false;
detail1.ErrorMessage = "No response returned from above URI.";
}
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail1);
return passed;
}
示例9: Verify
/// <summary>
/// Verifies the service implementation feature.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if the service implementation feature passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
info = null;
var svcStatus = ServiceStatus.GetInstance();
string entityTypeShortName;
var props = MetadataHelper.GetProperties("Edm.GeographyPoint", 1, out entityTypeShortName);
if (null == props || !props.Any())
{
return passed;
}
string propName = props[0].PropertyName;
string srid = props[0].SRID;
var entitySetUrl = entityTypeShortName.GetAccessEntitySetURL();
if (string.IsNullOrEmpty(entitySetUrl))
{
return passed;
}
string url = svcStatus.RootURL.TrimEnd('/') + "/" + entitySetUrl;
var resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
if (null != resp && HttpStatusCode.OK == resp.StatusCode)
{
JObject jObj = JObject.Parse(resp.ResponsePayload);
JArray jArr = jObj.GetValue(Constants.Value) as JArray;
var entity = jArr.First as JObject;
var propVal = entity[propName]["coordinates"] as JArray;
var pt1 = new Point(Convert.ToDouble(propVal[0]), Convert.ToDouble(propVal[1]));
var pt2 = new Point(0.0, 0.0);
var distance = Point.GetDistance(pt1, pt2);
url = string.Format("{0}?$filter=geo.distance({1}, geography'POINT(0.0 0.0)') ge {2}", url, propName, distance);
resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
var detail = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Get, string.Empty);
info = new ExtensionRuleViolationInfo(new Uri(url), string.Empty, detail);
if (null != resp && HttpStatusCode.OK == resp.StatusCode)
{
jObj = JObject.Parse(resp.ResponsePayload);
jArr = jObj.GetValue(Constants.Value) as JArray;
foreach (JObject et in jArr)
{
propVal = et[propName]["coordinates"] as JArray;
pt1 = new Point(Convert.ToDouble(propVal[0]), Convert.ToDouble(propVal[1]));
pt2 = new Point(0.0, 0.0);
var dis = Point.GetDistance(pt1, pt2);
passed = dis >= distance;
}
}
else
{
passed = false;
}
}
return passed;
}
开发者ID:RongfangWang,项目名称:ValidationTool,代码行数:67,代码来源:ServiceImpl_SystemQueryOptionFilter_GeoDistance.cs
示例10: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
info = null;
ServiceStatus serviceStatus = ServiceStatus.GetInstance();
TermDocuments termDocs = TermDocuments.GetInstance();
DataFactory dFactory = DataFactory.Instance();
ExtensionRuleResultDetail detail1 = new ExtensionRuleResultDetail(this.Name);
ExtensionRuleResultDetail detail2 = new ExtensionRuleResultDetail(this.Name);
ExtensionRuleResultDetail detail3 = new ExtensionRuleResultDetail(this.Name);
List<string> keyPropertyTypes = new List<string>() { "Edm.Int32", "Edm.Int16", "Edm.Int64", "Edm.Guid", "Edm.String" };
List<EntityTypeElement> entityTypeElements = MetadataHelper.GetEntityTypes(serviceStatus.MetadataDocument, 1, keyPropertyTypes, null, NavigationRoughType.CollectionValued).ToList();
if (entityTypeElements == null || entityTypeElements.Count == 0)
{
detail1.ErrorMessage = "To verify this rule it expects an entity type with Int32/Int64/Int16/Guid/String key property, but there is no this entity type in metadata so can not verify this rule.";
info = new ExtensionRuleViolationInfo(new Uri(serviceStatus.RootURL), serviceStatus.ServiceDocument, detail1);
return passed;
}
foreach (var et in entityTypeElements)
{
string navigPropName = null;
string navigPropRelatedEntitySetUrl = null;
string navigPropRelatedEntityTypeKeyName = null;
var matchEntity = et.EntitySetName.GetRestrictions(serviceStatus.MetadataDocument, termDocs.VocCapabilitiesDoc,
new List<Func<string, string, string, List<NormalProperty>, List<NavigProperty>, bool>>()
{
AnnotationsHelper.GetDeleteRestrictions, AnnotationsHelper.GetInsertRestrictions
});
if (string.IsNullOrEmpty(matchEntity.Item1)
|| matchEntity.Item2 == null || !matchEntity.Item2.Any()
|| matchEntity.Item3 == null || !matchEntity.Item3.Any())
{
continue;
}
foreach (var np in matchEntity.Item3)
{
navigPropName = np.NavigationPropertyName;
string navigEntityTypeShortName = np.NavigationPropertyType.RemoveCollectionFlag().GetLastSegment();
List<NormalProperty> navigKeyProps = MetadataHelper.GetKeyProperties(serviceStatus.MetadataDocument, navigEntityTypeShortName).ToList();
if (navigKeyProps.Count == 1 && keyPropertyTypes.Contains(navigKeyProps[0].PropertyType))
{
navigPropRelatedEntitySetUrl = navigEntityTypeShortName.MapEntityTypeShortNameToEntitySetURL();
navigPropRelatedEntityTypeKeyName = navigKeyProps[0].PropertyName;
break;
}
}
if (!string.IsNullOrEmpty(navigPropRelatedEntityTypeKeyName) && !string.IsNullOrEmpty(navigPropRelatedEntitySetUrl))
{
string entitySetUrl = et.EntitySetName.MapEntitySetNameToEntitySetURL();
string url = serviceStatus.RootURL.TrimEnd('/') + @"/" + entitySetUrl;
var additionalInfos = new List<AdditionalInfo>();
var reqData = dFactory.ConstructInsertedEntityData(et.EntitySetName, et.EntityTypeShortName, null, out additionalInfos);
string reqDataStr = reqData.ToString();
bool isMediaType = !string.IsNullOrEmpty(additionalInfos.Last().ODataMediaEtag);
var resp = WebHelper.CreateEntity(url, context.RequestHeaders, reqData, isMediaType, ref additionalInfos);
detail1 = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Post, string.Empty, resp, string.Empty, reqDataStr);
if (resp.StatusCode.HasValue && HttpStatusCode.Created == resp.StatusCode)
{
string entityId = additionalInfos.Last().EntityId;
url = serviceStatus.RootURL.TrimEnd('/') + @"/" + navigPropRelatedEntitySetUrl;
resp = WebHelper.Get(new Uri(url), Constants.V4AcceptHeaderJsonFullMetadata, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, serviceStatus.DefaultHeaders);
detail2 = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Get, StringHelper.MergeHeaders(Constants.V4AcceptHeaderJsonFullMetadata, serviceStatus.DefaultHeaders), resp, string.Empty, reqDataStr);
if (null != resp && resp.StatusCode == HttpStatusCode.OK)
{
JObject feed;
resp.ResponsePayload.TryToJObject(out feed);
var entities = JsonParserHelper.GetEntries(feed);
if (entities.Count > 0)
{
// Use the HTTP method POST to link the navigation property for an entity.
reqDataStr = @"{""" + Constants.V4OdataId + @""" : """ + entities[0][Constants.V4OdataId].ToString() + @"""}";
url = string.Format("{0}/{1}/$ref", entityId, navigPropName);
resp = WebHelper.CreateEntity(url, reqDataStr);
detail3 = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Post, string.Empty, resp, string.Empty, reqDataStr);
if (null != resp && resp.StatusCode == HttpStatusCode.NoContent)
{
passed = true;
}
else
{
//.........这里部分代码省略.........
示例11: Verify
/// <summary>
/// Verifies the service implementation feature.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if the service implementation feature passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name);
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
detail = info.Details[0];
List<string> supportedPropertyTypes = new List<string>{
PrimitiveDataTypes.Int16, PrimitiveDataTypes.Int32, PrimitiveDataTypes.Int64,
PrimitiveDataTypes.Decimal, PrimitiveDataTypes.Double
};
var filterRestrictions = AnnotationsHelper.GetFilterRestrictions(context.MetadataDocument, context.VocCapabilities, supportedPropertyTypes,NavigationRoughType.None);
if (string.IsNullOrEmpty(filterRestrictions.Item1) ||
null == filterRestrictions.Item2 || !filterRestrictions.Item2.Any())
{
detail.ErrorMessage = "Cannot find an appropriate entity-set which supports Less Than system query options in the service.";
return passed;
}
string entitySet = filterRestrictions.Item1;
string primitivePropName = filterRestrictions.Item2.First().PropertyName;
string url = string.Format("{0}/{1}", context.ServiceBaseUri, entitySet);
var resp = WebHelper.Get(new Uri(url), Constants.AcceptHeaderJson, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);
if (null == resp || HttpStatusCode.OK != resp.StatusCode)
{
detail.ErrorMessage = JsonParserHelper.GetErrorMessage(resp.ResponsePayload);
return passed;
}
JObject feed;
resp.ResponsePayload.TryToJObject(out feed);
if (feed == null || JTokenType.Object != feed.Type)
{
detail.ErrorMessage = "The service does not return a valid response for system query option";
return passed;
}
var entities = JsonParserHelper.GetEntries(feed);
Int64 propVal = entities[0].Value<Int64>(primitivePropName) - 1;
string pattern = "{0}/{1}?$filter=(1 add 0) mul {2} sub {3} lt 2";
url = string.Format(pattern, context.ServiceBaseUri, entitySet, primitivePropName, propVal);
resp = WebHelper.Get(new Uri(url), Constants.AcceptHeaderJson, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);
detail.URI = url;
detail.HTTPMethod = "GET";
detail.RequestHeaders = StringHelper.MergeHeaders(Constants.AcceptHeaderJson, context.RequestHeaders);
detail.ResponseStatusCode = resp != null && resp.StatusCode.HasValue ? resp.StatusCode.Value.ToString() : "";
detail.ResponseHeaders = string.IsNullOrEmpty(resp.ResponseHeaders) ? "" : resp.ResponseHeaders;
detail.ResponsePayload = string.IsNullOrEmpty(resp.ResponsePayload) ? "" : resp.ResponsePayload;
if (resp.StatusCode != HttpStatusCode.OK)
{
passed = false;
detail.ErrorMessage = "Request failed with system query option $filter '()'.";
return passed;
}
JObject feed2;
resp.ResponsePayload.TryToJObject(out feed2);
if (feed2 == null || JTokenType.Object != feed2.Type)
{
passed = false;
detail.ErrorMessage = "The service does not return a valid response for system query option $filter '()'.";
return passed;
}
var entities2 = JsonParserHelper.GetEntries(feed2).ToList();
var temp = entities2.FindAll(en => en.Value<Int64>(primitivePropName) - propVal < 2).Select(en => en);
if (entities2.Count() == temp.Count())
{
passed = true;
}
else
{
passed = false;
detail.ErrorMessage = "The service does not execute an accurate result with system query option $filter '()'.";
}
//.........这里部分代码省略.........
示例12: Verify
/// <summary>
/// Verifies the service implementation feature.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if the service implementation feature passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
info = null;
var svcStatus = ServiceStatus.GetInstance();
string entityTypeShortName;
var propTypes = new string[2] { "Edm.Date", "Edm.DateTimeOffset" };
var propNames = MetadataHelper.GetPropertyNames(propTypes, out entityTypeShortName);
if (null == propNames || !propNames.Any())
{
return passed;
}
string propName = propNames[0];
var entitySetUrl = entityTypeShortName.GetAccessEntitySetURL();
if (string.IsNullOrEmpty(entitySetUrl))
{
return passed;
}
string url = svcStatus.RootURL.TrimEnd('/') + "/" + entitySetUrl;
var resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
if (null != resp && HttpStatusCode.OK == resp.StatusCode)
{
var settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.None;
JObject jObj = JsonConvert.DeserializeObject(resp.ResponsePayload, settings) as JObject;
JArray jArr = jObj.GetValue(Constants.Value) as JArray;
var entity = jArr.First as JObject;
var propVal = entity[propName].ToString();
int index = propVal.IndexOf('T');
propVal = propVal.Substring(index + 1, 2);
url = string.Format("{0}?$filter=hour({1}) eq {2}", url, propName, propVal);
resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
var detail = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Get, string.Empty);
info = new ExtensionRuleViolationInfo(new Uri(url), string.Empty, detail);
if (null != resp && HttpStatusCode.OK == resp.StatusCode)
{
jObj = JsonConvert.DeserializeObject(resp.ResponsePayload, settings) as JObject;
jArr = jObj.GetValue(Constants.Value) as JArray;
if (null == jArr || !jArr.Any())
{
return false;
}
foreach (JObject et in jArr)
{
passed = et[propName].ToString().Substring(index + 1, 2) == propVal;
}
}
else
{
passed = false;
}
}
return passed;
}
示例13: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name);
var entitySets = MetadataHelper.GetFeeds(context.MetadataDocument);
if (null == entitySets || !entitySets.Any())
{
detail.ErrorMessage = "Cannot find any entity-sets in metadata document.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
string entitySet = string.Empty;
foreach (var set in entitySets)
{
if (true == set.IsSupportAsynchronousOperation(context.MetadataDocument, new List<string>() { context.VocCapabilities }))
{
entitySet = set;
break;
}
}
if (string.IsNullOrEmpty(entitySet))
{
detail.ErrorMessage = "Cannot find an appropriate entity-set which supports asynchronous operation in metadata document.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
string url = string.Format("{0}/{1}", context.ServiceBaseUri, entitySet);
List<KeyValuePair<string, string>> requestHeaders = new List<KeyValuePair<string,string>>();
foreach (KeyValuePair<string, string> kvp in context.RequestHeaders)
{
requestHeaders.Add(new KeyValuePair<string, string>(kvp.Key, kvp.Value));
}
// Add respond-async preference in request header.
KeyValuePair<string, string> prefer = new KeyValuePair<string, string>("Prefer", "respond-async");
requestHeaders.Add(prefer);
Response response = WebHelper.Get(new Uri(url), Constants.V4AcceptHeaderJsonFullMetadata, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, requestHeaders);
detail = new ExtensionRuleResultDetail(this.Name, url, "GET", StringHelper.MergeHeaders(Constants.V4AcceptHeaderJsonFullMetadata, requestHeaders), response);
if (response != null )
{
if(response.StatusCode == HttpStatusCode.Accepted)
{
passed = true;
}
else if (!string.IsNullOrEmpty(response.ResponseHeaders))
{
string preferHeader = response.ResponseHeaders.GetHeaderValue("Preference-Applied");
if (!string.IsNullOrEmpty(preferHeader) && preferHeader.Contains("respond-async"))
{
passed = true;
}
else
{
passed = false;
detail.ErrorMessage = "The service does not support Asynchronous operations.";
}
}
}
else
{
detail.ErrorMessage = "The service does not support Asynchronous operations.";
}
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);
return passed;
}
示例14: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
info = null;
ServiceStatus serviceStatus = ServiceStatus.GetInstance();
TermDocuments termDocs = TermDocuments.GetInstance();
DataFactory dFactory = DataFactory.Instance();
var detail1 = new ExtensionRuleResultDetail(this.Name, serviceStatus.RootURL, HttpMethod.Post, string.Empty);
var detail2 = new ExtensionRuleResultDetail(this.Name);
List<string> keyPropertyTypes = new List<string>() { "Edm.Int32", "Edm.Int16", "Edm.Int64", "Edm.Guid", "Edm.String" };
List<EntityTypeElement> entityTypeElements = MetadataHelper.GetEntityTypes(serviceStatus.MetadataDocument, 1, keyPropertyTypes, null, NavigationRoughType.None).ToList();
if (null == entityTypeElements || 0 == entityTypeElements.Count())
{
detail1.ErrorMessage = "To verify this rule it expects an entity type with Int32/Int64/Int16/Guid/String key property, but there is no this entity type in metadata so can not verify this rule.";
info = new ExtensionRuleViolationInfo(new Uri(serviceStatus.RootURL), serviceStatus.ServiceDocument, detail1);
return passed;
}
// To get test entity which is of media type
EntityTypeElement entityType = null;
foreach (var entityEle in entityTypeElements)
{
if (MetadataHelper.IsMediaEntity(serviceStatus.MetadataDocument, entityEle.EntityTypeShortName, context))
{
entityType = entityEle;
break;
}
}
if (null == entityType)
{
detail1.ErrorMessage = "To verify this rule it expects an entity type with deleteable and insertable restrictions, but there is no entity type in metadata which is insertable and deleteable.";
info = new ExtensionRuleViolationInfo(new Uri(serviceStatus.RootURL), serviceStatus.ServiceDocument, detail1);
return passed;
}
// Map 'entity-set name' to 'entity-set URL'.
string entitySetUrl = entityType.EntitySetName.MapEntitySetNameToEntitySetURL();
if (string.IsNullOrEmpty(entitySetUrl))
{
detail1.ErrorMessage = string.Format("Cannot find the entity-set URL which is matched with {0}", entityType.EntityTypeShortName);
info = new ExtensionRuleViolationInfo(new Uri(serviceStatus.RootURL), serviceStatus.ServiceDocument, detail1);
return passed;
}
string url = serviceStatus.RootURL.TrimEnd('/') + @"/" + entitySetUrl;
var additionalInfos = new List<AdditionalInfo>();
var reqData = dFactory.ConstructInsertedEntityData(entityType.EntitySetName, entityType.EntityTypeShortName, null, out additionalInfos);
string reqDataStr = reqData.ToString();
var resp = WebHelper.CreateEntity(url, context.RequestHeaders, reqData, true, ref additionalInfos);
detail1 = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Post, string.Empty, resp, string.Empty, reqDataStr);
if (resp.StatusCode.HasValue && (HttpStatusCode.Created == resp.StatusCode || HttpStatusCode.NoContent == resp.StatusCode))
{
string entityId = additionalInfos.Last().EntityId;
resp = WebHelper.GetEntity(entityId);
detail2 = new ExtensionRuleResultDetail(this.Name, entityId, HttpMethod.Get, string.Empty, resp);
if (resp.StatusCode.HasValue && HttpStatusCode.OK == resp.StatusCode)
{
passed = true;
}
else
{
passed = false;
detail2.ErrorMessage = "Can not get created entity from above URI.";
}
// Restore the service.
var resps = WebHelper.DeleteEntities(context.RequestHeaders, additionalInfos);
}
else
{
passed = false;
detail1.ErrorMessage = "Created the new entity failed for above URI.";
}
var details = new List<ExtensionRuleResultDetail>() { detail1, detail2 }.RemoveNullableDetails();
info = new ExtensionRuleViolationInfo(new Uri(serviceStatus.RootURL), serviceStatus.ServiceDocument, details);
return passed;
}
示例15: Verify
/// <summary>
/// Verifies the extension rule.
/// </summary>
/// <param name="context">The Interop service context</param>
/// <param name="info">out parameter to return violation information when rule does not pass</param>
/// <returns>true if rule passes; false otherwise</returns>
public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool? passed = null;
List<string> expectedTypes = new List<string>() { "Edm.String", "Edm.Int32", "Edm.Int16", "Edm.Single", "Edm.Double", "Edm.Boolean", "Edm.DateTimeOffset", "Edm.Guid" };
ExtensionRuleResultDetail detail1 = new ExtensionRuleResultDetail(this.Name);
var restrictions = AnnotationsHelper.GetRestrictions(
context.MetadataDocument,
context.VocCapabilities,
new List<Func<string, string, string, List<NormalProperty>, List<NavigProperty>, bool>>() { AnnotationsHelper.GetExpandRestrictions, AnnotationsHelper.GetFilterRestrictions },
null,
NavigationRoughType.CollectionValued);
if (string.IsNullOrEmpty(restrictions.Item1) ||
null == restrictions.Item2 || !restrictions.Item2.Any() ||
null == restrictions.Item3 || !restrictions.Item3.Any())
{
detail1.ErrorMessage = "Cannot find an appropriate entity-set which supports $expand, $filter system query options in the service.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail1);
return passed;
}
string entitySet = restrictions.Item1;
string navigPropName = string.Empty;
string navigPropType = string.Empty;
foreach (var np in restrictions.Item3)
{
if (NavigationRoughType.CollectionValued == np.NavigationRoughType)
{
navigPropName = np.NavigationPropertyName;
navigPropType = np.NavigationPropertyType;
}
}
string entityType = navigPropType.RemoveCollectionFlag().GetLastSegment();
var tmp = MetadataHelper.GetPropertiesWithSpecifiedTypeFromEntityType(entityType, context.MetadataDocument, expectedTypes);
if (null == tmp && !tmp.Any())
{
detail1.ErrorMessage = "Cannot find an appropriate primitive properties of entity type in the service.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail1);
return passed;
}
var str = tmp.First().Split(',');
string primitivePropertyName = str[0];
string primitivePropertyType = str[1];
if (string.IsNullOrEmpty(entitySet))
{
detail1.ErrorMessage = "Cannot find an appropriate entity-set which supports $expand system query option.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail1);
return passed;
}
if (string.IsNullOrEmpty(navigPropName) || string.IsNullOrEmpty(navigPropType))
{
detail1.ErrorMessage = "Cannot get expanded entities because cannot get collection type of navigation property from metadata";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail1);
return passed;
}
if (string.IsNullOrEmpty(primitivePropertyName) || string.IsNullOrEmpty(primitivePropertyType))
{
detail1.ErrorMessage = "Cannot get an appropriate primitive property from navigation properties.";
info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail1);
return passed;
}
Uri uri = new Uri(string.Format("{0}/{1}?$expand={2}", context.ServiceBaseUri, entitySet, navigPropName));
var response = WebHelper.Get(uri, Constants.AcceptHeaderJson, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);
if (HttpStatusCode.OK != response.StatusCode)
{
passed = false;
detail1.ErrorMessage = JsonParserHelper.GetErrorMessage(response.ResponsePayload);
info = new ExtensionRuleViolationInfo(uri, response.ResponsePayload, detail1);
return passed;
}
JObject feed;
response.ResponsePayload.TryToJObject(out feed);
//.........这里部分代码省略.........