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.SaslResponseType;
14 
15 import hunt.collection.Collections;
16 import hunt.collection.List;
17 import hunt.proton.amqp.Binary;
18 import hunt.proton.amqp.Symbol;
19 import hunt.proton.amqp.UnsignedLong;
20 import hunt.proton.amqp.security.SaslResponse;
21 import hunt.proton.codec.AbstractDescribedType;
22 import hunt.proton.codec.DecodeException;
23 import hunt.proton.codec.Decoder;
24 import hunt.proton.codec.DescribedTypeConstructor;
25 import hunt.proton.codec.EncoderImpl;
26 import hunt.Exceptions;
27 import hunt.Object;
28 import hunt.logging;
29 import std.concurrency : initOnce;
30 
31 class SaslResponseType : AbstractDescribedType!(SaslResponse, List!Binary) , DescribedTypeConstructor!(SaslResponse)
32 {
33     //private static Object[] DESCRIPTORS =
34     //{
35     //    UnsignedLong.valueOf(0x0000000000000043L), Symbol.valueOf("amqp:sasl-response:list"),
36     //};
37 
38    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000043L);
39 
40 
41     static Object[]  DESCRIPTORS() {
42         __gshared Object[]  inst;
43         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000043L), Symbol.valueOf("amqp:sasl-response:list")]);
44     }
45 
46     static UnsignedLong  DESCRIPTOR() {
47         __gshared UnsignedLong  inst;
48         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000043L));
49     }
50 
51 
52     this(EncoderImpl encoder)
53     {
54         super(encoder);
55     }
56 
57     override
58     public UnsignedLong getDescriptor()
59     {
60         return DESCRIPTOR;
61     }
62 
63     override
64     protected List!Binary wrap(SaslResponse val)
65     {
66         return Collections.singletonList!Binary(val.getResponse());
67     }
68 
69 
70     public SaslResponse newInstance(Object described)
71     {
72         List!Object l = cast(List!Object) described;
73 
74         SaslResponse o = new SaslResponse();
75 
76         if(l.isEmpty())
77         {
78             logError("The response field cannot be omitted");
79            // throw new DecodeException("The response field cannot be omitted");
80         }
81 
82         switch(1 - l.size())
83         {
84 
85             case 0:
86                 o.setResponse( cast(Binary) l.get( 0 ) );
87                 break;
88             default:
89                 break;
90         }
91 
92 
93         return o;
94     }
95 
96     public TypeInfo getTypeClass()
97     {
98         return typeid(SaslResponse);
99     }
100 
101 
102 
103     static void register(Decoder decoder, EncoderImpl encoder)
104     {
105         SaslResponseType type = new SaslResponseType(encoder);
106        // implementationMissing(false);
107         foreach(Object descriptor ; DESCRIPTORS)
108         {
109             decoder.registerDynamic(descriptor, type);
110         }
111         encoder.register(type);
112     }
113 }