Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HttpConfigurationExtensions.cs « System.Web.Http.OData « src - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9174855a69e8621efa6219dd011fd6d5e6b59fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

using System.ComponentModel;
using System.Linq;
using System.Web.Http.OData.Formatter;
using System.Web.Http.OData.Properties;
using Microsoft.Data.Edm;

namespace System.Web.Http
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public static class HttpConfigurationExtensions
    {
        private const string EdmModelKey = "MS_EdmModel";
        private const string ODataFormatterKey = "MS_ODataFormatter";

        /// <summary>
        /// Retrieve the EdmModel from the configuration Properties collection. Null if user has not set it.
        /// </summary>
        /// <param name="configuration">Configuration to look into.</param>
        /// <returns>Returns an EdmModel for this configuration</returns>
        public static IEdmModel GetEdmModel(this HttpConfiguration configuration)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            // returns one if user sets one, null otherwise
            object result;
            if (configuration.Properties.TryGetValue(EdmModelKey, out result))
            {
                return result as IEdmModel;
            }

            return null;
        }

        /// <summary>
        /// Sets the given EdmModel with the configuration.
        /// </summary>
        /// <param name="configuration">Configuration to be updated.</param>
        /// <param name="model">The EdmModel to update.</param>
        public static void SetEdmModel(this HttpConfiguration configuration, IEdmModel model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (model == null)
            {
                throw Error.ArgumentNull("model");
            }

            if (configuration.GetODataFormatter() != null)
            {
                throw Error.NotSupported(
                    SRResources.EdmModelMismatch,
                    typeof(IEdmModel).Name,
                    typeof(ODataMediaTypeFormatter).Name,
                    "SetODataFormatter");
            }

            configuration.Properties.AddOrUpdate(EdmModelKey, model, (a, b) =>
                {
                    return model;
                });
        }

        /// <summary>
        /// Retrieve the <see cref="ODataMediaTypeFormatter"/> from the configuration Properties collection. Null if user has not set it.
        /// </summary>
        /// <param name="configuration">Configuration to look into.</param>
        /// <returns>Returns an <see cref="ODataMediaTypeFormatter"/> for this configuration.</returns>
        public static ODataMediaTypeFormatter GetODataFormatter(this HttpConfiguration configuration)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            // returns one if user sets one, else null.
            object result;
            if (configuration.Properties.TryGetValue(ODataFormatterKey, out result))
            {
                return result as ODataMediaTypeFormatter;
            }

            // Instead of trying to get the odata formatter from the formatter collection which works only if tracing is not enabled
            // fail here so that the user doesn't have a surprise when he enables tracing.
            return null;
        }

        /// <summary>
        /// Sets the given <see cref="ODataMediaTypeFormatter"/> on the configuration and adds it to the formatter collection.
        /// </summary>
        /// <param name="configuration">Configuration to be updated.</param>
        /// <param name="formatter">The <see cref="ODataMediaTypeFormatter"/> to update.</param>
        public static void SetODataFormatter(this HttpConfiguration configuration, ODataMediaTypeFormatter formatter)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (formatter == null)
            {
                throw Error.ArgumentNull("formatter");
            }

            if (configuration.GetODataFormatter() != null || configuration.Formatters.OfType<ODataMediaTypeFormatter>().Any())
            {
                throw Error.NotSupported(SRResources.ResetODataFormatterNotSupported, typeof(ODataMediaTypeFormatter).Name, typeof(HttpConfiguration).Name);
            }
            else if (configuration.GetEdmModel() != null && configuration.GetEdmModel() != formatter.Model)
            {
                throw Error.NotSupported(
                    SRResources.EdmModelOnConfigurationMismatch,
                    typeof(IEdmModel).Name,
                    typeof(ODataMediaTypeFormatter).Name,
                    "SetODataFormatter");
            }
            else
            {
                // This is a workaround for Bug 464640 where the formatter tracer wraps on to the formatter and there is no 
                // easy way to retrieve the ODataFormatter from configuration afterwards.
                configuration.SetEdmModel(formatter.Model);
                configuration.Properties.TryAdd(ODataFormatterKey, formatter);
                configuration.Formatters.Insert(0, formatter);
            }
        }
    }
}