/WCFWebApi/src/Microsoft.ApplicationServer.Http/Microsoft/ApplicationServer/Http/Description/HttpOperationDescriptionExtensionMethods.cs
C# | 204 lines | 143 code | 32 blank | 29 comment | 38 complexity | bb0dcc5c73d44c9cb14a03e45f10cb75 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
- // <copyright>
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
-
- namespace Microsoft.ApplicationServer.Http.Description
- {
- using System;
- using System.ComponentModel;
- using System.Net.Http;
- using System.ServiceModel.Description;
- using System.ServiceModel.Web;
- using System.Text;
- using System.Xml;
- using Microsoft.Server.Common;
-
- /// <summary>
- /// Provides extension methods for <see cref="OperationDescription"/> instances.
- /// </summary>
- public static class HttpOperationDescriptionExtensionMethods
- {
- private static readonly Type webGetAttributeType = typeof(WebGetAttribute);
- private static readonly Type webInvokeAttributeType = typeof(WebInvokeAttribute);
- private static readonly Type descriptionAttributeType = typeof(DescriptionAttribute);
-
- /// <summary>
- /// Creates an <see cref="HttpOperationDescription"/> instance based on the given
- /// <see cref="OperationDescription"/> instance.
- /// </summary>
- /// <param name="operation">The <see cref="OperationDescription"/> instance.</param>
- /// <returns>A new <see cref="HttpOperationDescription"/> instance.</returns>
- public static HttpOperationDescription ToHttpOperationDescription(this OperationDescription operation)
- {
- if (operation == null)
- {
- throw Fx.Exception.ArgumentNull("operation");
- }
-
- return new HttpOperationDescription(operation);
- }
-
- /// <summary>
- /// Gets the <see cref="HttpMethod"/> for the given <paramref name="operation"/>.
- /// </summary>
- /// <param name="operation">The <see cref="HttpOperationDescription"/> instance.</param>
- /// <returns>The <see cref="HttpMethod"/>.</returns>
- public static HttpMethod GetHttpMethod(this HttpOperationDescription operation)
- {
- if (operation == null)
- {
- throw Fx.Exception.ArgumentNull("operation");
- }
-
- return new HttpMethod(GetWebMethod(operation));
- }
-
- /// <summary>
- /// Gets the <see cref="UriTemplate"/> associated with the given <paramref name="operation"/>.
- /// </summary>
- /// <param name="operation">The <see cref="HttpOperationDescription"/> instance.</param>
- /// <returns>The <see cref="UriTemplate"/>.</returns>
- public static UriTemplate GetUriTemplate(this HttpOperationDescription operation)
- {
- // AutoRedirect is default TrailingSlashMode
- return GetUriTemplate(operation, TrailingSlashMode.AutoRedirect);
- }
-
- /// <summary>
- /// Gets the <see cref="UriTemplate"/> associated with the given <paramref name="operation"/>.
- /// </summary>
- /// <param name="operation">The <see cref="HttpOperationDescription"/> instance.</param>
- /// <param name="trailingSlashMode">The <see cref="TrailingSlashMode"/> option to use for the <see cref="UriTemplate"/>.</param>
- /// <returns>The <see cref="UriTemplate"/>.</returns>
- public static UriTemplate GetUriTemplate(this HttpOperationDescription operation, TrailingSlashMode trailingSlashMode)
- {
- if (operation == null)
- {
- throw Fx.Exception.ArgumentNull("operation");
- }
-
- TrailingSlashModeHelper.Validate(trailingSlashMode, "trailingSlashMode");
-
- return new UriTemplate(operation.GetUriTemplateStringOrDefault(), trailingSlashMode == TrailingSlashMode.Ignore);
- }
-
- internal static string GetDescription(this HttpOperationDescription operation)
- {
- Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
-
- OperationDescription operationDescription = operation.ToOperationDescription();
-
- object[] attributes = null;
- if (operationDescription.SyncMethod != null)
- {
- attributes = operationDescription.SyncMethod.GetCustomAttributes(descriptionAttributeType, true);
- }
- else if (operationDescription.BeginMethod != null)
- {
- attributes = operationDescription.BeginMethod.GetCustomAttributes(descriptionAttributeType, true);
- }
-
- if (attributes != null && attributes.Length > 0)
- {
- return ((DescriptionAttribute)attributes[0]).Description;
- }
- else
- {
- return String.Empty;
- }
- }
-
- private static string GetWebMethod(this HttpOperationDescription operation)
- {
- Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
-
- WebGetAttribute webGet = operation.Behaviors.Find<WebGetAttribute>();
- WebInvokeAttribute webInvoke = operation.Behaviors.Find<WebInvokeAttribute>();
- EnsureOneOneWebAttribute(webGet, webInvoke, operation);
- if (webGet != null)
- {
- return HttpMethod.Get.ToString();
- }
-
- if (webInvoke == null)
- {
- return HttpMethod.Post.ToString();
- }
-
- return webInvoke.Method ?? HttpMethod.Post.ToString();
- }
-
- private static string GetWebUriTemplate(this HttpOperationDescription operation)
- {
- Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
-
- WebGetAttribute webGet = operation.Behaviors.Find<WebGetAttribute>();
- WebInvokeAttribute webInvoke = operation.Behaviors.Find<WebInvokeAttribute>();
- EnsureOneOneWebAttribute(webGet, webInvoke, operation);
- if (webGet != null)
- {
- return webGet.UriTemplate;
- }
-
- if (webInvoke != null)
- {
- return webInvoke.UriTemplate;
- }
-
- return null;
- }
-
- private static string GetUriTemplateStringOrDefault(this HttpOperationDescription operation)
- {
- Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
-
- string webUriTemplate = GetWebUriTemplate(operation);
- if ((webUriTemplate == null) && (GetWebMethod(operation) == HttpMethod.Get.ToString()))
- {
- webUriTemplate = MakeDefaultGetUriTemplateString(operation);
- }
-
- if (webUriTemplate == null)
- {
- webUriTemplate = operation.Name;
- }
-
- return webUriTemplate;
- }
-
- private static string MakeDefaultGetUriTemplateString(this HttpOperationDescription operation)
- {
- Fx.Assert(operation != null, "The 'operation' parameter should not be null.");
-
- StringBuilder builder = new StringBuilder(XmlConvert.DecodeName(operation.Name));
- builder.Append("?");
-
- foreach (HttpParameter parameterDescription in operation.InputParameters)
- {
- string decodedName = XmlConvert.DecodeName(parameterDescription.Name);
-
- builder.Append(decodedName);
- builder.Append("={");
- builder.Append(decodedName);
- builder.Append("}&");
- }
-
- builder.Remove(builder.Length - 1, 1);
- return builder.ToString();
- }
-
- private static void EnsureOneOneWebAttribute(WebGetAttribute webGet, WebInvokeAttribute webInvoke, HttpOperationDescription operation)
- {
- if (webGet != null && webInvoke != null)
- {
- throw Fx.Exception.AsError(
- new InvalidOperationException(
- Http.SR.MultipleWebAttributes(
- operation.Name,
- operation.DeclaringContract.Name,
- webGetAttributeType.Name,
- webInvokeAttributeType.Name)));
- }
- }
- }
- }