1 /* 2 * hunt-proton: AMQP Protocol library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 13 module hunt.proton.codec.security.SaslOutcomeType; 14 15 import hunt.collection.AbstractList; 16 import hunt.collection.List; 17 import hunt.proton.amqp.Binary; 18 import hunt.proton.amqp.Symbol; 19 import hunt.proton.amqp.UnsignedByte; 20 import hunt.proton.amqp.UnsignedLong; 21 import hunt.proton.amqp.security.SaslCode; 22 import hunt.proton.amqp.security.SaslOutcome; 23 import hunt.proton.codec.AbstractDescribedType; 24 import hunt.proton.codec.DecodeException; 25 import hunt.proton.codec.Decoder; 26 import hunt.proton.codec.DescribedTypeConstructor; 27 import hunt.proton.codec.EncoderImpl; 28 import hunt.Exceptions; 29 import hunt.Object; 30 import std.concurrency : initOnce; 31 import std.conv:to; 32 import hunt.logging; 33 34 class SaslOutcomeWrapper : AbstractList!Object 35 { 36 private SaslOutcome _impl; 37 38 this(SaslOutcome impl) 39 { 40 _impl = impl; 41 } 42 43 44 override 45 public Object get(int index) 46 { 47 48 switch(index) 49 { 50 case 0: 51 return _impl.getCode().getValue(); 52 case 1: 53 return _impl.getAdditionalData(); 54 default: 55 return null; 56 } 57 58 // throw new IllegalStateException("Unknown index " ~ to!string(index)); 59 60 } 61 62 override 63 public int size() 64 { 65 return _impl.getAdditionalData() !is null 66 ? 2 67 : 1; 68 } 69 } 70 71 class SaslOutcomeType : AbstractDescribedType!(SaslOutcome,List!Object) , DescribedTypeConstructor!(SaslOutcome) 72 { 73 //private static Object[] DESCRIPTORS = 74 //{ 75 // UnsignedLong.valueOf(0x0000000000000044L), Symbol.valueOf("amqp:sasl-outcome:list"), 76 //}; 77 78 static Object[] DESCRIPTORS() { 79 __gshared Object[] inst; 80 return initOnce!inst([UnsignedLong.valueOf(0x0000000000000044L), Symbol.valueOf("amqp:sasl-outcome:list")]); 81 } 82 83 static UnsignedLong DESCRIPTOR() { 84 __gshared UnsignedLong inst; 85 return initOnce!inst(UnsignedLong.valueOf(0x0000000000000044L)); 86 } 87 88 //private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000044L); 89 90 this(EncoderImpl encoder) 91 { 92 super(encoder); 93 } 94 95 override 96 public UnsignedLong getDescriptor() 97 { 98 return DESCRIPTOR; 99 } 100 101 override 102 protected List!Object wrap(SaslOutcome val) 103 { 104 return new SaslOutcomeWrapper(val); 105 } 106 107 108 109 110 public SaslOutcome newInstance(Object described) 111 { 112 List!Object l = cast(List!Object) described; 113 114 SaslOutcome o = new SaslOutcome(); 115 116 if(l.isEmpty()) 117 { 118 logError("The code field cannot be omitted"); 119 // throw new DecodeException("The code field cannot be omitted"); 120 } 121 122 switch(2 - l.size()) 123 { 124 125 case 0: 126 o.setAdditionalData( cast(Binary) l.get( 1 ) ); 127 goto case; 128 case 1: 129 o.setCode(SaslCode.valueOf(cast(UnsignedByte) l.get(0))); 130 break; 131 default: 132 break; 133 } 134 135 136 return o; 137 } 138 139 public TypeInfo getTypeClass() 140 { 141 return typeid(SaslOutcome); 142 } 143 144 145 146 public static void register(Decoder decoder, EncoderImpl encoder) 147 { 148 SaslOutcomeType type = new SaslOutcomeType(encoder); 149 // implementationMissing(false); 150 foreach(Object descriptor ; DESCRIPTORS) 151 { 152 decoder.registerDynamic(descriptor, type); 153 } 154 encoder.register(type); 155 } 156 }