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.SaslMechanismsType;
14 
15 import hunt.collection.Collections;
16 import hunt.collection.List;
17 import hunt.proton.amqp.Symbol;
18 import hunt.proton.amqp.UnsignedLong;
19 import hunt.proton.amqp.security.SaslMechanisms;
20 import hunt.proton.codec.AbstractDescribedType;
21 import hunt.proton.codec.DecodeException;
22 import hunt.proton.codec.Decoder;
23 import hunt.proton.codec.DescribedTypeConstructor;
24 import hunt.proton.codec.EncoderImpl;
25 import hunt.logging;
26 import hunt.Object;
27 import std.concurrency : initOnce;
28 import hunt.collection.ArrayList;
29 import hunt.Exceptions;
30 
31 class SaslMechanismsType : AbstractDescribedType!(SaslMechanisms,List!(Object)) , DescribedTypeConstructor!(SaslMechanisms)
32 {
33     //private static Object[] DESCRIPTORS =
34     //{
35     //    UnsignedLong.valueOf(0x0000000000000040L), Symbol.valueOf("amqp:sasl-mechanisms:list"),
36     //};
37 
38    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000040L);
39 
40     static Object[]  DESCRIPTORS() {
41         __gshared Object[]  inst;
42         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000040L), Symbol.valueOf("amqp:sasl-mechanisms:list")]);
43     }
44 
45     static UnsignedLong  DESCRIPTOR() {
46         __gshared UnsignedLong  inst;
47         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000040L));
48     }
49 
50     this(EncoderImpl encoder)
51     {
52         super(encoder);
53     }
54 
55     override
56     public UnsignedLong getDescriptor()
57     {
58         return DESCRIPTOR;
59     }
60 
61 
62     override
63     protected List!(Object) wrap(SaslMechanisms val)
64     {
65          List!(Object) lst = new ArrayList!(Object);
66          foreach (Symbol v ; val.getSaslServerMechanisms())
67          {
68                 lst.add(v);
69          }
70          return lst;
71         //return Collections.singletonList!(Symbol)(val.getSaslServerMechanisms());
72     }
73 
74     public SaslMechanisms newInstance(Object described)
75     {
76         List!Object l = cast(List!Object) described;
77 
78         SaslMechanisms o = new SaslMechanisms();
79 
80         if(l.isEmpty())
81         {
82             logError("The sasl-server-mechanisms field cannot be omitted");
83            // throw new DecodeException("The sasl-server-mechanisms field cannot be omitted");
84         }
85 
86         List!Symbol tmp = new ArrayList!Symbol;
87 
88         Object val0 = l.get( 0 );
89         List!Object ls = cast(List!Object) val0;
90         if(ls !is null)
91         {
92             foreach(Object m ; ls)
93             {
94               Symbol s = cast(Symbol)m;
95               if (s !is null)
96               {
97                   tmp.add(s);
98               }
99             }
100         }else
101         {
102           tmp.add(cast(Symbol) val0);
103         }
104         //if( val0 == null || val0.getClass().isArray() )
105         //{
106         //    o.setSaslServerMechanisms( (Symbol[]) val0 );
107         //}
108         //else
109         //{
110         //    o.setSaslServerMechanisms( (Symbol) val0 );
111         //}
112 
113 
114      //   tmp.add(Symbol.valueOf("ANONYMOUS"));
115 
116 
117 
118         //foreach(Object ob;l)
119         //{
120         //    Symbol v = cast(Symbol)ob;
121         //    if (v is null)
122         //    {
123         //        logError("5555555555555555555555555555");
124         //    }
125         //    tmp.add(v);
126         //}
127 
128        // Object val0 = l.get( 0 );
129         //if( val0 is null || val0.getClass().isArray() )
130         //{
131         //    o.setSaslServerMechanisms( (Symbol[]) val0 );
132         //}
133         //else
134         //{
135         o.setSaslServerMechanisms( tmp );
136 //        }
137 
138         return o;
139     }
140 
141     public TypeInfo getTypeClass()
142     {
143         return typeid(SaslMechanisms);
144     }
145 
146 
147 
148     public static void register(Decoder decoder, EncoderImpl encoder)
149     {
150         SaslMechanismsType type = new SaslMechanismsType(encoder);
151        // implementationMissing(false);
152         foreach(Object descriptor ; DESCRIPTORS)
153         {
154             decoder.registerDynamic(descriptor, type);
155         }
156         encoder.register(type);
157     }
158 
159 
160 }