• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using System.Text;
21 using Grpc.Core.Utils;
22 
23 namespace Grpc.Core
24 {
25     /// <summary>
26     /// A property of an <see cref="AuthContext"/>.
27     /// Note: experimental API that can change or be removed without any prior notice.
28     /// </summary>
29     public class AuthProperty
30     {
31         static readonly Encoding EncodingUTF8 = System.Text.Encoding.UTF8;
32         string name;
33         byte[] valueBytes;
34         string lazyValue;
35 
AuthProperty(string name, byte[] valueBytes)36         private AuthProperty(string name, byte[] valueBytes)
37         {
38             this.name = GrpcPreconditions.CheckNotNull(name);
39             this.valueBytes = GrpcPreconditions.CheckNotNull(valueBytes);
40         }
41 
42         /// <summary>
43         /// Gets the name of the property.
44         /// </summary>
45         public string Name
46         {
47             get
48             {
49                 return name;
50             }
51         }
52 
53         /// <summary>
54         /// Gets the string value of the property.
55         /// </summary>
56         public string Value
57         {
58             get
59             {
60                 return lazyValue ?? (lazyValue = EncodingUTF8.GetString(this.valueBytes));
61             }
62         }
63 
64         /// <summary>
65         /// Gets the binary value of the property.
66         /// </summary>
67         public byte[] ValueBytes
68         {
69             get
70             {
71                 var valueCopy = new byte[valueBytes.Length];
72                 Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
73                 return valueCopy;
74             }
75         }
76 
77         /// <summary>
78         /// Creates an instance of <c>AuthProperty</c>.
79         /// </summary>
80         /// <param name="name">the name</param>
81         /// <param name="valueBytes">the binary value of the property</param>
Create(string name, byte[] valueBytes)82         public static AuthProperty Create(string name, byte[] valueBytes)
83         {
84             GrpcPreconditions.CheckNotNull(valueBytes);
85             var valueCopy = new byte[valueBytes.Length];
86             Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
87             return new AuthProperty(name, valueCopy);
88         }
89 
90         /// <summary>
91         /// Gets the binary value of the property (without making a defensive copy).
92         /// </summary>
93         internal byte[] ValueBytesUnsafe
94         {
95             get
96             {
97                 return valueBytes;
98             }
99         }
100 
101         /// <summary>
102         /// Creates and instance of <c>AuthProperty</c> without making a defensive copy of <c>valueBytes</c>.
103         /// </summary>
CreateUnsafe(string name, byte[] valueBytes)104         internal static AuthProperty CreateUnsafe(string name, byte[] valueBytes)
105         {
106             return new AuthProperty(name, valueBytes);
107         }
108     }
109 }
110