File: web_elements.adb

package info (click to toggle)
libaws 2.2dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,624 kB
  • ctags: 1,173
  • sloc: ada: 61,829; ansic: 6,483; makefile: 1,282; xml: 196; sh: 119; java: 112; python: 66; sed: 40
file content (237 lines) | stat: -rw-r--r-- 8,151 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                            Copyright (C) 2004                            --
--                                ACT-Europe                                --
--                                                                          --
--  This library is free software; you can redistribute it and/or modify    --
--  it under the terms of the GNU General Public License as published by    --
--  the Free Software Foundation; either version 2 of the License, or (at   --
--  your option) any later version.                                         --
--                                                                          --
--  This library 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       --
--  along with this library; if not, write to the Free Software Foundation, --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Text_IO;

with AWS.Config.Set;
with AWS.Default;
with AWS.Dispatchers.Callback;
with AWS.MIME;
with AWS.OS_Lib;
with AWS.Parameters;
with AWS.Response;
with AWS.Server;
with AWS.Services.Callbacks;
with AWS.Services.Dispatchers.URI;
with AWS.Status;
with AWS.Templates;
with AWS.Utils;

with Web_Elements_Containers;

procedure Web_Elements is

   use Ada;
   use AWS;
   use Web_Elements_Containers;

   WWW_Root : constant String := "../web_elements";

   C        : Natural := 0;

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

   function CB (Request : in Status.Data) return Response.Data is
      URI      : constant String := Status.URI (Request);
      Filename : constant String := URI (URI'First + 1 .. URI'Last);
      P_List   : constant Parameters.List := Status.Parameters (Request);
   begin
      if URI = "/" then
         return Response.Build
           (MIME.Text_HTML,
            Message_Body => Templates.Parse ("we_library.html"));

      elsif URI = "/clickme" then
         C := C + 1;
         return Response.Build
           (MIME.Text_HTML,
            Message_Body => Natural'Image (C) &
               ". response for the click me button!");

      elsif URI = "/select_color" then
         declare
            Color : constant String := Parameters.Get (P_List, "select_color");
         begin
            return Response.Build
              (MIME.Text_HTML,
               "<p>You have chosen the " &
               "<span class=""" & Color & """><b>" & Color & "</b></span> " &
               "color</p>");
         end;

      elsif URI = "/data" then
         C := C + 1;
         return Response.Build
           (MIME.Text_HTML,
            Message_Body => Natural'Image (C) &
              ". some data from the server, and the field value '" &
              Parameters.Get (P_List, "field") & ''');

      elsif URI = "/small_form" then
         declare
            Color : constant String := Parameters.Get (P_List, "form_color");
            Size  : constant String := Parameters.Get (P_List, "size");
         begin
            return Response.Build
              (MIME.Text_HTML,
               Message_Body =>
                 "You have ordered a " &
               "<span class=""" & Size & """><b>" & Size & "</b></span> " &
               "<span class=""" & Color & """><b>" & Color & "</b></span> " &
               "pant.");
         end;

      elsif URI = "/db_sel1" then
         return Response.Build
           (MIME.Text_HTML,
            String'(Templates.Parse ("we_ajax_group.html")));

      elsif URI = "/db_sel2" then
         return Response.Build
           (MIME.Text_HTML,
            String'(Templates.Parse
              ("we_ajax_user.html",
                 (1 => Templates.Assoc ("GROUP_V", Get_Groups)))));

      elsif URI = "/g_action" then
         Add_Group (Parameters.Get (P_List, "g_name"));
         return Response.Build
           (MIME.Text_HTML,
            String'(Templates.Parse ("we_ajax_group.html")));

      elsif URI = "/u_action" then
         Add_User (Parameters.Get (P_List, "u_name"));
         return Response.Build
           (MIME.Text_HTML,
            String'(Templates.Parse
              ("we_ajax_user.html",
                 (1 => Templates.Assoc ("GROUP_V", Get_Groups)))));

      elsif URI = "/u_list" then
         return Response.Build
           (MIME.Text_HTML,
            String'(Templates.Parse
              ("we_ajax_user.html",
                 (1 => Templates.Assoc ("LIST", True),
                  2 => Templates.Assoc ("USER_V", Get_Users)))));

      elsif URI = "/g_list" then
         return Response.Build
           (MIME.Text_HTML,
            String'(Templates.Parse
              ("we_ajax_group.html",
                 (1 => Templates.Assoc ("LIST", True),
                  2 => Templates.Assoc ("GROUP_V", Get_Groups)))));

      elsif URI = "/xml_action" then
         C := C + 1;
         return Response.Build
           (MIME.Text_XML,
            String'(Templates.Parse
              ("xml_action.txml",
                 (1 => Templates.Assoc ("COUNTER", C)))));

      elsif URI = "/xml_get_list" then
         C := C + 1;
         declare
            use type Templates.Tag;
            Result : constant Parameters.VString_Array :=
                       Parameters.Get_Values (P_List, "tolist[]");
            List_V : Templates.Tag;
         begin
            for K in Result'Range loop
               List_V := List_V & Result (K);
            end loop;

            return Response.Build
              (MIME.Text_XML,
               String'(Templates.Parse
                 ("xml_get_list.txml",
                    (1 => Templates.Assoc ("COUNTER", C),
                     2 => Templates.Assoc ("LIST_V", List_V)))));
         end;

      elsif URI'Length > 4
        and then URI (URI'First .. URI'First + 3) = "/xml"
      then
         return AWS.Response.File
           (MIME.Text_XML,
            Filename => URI (URI'First + 1 .. URI'Last) & ".xml");

      elsif OS_Lib.Is_Regular_File (WWW_Root & URI) then
         return AWS.Response.File
           (MIME.Content_Type (Filename),
            Filename => WWW_Root & URI);

      else
         return AWS.Response.Build
           (MIME.Text_HTML, Message_Body => Templates.Parse (Filename));
      end if;
   end CB;

   --------------
   -- CB_Icons --
   --------------

   function CB_Icons is
     new Services.Callbacks.File ("/we_icons/", WWW_Root & "/icons/");

   -----------
   -- CB_JS --
   -----------

   function CB_JS is
     new Services.Callbacks.File ("/we_js/", WWW_Root & "/javascripts/");

   Port : constant Integer := 2400;

   WS   : Server.HTTP;
   Conf : Config.Object := Config.Get_Current;
   Disp : Services.Dispatchers.URI.Handler;

begin
   Text_IO.Put_Line
     ("Connect to http://localhost:" & Utils.Image (Port) & "/");

   Config.Set.Server_Port (Conf, Port);
   Config.Set.Max_Connection (Conf, 1);
   Config.Set.WWW_Root (Conf, WWW_Root);

   Services.Dispatchers.URI.Register
     (Disp, "/we_icons/", CB_Icons'Unrestricted_Access, Prefix => True);
   Services.Dispatchers.URI.Register
     (Disp, "/we_js/", CB_JS'Unrestricted_Access, Prefix => True);

   Services.Dispatchers.URI.Register_Default_Callback
     (Disp, Dispatchers.Callback.Create (CB'Unrestricted_Access));

   Server.Start
     (WS,
      Config     => Conf,
      Dispatcher => Disp);

   Server.Wait (AWS.Server.Q_Key_Pressed);

   Server.Shutdown (WS);
end Web_Elements;