private void ValidateOutgoingJsonMediaType()
        {
            var contentType        = Representation.GetContentType();
            var headers            = requestMessage.GetTypedHeaders();
            var incomingParameters = headers.Accept.SelectMany(a => a.Parameters).ToList();

            var incomingProfiles = incomingParameters.Where(nv => nv.Name.ToString() == "profile").Select(nv => nv.Value).Distinct().ToArray();
            var outgoingProfiles = contentType != null?contentType.Parameters.Where(nv => nv.Name == "profile").Select(nv => nv.Value).Distinct().ToArray() : new StringSegment[]
            {
            };

            if (incomingProfiles.Any() && outgoingProfiles.Any() && !outgoingProfiles.Intersect(incomingProfiles).Any())
            {
                if (outgoingProfiles.Contains(UriMtHelper.GetJsonMediaType(RepresentationTypes.Error).Parameters.First().Value))
                {
                    // outgoing error so even though incoming profiles does not include error representation return error anyway but with 406 code
                    HttpStatusCode = HttpStatusCode.NotAcceptable;
                }
                else
                {
                    var msg = DebugFilter(() => $"Failed outgoing json MT validation ic: {string.Join(',', incomingProfiles.ToArray())} og: {string.Join(',', outgoingProfiles.ToArray())}");

                    // outgoing profile not included in incoming profiles and not already an error so throw a 406
                    throw new ValidationException((int)HttpStatusCode.NotAcceptable, msg);
                }
            }
        }
Example #2
0
        private void ValidateOutgoingAttachmentMediaType()
        {
            var contentType = Representation.GetContentType();

            List <string> incomingMediaTypes = requestMessage.Headers.Accept.Select(a => a.MediaType).ToList();
            string        outgoingMediaType  = contentType == null ? "" : contentType.MediaType;

            if (!incomingMediaTypes.Contains(outgoingMediaType))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable));
            }
        }
        private void ValidateOutgoingAttachmentMediaType()
        {
            var contentType = Representation.GetContentType();
            var headers     = requestMessage.GetTypedHeaders();

            var incomingMediaTypes = headers.Accept.Select(a => a.MediaType).ToList();
            var outgoingMediaType  = contentType?.MediaType ?? "";

            if (!incomingMediaTypes.Contains(outgoingMediaType))
            {
                var msg = DebugFilter(() => $"Failed outgoing attachment MT validation ic: {string.Join(',', incomingMediaTypes.ToArray())} og: {outgoingMediaType}");

                throw new ValidationException((int)HttpStatusCode.NotAcceptable, msg);
            }
        }
Example #4
0
        private void ValidateOutgoingJsonMediaType()
        {
            var contentType = Representation.GetContentType();
            List <NameValueHeaderValue> incomingParameters = requestMessage.Headers.Accept.SelectMany(a => a.Parameters).ToList();

            string[] incomingProfiles = incomingParameters.Where(nv => nv.Name == "profile").Select(nv => nv.Value).Distinct().ToArray();
            string[] outgoingProfiles = contentType != null?contentType.Parameters.Where(nv => nv.Name == "profile").Select(nv => nv.Value).Distinct().ToArray() : new string[]
            {
            };

            if (incomingProfiles.Any() && outgoingProfiles.Any() && !outgoingProfiles.Intersect(incomingProfiles).Any())
            {
                if (outgoingProfiles.Contains(UriMtHelper.GetJsonMediaType(RepresentationTypes.Error).Parameters.First().Value))
                {
                    // outgoing error so even though incoming profiles does not include error representation return error anyway but with 406 code
                    HttpStatusCode = HttpStatusCode.NotAcceptable;
                }
                else
                {
                    // outgoing profile not included in incoming profiles and not already an error so throw a 406
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotAcceptable));
                }
            }
        }