lib/Mojolicious/Plugin/SOAP/Server.pm

filter: Statementstmt 89.1% 66 / 74 Branchbran 68.7% 11 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 12 / 14 Podpod 100% 1 / 1 totaltotal 82.7% 91 / 110 CC 12 SCAR 12.6
CC
12
Cov
84%
register
6.4
__ANON__
5.5
__ANON__
3.1
CRAP 3.5
↑ Summary example/exsoaple.pl »
1 package Mojolicious::Plugin::SOAP::Server;
2  
3 =pod
4  
5 =begin markdown
6  
7 ![](https://github.com/oposs/mojolicious-plugin-soap-server/workflows/Unit%20Tests/badge.svg?branch=master)
8  
9 =end markdown
10  
11 =head1 NAME
12  
13 Mojolicious::Plugin::SOAP::Server - implement a SOAP service
14  
15 =head1 SYNOPSIS
16  
17 use Mojolicious::Lite;
18 use Mojo::File 'curfile';
19  
20 plugin 'SOAP::Server' => {
21 wsdl => curfile->sibling('nameservice.wsdl'),
22 xsds => [curfile->sibling('nameservice.xsd')],
23 controller => SoapCtrl->new(x => '1'),
24 endPoint => '/SOAP'
25 };
26  
27 app->start;
28  
29 package SoapCtrl;
30  
31 use Mojo::Base -base,-signatures;
32  
33 has 'x' => 2;
34  
35 sub getCountries ($self,$server,$params,$controller) {
36 return {
37 country => [qw(Switzerland Germany), $self->x]
38 };
39 }
40  
41 sub getNamesInCountry ($self,$server,$params,$controller) {
42 my $name = $params->{parameters}{country};
43 $controller->log->debug("Test Message");
44 if ($name eq 'Die') {
45 die {
46 status => 401,
47 text => 'Unauthorized'
48 };
49 }
50 return {
51 name => [qw(A B C),$name]
52 };
53 }
54  
55 =head1 DESCRIPTION
56  
57 The L<Mojolicious::Plugin::SOAP::Server> is a thin wrapper around L<XML::Compile::SOAP::Daemon> which makes it pretty simple to implement SOAP services in perl.
58  
59 The plugin supports the following configuration options:
60  
61 =over
62  
63 =item wsdl
64  
65 A wsdl filename with definitions for the services provided
66  
67 =item xsds
68  
69 An array pointer with xsd files for the data types used in the wsdl.
70  
71 =item controller
72  
73 A mojo Object whose methods match the service names defined in the wsdl file.
74  
75 sub methodName ($self,$server,$params,$controller) {
76  
77 see example folder for inspiration.
78  
79 =item default_cb
80  
81 A default callback to be called if the requested method does not exist in the controller.
82  
83 =item endPoint
84  
85 Where to 'mount' the SOAP service.
86  
87 =back
88  
89 =cut
90  
91 1 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 1 ▶use XML::Compile::WSDL11;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 1 ▶use XML::Compile::SOAP11;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 ▶use XML::Compile::SOAP12;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use XML::Compile::SOAP::Daemon::CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 1 ▶use Mojo::Util qw(dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 our $VERSION = '0.1.6';
99 1 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 0 ▶has wsdl => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 XML::Compile::WSDL11->new;
103 1 };
104  
105 1 ▶has daemon => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
106 1 XML::Compile::SOAP::Daemon::CGI->new;
107 1 };
108  
109 # do not depend on LWP
110 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 RC_OK => 200,
112 RC_METHOD_NOT_ALLOWED => 405,
113 RC_NOT_ACCEPTABLE => 406,
114 1 };
115  
116 1 ▶sub register ($self,$app,$conf={}) {
Subroutine register: called CC=6 SCAR=18.6
Pod documented
117 1 my $log = $app->log;
118 1 my $wsdl = XML::Compile::WSDL11->new($conf->{wsdl});
119 $wsdl->importDefinitions(
120 $conf->{xsds}
121 1 ▶ ) if $conf->{xsds};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"xsds"} 1 0 1
122  
123 1 my $controller = $conf->{controller};
124 1 for my $op ($wsdl->operations()){
125 4 my $code;
126 4 my $method = $op->name;
127 4 ▶ if ($controller->can($method)){
Branch: 1 branch
BranchTrueFalseTotal
if ($controller->can($method)) { } 2 2 4
128 2 $app->log->debug(__PACKAGE__ . " Register handler for $method");
129 $code = $op->compileHandler(
130 3 ▶ callback => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
131 3 my ($ctrl,$param,$c) = @_;
132 3 my $ret = eval {
133 3 local $ENV{__DIE__};
134 3 $controller->$method(@_);
135 };
136 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
137 1 ▶ if (ref $@ eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ eq "HASH") 1 0 1
138 1 $c->log->error("$method - $@->{status} $@->{text}");
139 return {
140 _RETURN_CODE => $@->{status},
141 _RETURN_TEXT => $@->{text},
142 }
143 1 }
144 0 $log->error("$method - $@");
145 return {
146 0 _RETURN_CODE => 500,
147 _RETURN_TEXT => 'Internal Error'
148 }
149 }
150 2 return $ret;
151 }
152 2 );
153 }
154 else {
155 2 $app->log->debug(__PACKAGE__ . " Adding stub handler $method");
156 $code = $op->compileHandler(
157 0 ▶ callback => $conf->{default_cb} || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 warn "No handler for $method";
159 return {
160 0 _RETURN_CODE => 404,
161 _RETURN_TEXT => 'No handler found',
162 };
163 }
164 2 ▶ );
Condition33%1/3
$conf->{'default_cb'} || sub { warn "No handler for $method"; return {'_RETURN_CODE', 404, '_RETURN_TEXT', 'No handler found'}; }
l!l&&r!l&&!r
020
MC/DC0%0/2
$conf->{'default_cb'} || sub { warn "No handler for $method"; return {'_RETURN_CODE', 404, '_RETURN_TEXT', 'No handler found'}; }
$conf->{'default_cb'} sub { warn "No handler for $method"; return {'_RETURN_CODE', 404, '_RETURN_TEXT', 'No handler found'}; }
Truth table33%1/3
A || B
A= $conf->{'default_cb'}B= sub { warn "No handler for $method"; return {'_RETURN_CODE', 404, '_RETURN_TEXT', 'No handler found'}; }
ABresult
1X1
011
000
165 }
166 4 $self->daemon->addHandler($op->name,$op,$code);
167 }
168 1 my $r = $app->routes;
169 1 $app->types->type(
170 soapxml => 'text/xml; charset="utf-8"'
171 );
172 $r->any($conf->{endPoint})
173 3 ▶ ->to(cb => sub ($c) {
Subroutine __ANON__: called CC=5 SCAR=17.1
174 3 ▶ if ( $c->req->method !~ /^(M-)?POST$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->req->method =~ /^(M-)?POST$/u) 0 3 3
175 0 return $c->render(
176 status => RC_METHOD_NOT_ALLOWED . " Expected POST",
177 text => 'SOAP wants you to POST!'
178 );
179 }
180 3 my $format = 'txt';
181 3 my $body = $c->req->body;
182 3 my ($rc,$msg,$xml) = $self->daemon->process(
183 \$body,
184 $c,
185 $c->req->headers->header('soapaction')
186 );
187 3 my $bytes = $xml;
188 3 my $err;
189 3 ▶ if(UNIVERSAL::isa($bytes, 'XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($bytes, 'XML::LibXML::Document')) { } 3 0 3
190 3 ▶ $bytes = $bytes->toString($rc == RC_OK ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$rc == 200 ? : 2 1 3
191 3 $format = 'soapxml';
192 }
193 else {
194 0 $err = $bytes;
195 }
196 3 ▶ if (not $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 0 3 3
197 0 $bytes = "[$rc] $err";
198 }
199
200 $c->render(
201 3 status => $rc,
202 format => $format,
203 data => $bytes,
204 );
205 1 });
206 }
207  
208 1 1;
209  
210 =head1 ACKNOWLEDGEMENT
211  
212 This is really just a very thin layer on top of Mark Overmeers great L<XML::Compile::SOAP::Daemon> module. Thanks Mark!
213  
214 =head1 AUTHOR
215  
216 S<Tobias Oetiker, E<lt>tobi@oetiker.chE<gt>>
217  
218 =head1 COPYRIGHT
219  
220 Copyright OETIKER+PARTNER AG 2019
221  
222 =head1 LICENSE
223  
224 This library is free software; you can redistribute it and/or modify
225 it under the same terms as Perl itself, either Perl version 5.10 or,
226 at your option, any later version of Perl 5 you may have available.
227  
228 =cut
↑ Summary example/exsoaple.pl »