private static object BindObject(string name, Type objectType, IServiceContext context) { string value = context.GetHttpContext().Request.Form.Get(name); object changedValue; return SafeConvert.TryChangeType(value, objectType, out changedValue) ? changedValue : null; }
/// <summary> /// Called before a service method is executed. /// </summary> /// <param name="serviceContext">The service context.</param> /// <param name="behaviorContext">The "method executing" behavior context.</param> /// <returns>A service method action.</returns> public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext) { if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } if (behaviorContext == null) { throw new ArgumentNullException("behaviorContext"); } if (behaviorContext.Resource == null || m_validator == null) { return BehaviorMethodAction.Execute; } IReadOnlyCollection<ValidationError> validationErrors; if (!m_validator.IsValid(behaviorContext.Resource, out validationErrors)) { serviceContext.GetHttpContext().Items[ResourceValidator.ValidationErrorKey] = new ResourceState(validationErrors); } return BehaviorMethodAction.Execute; }
/// <summary> /// Called during the authorization process before a service method or behavior is executed. /// </summary> /// <param name="serviceContext">The service context.</param> /// <param name="behaviorContext">The "method authorizing" behavior context.</param> /// <returns>A service method action.</returns> public override BehaviorMethodAction OnMethodAuthorizing(IServiceContext serviceContext, MethodAuthorizingContext behaviorContext) { if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } var ranges = IPAddressRange.GetConfiguredRanges(m_sectionName).ToList(); if (ranges.Count == 0) { return BehaviorMethodAction.Stop; } bool isAllowed = false; foreach (var range in ranges) { if (range.IsInRange(serviceContext.GetHttpContext().Request.UserHostAddress)) { isAllowed = true; break; } } return isAllowed ? BehaviorMethodAction.Execute : BehaviorMethodAction.Stop; }
/// <summary> /// Called before a service method is executed. /// </summary> /// <param name="serviceContext">The service context.</param> /// <param name="behaviorContext">The "method executing" behavior context.</param> /// <returns>A service method action.</returns> public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext) { if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } serviceContext.GetHttpContext().Items[ServiceCallConstants.MaxQueryResults] = AllowOverride ? (MaxResults * -1) : MaxResults; return BehaviorMethodAction.Execute; }
private static object BindArray(string name, Type objectType, IServiceContext context) { Type elementType = objectType.GetElementType(); string[] values = context.GetHttpContext().Request.Form.GetValues(name) ?? new string[0]; var changedValues = Array.CreateInstance(elementType, values.Length); for (int i = 0; i < values.Length; i++) { object changedArrayValue; changedValues.SetValue(SafeConvert.TryChangeType(values[i], elementType, out changedArrayValue) ? changedArrayValue : null, i); } return changedValues; }
/// <summary> /// Called after a service method is executed. /// </summary> /// <param name="serviceContext">The service context.</param> /// <param name="behaviorContext">The "method executed" behavior context.</param> public override void OnMethodExecuted(IServiceContext serviceContext, MethodExecutedContext behaviorContext) { if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } HttpContextBase httpContext = serviceContext.GetHttpContext(); if (httpContext == null) { throw new ArgumentException(Resources.Global.MissingHttpContext, "serviceContext"); } httpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); httpContext.Response.Cache.SetValidUntilExpires(false); httpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); httpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); httpContext.Response.Cache.SetNoStore(); }
/// <summary> /// Deserializes HTTP message body data into an object instance of the provided type. /// </summary> /// <param name="context">The service context.</param> /// <param name="objectType">The object type.</param> /// <returns>The deserialized object.</returns> /// <exception cref="HttpResponseException">If the object cannot be deserialized.</exception> public virtual object FormatRequest(IServiceContext context, Type objectType) { if (context == null) { throw new ArgumentNullException("context"); } if (objectType == null) { throw new ArgumentNullException("objectType"); } TryValidateFormValues(context.GetHttpContext()); object resource = InitializeResource(objectType); if (resource == null) { return null; } NameValueCollection formData = PopulateFormData(context); if (formData.Count == 0) { return resource; } dynamic dynamicResource = resource as DynamicResult; if (dynamicResource != null) { PopulateDynamicResourceData(dynamicResource, formData); return dynamicResource; } PopulateResourceData(resource, formData); return resource; }
void ISecureServiceBehavior.OnMethodAuthorizing(IServiceContext serviceContext, MethodAuthorizingContext behaviorContext) { if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } if (OnMethodAuthorizing(serviceContext, behaviorContext) == BehaviorMethodAction.Stop) { HttpStatusCode statusCode = serviceContext.Response.GetStatusCode(); if (statusCode != HttpStatusCode.Unauthorized && statusCode != m_statusCode) { throw new HttpResponseException(m_statusCode, m_statusDescription); } throw new HttpResponseException(statusCode, serviceContext.Response.GetStatusDescription()); } HttpCachePolicyBase cache = serviceContext.GetHttpContext().Response.Cache; cache.SetProxyMaxAge(new TimeSpan(0L)); cache.AddValidationCallback(CacheValidationHandler, new CacheValidationHandlerData(serviceContext, behaviorContext)); }
private static void TrySetMaxQueryResults(IServiceContext context, NameValueCollection queryString) { object maxResultString = context.GetHttpContext().Items[ServiceCallConstants.MaxQueryResults] ?? Rest.Configuration.Options.ODataSettings.MaxResults.ToString(CultureInfo.InvariantCulture); int maxQueryResults = Convert.ToInt32(maxResultString, CultureInfo.InvariantCulture); if (maxQueryResults < 0) { if (!String.IsNullOrEmpty(queryString[TopKey])) { return; } queryString[TopKey] = (maxQueryResults * -1).ToString(CultureInfo.InvariantCulture); } else if (maxQueryResults > 0) { string topValue = queryString[TopKey]; if (!String.IsNullOrEmpty(topValue)) { int top; if (!Int32.TryParse(topValue, out top)) { return; } if (top > 0 && top < maxQueryResults) { maxQueryResults = top; } } queryString[TopKey] = maxQueryResults.ToString(CultureInfo.InvariantCulture); } }