1 package BroadWorks::OCI;
7 use XML::Compile::Cache;
8 use Log::Report 'broadworks-oci', syntax => 'SHORT';
10 use IO::Socket::Timeout;
11 use Errno qw(ETIMEDOUT EWOULDBLOCK);
16 use MooseX::ClassAttribute;
23 default => sub { {} },
29 $self->_XCC->{$schema} ||= $self->_build_XCC($schema);
35 my @path = split('/', $schema);
37 $schema = join('/', 'OCISchemaAS', @path) .
38 '/OCISchema' . $schema . '.xsd';
39 info "Loading $schema";
41 my $dist_dir = File::ShareDir::dist_dir('BroadWorks-OCI');
43 "OCISchemaBASE/OCISchemaBASE.xsd",
44 "OCISchemaAS/OCISchemaDataTypes.xsd",
45 "OCISchemaAS/OCISchemaSearchCriteria.xsd",
46 "OCISchemaAS/OCISchemaSortCriteria.xsd",
50 # results in some repeat inclusion of BASE and DataTypes, but not in
51 # importing a huge pile of definitions we aren't going to use
52 my $XCC = XML::Compile::Cache->new(
54 schema_dirs => $dist_dir,
56 '{C}OCICommand' => 'AUTO',
59 # we only ever send/receive one type of root element
60 $XCC->declare(RW => '{C}BroadsoftDocument');
68 $self->XCC($schema)->reader('{C}BroadsoftDocument');
74 $self->XCC($schema)->writer('{C}BroadsoftDocument');
80 Digest::MD5::md5_hex(time().{}.rand().$$);
86 default => 'ews.xdp.broadsoft.com', # test server
97 builder => '_connect',
107 my $host = $self->hostname;
108 my $port = $self->port;
109 info("opening socket to $host:$port");
110 my $socket = IO::Socket::INET->new(
114 Timeout => $self->timeout,
115 ) or die "failed to connect: $!\n";
117 IO::Socket::Timeout->enable_timeouts_on($socket);
118 $socket->read_timeout($self->timeout);
119 $socket->write_timeout($self->timeout);
145 # XML::Compile's way to deal with the "xsi:type=" attribute for late-binding
147 $args{'XSI_TYPE'} = $command;
149 my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
150 info("sending $command");
152 'sessionId' => $self->sessionId,
157 # format and send request
160 $doc->setDocumentElement( $self->writer($schema)->( $doc, $content ) );
162 $self->socket->print("$doc");
164 return (0, $@) if $@;
167 # responses from the API are _usually_ two lines, but sometimes only one.
168 my $xml_response = $self->socket->getline;
169 if ($xml_response and $xml_response !~ m[</BroadsoftDocument]) {
170 $xml_response .= $self->socket->getline;
172 if (!$xml_response and ($! == ETIMEDOUT or $! == EWOULDBLOCK)) {
173 return (0, 'OCI request timed out');
177 trace("$xml_response\n\n");
178 my $response = eval { $self->reader($schema)->( $xml_response ) };
179 return (0, $@) if $@;
181 $command = $response->{command};
182 $command = $command->[0] if ref($command) eq 'ARRAY';
184 my $response_type = $command->{XSI_TYPE};
185 if ($response_type eq 'SuccessResponse') {
187 } elsif ( $response_type eq '{C}ErrorResponse' ) {
188 return (0, $command->{summary});
189 } else { # all other response types
190 info("received document type '$response_type'");
191 return (1, $command);
197 my ($success, $message) = $self->request( 'Login', 'AuthenticationRequest',
198 userId => $self->userId,
200 return ($success, $message) unless $success;
202 # construct challenge response
203 my $token = Digest::MD5::md5_hex(
204 $message->{nonce} . ':' . Digest::SHA::sha1_hex($self->password)
206 return $self->request('Login', 'LoginRequest14sp4',
207 userId => $self->userId,
208 signedPassword => $token,
214 my ($success, $message) = $self->login;
215 die "Login failed: $message" if !$success;
221 BroadWorks::OCI - Open Client Interface for BroadWorks Application Server.
229 our $VERSION = '0.01';
233 Quick summary of what the module does.
235 Perhaps a little code snippet.
239 my $foo = BroadWorks::OCI->new();
248 Mark Wells, C<< <mark at freeside.biz> >>
253 You can find documentation for this module with the perldoc command.
255 perldoc BroadWorks::OCI
258 You can also look for information at:
262 =item * RT: CPAN's request tracker (report bugs here)
264 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=BroadWorks-OCI>
266 =item * AnnoCPAN: Annotated CPAN documentation
268 L<http://annocpan.org/dist/BroadWorks-OCI>
272 L<http://cpanratings.perl.org/d/BroadWorks-OCI>
276 L<http://search.cpan.org/dist/BroadWorks-OCI/>
281 =head1 ACKNOWLEDGEMENTS
284 =head1 LICENSE AND COPYRIGHT
286 Copyright 2015 Mark Wells.
288 This program is free software; you can redistribute it and/or modify it
289 under the terms of the the Artistic License (2.0). You may obtain a
290 copy of the full license at:
292 L<http://www.perlfoundation.org/artistic_license_2_0>
294 Any use, modification, and distribution of the Standard or Modified
295 Versions is governed by this Artistic License. By using, modifying or
296 distributing the Package, you accept this license. Do not use, modify,
297 or distribute the Package, if you do not accept this license.
299 If your Modified Version has been derived from a Modified Version made
300 by someone other than you, you are nevertheless required to ensure that
301 your Modified Version complies with the requirements of this license.
303 This license does not grant you the right to use any trademark, service
304 mark, tradename, or logo of the Copyright Holder.
306 This license includes the non-exclusive, worldwide, free-of-charge
307 patent license to make, have made, use, offer to sell, sell, import and
308 otherwise transfer the Package with respect to any patent claims
309 licensable by the Copyright Holder that are necessarily infringed by the
310 Package. If you institute patent litigation (including a cross-claim or
311 counterclaim) against any party alleging that the Package constitutes
312 direct or contributory patent infringement, then this Artistic License
313 to you shall terminate on the date that such litigation is filed.
315 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
316 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
317 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
318 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
319 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
320 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
321 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
322 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327 1; # End of BroadWorks::OCI