File Coverage

blib/lib/XML/Compile/SOAP/Daemon/Dancer2.pm
Criterion Covered Total %
statement 21 23 91.3
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 29 31 93.5


line stmt bran cond sub pod time code
1             package XML::Compile::SOAP::Daemon::Dancer2;
2 3     3   566381 use strict;
  3         5  
  3         69  
3 3     3   10 use warnings;
  3         3  
  3         52  
4 3     3   66 use 5.008_005;
  3         9  
5             our $VERSION = '0.07';
6 3     3   1490 use Dancer2::Plugin;
  3         72317  
  3         16  
7 3     3   17709 use Dancer2::FileUtils 'path';
  3         4  
  3         149  
8 3     3   1188 use Class::Load qw(try_load_class);
  3         32675  
  3         136  
9 3     3   14 use Carp;
  3         4  
  3         113  
10              
11 3     3   1199 use XML::Compile::SOAP::Daemon::Dancer2::Handler;
  0            
  0            
12             use XML::Compile::WSDL11;
13             use XML::Compile::SOAP11;
14             use XML::Compile::SOAP12;
15              
16             sub get_implementation {
17             my ( $class_name, $dsl ) = @_;
18             my ($ok, $error) = try_load_class($class_name);
19             if (! $ok) {
20             confess "Cannot load implementation class $class_name: $error";
21             }
22              
23             my $implementation = $class_name->new( dsl => $dsl );
24              
25             die "Implementation class needs to do the role XML::Compile::SOAP::Daemon::Dancer2::Role::Implementation"
26             unless $implementation->DOES('XML::Compile::SOAP::Daemon::Dancer2::Role::Implementation');
27              
28             return $implementation;
29             }
30              
31             register 'wsdl_endpoint' => sub {
32             my ($dsl, $path, $options) = @_;
33              
34             my $settings = plugin_setting;
35             my $wsdl_path = path( $dsl->setting('appdir'), $settings->{wsdl_path}//"wsdl" );
36             my $xsd_path = path( $dsl->setting('appdir'), $settings->{xsd_path}//"wsdl" );
37              
38             for my $key ( qw(wsdl) ) {
39             confess "$key option mandatory when calling wsdl_endpoint" unless exists $options->{$key};
40             }
41              
42             my $wsdl_file = path( $wsdl_path, $options->{wsdl} );
43             unless( -f $wsdl_file ) {
44             confess "wsdl file not found ($wsdl_file)";
45             }
46              
47             my $wsdl = XML::Compile::WSDL11->new( $wsdl_file );
48              
49             if( exists $options->{xsd} ) {
50             if( ref $options->{xsd} eq 'ARRAY' ) {
51             for my $file ( @{$options->{xsd}} ) {
52             my $xsd_file = path( $xsd_path, $file );
53             $wsdl->importDefinitions( $xsd_file );
54             }
55             } else {
56             confess 'xsd must be an array ref of xsd files';
57             }
58             }
59              
60             my $operations = {};
61             if( exists $options->{operations} ) {
62             confess "operation should be a hasref of operation => sub {}" unless ref $options->{operations} eq "HASH";
63             $operations = $options->{operations};
64             }
65             if( exists $options->{implementation_class} ) {
66             #class provided for inplementation
67             #try to load the class
68             my $implementation = get_implementation( $options->{implementation_class}, $dsl );
69              
70             for my $operation ( $wsdl->operations() ) {
71             #$dsl->error( $operation->action );
72             if( my $call = $implementation->can("soapaction_".$operation->action) ) {
73             $operations->{$operation->action} = sub { $implementation->$call( @_ ) };
74             }
75             }
76             }
77              
78             my $daemon = XML::Compile::SOAP::Daemon::Dancer2::Handler->new();
79              
80             $daemon->operationsFromWSDL(
81             $wsdl,
82             callbacks => $operations,
83             );
84              
85             $dsl->app->add_route(
86             method => 'get',
87             regexp => $path,
88             code => sub {
89             my $params = $dsl->params;
90              
91             if( exists $params->{wsdl} ) {
92             $dsl->content_type( 'application/wsdl+xml' );
93             $dsl->send_file( $wsdl_file , system_path => 1 );
94             }
95             }
96             );
97              
98             $dsl->app->add_route(
99             method => 'post',
100             regexp => $path,
101             code => sub {
102             $daemon->handle( $dsl );
103             }
104             );
105             };
106              
107             register_plugin;
108             1;
109             __END__