blob: 49765da6396cf28c440ffb48c628b838c776f601 [file] [log] [blame]
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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.
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -080016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using Grpc.Core.Internal;
23using Grpc.Core.Utils;
24
25namespace Grpc.Core
26{
27 /// <summary>
28 /// A property of an <see cref="AuthContext"/>.
29 /// Note: experimental API that can change or be removed without any prior notice.
30 /// </summary>
31 public class AuthProperty
32 {
33 string name;
34 byte[] valueBytes;
35 Lazy<string> value;
36
37 private AuthProperty(string name, byte[] valueBytes)
38 {
39 this.name = GrpcPreconditions.CheckNotNull(name);
40 this.valueBytes = GrpcPreconditions.CheckNotNull(valueBytes);
41 this.value = new Lazy<string>(() => MarshalUtils.GetStringUTF8(this.valueBytes));
42 }
43
44 /// <summary>
45 /// Gets the name of the property.
46 /// </summary>
47 public string Name
48 {
49 get
50 {
51 return name;
52 }
53 }
54
55 /// <summary>
56 /// Gets the string value of the property.
57 /// </summary>
58 public string Value
59 {
60 get
61 {
62 return value.Value;
63 }
64 }
65
66 /// <summary>
67 /// Gets the binary value of the property.
68 /// </summary>
69 public byte[] ValueBytes
70 {
71 get
72 {
73 var valueCopy = new byte[valueBytes.Length];
74 Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
75 return valueCopy;
76 }
77 }
78
79 /// <summary>
80 /// Creates an instance of <c>AuthProperty</c>.
81 /// </summary>
82 /// <param name="name">the name</param>
83 /// <param name="valueBytes">the binary value of the property</param>
84 public static AuthProperty Create(string name, byte[] valueBytes)
85 {
86 GrpcPreconditions.CheckNotNull(valueBytes);
87 var valueCopy = new byte[valueBytes.Length];
88 Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
89 return new AuthProperty(name, valueCopy);
90 }
91
92 /// <summary>
93 /// Gets the binary value of the property (without making a defensive copy).
94 /// </summary>
95 internal byte[] ValueBytesUnsafe
96 {
97 get
98 {
99 return valueBytes;
100 }
101 }
102
103 /// <summary>
104 /// Creates and instance of <c>AuthProperty</c> without making a defensive copy of <c>valueBytes</c>.
105 /// </summary>
106 internal static AuthProperty CreateUnsafe(string name, byte[] valueBytes)
107 {
108 return new AuthProperty(name, valueBytes);
109 }
110 }
111}