| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Messaging.ServiceBus.Management; |
| | 6 | |
|
| | 7 | | namespace Azure.Messaging.ServiceBus |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// This class can be used to format the path for different Service Bus entity types. |
| | 11 | | /// </summary> |
| | 12 | | public static class EntityNameFormatter |
| | 13 | | { |
| | 14 | | private const string PathDelimiter = @"/"; |
| | 15 | | private const string SubscriptionsSubPath = "Subscriptions"; |
| | 16 | | private const string RulesSubPath = "Rules"; |
| | 17 | | private const string SubQueuePrefix = "$"; |
| | 18 | | private const string DeadLetterQueueSuffix = "DeadLetterQueue"; |
| | 19 | | private const string DeadLetterQueueName = SubQueuePrefix + DeadLetterQueueSuffix; |
| | 20 | | private const string Transfer = "Transfer"; |
| | 21 | | private const string TransferDeadLetterQueueName = SubQueuePrefix + Transfer + PathDelimiter + DeadLetterQueueNa |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Formats the dead letter path for either a queue, or a subscription. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="entityPath">The name of the queue, or path of the subscription.</param> |
| | 27 | | /// <returns>The path as a string of the dead letter entity.</returns> |
| | 28 | | public static string FormatDeadLetterPath(string entityPath) |
| | 29 | | { |
| 0 | 30 | | return EntityNameFormatter.FormatSubQueuePath(entityPath, EntityNameFormatter.DeadLetterQueueName); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Formats the subqueue path for either a queue, or a subscription. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="entityPath">The name of the queue, or path of the subscription.</param> |
| | 37 | | /// <param name="subQueueName"></param> |
| | 38 | | /// <returns>The path as a string of the subqueue entity.</returns> |
| | 39 | | public static string FormatSubQueuePath(string entityPath, string subQueueName) |
| | 40 | | { |
| 0 | 41 | | return string.Concat(entityPath, EntityNameFormatter.PathDelimiter, subQueueName); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Formats the subscription path, based on the topic path and subscription name. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="topicPath">The name of the topic, including slashes.</param> |
| | 48 | | /// <param name="subscriptionName">The name of the subscription.</param> |
| | 49 | | public static string FormatSubscriptionPath(string topicPath, string subscriptionName) |
| | 50 | | { |
| 68 | 51 | | return string.Concat(topicPath, PathDelimiter, SubscriptionsSubPath, PathDelimiter, subscriptionName); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Formats the rule path, based on the topic path, subscription name and rule name. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="topicPath">The name of the topic, including slashes.</param> |
| | 58 | | /// <param name="subscriptionName">The name of the subscription.</param> |
| | 59 | | /// <param name="ruleName">The name of the rule</param> |
| | 60 | | public static string FormatRulePath(string topicPath, string subscriptionName, string ruleName) |
| | 61 | | { |
| 48 | 62 | | return string.Concat( |
| 48 | 63 | | topicPath, PathDelimiter, |
| 48 | 64 | | SubscriptionsSubPath, PathDelimiter, |
| 48 | 65 | | subscriptionName, PathDelimiter, |
| 48 | 66 | | RulesSubPath, PathDelimiter, ruleName); |
| | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Utility method that creates the name for the transfer dead letter receiver, specified by <paramref name="ent |
| | 71 | | /// </summary> |
| | 72 | | public static string FormatTransferDeadLetterPath(string entityPath) |
| | 73 | | { |
| 0 | 74 | | return string.Concat(entityPath, PathDelimiter, TransferDeadLetterQueueName); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | internal static void CheckValidQueueName(string queueName, string paramName = "queuePath") |
| | 78 | | { |
| 128 | 79 | | CheckValidEntityName(GetPathWithoutBaseUri(queueName), ManagementClientConstants.QueueNameMaximumLength, tru |
| 108 | 80 | | } |
| | 81 | |
|
| | 82 | | internal static void CheckValidTopicName(string topicName, string paramName = "topicPath") |
| | 83 | | { |
| 444 | 84 | | CheckValidEntityName(topicName, ManagementClientConstants.TopicNameMaximumLength, true, paramName); |
| 444 | 85 | | } |
| | 86 | |
|
| | 87 | | internal static void CheckValidSubscriptionName(string subscriptionName, string paramName = "subscriptionName") |
| | 88 | | { |
| 216 | 89 | | CheckValidEntityName(subscriptionName, ManagementClientConstants.SubscriptionNameMaximumLength, false, param |
| 216 | 90 | | } |
| | 91 | |
|
| | 92 | | internal static void CheckValidRuleName(string ruleName, string paramName = "ruleName") |
| | 93 | | { |
| 20 | 94 | | CheckValidEntityName(ruleName, ManagementClientConstants.RuleNameMaximumLength, false, paramName); |
| 20 | 95 | | } |
| | 96 | |
|
| | 97 | | private static void CheckValidEntityName(string entityName, int maxEntityNameLength, bool allowSeparator, string |
| | 98 | | { |
| 808 | 99 | | if (string.IsNullOrWhiteSpace(entityName)) |
| | 100 | | { |
| 0 | 101 | | throw new ArgumentNullException(paramName); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | // and "\" will be converted to "/" on the REST path anyway. Gateway/REST do not |
| | 105 | | // have to worry about the begin/end slash problem, so this is purely a client side check. |
| 808 | 106 | | var tmpName = entityName.Replace(@"\", Constants.PathDelimiter); |
| 808 | 107 | | if (tmpName.Length > maxEntityNameLength) |
| | 108 | | { |
| 20 | 109 | | throw new ArgumentOutOfRangeException(paramName, $@"Entity path '{entityName}' exceeds the '{maxEntityNa |
| | 110 | | } |
| | 111 | |
|
| 788 | 112 | | if (tmpName.StartsWith(Constants.PathDelimiter, StringComparison.OrdinalIgnoreCase) || |
| 788 | 113 | | tmpName.EndsWith(Constants.PathDelimiter, StringComparison.OrdinalIgnoreCase)) |
| | 114 | | { |
| 0 | 115 | | throw new ArgumentException($@"The entity name/path cannot contain '/' as prefix or suffix. The supplied |
| | 116 | | } |
| | 117 | |
|
| 788 | 118 | | if (!allowSeparator && tmpName.Contains(Constants.PathDelimiter)) |
| | 119 | | { |
| 0 | 120 | | throw new ArgumentException($@"The entity name/path contains an invalid character '{Constants.PathDelimi |
| | 121 | | } |
| | 122 | |
|
| 7880 | 123 | | foreach (var uriSchemeKey in ManagementClientConstants.InvalidEntityPathCharacters) |
| | 124 | | { |
| 3152 | 125 | | if (entityName.IndexOf(uriSchemeKey) >= 0) |
| | 126 | | { |
| 0 | 127 | | throw new ArgumentException($@"'{entityName}' contains character '{uriSchemeKey}' which is not allow |
| | 128 | | } |
| | 129 | | } |
| 788 | 130 | | } |
| | 131 | |
|
| | 132 | | private static string GetPathWithoutBaseUri(string entityName) |
| | 133 | | { |
| | 134 | | // Note: on Linux/macOS, "/path" URLs are treated as valid absolute file URLs. |
| | 135 | | // To ensure relative queue paths are correctly rejected on these platforms, |
| | 136 | | // an additional check using IsWellFormedOriginalString() is made here. |
| | 137 | | // See https://github.com/dotnet/corefx/issues/22098 for more information. |
| 128 | 138 | | if (Uri.TryCreate(entityName, UriKind.Absolute, out Uri uriValue) && |
| 128 | 139 | | uriValue.IsWellFormedOriginalString()) |
| | 140 | | { |
| 24 | 141 | | entityName = uriValue.PathAndQuery; |
| 24 | 142 | | return entityName.TrimStart('/'); |
| | 143 | | } |
| 104 | 144 | | return entityName; |
| | 145 | | } |
| | 146 | | } |
| | 147 | | } |