aws_24.0.0_2b75fe6d/regtests/0333_soap_optional_item/optional.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                        Copyright (C) 2021, AdaCore                       --
--                                                                          --
--  This is free software;  you can redistribute it  and/or modify it       --
--  under terms of the  GNU General Public License as published  by the     --
--  Free Software  Foundation;  either version 3,  or (at your option) any  --
--  later version.  This software is distributed in the hope  that it will  --
--  be useful, but WITHOUT ANY WARRANTY;  without even the implied warranty --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     --
--  General Public License for  more details.                               --
--                                                                          --
--  You should have  received  a copy of the GNU General  Public  License   --
--  distributed  with  this  software;   see  file COPYING3.  If not, go    --
--  to http://www.gnu.org/licenses for a complete copy of the license.      --
------------------------------------------------------------------------------

with Ada.Calendar;
with Ada.Integer_Text_IO;
with Ada.Strings.Unbounded;
with Ada.Text_IO;

with AWS.Config.Set;
with AWS.MIME;
with AWS.Net;
with AWS.Response;
with AWS.Server.Status;
with AWS.Status;
with AWS.Translator;
with SOAP.Client;
with SOAP.Message.Payload;
with SOAP.Message.Response;
with SOAP.Message.XML;

with Toptional.Client;
with Toptional.Server;
with Toptional.Types;

procedure Optional is

   use Ada;
   use Ada.Strings.Unbounded;
   use AWS;

   use Toptional.Types;

   H_Server : Server.HTTP;

   function "+" (Str : String) return Unbounded_String
     renames To_Unbounded_String;

   package FIO is new Text_IO.Float_IO (Float);

   ------------
   -- Output --
   ------------

   procedure Output (S : SOAPStruct_Type) is
   begin
      Integer_Text_IO.Put (S.varInt); Text_IO.New_Line;
      FIO.Put (S.VarFloat, Exp => 0, Aft => 2); Text_IO.New_Line;
      Text_IO.Put_Line (To_String (S.varString));
   end Output;

   ---------------------
   -- T_echoString_CB --
   ---------------------

   function T_echoString_CB (inputString : String) return String is
   begin
      return inputString;
   end T_echoString_CB;

   function echoString_CB is
      new Toptional.Server.echoString_CB (T_echoString_CB);

   ------------------
   -- T_echoString --
   ------------------

   procedure T_echoString is
      Res : constant String := Toptional.Client.echoString
              ("This is the real value for the string!");
   begin
      Text_IO.Put_Line ("Echo String");

      Text_IO.Put_Line (Res);
      Text_IO.New_Line;
   end T_echoString;

   ---------------------
   -- T_echoStruct_CB --
   ---------------------

   function T_echoStruct_CB
     (inputStruct : SOAPStruct_Type)
      return echoStruct_Result is
   begin
      return inputStruct;
   end T_echoStruct_CB;

   function echoStruct_CB is
     new Toptional.Server.echoStruct_CB (T_echoStruct_CB);

   ------------------
   -- T_echoStruct --
   ------------------

   procedure T_echoStruct is
      Struct : constant SOAPStruct_Type := (6, 6.6, +"666");
      Res    : constant echoStruct_Result :=
                 Toptional.Client.echoStruct (Struct);
   begin
      Text_IO.Put_Line ("Echo Struct");
      Output (Res);
      Text_IO.New_Line;
   end T_echoStruct;

   --------
   -- CB --
   --------

   function CB (Request : Status.Data) return Response.Data is
      SOAPAction : constant String := Status.SOAPAction (Request);
      P_Str      : aliased constant String := AWS.Status.Payload (Request);
      Payload    : constant SOAP.Message.Payload.Object :=
                     SOAP.Message.XML.Load_Payload
                       (P_Str, Schema => Toptional.Schema);
      Proc       : constant String :=
                     SOAP.Message.Payload.Procedure_Name (Payload);
   begin
      Text_IO.Put_Line ("@ " & Proc);

      if Proc = "echoString" then
         return echoString_CB (SOAPAction, Payload, Request);

      elsif Proc = "echoStruct" then
         return echoStruct_CB (SOAPAction, Payload, Request);

      else
         return Response.Build
           (MIME.Text_HTML, "Not a SOAP request, Proc=" & Proc);
      end if;
   end CB;

   CNF : Config.Object := Config.Get_Current;

begin
   Config.Set.Server_Name     (CNF, "WSDL Optional Server");
   Config.Set.Server_Host     (CNF, "localhost");
   Config.Set.Server_Port     (CNF, Toptional.Server.Port);
   Config.Set.Protocol_Family (CNF, "FAMILY_INET");

   Server.Start (H_Server, CB'Unrestricted_Access, CNF);

   if Net.IPv6_Available then
      Server.Add_Listening
        (H_Server, "localhost", Toptional.Server.Port, Net.FAMILY_INET6);
   end if;

   T_echoString;
   T_echoStruct;

   --  Now check struct with a xsi:nil (optional value)

   declare
      N   : constant String := String'(1 => ASCII.CR) & ASCII.LF;
      Mes : aliased constant String :=
               "<?xml version='1.0' encoding='UTF-8'?>" & N
               & "<soapenv:Envelope soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:soap-enc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">" & N
               & "<soapenv:Body>" & N
               & "<ns2:echoStruct xmlns:ns2=""http://nsoptional.org/"""
               & " xmlns:ns1=""http://nsoptional.org/xsd"">" & N
               & "<ns1:inputStruct xsi:type=""ns1:SOAPStruct"">"
               &  "<ns1:varInt xsi:type=""xsd:int"">7</ns1:varInt>"
               &  "<ns1:varFloat xsi:type=""xsd:float"">7.0</ns1:varFloat>"
               &  "<ns1:varString xsi:type=""xsd:string"" xsi:nil=""true""/>"
               & "</ns1:inputStruct>"
               & "</ns2:echoStruct>" & N
               & "</soapenv:Body>" & N
               & "</soapenv:Envelope>";
      Pl : constant SOAP.Message.Payload.Object :=
             SOAP.Message.XML.Load_Payload (Mes);
      R  : constant SOAP.Message.Response.Object'Class :=
             SOAP.Client.Call
               (URL        => AWS.Server.Status.Local_URL (H_Server),
                P          => Pl,
                SOAPAction => "http://nsoptional.org/");
   begin
      Text_IO.Put_Line (SOAP.Message.XML.Image (Pl));
      Text_IO.Put_Line (SOAP.Message.XML.Image (R));
   end;

   Server.Shutdown (H_Server);
end Optional;