Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 800 lines (695 sloc) 31.025 kb
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 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
%%%-------------------------------------------------------------------
%%% Created : 29 Nov 2006 by Torbjorn Tornkvist <tobbe@tornkvist.org>
%%% Author : Willem de Jong (w.a.de.jong@gmail.com).
%%% 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
%%% copied from yaws_soap_lib (Kaloyan Dimitrov, February 2012): to be used for soap12 calls

-module(yaws_soap12_lib).

-export([initModel/1, initModel/2,
initModelFile/1,
config_file_xsd/0,
call/3, call/4, call/5, call/6,
call_attach/4, call_attach/5, 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("../include/yaws_soap.hrl").
-include("../include/soap-envelope.hrl").
-include("../include/wsdl11soap12.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(yaws_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}).

-define(DefaultPrefix, "p").


%%%
%%% 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, PrefixOrOptions)
  when is_list(WsdlURL),is_list(PrefixOrOptions) ->
    write_hrl(initModel(WsdlURL, PrefixOrOptions), 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(WsdlURL, Operation, ListOfData) when is_list(WsdlURL) ->
    Wsdl = initModel(WsdlURL, ?DefaultPrefix),
    call(Wsdl, Operation, ListOfData);
call(Wsdl, Operation, ListOfData) when is_record(Wsdl, wsdl) ->
    case get_operation(Wsdl#wsdl.operations, Operation) of
{ok, Op} ->
Msg = mk_msg(?DefaultPrefix, Operation, ListOfData),
call(Wsdl, Operation, Op#operation.port,
                 Op#operation.service, [], Msg);
Else ->
Else
    end.

%%% --------------------------------------------------------------------
%%% Takes http headers
%%% --------------------------------------------------------------------
call(WsdlURL, Operation, ListOfData, http_headers, HttpHeaders) when is_list(WsdlURL) ->
    Wsdl = initModel(WsdlURL, ?DefaultPrefix),
    call(Wsdl, Operation, ListOfData, http_headers, HttpHeaders);
call(Wsdl, Operation, ListOfData, http_headers, HttpHeaders) when is_record(Wsdl, wsdl) ->
    case get_operation(Wsdl#wsdl.operations, Operation) of
{ok, Op} ->
Msg = mk_msg(?DefaultPrefix, Operation, ListOfData),
call(Wsdl, Operation, Op#operation.port,
                 Op#operation.service, [], Msg, http_headers, HttpHeaders);
Else ->
Else
    end;

%%% --------------------------------------------------------------------
%%% With additional specified prefix
%%% --------------------------------------------------------------------
call(WsdlURL, Operation, ListOfData, prefix, Prefix) when is_list(WsdlURL) ->
    Wsdl = initModel(WsdlURL, Prefix),
    call(Wsdl, Operation, ListOfData, prefix, Prefix );
call(Wsdl, Operation, ListOfData, prefix, Prefix) 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);
Else ->
Else
    end.


%%% --------------------------------------------------------------------
%%% Takes the actual records for the Header and Body message.
%%% --------------------------------------------------------------------
call(WsdlURL, Operation, Header, Msg) when is_list(WsdlURL) ->
    Wsdl = initModel(WsdlURL, ?DefaultPrefix),
    call(Wsdl, Operation, Header, Msg);
call(Wsdl, Operation, Header, Msg) 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);
Else ->
Else
    end.


mk_msg(_Prefix, _Operation, ListOfData) ->
    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_attach(Wsdl, Operation, Port, Service, Headers, Message, [], []).

%%% --------------------------------------------------------------------
%%% Make a SOAP request (with http headers)
%%% --------------------------------------------------------------------
call(Wsdl, Operation, Port, Service, Headers, Message, http_headers, HttpHeaders) ->
    call_attach(Wsdl, Operation, Port, Service, Headers, Message, [], HttpHeaders).


%%% --------------------------------------------------------------------
%%% For Quick deployment (with attachments)
%%% --------------------------------------------------------------------
call_attach(WsdlURL, Operation, ListOfData, Attachments)
  when is_list(WsdlURL) ->
    Wsdl = initModel(WsdlURL, ?DefaultPrefix),
    call_attach(Wsdl, Operation, ListOfData, Attachments);
call_attach(Wsdl, Operation, ListOfData, Attachments)
  when is_record(Wsdl, wsdl) ->
    case get_operation(Wsdl#wsdl.operations, Operation) of
{ok, Op} ->
Msg = mk_msg(?DefaultPrefix, Operation, ListOfData),
call_attach(Wsdl, Operation, Op#operation.port,
                        Op#operation.service, [], Msg, Attachments, []);
Else ->
Else
    end.

%%% --------------------------------------------------------------------
%%% Takes the actual records for the Header and Body message
%%% (with attachments)
%%% --------------------------------------------------------------------
call_attach(WsdlURL, Operation, Header, Msg, Attachments)
  when is_list(WsdlURL) ->
    Wsdl = initModel(WsdlURL, ?DefaultPrefix),
    call_attach(Wsdl, Operation, Header, Msg, Attachments);
call_attach(Wsdl, Operation, Header, Msg, Attachments)
  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, []);
Else ->
Else
    end.


%%% --------------------------------------------------------------------
%%% Make a SOAP request (with attachments)
%%% --------------------------------------------------------------------
call_attach(#wsdl{operations = Operations, model = Model},
            Operation, Port, Service, Headers, Message, Attachments, HttpHeaders) ->
    %% find the operation
    case findOperation(Operation, Port, Service, Operations) of
#operation{address = URL, action=Action, operation = Operation} ->
%% Add the Soap envelope
Envelope = mk_envelope(Message, Headers),
%% Encode the message
case erlsom:write(Envelope, Model) of
{ok, XmlMessage} ->

{ContentType, Request} =
                        make_request_body(XmlMessage, Attachments, Operation),
HttpClientOptions = [],
                    ?dbg("+++ Request = ~p~n", [Request]),
HttpRes = http_request(URL, Action, Request,
                                           HttpClientOptions, HttpHeaders,
                                           ContentType),
                    ?dbg("+++ HttpRes = ~p~n", [HttpRes]),
case HttpRes of
{ok, _Code, _ReturnHeaders, 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) ->
    Parsed = erlsom:scan(Message, Model),
    case Parsed 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, ?DefaultPrefix).

%% PrefixOrOptions can be a property list that contains the options
%% for Erlsom, or a String. If it is a string, this is used as the
%% Erlsom 'prefix' option (and the other options are left unspecified).
initModel(WsdlFile, PrefixOrOptions) ->
    Options = case is_string(PrefixOrOptions) of
        no ->
          %% It is an option list
%% Add the default prefix at the end - it will only be used
%% if no other prefix is specified
PrefixOrOptions ++ [{prefix, ?DefaultPrefix}];
        _ ->
          %% just the prefix
          [{prefix, PrefixOrOptions}]
    end,
    PrivDir = priv_dir(),
    initModel2(WsdlFile, Options, 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),
    #yaws_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, Prefix}], XsdPath, Import, AddFiles).

priv_dir() ->
    case code:priv_dir(yaws) of
        {error, bad_name} ->
            filename:join([filename:dirname(code:which(yaws)),"..", "priv"]);
        A ->
            A
    end.

initModel2(WsdlFile, ErlsomOptions, 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, "wsdl11soap12.xsd"]),
                        [{prefix, "soap"},
                         {include_files, [IncludeWsdl]}]),
    %% uncomment to generate the wsdl11soap12.hrl file
    %% erlsom:write_hrl(WsdlModel, "/home/kalski/test/wsdl11soap12.hrl"),
    %% add the xsd model (since xsd is also used in the wsdl)
    WsdlModel2 = erlsom:add_xsd_model(WsdlModel),
    Options = ErlsomOptions ++ makeOptions(Import),
    %% parse Wsdl
    {Model, Operations} = parseWsdls([WsdlFile], WsdlModel2,
                                     Options, {undefined, []}),
    %% TODO: add files as required
    %% now compile envelope.xsd, and add Model
    {ok, EnvelopeModel} = erlsom:compile_xsd_file(
                            filename:join([Path, "soap-envelope.xsd"]),
                            [{prefix, "soap"},
                             {include_files, [{"http://www.w3.org/XML/1998/namespace", undefined, filename:join([Path, "xml.xsd"])}]}]),
    SoapModel = erlsom:add_model(EnvelopeModel, Model),
    %% uncomment to generate the soap-envelope.hrl file
    %% erlsom:write_hrl(EnvelopeModel, "/home/kalski/test/soap-envelope.hrl"),
    SoapModel2 = addModels(AddFiles, SoapModel),
    #wsdl{operations = Operations, model = SoapModel2}.


%%% --------------------------------------------------------------------
%%% Parse a list of WSDLs and import (recursively)
%%% Returns {Model, Operations}
%%% --------------------------------------------------------------------
parseWsdls([], _WsdlModel, _Options, Acc) ->
    Acc;
parseWsdls([WsdlFile | Tail], WsdlModel, Options, {AccModel, AccOperations}) ->
    WsdlFileNoSpaces = rmsp(WsdlFile),
    {ok, WsdlFileContent} = get_url_file(WsdlFileNoSpaces),
    {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 includes one of the other schemas.
    %% The AXIS java2wsdl tool generates wsdls that depend on this feature.
    ImportList = makeImportList(Xsds, []),
    Model2 = addSchemas(Xsds, AccModel, Options, ImportList),
    Ports = getPorts(ParsedWsdl),
    Operations = getOperations(ParsedWsdl, Ports),
    Imports = getImports(filename:dirname(WsdlFileNoSpaces), ParsedWsdl),
    Model3 = addSchemaFiles(Imports, Model2, Options, []),
    Acc2 = {Model3, 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.
    %% TODO uncomment if imports can be WSDL
    %%Acc3 = parseWsdls(Imports, WsdlModel, Options, Acc2),
    parseWsdls(Tail, WsdlModel, Options, Acc2).

%%% --------------------------------------------------------------------
%%% 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 tool 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, _Options, _ImportList) ->
    AccModel;
addSchemas([Xsd| Tail], AccModel, Options, ImportList) ->
    Model2 = case Xsd of
                 undefined ->
                     AccModel;
                 _ ->
                     {ok, Model} =
                         erlsom_compile:compile_parsed_xsd(
                           Xsd,
                           [{include_files, ImportList} |Options]),
                     case AccModel of
                         undefined -> Model;
                         _ -> erlsom:add_model(AccModel, Model)
                     end
             end,
    addSchemas(Tail, Model2, Options, ImportList).

%%% --------------------------------------------------------------------
%%% compile each of the schema files, and add it to the model.
%%% Returns Model
%%% (TODO: using the same prefix for all XSD files makes no sense)
%%% --------------------------------------------------------------------
addSchemaFiles([], AccModel, _Options, _ImportList) ->
    AccModel;
addSchemaFiles([Xsd| Tail], AccModel, Options, ImportList) ->
    {ok, Model} =
        erlsom:compile_xsd_file(get_file_with_path(Xsd),
                                [{include_files, ImportList} |Options]),
    Model2 = case AccModel of
                 undefined -> Model;
                 _ -> erlsom:add_model(AccModel, Model)
             end,
    addSchemaFiles(Tail, Model2, Options, ImportList).

%%% --------------------------------------------------------------------
%%% Get a file from an URL spec.
%%% --------------------------------------------------------------------
get_url_file("http://"++_ = 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_url_file("file://"++Fname) ->
    {ok, Bin} = file:read_file(Fname),
    {ok, binary_to_list(Bin)};
%% added this, since this is what is used in many WSDLs (i.e.: just a filename).
get_url_file(Fname) ->
    {ok, Bin} = file:read_file(Fname),
    {ok, binary_to_list(Bin)}.


%%% --------------------------------------------------------------------
%%% Make a HTTP Request
%%% --------------------------------------------------------------------
http_request(URL, Action, 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, Action, Request, Options,
                            Headers, ContentType);
_ ->
%% ...otherwise, let's use the OTP http client.
inets_request(URL, Action, Request, Options,
                          Headers, ContentType)
    end.

inets_request(URL, Action, Request, Options, Headers, ContentType) ->
    case Action of
      undefined ->
NHeaders = Headers;
      _ ->
NHeaders = [{"SOAPAction", Action} | Headers]
    end,
    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, Action, Request, Options, Headers, ContentType) ->
    case start_ibrowse() of
        ok ->
NewHeaders = [{"Content-Type", ContentType} |
                          case Action of
                             undefined ->
                                  Headers;
                              _ ->
                                  [{"SOAPAction", Action} | Headers]
                          end],
            case ibrowse:send_req(URL, NewHeaders, post, Request, Options) 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, [], Operation) ->
    {"application/soap+xml;charset=UTF-8;action=\"" ++ Operation ++ "\"",
     "<?xml version=\"1.0\" encoding=\"utf-8\"?>"++ Content};
make_request_body(Content, AttachedFiles, _Operation) ->
    {"application/dime",
     yaws_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(yaws:to_lower(Label), Headers).

findHeader0(_Label, []) ->
    undefined;
findHeader0(Label, [{_,_,Hdr,_,Val}|T]) ->
    case {Label, yaws: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'{choice1 = 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.

get_file_with_path(Url) ->
  case Url of
    "http://" ++ _ ->
      undefined;
    "file://" ++ FName ->
      FName;
    _ ->
      Url
  end.


getImports(WsdlDirname, Definitions) ->
    Imports = getTopLevelElements(Definitions, 'wsdl:tImport'),
    lists:map(fun(Import) ->
case WsdlDirname of
"http://" ++ _AbsDirname ->
WsdlDirname ++ "/" ++ Import#'wsdl:tImport'.location;
"file://" ++ _AbsDirname ->
WsdlDirname ++ "/" ++ Import#'wsdl:tImport'.location;
Fname ->
filename:join(Fname, Import#'wsdl:tImport'.location)
end
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, []).

getOperationsFromOperation(BindingName, BindingType, Ports, Name, Action, Operations, Tail, Acc) ->
  %% 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([], _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}] ->
getOperationsFromOperation(BindingName, BindingType, Ports, Name, Action, Operations, Tail, Acc);
_ ->
            getOperationsFromOperation(BindingName, BindingType, Ports, Name, undefined, Operations, Tail, 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.

%% copied from yaws/json.erl
is_string([]) -> yes;
is_string(List) -> is_string(List, non_unicode).

is_string([C|Rest], non_unicode) when C >= 0, C =< 255 -> is_string(Rest, non_unicode);
is_string([C|Rest], _) when C =< 65000 -> is_string(Rest, unicode);
is_string([], non_unicode) -> yes;
is_string([], unicode) -> unicode;
is_string(_, _) -> no.

getXsdsFromWsdl(Definitions) ->
    case getTopLevelElements(Definitions, 'wsdl:tTypes') of
        [#'wsdl:tTypes'{choice = Xsds}] -> Xsds;
        [] -> []
    end.

config_file_xsd() ->
    "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
        " <xs:element name=\"yaws_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>".
Something went wrong with that request. Please try again.