1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.web.soap.client.envelope;
7 
8 import diamond.web.soap.client.envelopebody;
9 
10 /// Wrapper around a soap envelope.
11 final class SoapEnvelope
12 {
13   private:
14   /// The xml version of the envelope.
15   string _xmlVersion;
16   /// The xml encoding of the envelope.
17   string _xmlEncoding;
18   /// The xml soap environment.
19   string _xmlSoapEnvironment;
20   /// The xml xsi.
21   string _xmlXsi;
22   /// The xml xsd.
23   string _xmlXsd;
24   /// The soap body.
25   SoapEnvelopeBody _body;
26 
27   public:
28   final:
29   /**
30   * Creates a new soap envelope.
31   * Params:
32   *   body =               The body of the envelope.
33   *   xmlVersion =         The version of the xml.
34   *   xmlEncoding =        The xml encoding of the envelope.
35   *   xmlSoapEnvironment = The environment of the soap specification.
36   *   xmlXsi =             The xml schema instance.
37   *   xmlXsd =             The xml schema.
38   */
39   this
40   (
41     SoapEnvelopeBody body,
42     string xmlVersion = "1.0",
43     string xmlEncoding = "UTF-8",
44     string xmlSoapEnvironment = "http://www.w3.org/2003/05/soap-envelope",
45     string xmlXsi = "http://www.w3.org/1999/XMLSchema-instance",
46     string xmlXsd = "http://www.w3.org/1999/XMLSchema"
47   )
48   {
49     import diamond.errors.checks;
50 
51     enforce(body !is null, "No soap body specified.");
52 
53     _body = body;
54   }
55 
56   @property
57   {
58     /// Gets the xml version.
59     string xmlVersion() { return _xmlVersion; }
60 
61     /// Gets the xml encoding of the envelope
62     string xmlEncoding() { return _xmlEncoding; }
63 
64     /// Gets the soap body.
65     SoapEnvelopeBody body() { return _body; }
66   }
67 
68   /// Transforms the envelope into a properly formatted soap xml entry.
69   override string toString()
70   {
71     import diamond.xml;
72 
73     auto document = new XmlDocument(new XmlParserSettings);
74 
75     document.xmlVersion = _xmlVersion;
76     document.encoding = _xmlEncoding;
77 
78     auto envelope = new XmlNode(null);
79 
80     document.root = envelope;
81     envelope.name = "soap:Envelope";
82     envelope.addAttribute("xmlns:soap", _xmlSoapEnvironment);
83     envelope.addAttribute("xmlns:xsi", _xmlXsi);
84     envelope.addAttribute("xmlns:xsd", _xmlXsd);
85 
86     auto envelopeHeader = new XmlNode(envelope);
87     envelopeHeader.name = "soap:Header";
88     envelope.addChild(envelopeHeader);
89 
90     auto envelopeBody = new XmlNode(envelope);
91     envelopeBody.name = "soap:Body";
92     envelope.addChild(envelopeBody);
93 
94     auto envelopeMethod = new XmlNode(envelopeBody);
95     envelopeMethod.name = "m:" ~ _body.method;
96     envelopeBody.addChild(envelopeMethod);
97 
98     if (_body.parameters && _body.parameters.length)
99     {
100       foreach (param; _body.parameters)
101       {
102         auto envelopeParam = new XmlNode(envelopeMethod);
103         envelopeMethod.addChild(envelopeParam);
104 
105         envelopeParam.name = "m:" ~ param.name;
106         envelopeParam.text = param.value;
107       }
108     }
109 
110     return document.toString();
111   }
112 }