1 |
originally posted 2014-08-06 |
Generates a unique namespace that conforms to DNS name format from the input.
Releated: Validate Format of a Domain Name post
Input formats:
If email address, the host part is used.
If URI, the primary domain name is used.
If neither of the above formats then uses the input as is.
Looks in the [Subscription] table of the specified database table for the [Namespace] column to determine uniqueness.
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 |
//LinqPad Query, Language = "C# Program" void Main() { string value = "justin@elementalp.com"; GenerateNamespace(value).Dump(value + " becomes..."); value = "The ABC Company!"; GenerateNamespace(value).Dump(value + " becomes..."); value = "Foo"; GenerateNamespace(value).Dump(value + " becomes..."); } /// <summary> /// Uses an input value as the basis for generating a unique namespace /// If the formatted input name is already in use, a number will be appended to it until a unique namespace results. /// Input value defaults to "ORG" /// Output is made to confirm to DNS name requirements. /// Examines the SQL object "[npt_cloud].[dbo].[Subscription].[Namespace]" to check for uniqueness. /// </summary> public string GenerateNamespace(string value) { //string value = "https://this.that.these.those.elementalp.com"; //string value = "----= The-ElementalP-Company =----"; string result = null; const string sql = @"declare @pattern nvarchar(100) = '%[^0-9A-Za-z\-]%' --filter chars that are not alpha, numerics, or dashes declare @defaultName nvarchar(100) = 'ORG'; --the default namespace name declare @try nvarchar(100) = null if(@ns is null or len(@ns)=0) goto thenext; --remove characters not in the pattern while patindex(@pattern, @ns) > 0 set @ns = stuff(@ns, patindex(@pattern, @ns), 1, ''); set @ns = ltrim(rtrim(@ns)); if(@ns is null or len(@ns)=0) goto thenext; --remove leading dashes while (patindex('-%', @ns) = 1) set @ns = substring(@ns,2,len(@ns)); --remove trailing dashes while (patindex('%-', @ns) = len(@ns)) set @ns = substring(@ns,1,len(@ns)-1); --shorten to 63 chars, make upper case, and trim set @ns = ltrim(rtrim(upper(substring(@ns,1,63)))); thenext: --default empty namespace if(@ns is null or len(@ns)=0) set @ns = @defaultName --goto theend; set @try = @ns declare @num int = 0; while (1=1) begin if exists (select [Namespace] from [dbo].[Subscription] where [Namespace] like @try) begin set @num = @num + 1; set @try = @ns + convert(nvarchar(10),@num) end else break; end theend: select [Namespace] = @try;"; System.Diagnostics.Debug.WriteLine(string.Format("Input: {0}", value!=null ? string.Format("\"{0}\"",value) : "null")); //parse host name from namespace if(!string.IsNullOrWhiteSpace(value))value = ParseNamespace(value); if(value==null) value = string.Empty; using (var connection = new SqlConnection("Data Source=.;Initial Catalog=npt_cloud;Application Name=npt;Integrated Security=true;")) { connection.Open(); using (var command = new SqlCommand(sql, connection)) { var param = new SqlParameter("@ns", System.Data.SqlDbType.NVarChar); param.Value = value; command.Parameters.Add(param); result = command.ExecuteScalar() as string; connection.Close(); return result; } } } /// <summary> /// Parses a namespace from input value. /// If the input value is an email address, the output will be the host name. /// If the input value is a URL, the output is the primary domain name. /// If the input value is not one of the above formats, the input text before the first period (if any) is returned. /// </summary> public static string ParseNamespace(string text) { string ns = null; try { //try get domain as email address var addr = new MailAddress(text); ns = addr.Host; } catch (System.FormatException ex1) { try { //try get domain as uri var uri = new Uri(text); string[] hostParts = uri.Host.Split(new string[1] { "." }, StringSplitOptions.RemoveEmptyEntries); ns = hostParts[hostParts.Length-2]; } catch (System.UriFormatException ex2) { //use id as the domain ns = text; } } if (ns != null) ns = ns.Split(new char[] { '.' }, 2)[0]; return ns; } |