-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdetergent.erl
681 lines (579 loc) · 25.8 KB
/
detergent.erl
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
%%%-------------------------------------------------------------------
%%% Created : 29 Nov 2006 by Torbjorn Tornkvist <[email protected]>
%%% Author : Willem de Jong ([email protected]).
%%% Desc. : Common SOAP code.
%%%-------------------------------------------------------------------
%%% modified (WdJ, May 2007): deal with imports in the WSDL.
%%% modified (WdJ, August 2007): the WSDL can contain more than 1 schema
-module(detergent).
-export([initModel/1, initModel/2,
initModelFile/1,
config_file_xsd/0,
call/3, call/4, call/5, call/6, call/7,
call_attach/4, call_attach/5, call_attach/6, call_attach/8,
write_hrl/2, write_hrl/3,
findHeader/2,
parseMessage/2,
makeFault/2,
is_wsdl/1, wsdl_model/1, wsdl_op_service/1,
wsdl_op_port/1, wsdl_op_operation/1,
wsdl_op_binding/1, wsdl_op_address/1,
wsdl_op_action/1, wsdl_operations/1,
get_operation/2
]).
%%% For testing...
-export([qtest/0]).
-include_lib("detergent.hrl").
-define(HTTP_REQ_TIMEOUT, 20000).
%%-define(dbg(X,Y),
%% error_logger:info_msg("*dbg ~p(~p): " X,
%% [?MODULE, ?LINE | Y])).
-define(dbg(X,Y), true).
-record(soap_config, {atts, xsd_path, user_module, wsdl_file, add_files}).
-record(xsd_file, {atts, name, prefix, import_specs}).
-record(import_specs, {atts, namespace, prefix, location}).
%%%
%%% Writes the header file (record definitions) for a WSDL file
%%%
write_hrl(WsdlURL, Output) when is_list(WsdlURL) ->
write_hrl(initModel(WsdlURL), Output);
write_hrl(#wsdl{model = Model}, Output) when is_list(Output) ->
erlsom:write_hrl(Model, Output).
write_hrl(WsdlURL, Output, Prefix) when is_list(WsdlURL),is_list(Prefix) ->
write_hrl(initModel(WsdlURL, Prefix), Output).
%%% For testing only...
qtest() ->
call("http://www.webservicex.net/WeatherForecast.asmx?WSDL",
"GetWeatherByPlaceName",
["Boston"]).
%%% --------------------------------------------------------------------
%%% Access functions
%%% --------------------------------------------------------------------
is_wsdl(Wsdl) when is_record(Wsdl,wsdl) -> true;
is_wsdl(_) -> false.
wsdl_operations(#wsdl{operations = Ops}) -> Ops.
wsdl_model(#wsdl{model = Model}) -> Model.
wsdl_op_service(#operation{service = Service}) -> Service.
wsdl_op_port(#operation{port = Port}) -> Port.
wsdl_op_operation(#operation{operation = Op}) -> Op.
wsdl_op_binding(#operation{binding = Binding}) -> Binding.
wsdl_op_address(#operation{address = Address}) -> Address.
wsdl_op_action(#operation{action = Action}) -> Action.
%%% --------------------------------------------------------------------
%%% For Quick deployment
%%% --------------------------------------------------------------------
call(Wsdl, Operation, ListOfData) ->
call(Wsdl, Operation, ListOfData, #call_opts{}).
call(WsdlURL, Operation, ListOfData, #call_opts{prefix=Prefix}=CallOpts)
when is_list(WsdlURL) ->
Wsdl = initModel(WsdlURL, Prefix),
call(Wsdl, Operation, ListOfData, CallOpts);
call(Wsdl, Operation, ListOfData, #call_opts{prefix=Prefix}=CallOpts) when is_record(Wsdl, wsdl) ->
case get_operation(Wsdl#wsdl.operations, Operation) of
{ok, Op} ->
Msg = mk_msg(Prefix, Operation, ListOfData),
call(Wsdl, Operation, Op#operation.port,
Op#operation.service, [], Msg, CallOpts);
Else ->
Else
end;
%%% --------------------------------------------------------------------
%%% Takes the actual records for the Header and Body message.
%%% --------------------------------------------------------------------
call(Wsdl, Operation, Header, Msg) ->
call(Wsdl, Operation, Header, Msg, #call_opts{}).
call(WsdlURL, Operation, Header, Msg, #call_opts{prefix=Prefix}=CallOpts)
when is_list(WsdlURL) ->
Wsdl = initModel(WsdlURL, Prefix),
call(Wsdl, Operation, Header, Msg, CallOpts);
call(Wsdl, Operation, Header, Msg, CallOpts) when is_record(Wsdl, wsdl) ->
case get_operation(Wsdl#wsdl.operations, Operation) of
{ok, Op} ->
call(Wsdl, Operation, Op#operation.port, Op#operation.service,
Header, Msg, CallOpts);
Else ->
Else
end.
mk_msg(Prefix, Operation, ListOfData) ->
list_to_tuple([list_to_atom(Prefix++":"++Operation), % record name
[] % anyAttribs
| ListOfData]). % rest of record data
get_operation([#operation{operation = X} = Op|_], X) ->
{ok, Op};
get_operation([_|T], Op) ->
get_operation(T, Op);
get_operation([], _Op) ->
{error, "operation not found"}.
%%% --------------------------------------------------------------------
%%% Make a SOAP request (no attachments)
%%% --------------------------------------------------------------------
call(Wsdl, Operation, Port, Service, Headers, Message) ->
call(Wsdl, Operation, Port, Service, Headers, Message, #call_opts{}).
call(Wsdl, Operation, Port, Service, Headers, Message, CallOpts) ->
call_attach(Wsdl, Operation, Port, Service, Headers, Message, [], CallOpts).
%%% --------------------------------------------------------------------
%%% For Quick deployment (with attachments)
%%% --------------------------------------------------------------------
call_attach(Wsdl, Operation, ListOfData, Attachments) ->
call_attach(Wsdl, Operation, ListOfData, Attachments, #call_opts{}).
call_attach(WsdlURL, Operation, ListOfData, Attachments, #call_opts{prefix=Prefix}=CallOpts)
when is_list(WsdlURL) ->
Wsdl = initModel(WsdlURL, Prefix),
call_attach(Wsdl, Operation, ListOfData, Attachments, CallOpts);
call_attach(Wsdl, Operation, ListOfData, Attachments, #call_opts{prefix=Prefix}=CallOpts)
when is_record(Wsdl, wsdl) ->
case get_operation(Wsdl#wsdl.operations, Operation) of
{ok, Op} ->
Msg = mk_msg(Prefix, Operation, ListOfData),
call_attach(Wsdl, Operation, Op#operation.port,
Op#operation.service, [], Msg, Attachments, CallOpts);
Else ->
Else
end.
%%% --------------------------------------------------------------------
%%% Takes the actual records for the Header and Body message
%%% (with attachments)
%%% --------------------------------------------------------------------
call_attach(WsdlURL, Operation, Header, Msg, Attachments, #call_opts{prefix=Prefix}=CallOpts)
when is_list(WsdlURL) ->
Wsdl = initModel(WsdlURL, Prefix),
call_attach(Wsdl, Operation, Header, Msg, Attachments, CallOpts);
call_attach(Wsdl, Operation, Header, Msg, Attachments, CallOpts)
when is_record(Wsdl, wsdl) ->
case get_operation(Wsdl#wsdl.operations, Operation) of
{ok, Op} ->
call_attach(Wsdl, Operation, Op#operation.port,
Op#operation.service,
Header, Msg, Attachments, CallOpts);
Else ->
Else
end.
%%% --------------------------------------------------------------------
%%% Make a SOAP request (with attachments)
%%% --------------------------------------------------------------------
call_attach(#wsdl{operations = Operations, model = Model},
Operation, Port, Service, Headers, Message, Attachments,
#call_opts{url=Url, http_headers=HttpHeaders,
http_client_options=HttpClientOptions,
request_logger=RequestLogger,
response_logger=ResponseLogger}) ->
%% find the operation
case findOperation(Operation, Port, Service, Operations) of
#operation{address = Address, action = SoapAction} ->
%% Add the Soap envelope
Envelope = mk_envelope(Message, Headers),
%% Encode the message
case erlsom:write(Envelope, Model) of
{ok, XmlMessage} ->
RequestLogger(XmlMessage),
{ContentType, Request} =
make_request_body(XmlMessage, Attachments),
?dbg("+++ Request = ~p~n", [Request]),
URL = case Url of
undefined ->
Address;
_ ->
Url
end,
HttpRes = http_request(URL, SoapAction, Request,
HttpClientOptions, HttpHeaders,
ContentType),
?dbg("+++ HttpRes = ~p~n", [HttpRes]),
case HttpRes of
{ok, _Code, _ReturnHeaders, Body} ->
ResponseLogger(Body),
parseMessage(Body, Model);
Error ->
%% in case of HTTP error: return
%% {error, description}
Error
end;
{error, EncodingError} ->
{error, {encoding_error, EncodingError}}
end;
false ->
{error, {unknown_operation, Operation}}
end.
%%%
%%% returns {ok, Header, Body} | {error, Error}
%%%
parseMessage(Message, #wsdl{model = Model}) ->
parseMessage(Message, Model);
%%
parseMessage(Message, Model) ->
case erlsom:scan(Message, Model) of
{ok, #'soap:Envelope'{'Body' = #'soap:Body'{choice = Body},
'Header' = undefined}, _} ->
{ok, undefined, Body};
{ok, #'soap:Envelope'{'Body' = #'soap:Body'{choice = Body},
'Header' = #'soap:Header'{choice = Header}}, _} ->
{ok, Header, Body};
{error, ErrorMessage} ->
{error, {decoding, ErrorMessage}}
end.
findOperation(_Operation, _Port, _Service, []) ->
false;
findOperation(Operation, Port, Service,
[Op = #operation{operation = Operation,
port = Port, service = Service} | _]) ->
Op;
findOperation(Operation, Port, Service, [#operation{} | Tail]) ->
findOperation(Operation, Port, Service, Tail).
mk_envelope(M, H) when is_tuple(M) -> mk_envelope([M], H);
mk_envelope(M, H) when is_tuple(H) -> mk_envelope(M, [H]);
%%
mk_envelope(Messages, []) when is_list(Messages) ->
#'soap:Envelope'{'Body' = #'soap:Body'{choice = Messages}};
mk_envelope(Messages, Headers) when is_list(Messages),is_list(Headers) ->
#'soap:Envelope'{'Body' = #'soap:Body'{choice = Messages},
'Header' = #'soap:Header'{choice = Headers}}.
%%% --------------------------------------------------------------------
%%% Parse a WSDL file and return a 'Model'
%%% --------------------------------------------------------------------
initModel(WsdlFile) ->
initModel(WsdlFile, ?DEFAULT_PREFIX).
initModel(WsdlFile, Prefix) ->
PrivDir = priv_dir(),
initModel2(WsdlFile, Prefix, PrivDir, undefined, undefined).
initModelFile(ConfigFile) ->
{ok, ConfigSchema} = erlsom:compile_xsd(config_file_xsd()),
%% read (parse) the config file
{ok, Config, _} = erlsom:scan_file(ConfigFile, ConfigSchema),
#soap_config{xsd_path = XsdPath,
wsdl_file = Wsdl,
add_files = AddFiles} = Config,
#xsd_file{name = WsdlFile, prefix = Prefix, import_specs = Import} = Wsdl,
initModel2(WsdlFile, Prefix, XsdPath, Import, AddFiles).
priv_dir() ->
case code:priv_dir(detergent) of
{error, bad_name} ->
filename:join([filename:dirname(code:which(detergent)),"..", "priv"]);
A ->
A
end.
initModel2(WsdlFile, Prefix, Path, Import, AddFiles) ->
WsdlName = filename:join([Path, "wsdl.xsd"]),
IncludeWsdl = {"http://schemas.xmlsoap.org/wsdl/", "wsdl", WsdlName},
{ok, WsdlModel} = erlsom:compile_xsd_file(filename:join([Path, "soap.xsd"]),
[{prefix, "soap"},
{include_files, [IncludeWsdl]}]),
%% add the xsd model (since xsd is also used in the wsdl)
WsdlModel2 = erlsom:add_xsd_model(WsdlModel),
IncludeDir = filename:dirname(WsdlFile),
Options = [{dir_list, [IncludeDir]} | makeOptions(Import)],
%% parse Wsdl
{Model, Operations} = parseWsdls([WsdlFile], Prefix, WsdlModel2, Options, {undefined, []}),
%% TODO: add files as required
%% now compile envelope.xsd, and add Model
{ok, EnvelopeModel} = erlsom:compile_xsd_file(filename:join([Path, "envelope.xsd"]),
[{prefix, "soap"}]),
SoapModel = erlsom:add_model(EnvelopeModel, Model),
SoapModel2 = addModels(AddFiles, SoapModel),
#wsdl{operations = Operations, model = SoapModel2}.
%%% --------------------------------------------------------------------
%%% Parse a list of WSDLs and import (recursively)
%%% Returns {Model, Operations}
%%% --------------------------------------------------------------------
parseWsdls([], _Prefix, _WsdlModel, _Options, Acc) ->
Acc;
parseWsdls([WsdlFile | Tail], Prefix, WsdlModel, Options, {AccModel, AccOperations}) ->
{ok, WsdlFileContent} = get_url_file(rmsp(WsdlFile)),
{ok, ParsedWsdl, _} = erlsom:scan(WsdlFileContent, WsdlModel),
%% get the xsd elements from this model, and hand it over to erlsom_compile.
Xsds = getXsdsFromWsdl(ParsedWsdl),
%% Now we need to build a list: [{Namespace, Xsd, Prefix}, ...] for all the Xsds in the WSDL.
%% This list is used when a schema inlcudes one of the other schemas. The AXIS java2wsdl
%% generates wsdls that depend on this feature.
ImportList = makeImportList(Xsds, []),
%% TODO: pass the right options here
Model2 = addSchemas(Xsds, AccModel, Prefix, Options, ImportList),
Ports = getPorts(ParsedWsdl),
Operations = getOperations(ParsedWsdl, Ports),
Imports = getImports(ParsedWsdl),
Acc2 = {Model2, Operations ++ AccOperations},
%% process imports (recursively, so that imports in the imported files are
%% processed as well).
%% For the moment, the namespace is ignored on operations etc.
%% this makes it a bit easier to deal with imported wsdl's.
Acc3 = parseWsdls(Imports, Prefix, WsdlModel, Options, Acc2),
parseWsdls(Tail, Prefix, WsdlModel, Options, Acc3).
%%% --------------------------------------------------------------------
%%% build a list: [{Namespace, Xsd}, ...] for all the Xsds in the WSDL.
%%% This list is used when a schema inlcudes one of the other schemas. The AXIS java2wsdl
%%% generates wsdls that depend on this feature.
makeImportList([], Acc) ->
Acc;
makeImportList([ Xsd | Tail], Acc) ->
makeImportList(Tail, [{erlsom_lib:getTargetNamespaceFromXsd(Xsd),
undefined, Xsd} | Acc]).
%%% --------------------------------------------------------------------
%%% compile each of the schemas, and add it to the model.
%%% Returns Model
%%% (TODO: using the same prefix for all XSDS makes no sense)
%%% --------------------------------------------------------------------
addSchemas([], AccModel, _Prefix, _Options, _ImportList) ->
AccModel;
addSchemas([Xsd| Tail], AccModel, Prefix, Options, ImportList) ->
Model2 = case Xsd of
undefined ->
AccModel;
_ ->
{ok, Model} =
erlsom_compile:compile_parsed_xsd(Xsd,
[{prefix, Prefix},
{include_files, ImportList} |Options]),
case AccModel of
undefined -> Model;
_ -> erlsom:add_model(AccModel, Model)
end
end,
addSchemas(Tail, Model2, Prefix, Options, ImportList).
%%% --------------------------------------------------------------------
%%% Get a file from an URL spec.
%%% --------------------------------------------------------------------
get_url_file(URL) ->
case xmerl_uri:parse(URL) of
{http, _, _, _, _} -> get_remote_file(URL);
{https, _, _, _, _} -> get_remote_file(URL);
_Other -> get_local_file(URL)
end.
get_remote_file(URL) ->
case httpc:request(URL) of
{ok,{{_HTTP,200,_OK}, _Headers, Body}} ->
{ok, Body};
{ok,{{_HTTP,RC,Emsg}, _Headers, _Body}} ->
error_logger:error_msg("~p: http-request got: ~p~n", [?MODULE, {RC, Emsg}]),
{error, "failed to retrieve: "++URL};
{error, Reason} ->
error_logger:error_msg("~p: http-request failed: ~p~n", [?MODULE, Reason]),
{error, "failed to retrieve: "++URL}
end.
get_local_file(Fname) ->
{ok, Bin} = file:read_file(Fname),
{ok, binary_to_list(Bin)}.
%%% --------------------------------------------------------------------
%%% Make a HTTP Request
%%% --------------------------------------------------------------------
http_request(URL, SoapAction, Request, Options, Headers, ContentType) ->
case code:ensure_loaded(ibrowse) of
{module, ibrowse} ->
%% If ibrowse exist in the path then let's use it...
ibrowse_request(URL, SoapAction, Request, Options, Headers, ContentType);
_ ->
%% ...otherwise, let's use the OTP http client.
inets_request(URL, SoapAction, Request, Options, Headers, ContentType)
end.
inets_request(URL, SoapAction, Request, Options, Headers, ContentType) ->
NHeaders = [{"SOAPAction", SoapAction}|Headers],
NewHeaders = case proplists:get_value("Host", NHeaders) of
undefined ->
[{"Host", "localhost:8800"}|NHeaders];
_ ->
NHeaders
end,
NewOptions = [{cookies, enabled}|Options],
httpc:set_options(NewOptions),
case httpc:request(post,
{URL,NewHeaders,
ContentType,
Request},
[{timeout,?HTTP_REQ_TIMEOUT}],
[{sync, true}, {full_result, true}, {body_format, string}]) of
{ok,{{_HTTP,200,_OK},ResponseHeaders,ResponseBody}} ->
{ok, 200, ResponseHeaders, ResponseBody};
{ok,{{_HTTP,500,_Descr},ResponseHeaders,ResponseBody}} ->
{ok, 500, ResponseHeaders, ResponseBody};
{ok,{{_HTTP,ErrorCode,_Descr},ResponseHeaders,ResponseBody}} ->
{ok, ErrorCode, ResponseHeaders, ResponseBody};
Other ->
Other
end.
ibrowse_request(URL, SoapAction, Request, Options, Headers, ContentType) ->
case start_ibrowse() of
ok ->
NewHeaders = [{"Content-Type", ContentType}, {"SOAPAction", SoapAction} | Headers],
NewOptions = Options,
%%[{content_type, "text/xml; encoding=utf-8"} | Options],
case ibrowse:send_req(URL, NewHeaders, post, Request, NewOptions) of
{ok, Status, ResponseHeaders, ResponseBody} ->
{ok, list_to_integer(Status), ResponseHeaders, ResponseBody};
{error, Reason} ->
{error, Reason}
end;
error ->
{error, "could not start ibrowse"}
end.
start_ibrowse() ->
case ibrowse:start() of
{ok, _} -> ok;
{error, {already_started, _}} -> ok;
_ -> error
end.
rmsp(Str) -> string:strip(Str, left).
make_request_body(Content, []) ->
{"text/xml; charset=utf-8", "<?xml version=\"1.0\" encoding=\"utf-8\"?>"++Content};
make_request_body(Content, AttachedFiles) ->
{"application/dime", detergent_dime:encode("<?xml version=\"1.0\" encoding=\"utf-8\"?>"++Content, AttachedFiles)}.
makeFault(FaultCode, FaultString) ->
try
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<SOAP-ENV:Body>"
"<SOAP-ENV:Fault>"
"<faultcode>SOAP-ENV:" ++ FaultCode ++ "</faultcode>" ++
"<faultstring>" ++ FaultString ++ "</faultstring>" ++
"</SOAP-ENV:Fault>"
"</SOAP-ENV:Body>"
"</SOAP-ENV:Envelope>"
catch
_:_ ->
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<SOAP-ENV:Body>"
"<SOAP-ENV:Fault>"
"<faultcode>SOAP-ENV:Server</faultcode>"
"<faultstring>Server error</faultstring>"
"</SOAP-ENV:Fault>"
"</SOAP-ENV:Body>"
"</SOAP-ENV:Envelope>"
end.
%% record http_header is not defined??
findHeader(Label, Headers) ->
findHeader0(string:to_lower(Label), Headers).
findHeader0(_Label, []) ->
undefined;
findHeader0(Label, [{_,_,Hdr,_,Val}|T]) ->
case {Label, string:to_lower(Hdr)} of
{X,X} -> Val;
_ -> findHeader0(Label, T)
end;
findHeader0(_Label, undefined) ->
undefined.
makeOptions(undefined) ->
[];
makeOptions(Import) ->
lists:map(fun makeOption/1, Import).
%% -record(import_specs, {atts, namespace, prefix, location}).
makeOption(#import_specs{namespace = Ns, prefix = Pf, location = Lc}) ->
{Ns, Pf, Lc}.
addModels(undefined, Model) ->
Model;
addModels(Import, Model) ->
lists:foldl(fun addModel/2, Model, Import).
%% -record(xsd_file, {atts, name, prefix, import_specs}).
addModel(undefined, Acc) ->
Acc;
addModel(#xsd_file{name = XsdFile, prefix = Prefix, import_specs = Import}, Acc) ->
Options = makeOptions(Import),
{ok, Model2} = erlsom:add_xsd_file(XsdFile, [{prefix, Prefix} | Options], Acc),
Model2.
%% returns [#port{}]
%% -record(port, {service, port, binding, address}).
getPorts(ParsedWsdl) ->
Services = getTopLevelElements(ParsedWsdl, 'wsdl:tService'),
getPortsFromServices(Services, []).
getPortsFromServices([], Acc) ->
Acc;
getPortsFromServices([Service|Tail], Acc) ->
getPortsFromServices(Tail, getPortsFromService(Service) ++ Acc).
getPortsFromService(#'wsdl:tService'{name = Name, port = Ports}) ->
getPortsInfo(Ports, Name, []).
getPortsInfo([], _Name, Acc) ->
Acc;
getPortsInfo([#'wsdl:tPort'{name = Name,
binding = Binding,
choice = [#'soap:tAddress'{location = URL}]} | Tail], ServiceName, Acc) ->
getPortsInfo(Tail, ServiceName, [#port{service = ServiceName, port = Name, binding = Binding, address = URL}|Acc]);
%% non-soap bindings are ignored.
getPortsInfo([#'wsdl:tPort'{} | Tail], ServiceName, Acc) ->
getPortsInfo(Tail, ServiceName, Acc).
getTopLevelElements(#'wsdl:tDefinitions'{choice = TLElements}, Type) ->
getTopLevelElements(TLElements, Type, []).
getTopLevelElements([], _Type, Acc) ->
Acc;
getTopLevelElements([#'wsdl:anyTopLevelOptionalElement'{choice = Tuple}| Tail], Type, Acc) ->
case element(1, Tuple) of
Type -> getTopLevelElements(Tail, Type, [Tuple|Acc]);
_ -> getTopLevelElements(Tail, Type, Acc)
end.
getImports(Definitions) ->
Imports = getTopLevelElements(Definitions, 'wsdl:tImport'),
lists:map(fun(Import) -> Import#'wsdl:tImport'.location end, Imports).
%% returns [#operation{}]
getOperations(ParsedWsdl, Ports) ->
Bindings = getTopLevelElements(ParsedWsdl, 'wsdl:tBinding'),
getOperationsFromBindings(Bindings, Ports, []).
getOperationsFromBindings([], _Ports, Acc) ->
Acc;
getOperationsFromBindings([Binding|Tail], Ports, Acc) ->
getOperationsFromBindings(Tail, Ports, getOperationsFromBinding(Binding, Ports) ++ Acc).
getOperationsFromBinding(#'wsdl:tBinding'{name = BindingName,
type = BindingType,
choice = _Choice,
operation = Operations}, Ports) ->
%% TODO: get soap info from Choice
getOperationsFromOperations(Operations, BindingName, BindingType, Operations, Ports, []).
getOperationsFromOperations([], _BindingName, _BindingType, _Operations, _Ports, Acc) ->
Acc;
getOperationsFromOperations([#'wsdl:tBindingOperation'{name = Name, choice = Choice} | Tail],
BindingName, BindingType, Operations, Ports, Acc) ->
%% get SOAP action from Choice,
case Choice of
[#'soap:tOperation'{soapAction = Action}] ->
%% lookup Binding in Ports, and create a combined result
Ports2 = searchPorts(BindingName, Ports),
%% for each port, make an operation record
CombinedPorts = combinePorts(Ports2, Name, BindingName, Action),
getOperationsFromOperations(Tail, BindingName, BindingType, Operations, Ports, CombinedPorts ++ Acc);
_ ->
getOperationsFromOperations(Tail, BindingName, BindingType, Operations, Ports, Acc)
end.
combinePorts(Ports, Name, BindingName, Action) ->
combinePorts(Ports, Name, BindingName, Action, []).
combinePorts([], _Name, _BindingName, _Action, Acc) ->
Acc;
combinePorts([#port{service = Service, port = PortName, address = Address} | Tail],
Name, BindingName, Action, Acc) ->
combinePorts(Tail, Name, BindingName, Action,
[#operation{service = Service, port = PortName, operation = Name,
binding = BindingName, address = Address, action = Action} | Acc]).
searchPorts(BindingName, Ports) ->
searchPorts(BindingName, Ports, []).
searchPorts(_BindingName, [], Acc) ->
Acc;
searchPorts(BindingName, [Port | Tail], Acc) ->
PortBinding = erlsom_lib:localName(Port#port.binding),
case PortBinding of
BindingName ->
searchPorts(BindingName, Tail, [Port | Acc]);
_ ->
searchPorts(BindingName, Tail, Acc)
end.
getXsdsFromWsdl(Definitions) ->
case getTopLevelElements(Definitions, 'wsdl:tTypes') of
[#'wsdl:tTypes'{choice = Xsds}] -> Xsds;
[] -> undefined
end.
config_file_xsd() ->
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
" <xs:element name=\"soap_config\">"
" <xs:complexType>"
" <xs:sequence>"
" <xs:element name=\"xsd_path\" type=\"xs:string\" minOccurs=\"0\"/>"
" <xs:element name=\"user_module\" type=\"xs:string\"/>"
" <xs:element name=\"wsdl_file\" type=\"xsd_file\"/>"
" <xs:element name=\"add_file\" type=\"xsd_file\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
" </xs:sequence>"
" </xs:complexType>"
" </xs:element>"
" <xs:complexType name=\"xsd_file\">"
" <xs:sequence>"
" <xs:element name=\"import_specs\" type=\"import_specs\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
" </xs:sequence>"
" <xs:attribute name=\"name\" type=\"string\" use=\"required\"/>"
" <xs:attribute name=\"prefix\" type=\"string\"/>"
" </xs:complexType>"
" <xs:complexType name=\"import_specs\">"
" <xs:attribute name=\"namespace\" type=\"string\" use=\"required\"/>"
" <xs:attribute name=\"prefix\" type=\"string\"/>"
" <xs:attribute name=\"location\" type=\"string\"/>"
" </xs:complexType>"
"</xs:schema>".