Example #1
0
        /// <summary>
        /// Inspects the <see cref="RequestBase"/> and fill the <see cref="HttpHeaderCollection"/> with applicable headers.
        /// </summary>
        /// <param name="request">The <see cref="RequestBase"/>.</param>
        /// <param name="collectedHttpHeaders">The <see cref="HttpHeaderCollection"/> to fill with headers.</param>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="request"/>' and '<paramref name="collectedHttpHeaders"/>' cannot be null. </exception>
        public void CollectRequestHeaders(RequestBase request, HttpHeaderCollection collectedHttpHeaders)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (collectedHttpHeaders == null)
            {
                throw new ArgumentNullException(nameof(collectedHttpHeaders));
            }

            var members = request.GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public).Where(property => property.HasAttributeExactly <MapHeaderAttribute>());

            foreach (var memberInfo in members)
            {
                var mapHeaderAttribute = memberInfo.GetAttribute <MapHeaderAttribute>();
                var memberValue        = memberInfo.GetValue(request);
                if (memberValue == null)
                {
                    continue;
                }

                // ReSharper disable once AssignNullToNotNullAttribute Attribute is defined cause of filter in LINQ expression.
                var headerName = GetHeaderName(memberInfo, mapHeaderAttribute);
                var httpHeader = new HttpHeader(headerName, (string)memberValue);
                collectedHttpHeaders.Add(httpHeader);
            }
        }
Example #2
0
        /// <summary>
        /// Maps all <see cref="Header"/> instances of the supplied <see cref="HttpHeaderCollection"/> to the supplied <see cref="ResponseBase"/>.
        /// </summary>
        /// <param name="responseBase">The response.</param>
        /// <param name="httpHeaderCollection">The headers-</param>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="responseBase"/>' and '<paramref name="httpHeaderCollection"/>' cannot be null. </exception>
        /// <exception cref="MemberIsNotWritableException">The member is not writable.</exception>
        public void MapHeaders(ResponseBase responseBase, HttpHeaderCollection httpHeaderCollection)
        {
            if (responseBase == null)
            {
                throw new ArgumentNullException(nameof(responseBase));
            }

            if (httpHeaderCollection == null)
            {
                throw new ArgumentNullException(nameof(httpHeaderCollection));
            }

            var members = responseBase.GetType().GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public).Where(property => property.HasAttribute <MapHeaderAttribute>());

            foreach (var memberInfo in members)
            {
                if (memberInfo.IsReadonly())
                {
                    throw new MemberIsNotWritableException(memberInfo);
                }

                var mapHeaderAttribute = memberInfo.GetAttribute <MapHeaderAttribute>();
                var mappedHeader       = httpHeaderCollection.SingleOrDefault(h => h.Name.Equals(mapHeaderAttribute.HeaderName, StringComparison.InvariantCultureIgnoreCase));
                if (mappedHeader == null || String.IsNullOrEmpty(mappedHeader.Value))
                {
                    continue;
                }

                memberInfo.SetValue(responseBase, mappedHeader.Value);
            }
        }
Example #3
0
        /// <summary>
        /// Returns a new <see cref="HttpHeaderCollection"/> with collected headers from the <see cref="RequestBase"/>.
        /// </summary>
        /// <param name="request">The <see cref="RequestBase"/>.</param>
        /// <returns>The <see cref="HttpHeaderCollection"/>.</returns>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="request"/>' cannot be null. </exception>
        public HttpHeaderCollection CreateRequestHeaders(RequestBase request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var result = new HttpHeaderCollection();

            foreach (var requestHeaderCollector in this.requestHeaderCollectors)
            {
                requestHeaderCollector.CollectRequestHeaders(request, result);
            }

            return(result);
        }
        /// <summary>
        /// Inspects the <see cref="RequestBase"/> and creates a <see cref="HttpHeaderCollection"/> with applicable headers.
        /// </summary>
        /// <param name="request">The <see cref="RequestBase"/>.</param>
        /// <param name="collectedHttpHeaders"></param>
        /// <returns>A new instance of the <see cref="HttpHeaderCollection"/> class.</returns>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="request"/>' and '<paramref name="collectedHttpHeaders"/>' cannot be null. </exception>
        public void CollectRequestHeaders(RequestBase request, HttpHeaderCollection collectedHttpHeaders)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (collectedHttpHeaders == null)
            {
                throw new ArgumentNullException(nameof(collectedHttpHeaders));
            }

            foreach (var requestHeader in request.HttpHeaders)
            {
                collectedHttpHeaders.Add(requestHeader);
            }
        }
Example #5
0
        public static HttpHeaderCollection FromHttpResponseMessage([NotNull] HttpResponseMessage httpResponseMessage)
        {
            if (httpResponseMessage == null)
            {
                throw new ArgumentNullException(nameof(httpResponseMessage));
            }

            var result = new HttpHeaderCollection();

            foreach (var httpResponseHeader in httpResponseMessage.Headers)
            {
                var httpHeader = new HttpHeader(httpResponseHeader.Key, String.Join(" ", httpResponseHeader.Value));
                result.Add(httpHeader);
            }

            return(result);
        }
Example #6
0
        /// <summary>
        /// Inspects the <see cref="RequestBase"/> and creates a <see cref="HttpHeaderCollection"/> with applicable headers.
        /// </summary>
        /// <param name="request">The <see cref="RequestBase"/>.</param>
        /// <param name="collectedHttpHeaders"></param>
        /// <returns>A new instance of the <see cref="HttpHeaderCollection"/> class.</returns>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="request"/>' and '<paramref name="collectedHttpHeaders"/>' cannot be null. </exception>
        public void CollectRequestHeaders(RequestBase request, HttpHeaderCollection collectedHttpHeaders)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (collectedHttpHeaders == null)
            {
                throw new ArgumentNullException(nameof(collectedHttpHeaders));
            }

            foreach (var requestHeader in request.GetType().GetAttributesExactly <FixedRequestHeaderAttribute>().Select(fixedRequestHeaderAttribute => fixedRequestHeaderAttribute.ToHttpHeader()))
            {
                collectedHttpHeaders.Add(requestHeader);
            }
        }