=encoding utf8

=head1 NAME

XML::Compile::WSDL11 - create SOAP messages defined by WSDL 1.1

=head1 INHERITANCE

 XML::Compile::WSDL11
   is a XML::Compile::Cache

=head1 SYNOPSIS

 # preparation
 use XML::Compile::WSDL11;      # use WSDL version 1.1
 use XML::Compile::SOAP11;      # use SOAP version 1.1
 use XML::Compile::Transport::SOAPHTTP;

 my $wsdl = XML::Compile::WSDL11->new($wsdlfile);
 $wsdl->addWSDL(...more WSDL files...);
 $wsdl->importDefinitions(...more schemas...);

 # during initiation, for each used call
 my $call = $wsdl->compileClient('GetStockPrice', ...);

 # at "run-time", call as often as you want (fast)
 my $answer = $call->(%request);

 # capture useful trace information
 my ($answer, $trace) = $call->(%request);
 if($trace->errors)
 {   $trace->printError;
 }

 # no need to administer the operations by hand: alternative
 $wsdl->compileCalls;  # at initiation
 my $answer = $wsdl->call(GetStockPrice => %request);

 # investigate the %request structure (server input)
 print $wsdl->explain('GetStockPrice', PERL => 'INPUT', recurse => 1);

 # investigate the $answer structure (server output)
 print $wsdl->explain('GetStockPrice', PERL => 'OUTPUT');

 # when you like, get all operation definitions
 my @all_ops = $wsdl->operations;

 # Install XML::Compile::SOAP::Daemon
 my $server  = XML::Compile::SOAP::HTTPDaemon->new;
 $server->operationsFromWSDL($wsdl);
 undef $wsdl;    # not needed any further
 
 # For debug info, start your script with:
 use Log::Report mode => 'DEBUG';

=head1 DESCRIPTION

This module understands WSDL version 1.1.  An WSDL file defines a set of
messages to be send and received over (SOAP) connections. This involves
encoding of the message to be send into XML, sending the message to the
server, collect the answer, and finally decoding the XML to Perl.

As end-user, you do not have to worry about the complex details of the
messages and the way to exchange them: it's all simple Perl for you.
Also, faults are handled automatically.  The only complication you have
to worry about is to shape a nested HASH structure to the sending
message structure.  XML::Compile::Schema subroutine template may help you.

When the definitions are spread over multiple files you will need to
use L<addWSDL()|XML::Compile::WSDL11/"Extension"> (wsdl) or XML::Compile::WSDL11 subroutine importDefinitions (additional schema's)
explicitly. Usually, interreferences between those files are broken.
Often they reference over networks (you should never trust). So, on
purpose you B<must explicitly load> the files you need from local disk!
(of course, it is simple to find one-liners as work-arounds, but I will
to tell you how!)

=head1 METHODS

=head2 Constructors

=over 4

=item XML::Compile::WSDL11-E<gt>B<new>(XML, OPTIONS)

The XML is the WSDL file, which is anything accepted by
XML::Compile subroutine dataToXML.

=back

=head2 Accessors

=head2 Compilers

=over 4

=item $obj-E<gt>B<call>(OPNAME, DATA)

[2.20] Call the OPNAME (operation name) with DATA (HASH or LIST of parameters).
This only works when you have called L<compileCalls()|XML::Compile::WSDL11/"Compilers"> beforehand,
always during the initiation phase of the program.

example: 

   # at initiation time (compile once)
   $wsdl->compileCalls;

   # at runtime (run often)
   my $answer = $wsdl->call($operation, $request);

=item $obj-E<gt>B<compileAll>(['READERS'|'WRITERS'|'RW'|'CALLS', [NAMESPACE]])

[2.20] With explicit C<CALLS> or without any parameter, it will call
L<compileCalls()|XML::Compile::WSDL11/"Compilers">. Otherwise, see XML::Compile::Cache subroutine compileAll.

=item $obj-E<gt>B<compileCall>(OPERATION|OPNAME, OPTIONS)

[2.37] The call to the OPERATION object (which extends
XML::Compile::SOAP::Operation) gets compiled and cached so it can
be used with L<call()|XML::Compile::WSDL11/"Compilers">.

[2.38] Alteratively to an OPERATION object, you may also specify an
operation by name.

example: 

  my $op = $wsdl->operation(name => 'getInfo');
  $wsdl->compileCall($op);

  # as often as you need it
  my ($answer, $trace) = $wsdl->call('getInfo')->(%request);

=item $obj-E<gt>B<compileCalls>(OPTIONS)

[2.20] Compile a handler for each of the available operations. The OPTIONS are
passed to each call of L<compileClient()|XML::Compile::WSDL11/"Extension">, but will be overruled by more
specific declared options.

Additionally, OPTIONS can contain C<service>, C<port>, and C<binding>
to limit the set of involved calls. See L<operations()|XML::Compile::WSDL11/"Introspection"> for details on
these options.

You may declare additional specific compilation options with the
L<declare()|XML::Compile::WSDL11/"Administration"> method.

example: 

   my $trans = XML::Compile::Transport::SOAPHTTP
     ->new(timeout => 500, address => $wsdl->endPoint);
   $wsdl->compileCalls(transport => $trans);

   # alternatives for simple cases
   $wsdl->compileAll('CALLS');
   $wsdl->compileAll;
   
   my $answer = $wsdl->call($myop, $request);

=back

=head2 Extension

=over 4

=item $obj-E<gt>B<addWSDL>(XMLDATA)

The XMLDATA must be acceptable to XML::Compile subroutine dataToXML and 
should represent the top-level of a (partial) WSDL document.
The specification can be spread over multiple files, each of
which must have a C<definition> root element.

=item $obj-E<gt>B<compileClient>([NAME], OPTIONS)

Creates an XML::Compile::SOAP::Operation temporary object using
L<operation()|XML::Compile::WSDL11/"Extension">, and then calls C<compileClient()> on that.  This
results in a code reference which will handle all client-server
SOAP exchange.

The OPTIONS available include all of the options for:

=over 4

=item *

L<operation()|XML::Compile::WSDL11/"Extension"> (i.e. C<service> and C<port>), and all of

=item *

XML::Compile::SOAP::Operation subroutine compileClient (there are many of
these, for instance C<transport_hook> and C<server>)

=back

You B<cannot> pass options for XML::Compile::Schema subroutine compile, like
C<<sloppy_integers => 0>>, hooks or typemaps this way. Use XML::Compile::WSDL11 subroutine new option opts_rw
and friends to declare those.

When you use L<compileCall()|XML::Compile::WSDL11/"Compilers">, the compiled code references get cached
for you.  In that case, you can use L<call()|XML::Compile::WSDL11/"Compilers"> to use them.

example: 

  my $call = $wsdl->compileClient
    ( operation => 'HelloWorld'
    , port      => 'PrefillSoap' # only required when multiple ports
    );
  my ($answer, $trace) = $call->($request);

  # 'operation' keyword optional
  my $call = $wsdl->compileClient('HelloWorld');

=item $obj-E<gt>B<namesFor>(CLASS)

Returns the list of names available for a certain definition CLASS in
the WSDL. See L<index()|XML::Compile::WSDL11/"Introspection"> for a way to determine the available CLASS
information.

=item $obj-E<gt>B<operation>([NAME], OPTIONS)

Collect all information for a certain operation.  Returned is an
XML::Compile::SOAP::Operation object.

An operation is defined by a service name, a port, some bindings,
and an operation name, which can be specified explicitly and is often
left-out: in the many configurations where there are no alternative
choices. In case there are alternatives, you will be requested to
pick an option.

 -Option   --Default
  action     <undef>
  operation  <required>
  port       <only when just one port in WSDL>
  service    <only when just one service in WSDL>

=over 2

=item action => STRING

Overrule the soapAction from the WSDL.

=item operation => NAME

Ignored when the parameter list starts with a NAME (which is an
alternative for this option).  Optional when there is only
one operation defined within the portType.

=item port => NAME

Required when more than one port is defined.

=item service => QNAME|PREFIXED

Required when more than one service is defined.

=back

=back

=head2 Administration

=over 4

=item $obj-E<gt>B<declare>(GROUP, COMPONENT|ARRAY, OPTIONS)

Register specific compile OPTIONS for the specific COMPONENT. See also
XML::Compile::Cache subroutine declare. The GROUP is either C<READER>,
C<WRITER>, C<RW> (both reader and writer), or C<OPERATION>.  As COMPONENT,
you specify the element name (for readers and writers) or operation name
(for operations). OPTIONS are specified as LIST, ARRAY or HASH.

example: 

   $wsdl->declare(OPERATION => 'GetStockPrice', @extra_opts);
   $wsdl->compileCalls;
   my $answer = $wsdl->call(GetStockPrice => %request);

=back

=head2 Introspection

All of the following methods are usually NOT meant for end-users. End-users
should stick to the L<operation()|XML::Compile::WSDL11/"Extension"> and L<compileClient()|XML::Compile::WSDL11/"Extension"> methods.

=over 4

=item $obj-E<gt>B<endPoint>(OPTIONS)

[2.20] Returns the address of the server, as specified by the WSDL. When
there are no alternatives for service or port, you not not need to
specify those parameters.

The endpoint in the WSDL is often wrong.  All compile functions accept
the C<server> and C<endpoint> parameters to overrule the value.  With
C<server>, only the hostname:port is being replaced.  With C<endpoint>,
everything is replaced.

 -Option --Default
  port     <undef>
  service  <undef>

=over 2

=item port => NAME

=item service => QNAME|PREFIXED

=back

example: 

 my $devel = URI->new($wsdl->endPoint);
 $devel->path('/sdk');
 my $call = $wsdl->compileCall($opname, endpoint => $devel);

=item $obj-E<gt>B<explain>(OPERATION, FORMAT, DIRECTION, OPTIONS)

[2.13]
Produce templates (see XML::Compile::Schema subroutine template which detail
the use of the OPERATION. Currently, only the C<PERL> template FORMAT
is available.

The DIRECTION of operation is either C<INPUT> (input for the server,
hence to be produced by the client), or C<OUTPUT> (from the server,
received by the client).

The actual work is done by XML::Compile::SOAP::Operation subroutine explain. The
OPTIONS passed to that method include C<recurse> and C<skip_header>.

example: 

  print $wsdl->explain('CheckStatus', PERL => 'INPUT');

  print $wsdl->explain('CheckStatus', PERL => 'OUTPUT'
     , recurse => 1                 # explain options
     , port    => 'Soap12PortName'  # operation options
     );

=item $obj-E<gt>B<findDef>(CLASS, [QNAME|PREFIXED|NAME])

With a QNAME, the HASH which contains the parsed XML information
from the WSDL template for that CLASS-NAME combination is returned.
You may also have a PREFIXED name, using one of the predefined namespace
abbreviations.  Otherwise, NAME is considered to be the localName in
that class.  When the NAME is not found, an error is produced.

Without QNAME in SCALAR context, there may only be one such name
defined otherwise an error is produced.  In LIST context, all definitions
in CLASS are returned.

example: 

 $service  = $obj->findDef(service => 'http://xyz');
 @services = $obj->findDef('service');

=item $obj-E<gt>B<index>([CLASS, [QNAME]])

With a CLASS and QNAME, it returns one WSDL definition HASH or undef.
Returns the index for the CLASS group of names as HASH.  When no CLASS is
specified, a HASH of HASHes is returned with the CLASSes on the top-level.

CLASS includes C<service>, C<binding>, C<portType>, and C<message>.

=item $obj-E<gt>B<operations>(OPTIONS)

Return a list with all operations defined in the WSDL.

 -Option --Default
  binding  <undef>
  port     <undef>
  service  <undef>

=over 2

=item binding => NAME

Only return operations which use the binding with the specified NAME.
By default, all bindings are accepted.

=item port => NAME

Return only operations related to the specified port NAME.
By default operations from all ports.

=item service => NAME

Only return operations related to the NAMEd service, by default all services.

=back

=item $obj-E<gt>B<printIndex>([FILEHANDLE], OPTIONS)

For available OPTIONS, see L<operations()|XML::Compile::WSDL11/"Introspection">.  This method is useful to
understand the structure of your WSDL: it shows a nested list of
services, bindings, ports and portTypes.

=back

=head1 DETAILS

=head2 Initializing SOAP operations via WSDL

When you have a WSDL file, then SOAP is simple.  If there is no such file
at hand, then it is still possible to use SOAP.  See the DETAILS chapter
in XML::Compile::SOAP.

The WSDL file contains operations which can be addressed by name.
In the WSDL file you need to find the name of the port to be used.
In most cases, the WSDL has only one service, one port, one binding,
and one portType and those names can therefore be omitted.  If there is
a choice, then you must explicitly select one.

 use XML::Compile::WSDL11 ();

 # once in your program
 my $wsdl   = XML::Compile::WSDL11->new('def.wsdl');

 # XML::Compile::Schema refuses to follow "include" and
 # "import" commands, so you need to invoke them explicitly.
 # $wsdl->addWSDL('file2.wsdl');            # optional
 # $wsdl->importDefinitions('schema1.xsd'); # optional

 # once for each of the defined operations
 my $call   = $wsdl->compileClient('GetStockPrice');

 # see XML::Compile::SOAP chapter DETAILS about call params
 my $answer = $call->(%request);

=head1 SEE ALSO

This module is part of XML-Compile-WSDL11 distribution version 3.00,
built on January 06, 2014. Website: F<http://perl.overmeer.net/xml-compile/>

Other distributions in this suite:
L<XML::Compile>,
L<XML::Compile::SOAP>,
L<XML::Compile::WSDL11>,
L<XML::Compile::SOAP12>,
L<XML::Compile::SOAP::Daemon>,
L<XML::Compile::SOAP::WSA>,
L<XML::Compile::C14N>,
L<XML::Compile::WSS>,
L<XML::Compile::WSS::Signature>,
L<XML::Compile::Tester>,
L<XML::Compile::Cache>,
L<XML::Compile::Dumper>,
L<XML::Compile::RPC>,
L<XML::Rewrite>
and
L<XML::LibXML::Simple>.

Please post questions or ideas to the mailinglist at
F<http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/xml-compile> .
For live contact with other developers, visit the C<#xml-compile> channel
on C<irc.perl.org>.

=head1 LICENSE

Copyrights 2014 by [Mark Overmeer]. For other contributors see ChangeLog.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See F<http://www.perl.com/perl/misc/Artistic.html>

